Skip to content

Commit

Permalink
Fixed adding folder issue to already created album
Browse files Browse the repository at this point in the history
Removed predictive back gestures due to unstable crashing
Refactored viewmodel code for adding and removing wallpaper and folders
Changed string literals to string resources
  • Loading branch information
Anthonyy232 committed May 12, 2024
1 parent 3f412f4 commit 8eb9cb2
Show file tree
Hide file tree
Showing 21 changed files with 274 additions and 195 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ android {
applicationId = "com.anthonyla.paperize"
minSdk = 26
targetSdk = 34
versionCode = 8
versionName = "1.1.0"
versionCode = 9
versionName = "1.1.1"

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
vectorDrawables {
Expand Down
4 changes: 1 addition & 3 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,7 @@
android:icon="@mipmap/app_icon"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/Theme.Paperize"
android:enableOnBackInvokedCallback="true"
tools:targetApi="tiramisu">
android:theme="@style/Theme.Paperize" tools:targetApi="tiramisu">
<activity
android:name=".feature.wallpaper.presentation.MainActivity"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,6 @@ class AddAlbumViewModel @Inject constructor(
folders = _state.value.folders,
)
repository.upsertAlbumWithWallpaperAndFolder(albumWithWallpaperAndFolder)

//Clear viewModel state after adding album
_state.update { it.copy(
initialAlbumName = "",
displayedAlbumName = "",
Expand All @@ -76,13 +74,14 @@ class AddAlbumViewModel @Inject constructor(
}
is AddAlbumEvent.DeleteSelected -> {
viewModelScope.launch {
val wallpapersRemoved = _state.value.wallpapers.filterNot { it.wallpaperUri in _state.value.selectedWallpapers }
val foldersRemoved = _state.value.folders.filterNot { it.folderUri in _state.value.selectedFolders }
_state.update { it.copy(
wallpapers = wallpapersRemoved,
folders = foldersRemoved
) }
updateIsEmpty()
_state.update {
val wallpapersRemoved = it.wallpapers.filterNot { it.wallpaperUri in _state.value.selectedWallpapers }
val foldersRemoved = it.folders.filterNot { it.folderUri in _state.value.selectedFolders }
it.copy(
wallpapers = wallpapersRemoved,
folders = foldersRemoved,
isEmpty = wallpapersRemoved.isEmpty() && foldersRemoved.isEmpty()
) }
}
}
is AddAlbumEvent.SetAlbumName -> {
Expand All @@ -97,18 +96,21 @@ class AddAlbumViewModel @Inject constructor(
is AddAlbumEvent.ReflectAlbumName -> {
if (event.newAlbumName != _state.value.initialAlbumName) {
viewModelScope.launch {
_state.update { it.copy(
initialAlbumName = event.newAlbumName,
displayedAlbumName = event.newAlbumName,
coverUri = it.coverUri,
wallpapers = it.wallpapers.map { wallpaper ->
_state.update {
val wallpapers = it.wallpapers.map { wallpaper ->
wallpaper.copy(initialAlbumName = event.newAlbumName)
},
folders = it.folders.map { folder ->
}
val folders = it.folders.map { folder ->
folder.copy(initialAlbumName = event.newAlbumName)
},
) }
updateIsEmpty()
}
it.copy(
initialAlbumName = event.newAlbumName,
displayedAlbumName = event.newAlbumName,
coverUri = it.coverUri,
wallpapers = wallpapers,
folders = folders,
isEmpty = wallpapers.isEmpty() && folders.isEmpty()
) }
}
}
}
Expand Down Expand Up @@ -167,9 +169,9 @@ class AddAlbumViewModel @Inject constructor(
_state.update { it.copy(
selectedFolders = _state.value.folders.map { folder -> folder.folderUri },
selectedWallpapers = _state.value.wallpapers.map { wallpaper -> wallpaper.wallpaperUri },
selectedCount = it.folders.size + it.wallpapers.size
selectedCount = it.folders.size + it.wallpapers.size,
allSelected = true
) }
updateAllSelected()
}
}
}
Expand All @@ -186,44 +188,53 @@ class AddAlbumViewModel @Inject constructor(
is AddAlbumEvent.SelectFolder -> {
viewModelScope.launch {
if (!_state.value.selectedFolders.any { it == event.directoryUri }) {
_state.update { it.copy(
selectedFolders = it.selectedFolders.plus(event.directoryUri),
selectedCount = it.selectedCount + 1
) }
updateAllSelected()
_state.update {
val folders = it.selectedFolders.plus(event.directoryUri)
it.copy(
selectedFolders = folders,
selectedCount = it.selectedCount + 1,
allSelected = folders.size + it.selectedWallpapers.size >= it.wallpapers.size + it.folders.size
) }
}
}
}
is AddAlbumEvent.SelectWallpaper -> {
viewModelScope.launch {
if (!_state.value.selectedWallpapers.any { it == event.wallpaperUri }) {
_state.update { it.copy(
selectedWallpapers = it.selectedWallpapers.plus(event.wallpaperUri),
selectedCount = it.selectedCount + 1
) }
updateAllSelected()
_state.update {
val wallpapers = it.selectedWallpapers.plus(event.wallpaperUri)
it.copy(
selectedWallpapers = wallpapers,
selectedCount = it.selectedCount + 1,
allSelected = it.selectedFolders.size + wallpapers.size >= it.wallpapers.size + it.folders.size
) }
}
}
}
is AddAlbumEvent.RemoveFolderFromSelection -> {
viewModelScope.launch {
if (_state.value.selectedFolders.find { it == event.directoryUri } != null ) {
_state.update { it.copy(
selectedFolders = it.selectedFolders.minus(event.directoryUri),
selectedCount = it.selectedCount - 1
) }
updateAllSelected()
_state.update {
val folders = it.selectedFolders.minus(event.directoryUri)
it.copy(
selectedFolders = folders,
selectedCount = it.selectedCount - 1,
allSelected = folders.size + it.selectedWallpapers.size >= it.wallpapers.size + it.folders.size

) }
}
}
}
is AddAlbumEvent.RemoveWallpaperFromSelection -> {
viewModelScope.launch {
if (_state.value.selectedWallpapers.find { it == event.wallpaperUri } != null ) {
_state.update { it.copy(
selectedWallpapers = it.selectedWallpapers.minus(event.wallpaperUri),
selectedCount = it.selectedCount - 1
) }
updateAllSelected()
_state.update {
val wallpapers = it.selectedWallpapers.minus(event.wallpaperUri)
it.copy(
selectedWallpapers = wallpapers,
selectedCount = it.selectedCount - 1,
allSelected = it.selectedFolders.size + wallpapers.size >= it.wallpapers.size + it.folders.size
) }
}
}
}
Expand All @@ -246,22 +257,6 @@ class AddAlbumViewModel @Inject constructor(
}
}

private fun updateIsEmpty() {
viewModelScope.launch {
_state.update { it.copy(
isEmpty = it.wallpapers.isEmpty() && it.folders.isEmpty()
) }
}
}

private fun updateAllSelected() {
viewModelScope.launch {
_state.update { it.copy(
allSelected = it.selectedFolders.size + it.selectedWallpapers.size >= it.wallpapers.size + it.folders.size
) }
}
}

private fun getWallpaperFromFolder(folderUri: String, context: Context): List<String> {
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,17 +22,17 @@ fun DeleteImagesAlertDialog(
icon = {
Icon(Icons.Outlined.Delete, contentDescription = stringResource(R.string.delete_confirmation))
},
title = { Text(text = "Delete these?") },
text = { Text(text = "Are you sure you want to delete these wallpapers?") },
title = { Text(text = stringResource(R.string.delete_these)) },
text = { Text(text = stringResource(R.string.are_you_sure_you_want_to_delete_these_wallpapers)) },
onDismissRequest = { onDismissRequest() },
confirmButton = {
TextButton(onClick = { onConfirmation() }) {
Text("Confirm")
Text(stringResource(R.string.confirm))
}
},
dismissButton = {
TextButton(onClick = { onDismissRequest() }) {
Text("Cancel")
Text(stringResource(R.string.cancel))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.album_view_screen

import android.app.Application
import android.content.Context
import android.webkit.MimeTypeMap
import androidx.core.net.toUri
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
Expand Down Expand Up @@ -39,10 +38,10 @@ class AlbumViewScreenViewModel @Inject constructor(
event.albumsWithWallpaper.let { album ->
val folders = album.folders.filter { _state.value.selectedFolders.contains(it.folderUri)}
val wallpapers = album.wallpapers.filter { _state.value.selectedWallpapers.contains(it.wallpaperUri)}
val doesContainCover = wallpapers.any { it.wallpaperUri == album.album.coverUri }
val containsCoverUri = wallpapers.any { it.wallpaperUri == album.album.coverUri }
repository.deleteFolderList(folders)
repository.deleteWallpaperList(wallpapers)
if (doesContainCover) {
if (containsCoverUri) {
repository.updateAlbum(album.album.copy(coverUri = null))
}
}
Expand All @@ -52,15 +51,15 @@ class AlbumViewScreenViewModel @Inject constructor(
viewModelScope.launch {
event.albumsWithWallpaper.let { album ->
if (!_state.value.allSelected) {
_state.update { state ->
state.copy(
_state.update { it ->
it.copy(
selectedFolders = album.folders.map { it.folderUri },
selectedWallpapers = album.wallpapers.map { it.wallpaperUri },
selectedCount = album.folders.size + album.wallpapers.size,
maxSize = album.folders.size + album.wallpapers.size
maxSize = album.folders.size + album.wallpapers.size,
allSelected = true
)
}
updateAllSelected()
}
}
}
Expand All @@ -83,12 +82,13 @@ class AlbumViewScreenViewModel @Inject constructor(
viewModelScope.launch {
if (!_state.value.selectedFolders.any { it == event.directoryUri }) {
_state.update {
val folders = it.selectedFolders.plus(event.directoryUri)
it.copy(
selectedFolders = it.selectedFolders.plus(event.directoryUri),
selectedCount = it.selectedCount + 1
selectedFolders = folders,
selectedCount = it.selectedCount + 1,
allSelected = folders.size + it.selectedWallpapers.size >= it.maxSize
)
}
updateAllSelected()
}
}
}
Expand All @@ -97,12 +97,13 @@ class AlbumViewScreenViewModel @Inject constructor(
viewModelScope.launch {
if (!_state.value.selectedWallpapers.any { it == event.wallpaperUri }) {
_state.update {
val wallpapers = it.selectedWallpapers.plus(event.wallpaperUri)
it.copy(
selectedWallpapers = it.selectedWallpapers.plus(event.wallpaperUri),
selectedCount = it.selectedCount + 1
selectedCount = it.selectedCount + 1,
allSelected = it.selectedFolders.size + wallpapers.size >= it.maxSize
)
}
updateAllSelected()
}
}
}
Expand All @@ -111,12 +112,13 @@ class AlbumViewScreenViewModel @Inject constructor(
viewModelScope.launch {
if (_state.value.selectedFolders.find { it == event.directoryUri } != null) {
_state.update {
val folders = it.selectedFolders.minus(event.directoryUri)
it.copy(
selectedFolders = it.selectedFolders.minus(event.directoryUri),
selectedCount = it.selectedCount - 1
selectedFolders = folders,
selectedCount = it.selectedCount - 1,
allSelected = folders.size + it.selectedWallpapers.size >= it.maxSize
)
}
updateAllSelected()
}
}
}
Expand All @@ -125,12 +127,13 @@ class AlbumViewScreenViewModel @Inject constructor(
viewModelScope.launch {
if (_state.value.selectedWallpapers.find { it == event.wallpaperUri } != null) {
_state.update {
val wallpapers = it.selectedWallpapers.minus(event.wallpaperUri)
it.copy(
selectedWallpapers = it.selectedWallpapers.minus(event.wallpaperUri),
selectedCount = it.selectedCount - 1
selectedWallpapers = wallpapers,
selectedCount = it.selectedCount - 1,
allSelected = it.selectedFolders.size + wallpapers.size >= it.maxSize
)
}
updateAllSelected()
}
}
}
Expand Down Expand Up @@ -161,22 +164,22 @@ class AlbumViewScreenViewModel @Inject constructor(

is AlbumViewEvent.AddWallpapers -> {
viewModelScope.launch {
val newWallpaperUris = event.wallpaperUris.filterNot { it in event.album.wallpapers.map { wallpaper -> wallpaper.wallpaperUri } }
val newWallpapers = newWallpaperUris.map { uri ->
val wallpaperUris = event.wallpaperUris.filterNot { it in event.album.wallpapers.map { wallpaper -> wallpaper.wallpaperUri } }
val wallpapers = wallpaperUris.map { uri ->
Wallpaper(
initialAlbumName = event.album.album.initialAlbumName,
wallpaperUri = uri,
key = uri.hashCode() + event.album.album.initialAlbumName.hashCode(),
)
}
repository.upsertWallpaperList(newWallpapers)
repository.upsertWallpaperList(wallpapers)
_state.update {
it.copy(
selectedFolders = emptyList(),
selectedWallpapers = emptyList(),
allSelected = false,
selectedCount = 0,
maxSize = _state.value.maxSize + newWallpapers.size
maxSize = it.maxSize + wallpapers.size
)
}
}
Expand All @@ -203,7 +206,7 @@ class AlbumViewScreenViewModel @Inject constructor(
selectedWallpapers = emptyList(),
allSelected = false,
selectedCount = 0,
maxSize = _state.value.maxSize + 1
maxSize = it.maxSize + 1
)
}
}
Expand All @@ -212,16 +215,6 @@ class AlbumViewScreenViewModel @Inject constructor(
}
}

private fun updateAllSelected() {
viewModelScope.launch {
_state.update {
it.copy(
allSelected = it.selectedFolders.size + it.selectedWallpapers.size >= _state.value.maxSize
)
}
}
}

private fun getWallpaperFromFolder(folderUri: String, context: Context): List<String> {
val folderDocumentFile = DocumentFileCompat.fromTreeUri(context, folderUri.toUri())
return listFilesRecursive(folderDocumentFile, context)
Expand All @@ -233,9 +226,8 @@ class AlbumViewScreenViewModel @Inject constructor(
if (file.isDirectory()) {
files.addAll(listFilesRecursive(file, context))
} else {
val extension = MimeTypeMap.getFileExtensionFromUrl(file.uri.toString())
val allowedExtensions = listOf("jpg", "png", "heif", "webp")
if (extension in allowedExtensions) {
val allowedExtensions = listOf("jpg", "jpeg", "png", "heif", "webp", "JPG", "JPEG", "PNG", "HEIF", "WEBP")
if (file.extension in allowedExtensions) {
files.add(file.uri.toString())
}
}
Expand Down
Loading

0 comments on commit 8eb9cb2

Please sign in to comment.