Skip to content

Commit 1b103e9

Browse files
committed
Trigger MediaScanner once MOVE is complete
1 parent dce2539 commit 1b103e9

File tree

2 files changed

+31
-10
lines changed

2 files changed

+31
-10
lines changed

app/src/main/java/co/adityarajput/fileflow/data/models/Action.kt

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package co.adityarajput.fileflow.data.models
22

33
import android.content.Context
44
import android.content.Intent
5+
import android.media.MediaScannerConnection
56
import androidx.compose.runtime.Composable
67
import androidx.compose.ui.text.AnnotatedString
78
import androidx.compose.ui.text.buildAnnotatedString
@@ -108,6 +109,8 @@ sealed class Action {
108109
listOf(it.maxByOrNull(superlative.selector) ?: return)
109110
} ?: return
110111

112+
val destPaths = mutableListOf<String?>()
113+
111114
for (srcFile in srcFiles) {
112115
val srcFileName = srcFile.name ?: continue
113116
val destFileName = getDestFileName(srcFile)
@@ -143,12 +146,14 @@ sealed class Action {
143146
"Action",
144147
"Moving $srcFileName to ${destSubDir.path}/$destFileName",
145148
)
146-
srcFile.moveTo(
147-
destSubDir,
148-
destFileName,
149-
keepOriginal,
150-
overwriteExisting,
151-
context,
149+
destPaths.add(
150+
srcFile.moveTo(
151+
destSubDir,
152+
destFileName,
153+
keepOriginal,
154+
overwriteExisting,
155+
context,
156+
),
152157
)
153158
} catch (e: FileAlreadyExistsException) {
154159
Logger.e("Action", "$destFileName already exists", e)
@@ -160,6 +165,14 @@ sealed class Action {
160165

161166
registerExecution(srcFileName)
162167
}
168+
169+
MediaScannerConnection.scanFile(
170+
context,
171+
destPaths.filterNotNull().distinct().toTypedArray(),
172+
null,
173+
) { path, _ ->
174+
Logger.d("Action", "Scanned media at $path")
175+
}
163176
}
164177
}
165178

app/src/main/java/co/adityarajput/fileflow/utils/Files.kt

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ sealed class File {
4343
keepOriginal: Boolean,
4444
overwriteExisting: Boolean,
4545
context: Context,
46-
) {
46+
): String? {
4747
if (destDir !is SAFFile)
4848
throw IllegalArgumentException("Destination directory must be a SAFFile")
4949

@@ -63,6 +63,8 @@ sealed class File {
6363
}
6464

6565
if (!keepOriginal) delete()
66+
67+
return null
6668
}
6769
}
6870

@@ -73,16 +75,18 @@ sealed class File {
7375
keepOriginal: Boolean,
7476
overwriteExisting: Boolean,
7577
context: Context,
76-
) {
78+
): String? {
7779
if (destDir !is FSFile)
7880
throw IllegalArgumentException("Destination directory must be a FSFile")
7981

8082
val options = mutableListOf<StandardCopyOption>()
8183
if (keepOriginal) options.add(StandardCopyOption.COPY_ATTRIBUTES)
8284
if (overwriteExisting) options.add(StandardCopyOption.REPLACE_EXISTING)
85+
var destFilePath: String? = null
8386

8487
try {
8588
withContext(Dispatchers.IO) {
89+
destFilePath = destDir.ioFile.toPath().resolve(destFileName).toString()
8690
if (keepOriginal) {
8791
Files.copy(
8892
this@FSFile.ioFile.toPath(),
@@ -106,9 +110,11 @@ sealed class File {
106110
e,
107111
)
108112

109-
val destFile = IOFile(destDir.ioFile, destFileName)
110113
withContext(Dispatchers.IO) {
114+
val destFile = IOFile(destDir.ioFile, destFileName)
115+
111116
destFile.createNewFile()
117+
destFilePath = destFile.absolutePath
112118

113119
this@FSFile.ioFile.inputStream().use { srcStream ->
114120
destFile.outputStream().use { destStream ->
@@ -119,6 +125,8 @@ sealed class File {
119125

120126
if (!keepOriginal) delete()
121127
}
128+
129+
return destFilePath
122130
}
123131
}
124132

@@ -277,7 +285,7 @@ sealed class File {
277285
keepOriginal: Boolean,
278286
overwriteExisting: Boolean,
279287
context: Context,
280-
)
288+
): String?
281289

282290
fun delete() = when (this) {
283291
is SAFFile -> documentFile.delete()

0 commit comments

Comments
 (0)