Skip to content

Commit 7490742

Browse files
committed
fix categories not updating after list changes(#168)
1 parent fcf2003 commit 7490742

File tree

13 files changed

+127
-37
lines changed

13 files changed

+127
-37
lines changed

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/FilesTab.kt

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,21 @@ class FilesTab(
393393
if (isLoading) return false
394394

395395
// check if any file has been changed
396-
if (activeFolder is LocalFileHolder) {
396+
if (activeFolder is VirtualFileHolder) {
397+
validateBookmarks()
398+
validateSearchResult()
399+
val newContent = (activeFolder as VirtualFileHolder).listContent()
400+
// Check if the content size has changed
401+
if (newContent.size != activeFolderContent.size) {
402+
reloadFiles()
403+
return true
404+
}
405+
// Check each file to see if the source has changed
406+
if (activeFolderContent.any { (it as LocalFileHolder).hasSourceChanged() }) {
407+
reloadFiles()
408+
return true
409+
}
410+
} else if (activeFolder is LocalFileHolder) {
397411
val newContent =
398412
(activeFolder as LocalFileHolder).file.listFiles()?.toCollection(arrayListOf())
399413
?.apply {
@@ -455,6 +469,16 @@ class FilesTab(
455469
return false
456470
}
457471

472+
private fun validateBookmarks() {
473+
globalClass.preferencesManager.bookmarks = globalClass.preferencesManager.bookmarks.filter {
474+
File(it).exists()
475+
}.toSet()
476+
}
477+
478+
private fun validateSearchResult() {
479+
globalClass.searchManager.searchResults.removeIf { !File(it.file.uniquePath).exists() }
480+
}
481+
458482
fun quickReloadFiles() {
459483
scope.launch {
460484
// terminate if the tab is busy

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/holder/VirtualFileHolder.kt

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import com.raival.compose.file.explorer.R
55
import com.raival.compose.file.explorer.common.emptyString
66
import com.raival.compose.file.explorer.screen.main.tab.files.misc.ContentCount
77
import com.raival.compose.file.explorer.screen.main.tab.files.misc.FileListCategory
8-
import com.raival.compose.file.explorer.screen.main.tab.files.misc.sortFoldersFirst
98
import com.raival.compose.file.explorer.screen.main.tab.files.misc.sortNewerFirst
109
import com.raival.compose.file.explorer.screen.main.tab.files.provider.StorageProvider.getArchiveFiles
1110
import com.raival.compose.file.explorer.screen.main.tab.files.provider.StorageProvider.getAudioFiles
@@ -52,33 +51,35 @@ class VirtualFileHolder(val type: Int) : ContentHolder() {
5251
override suspend fun getParent() = null
5352

5453
override suspend fun listSortedContent(): ArrayList<out ContentHolder> {
55-
if (type != RECENT) {
54+
if (type == SEARCH || type == BOOKMARKS) {
5655
return super.listSortedContent()
5756
}
5857

5958
val sortingPrefs = globalClass.preferencesManager.getSortingPrefsFor(this)
6059

61-
if (sortingPrefs == globalClass.preferencesManager.getDefaultSortingPrefs()) {
60+
if (type == RECENT && sortingPrefs == globalClass.preferencesManager.getDefaultSortingPrefs()) {
6261
return listContent().apply {
6362
sortWith(sortNewerFirst)
64-
if (sortingPrefs.showFoldersFirst) sortWith(sortFoldersFirst)
6563
if (!globalClass.preferencesManager.showHiddenFiles) {
6664
removeIf { it.isHidden() }
6765
}
6866
}
67+
} else return listContent().apply {
68+
if (!globalClass.preferencesManager.showHiddenFiles) {
69+
removeIf { it.isHidden() }
70+
}
6971
}
70-
71-
return super.listSortedContent()
7272
}
7373

74-
override suspend fun listContent() = (if (contentList.isEmpty()) {
74+
override suspend fun listContent(): ArrayList<out ContentHolder> {
75+
val sortingPrefs = globalClass.preferencesManager.getSortingPrefsFor(this)
7576
when (type) {
7677
BOOKMARKS -> getBookmarks()
77-
AUDIO -> getAudioFiles()
78-
VIDEO -> getVideoFiles()
79-
IMAGE -> getImageFiles()
80-
ARCHIVE -> getArchiveFiles()
81-
DOCUMENT -> getDocumentFiles()
78+
AUDIO -> getAudioFiles(sortingPrefs)
79+
VIDEO -> getVideoFiles(sortingPrefs)
80+
IMAGE -> getImageFiles(sortingPrefs)
81+
ARCHIVE -> getArchiveFiles(sortingPrefs)
82+
DOCUMENT -> getDocumentFiles(sortingPrefs)
8283
RECENT -> getRecentFiles()
8384
SEARCH -> getSearchResult()
8485
else -> arrayListOf()
@@ -89,10 +90,12 @@ class VirtualFileHolder(val type: Int) : ContentHolder() {
8990
}
9091
if (type != BOOKMARKS) fetchCategories()
9192
}
92-
} else contentList).filter {
93-
if (selectedCategory == null) return@filter true
94-
it.uniquePath == (selectedCategory!!.data as File).path + File.separator + it.displayName
95-
}.toCollection(arrayListOf()).also { fileCount = it.size }
93+
94+
return contentList.filter {
95+
if (selectedCategory == null) return@filter true
96+
it.uniquePath == (selectedCategory!!.data as File).path + File.separator + it.displayName
97+
}.toCollection(arrayListOf()).also { fileCount = it.size }
98+
}
9699

97100
private fun fetchCategories() {
98101
categories.clear()

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/provider/StorageProvider.kt

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import com.raival.compose.file.explorer.screen.main.tab.files.holder.ContentHold
1515
import com.raival.compose.file.explorer.screen.main.tab.files.holder.LocalFileHolder
1616
import com.raival.compose.file.explorer.screen.main.tab.files.holder.RootFileHolder
1717
import com.raival.compose.file.explorer.screen.main.tab.files.holder.StorageDevice
18+
import com.raival.compose.file.explorer.screen.main.tab.files.misc.FileSortingPrefs
19+
import com.raival.compose.file.explorer.screen.main.tab.files.misc.SortingMethod.SORT_BY_DATE
20+
import com.raival.compose.file.explorer.screen.main.tab.files.misc.SortingMethod.SORT_BY_NAME
21+
import com.raival.compose.file.explorer.screen.main.tab.files.misc.SortingMethod.SORT_BY_SIZE
1822
import com.raival.compose.file.explorer.screen.main.tab.files.misc.StorageDeviceType.EXTERNAL_STORAGE
1923
import com.raival.compose.file.explorer.screen.main.tab.files.misc.StorageDeviceType.INTERNAL_STORAGE
2024
import com.raival.compose.file.explorer.screen.main.tab.files.misc.StorageDeviceType.ROOT
@@ -182,7 +186,9 @@ object StorageProvider {
182186
return storageList
183187
}
184188

185-
fun getDocumentFiles(): ArrayList<LocalFileHolder> {
189+
fun getDocumentFiles(
190+
sortingPrefs: FileSortingPrefs?
191+
): ArrayList<LocalFileHolder> {
186192
val documentFiles = ArrayList<LocalFileHolder>()
187193
val contentResolver: ContentResolver = globalClass.contentResolver
188194

@@ -211,7 +217,12 @@ object StorageProvider {
211217
projection,
212218
selection,
213219
selectionArgs,
214-
null
220+
when (sortingPrefs?.sortMethod) {
221+
SORT_BY_NAME -> "${MediaStore.Files.FileColumns.DISPLAY_NAME} ASC"
222+
SORT_BY_DATE -> "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
223+
SORT_BY_SIZE -> "${MediaStore.Files.FileColumns.SIZE} DESC"
224+
else -> null
225+
}
215226
)
216227

217228
cursor?.use {
@@ -223,10 +234,12 @@ object StorageProvider {
223234
}
224235
}
225236

226-
return documentFiles
237+
return documentFiles.also { if (sortingPrefs?.reverseSorting == true) it.reverse() }
227238
}
228239

229-
fun getArchiveFiles(): ArrayList<LocalFileHolder> {
240+
fun getArchiveFiles(
241+
sortingPrefs: FileSortingPrefs?
242+
): ArrayList<LocalFileHolder> {
230243
val archiveFiles = ArrayList<LocalFileHolder>()
231244
val contentResolver: ContentResolver = globalClass.contentResolver
232245

@@ -253,7 +266,12 @@ object StorageProvider {
253266
projection,
254267
selection,
255268
selectionArgs,
256-
null
269+
when (sortingPrefs?.sortMethod) {
270+
SORT_BY_NAME -> "${MediaStore.Files.FileColumns.DISPLAY_NAME} ASC"
271+
SORT_BY_DATE -> "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
272+
SORT_BY_SIZE -> "${MediaStore.Files.FileColumns.SIZE} DESC"
273+
else -> null
274+
}
257275
)
258276

259277
cursor?.use {
@@ -265,10 +283,12 @@ object StorageProvider {
265283
}
266284
}
267285

268-
return archiveFiles
286+
return archiveFiles.also { if (sortingPrefs?.reverseSorting == true) it.reverse() }
269287
}
270288

271-
fun getImageFiles(): ArrayList<LocalFileHolder> {
289+
fun getImageFiles(
290+
sortingPrefs: FileSortingPrefs?
291+
): ArrayList<LocalFileHolder> {
272292
val imageFiles = ArrayList<LocalFileHolder>()
273293
val contentResolver: ContentResolver = globalClass.contentResolver
274294

@@ -283,22 +303,29 @@ object StorageProvider {
283303
projection,
284304
null,
285305
null,
286-
null
306+
when (sortingPrefs?.sortMethod) {
307+
SORT_BY_NAME -> "${MediaStore.Files.FileColumns.DISPLAY_NAME} ASC"
308+
SORT_BY_DATE -> "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
309+
SORT_BY_SIZE -> "${MediaStore.Files.FileColumns.SIZE} DESC"
310+
else -> null
311+
}
287312
)
288313

289314
cursor?.use {
290-
val pathColumn = it.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA)
315+
val pathColumn = it.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)
291316

292317
while (it.moveToNext()) {
293318
val path = it.getString(pathColumn)
294319
imageFiles.add(LocalFileHolder(File(path)))
295320
}
296321
}
297322

298-
return imageFiles
323+
return imageFiles.also { if (sortingPrefs?.reverseSorting == true) it.reverse() }
299324
}
300325

301-
fun getVideoFiles(): ArrayList<LocalFileHolder> {
326+
fun getVideoFiles(
327+
sortingPrefs: FileSortingPrefs?
328+
): ArrayList<LocalFileHolder> {
302329
val videoFiles = ArrayList<LocalFileHolder>()
303330
val contentResolver: ContentResolver = globalClass.contentResolver
304331

@@ -313,7 +340,12 @@ object StorageProvider {
313340
projection,
314341
null,
315342
null,
316-
null
343+
when (sortingPrefs?.sortMethod) {
344+
SORT_BY_NAME -> "${MediaStore.Files.FileColumns.DISPLAY_NAME} ASC"
345+
SORT_BY_DATE -> "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
346+
SORT_BY_SIZE -> "${MediaStore.Files.FileColumns.SIZE} DESC"
347+
else -> null
348+
}
317349
)
318350

319351
cursor?.use {
@@ -325,13 +357,12 @@ object StorageProvider {
325357
}
326358
}
327359

328-
return videoFiles
360+
return videoFiles.also { if (sortingPrefs?.reverseSorting == true) it.reverse() }
329361
}
330362

331-
fun getBookmarks() = globalClass.preferencesManager.bookmarks
332-
.map { LocalFileHolder(File(it)) } as ArrayList<LocalFileHolder>
333-
334-
fun getAudioFiles(): ArrayList<LocalFileHolder> {
363+
fun getAudioFiles(
364+
sortingPrefs: FileSortingPrefs?
365+
): ArrayList<LocalFileHolder> {
335366
val audioFiles = ArrayList<LocalFileHolder>()
336367
val contentResolver: ContentResolver = globalClass.contentResolver
337368

@@ -346,7 +377,12 @@ object StorageProvider {
346377
projection,
347378
null,
348379
null,
349-
null
380+
when (sortingPrefs?.sortMethod) {
381+
SORT_BY_NAME -> "${MediaStore.Files.FileColumns.DISPLAY_NAME} ASC"
382+
SORT_BY_DATE -> "${MediaStore.Files.FileColumns.DATE_MODIFIED} DESC"
383+
SORT_BY_SIZE -> "${MediaStore.Files.FileColumns.SIZE} DESC"
384+
else -> null
385+
}
350386
)
351387

352388
cursor?.use {
@@ -358,9 +394,12 @@ object StorageProvider {
358394
}
359395
}
360396

361-
return audioFiles
397+
return audioFiles.also { if (sortingPrefs?.reverseSorting == true) it.reverse() }
362398
}
363399

400+
fun getBookmarks() = globalClass.preferencesManager.bookmarks
401+
.map { LocalFileHolder(File(it)) } as ArrayList<LocalFileHolder>
402+
364403
fun getRawRecentFiles(
365404
recentHours: Int = 24 * 5,
366405
limit: Int = 100

app/src/main/java/com/raival/compose/file/explorer/screen/main/tab/files/ui/dialog/FileOptionsMenuDialog.kt

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -232,13 +232,28 @@ fun FileOptionsMenuDialog(
232232
}
233233
}
234234

235+
if (tab.activeFolder is VirtualFileHolder && (tab.activeFolder as VirtualFileHolder).type == VirtualFileHolder.BOOKMARKS) {
236+
FileOption(
237+
Icons.Rounded.BookmarkAdd,
238+
stringResource(R.string.remove_from_bookmarks)
239+
) {
240+
onDismissRequest()
241+
val toRemove = targetFiles.map { it.uniquePath }
242+
val newBookmarks = globalClass.preferencesManager.bookmarks.filter {
243+
!toRemove.contains(it)
244+
}
245+
globalClass.preferencesManager.bookmarks = newBookmarks.toSet()
246+
tab.unselectAllFiles()
247+
tab.reloadFiles()
248+
}
249+
}
250+
235251
if (tab.activeFolder is LocalFileHolder ||
236252
(tab.activeFolder is VirtualFileHolder && (tab.activeFolder as VirtualFileHolder).type isNot VirtualFileHolder.BOOKMARKS)
237253
) {
238254
FileOption(Icons.Rounded.BookmarkAdd, stringResource(R.string.add_to_bookmarks)) {
239255
onDismissRequest()
240256
globalClass.preferencesManager.bookmarks += targetFiles.map { it.uniquePath }
241-
.distinct()
242257
globalClass.showMsg(R.string.added_to_bookmarks)
243258
tab.unselectAllFiles()
244259
}

app/src/main/res/values-es/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">Error al renombrar el archivo</string>
444444
<string name="updating">Actualizando</string>
445445
<string name="minimize">Minimizar</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

app/src/main/res/values-fa/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">تغییر نام فایل ناموفق بود</string>
444444
<string name="updating">در حال به‌روزرسانی</string>
445445
<string name="minimize">کمینه کردن</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

app/src/main/res/values-fr/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">Échec du renommage du fichier</string>
444444
<string name="updating">Mise à jour</string>
445445
<string name="minimize">Réduire</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

app/src/main/res/values-pl/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">Nie udało się zmienić nazwy pliku</string>
444444
<string name="updating">Aktualizowanie</string>
445445
<string name="minimize">Minimalizuj</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

app/src/main/res/values-pt-rBR/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">Falha ao renomear arquivo</string>
444444
<string name="updating">Atualizando</string>
445445
<string name="minimize">Minimizar</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

app/src/main/res/values-ru/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -443,4 +443,5 @@
443443
<string name="failed_to_rename_file">Не удалось переименовать файл</string>
444444
<string name="updating">Обновление</string>
445445
<string name="minimize">Свернуть</string>
446+
<string name="remove_from_bookmarks">Remove from bookmarks</string>
446447
</resources>

0 commit comments

Comments
 (0)