Fixed album not deleting when it is empty

This commit is contained in:
Anthonyy232 committed 2024-05-27 18:48:38 -07:00
1 parent 61e3144de4
commit b52a47770d
2 files changed
+11 -2

No files matched your search

@@ -8,8 +8,6 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope import androidx.lifecycle.viewModelScope
import com.anthonyla.paperize.core.findFirstValidUri import com.anthonyla.paperize.core.findFirstValidUri
import com.anthonyla.paperize.core.getWallpaperFromFolder import com.anthonyla.paperize.core.getWallpaperFromFolder
import com.anthonyla.paperize.feature.wallpaper.domain.model.Folder
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
import com.anthonyla.paperize.feature.wallpaper.domain.repository.AlbumRepository import com.anthonyla.paperize.feature.wallpaper.domain.repository.AlbumRepository
import com.lazygeniouz.dfc.file.DocumentFileCompat import com.lazygeniouz.dfc.file.DocumentFileCompat
import dagger.hilt.android.lifecycle.HiltViewModel import dagger.hilt.android.lifecycle.HiltViewModel
@@ -111,6 +109,11 @@ class AlbumsViewModel @Inject constructor (
} }
albumWithWallpapers = repository.getAlbumsWithWallpaperAndFolder().first() albumWithWallpapers = repository.getAlbumsWithWallpaperAndFolder().first()
albumWithWallpapers.forEach { albumWithWallpaper -> albumWithWallpapers.forEach { albumWithWallpaper ->
// Delete empty albums
if (albumWithWallpaper.wallpapers.isEmpty() && albumWithWallpaper.folders.all { it.wallpapers.isEmpty() }) {
repository.deleteAlbum(albumWithWallpaper.album)
}
else {
// Update album cover uri if null or invalid // Update album cover uri if null or invalid
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFile.fromSingleUri(context, it) } val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFile.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) { if (albumCoverFile == null || !albumCoverFile.exists()) {
@@ -120,6 +123,7 @@ class AlbumsViewModel @Inject constructor (
} }
} }
} }
}
/** /**
* Retrieve albums from database into viewModel * Retrieve albums from database into viewModel
@@ -836,6 +836,11 @@ class WallpaperService: Service() {
} }
} }
} }
// Delete empty albums
if (albumWithWallpaper.wallpapers.isEmpty() && albumWithWallpaper.folders.all { it.wallpapers.isEmpty() }) {
albumRepository.deleteAlbum(albumWithWallpaper.album)
}
} }
// Update selected album // Update selected album