Skip to content

Commit

Permalink
Fixed album not deleting when it is empty
Browse files Browse the repository at this point in the history
  • Loading branch information
Anthonyy232 committed May 28, 2024
1 parent 61e3144 commit b52a477
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.anthonyla.paperize.core.findFirstValidUri
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.lazygeniouz.dfc.file.DocumentFileCompat
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -111,11 +109,17 @@ class AlbumsViewModel @Inject constructor (
}
albumWithWallpapers = repository.getAlbumsWithWallpaperAndFolder().first()
albumWithWallpapers.forEach { albumWithWallpaper ->
// Update album cover uri if null or invalid
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFile.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) {
val newCoverUri = findFirstValidUri(context, albumWithWallpaper.wallpapers, albumWithWallpaper.folders)
repository.updateAlbum(albumWithWallpaper.album.copy(coverUri = newCoverUri))
// 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
val albumCoverFile = albumWithWallpaper.album.coverUri?.toUri()?.let { DocumentFile.fromSingleUri(context, it) }
if (albumCoverFile == null || !albumCoverFile.exists()) {
val newCoverUri = findFirstValidUri(context, albumWithWallpaper.wallpapers, albumWithWallpaper.folders)
repository.updateAlbum(albumWithWallpaper.album.copy(coverUri = newCoverUri))
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit b52a477

Please sign in to comment.