Fixed album wallpapers not being refreshed before changing wallpapers

Fixed album cover art not being refreshed
This commit is contained in:
Anthonyy232 committed 2024-06-02 16:09:17 -07:00
1 parent 87ef7ebaf0
commit 068f78f86c
4 files changed
+51 -20

No files matched your search

@@ -101,9 +101,7 @@ fun PaperizeApp(
} }
} }
albumsViewModel.onEvent( albumsViewModel.onEvent(
AlbumsEvent.DeleteAlbumWithWallpapers( AlbumsEvent.DeleteAlbumWithWallpapers(albumWithWallpapers)
albumWithWallpapers
)
) )
} }
} }
@@ -124,10 +122,18 @@ fun PaperizeApp(
val wallpapersUri = wallpapers.map { it.wallpaperUri }.toSet() val wallpapersUri = wallpapers.map { it.wallpaperUri }.toSet()
val newSelectedAlbum = SelectedAlbum( val newSelectedAlbum = SelectedAlbum(
album = foundAlbum.album.copy( album = foundAlbum.album.copy(
homeWallpapersInQueue = selectedAlbum.album.homeWallpapersInQueue.filter { it in wallpapersUri }, homeWallpapersInQueue = if (selectedAlbum.album.homeWallpapersInQueue.firstOrNull() in wallpapersUri) {
lockWallpapersInQueue = selectedAlbum.album.lockWallpapersInQueue.filter { it in wallpapersUri }, val firstElement = selectedAlbum.album.homeWallpapersInQueue.first()
currentHomeWallpaper = selectedAlbum.album.currentHomeWallpaper, val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
currentLockWallpaper = selectedAlbum.album.currentLockWallpaper, listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
lockWallpapersInQueue = if (selectedAlbum.album.lockWallpapersInQueue.firstOrNull() in wallpapersUri) {
val firstElement = selectedAlbum.album.lockWallpapersInQueue.first()
val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
currentHomeWallpaper = if (selectedAlbum.album.currentHomeWallpaper in wallpapersUri) selectedAlbum.album.currentHomeWallpaper else selectedAlbum.album.homeWallpapersInQueue.firstOrNull { it in wallpapersUri },
currentLockWallpaper = if (selectedAlbum.album.currentLockWallpaper in wallpapersUri) selectedAlbum.album.currentLockWallpaper else selectedAlbum.album.lockWallpapersInQueue.firstOrNull { it in wallpapersUri }
), ),
wallpapers = wallpapers wallpapers = wallpapers
) )
@@ -93,7 +93,7 @@ class AlbumsViewModel @Inject constructor (
repository.deleteWallpaperList(invalidWallpapers) repository.deleteWallpaperList(invalidWallpapers)
} }
// Update folder cover uri and children uri // Update folder cover uri and wallpapers uri
albumWithWallpaper.folders.forEach { folder -> albumWithWallpaper.folders.forEach { folder ->
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory -> DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
if (!folderDirectory.isDirectory()) { if (!folderDirectory.isDirectory()) {
@@ -41,6 +41,7 @@ import com.lazygeniouz.dfc.file.DocumentFileCompat
import dagger.hilt.android.AndroidEntryPoint import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.first import kotlinx.coroutines.flow.first
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import java.io.IOException import java.io.IOException
@@ -123,6 +124,8 @@ class WallpaperService1: Service() {
private fun workerTaskStart(setHomeOrLock: Boolean? = null) { private fun workerTaskStart(setHomeOrLock: Boolean? = null) {
workerHandler.post { workerHandler.post {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
refreshAlbum(this@WallpaperService1)
delay(1000)
changeWallpaper(this@WallpaperService1, setHomeOrLock) changeWallpaper(this@WallpaperService1, setHomeOrLock)
} }
stopSelf() stopSelf()
@@ -625,14 +628,16 @@ class WallpaperService1: Service() {
albumRepository.deleteWallpaperList(invalidWallpapers) albumRepository.deleteWallpaperList(invalidWallpapers)
} }
// Update folder wallpapers // Update folder cover uri and wallpapers uri
albumWithWallpaper.folders.forEach { folder -> albumWithWallpaper.folders.forEach { folder ->
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory -> DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
if (!folderDirectory.isDirectory()) { if (!folderDirectory.isDirectory()) {
albumRepository.deleteFolder(folder) albumRepository.deleteFolder(folder)
} else { } else {
val wallpapers = getWallpaperFromFolder(folder.folderUri, context) val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
albumRepository.updateFolder(folder.copy(wallpapers = wallpapers)) val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
} }
} }
} }
@@ -668,10 +673,18 @@ class WallpaperService1: Service() {
else { else {
val newSelectedAlbum = SelectedAlbum( val newSelectedAlbum = SelectedAlbum(
album = foundAlbum.album.copy( album = foundAlbum.album.copy(
homeWallpapersInQueue = wallpapersUri.shuffled(), homeWallpapersInQueue = if (selectedAlbum.album.homeWallpapersInQueue.firstOrNull() in wallpapersUri) {
lockWallpapersInQueue = wallpapersUri.shuffled(), val firstElement = selectedAlbum.album.homeWallpapersInQueue.first()
currentHomeWallpaper = selectedAlbum.album.currentHomeWallpaper, val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
currentLockWallpaper = selectedAlbum.album.currentLockWallpaper, listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
lockWallpapersInQueue = if (selectedAlbum.album.lockWallpapersInQueue.firstOrNull() in wallpapersUri) {
val firstElement = selectedAlbum.album.lockWallpapersInQueue.first()
val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
currentHomeWallpaper = if (selectedAlbum.album.currentHomeWallpaper in wallpapersUri) selectedAlbum.album.currentHomeWallpaper else selectedAlbum.album.homeWallpapersInQueue.firstOrNull { it in wallpapersUri },
currentLockWallpaper = if (selectedAlbum.album.currentLockWallpaper in wallpapersUri) selectedAlbum.album.currentLockWallpaper else selectedAlbum.album.lockWallpapersInQueue.firstOrNull { it in wallpapersUri }
), ),
wallpapers = wallpapers wallpapers = wallpapers
) )
@@ -125,6 +125,8 @@ class WallpaperService2: Service() {
workerHandler.post { workerHandler.post {
CoroutineScope(Dispatchers.IO).launch { CoroutineScope(Dispatchers.IO).launch {
delay(5 * 1000) delay(5 * 1000)
refreshAlbum(this@WallpaperService2)
delay(1000)
changeWallpaper(this@WallpaperService2, setHomeOrLock) changeWallpaper(this@WallpaperService2, setHomeOrLock)
} }
stopSelf() stopSelf()
@@ -630,14 +632,16 @@ class WallpaperService2: Service() {
albumRepository.deleteWallpaperList(invalidWallpapers) albumRepository.deleteWallpaperList(invalidWallpapers)
} }
// Update folder wallpapers // Update folder cover uri and wallpapers uri
albumWithWallpaper.folders.forEach { folder -> albumWithWallpaper.folders.forEach { folder ->
DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory -> DocumentFileCompat.fromTreeUri(context, folder.folderUri.toUri())?.let { folderDirectory ->
if (!folderDirectory.isDirectory()) { if (!folderDirectory.isDirectory()) {
albumRepository.deleteFolder(folder) albumRepository.deleteFolder(folder)
} else { } else {
val wallpapers = getWallpaperFromFolder(folder.folderUri, context) val wallpapers = getWallpaperFromFolder(folder.folderUri, context)
albumRepository.updateFolder(folder.copy(wallpapers = wallpapers)) val folderCoverFile = folder.coverUri?.let { DocumentFile.fromSingleUri(context, it.toUri()) }
val folderCover = folderCoverFile?.takeIf { it.exists() }?.uri?.toString() ?: wallpapers.randomOrNull()
albumRepository.updateFolder(folder.copy(coverUri = folderCover, wallpapers = wallpapers))
} }
} }
} }
@@ -673,10 +677,18 @@ class WallpaperService2: Service() {
else { else {
val newSelectedAlbum = SelectedAlbum( val newSelectedAlbum = SelectedAlbum(
album = foundAlbum.album.copy( album = foundAlbum.album.copy(
homeWallpapersInQueue = wallpapersUri.shuffled(), homeWallpapersInQueue = if (selectedAlbum.album.homeWallpapersInQueue.firstOrNull() in wallpapersUri) {
lockWallpapersInQueue = wallpapersUri.shuffled(), val firstElement = selectedAlbum.album.homeWallpapersInQueue.first()
currentHomeWallpaper = selectedAlbum.album.currentHomeWallpaper, val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
currentLockWallpaper = selectedAlbum.album.currentLockWallpaper, listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
lockWallpapersInQueue = if (selectedAlbum.album.lockWallpapersInQueue.firstOrNull() in wallpapersUri) {
val firstElement = selectedAlbum.album.lockWallpapersInQueue.first()
val modifiedWallpapersUri = wallpapersUri.filter { it != firstElement }
listOf(firstElement) + modifiedWallpapersUri.shuffled()
} else { wallpapersUri.shuffled() },
currentHomeWallpaper = if (selectedAlbum.album.currentHomeWallpaper in wallpapersUri) selectedAlbum.album.currentHomeWallpaper else selectedAlbum.album.homeWallpapersInQueue.firstOrNull { it in wallpapersUri },
currentLockWallpaper = if (selectedAlbum.album.currentLockWallpaper in wallpapersUri) selectedAlbum.album.currentLockWallpaper else selectedAlbum.album.lockWallpapersInQueue.firstOrNull { it in wallpapersUri }
), ),
wallpapers = wallpapers wallpapers = wallpapers
) )