Skip to content

Commit

Permalink
Fix a long delay while loading non nomedia folders #102
Browse files Browse the repository at this point in the history
  • Loading branch information
Hamza417 committed Oct 26, 2024
1 parent e110dc3 commit eb3e88e
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 11 deletions.
7 changes: 0 additions & 7 deletions app/src/main/java/app/simple/peri/compose/screens/Home.kt
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,6 @@ import app.simple.peri.models.DisplayDimension
import app.simple.peri.models.Wallpaper
import app.simple.peri.utils.FileUtils.toUri
import app.simple.peri.viewmodels.HomeScreenViewModel
import app.simple.peri.viewmodels.WallpaperViewModel
import com.bumptech.glide.integration.compose.CrossFade
import com.bumptech.glide.integration.compose.ExperimentalGlideComposeApi
import com.bumptech.glide.integration.compose.GlideImage
Expand Down Expand Up @@ -503,9 +502,3 @@ fun ShowTagDialog(title: String, onDismiss: () -> Unit) {
}
)
}

@Composable
fun InitWallpaperViewModel() {
val wallpaperViewModel: WallpaperViewModel = viewModel()
wallpaperViewModel.refresh()
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ interface WallpaperDao {
@Query("SELECT * FROM wallpapers WHERE uri_hashcode = :uriHashcode")
fun getWallpapersByUriHashcode(uriHashcode: Int): List<Wallpaper>

/**
* Get count of wallpapers of the specified [uriHashcode]
*/
@Query("SELECT COUNT(*) FROM wallpapers WHERE uri_hashcode = :uriHashcode")
fun getWallpapersCountByUriHashcode(uriHashcode: Int): Int

/**
* Clean any entry that doesn't have any of the
* specified extension
Expand Down
22 changes: 18 additions & 4 deletions app/src/main/java/app/simple/peri/viewmodels/WallpaperViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -176,17 +176,31 @@ class WallpaperViewModel(application: Application) : AndroidViewModel(applicatio
val folder = Folder().apply {
name = pickedDirectory.name
this.uri = uri.uri.toString()
count =
wallpaperDao?.getWallpapersByUriHashcode(uri.uri?.hashCode() ?: 0)?.size
?: 0
count = wallpaperDao?.getWallpapersCountByUriHashcode(uri.uri.hashCode()) ?: 0
hashcode = uri.uri.hashCode()
isNomedia = pickedDirectory.findFile(".nomedia")?.exists() == true
isNomedia = false
}

folders.add(folder)
}
}

foldersData.postValue(folders)

/**
* Finding .nomedia files can take a long time
* So we post the folders first and then find the .nomedia files
*/
folders.forEach { folder ->
val pickedDirectory = DocumentFile.fromTreeUri(getApplication(), Uri.parse(folder.uri))
if (pickedDirectory?.exists() == true) {
if (pickedDirectory.findFile(".nomedia")?.exists() == true) {
folder.isNomedia = true
}
}
}


foldersData.postValue(folders)
}
}
Expand Down

0 comments on commit eb3e88e

Please sign in to comment.