Fixed animations

Fixed an issue where selecting an album and then canceling it immediately would sometimes occur out of order due to async nature
This commit is contained in:
Anthonyy232 committed 2024-05-13 23:47:57 -07:00
1 parent 742d14ae3b
commit 626b0a4de5
11 files changed
+129 -89

No files matched your search

@@ -15,6 +15,7 @@ class SelectedAlbumRepositoryImpl(
}
override suspend fun upsertSelectedAlbum(selectedAlbum: SelectedAlbum) {
dao.deleteAll()
dao.upsertAlbum(selectedAlbum.album)
dao.upsertWallpaperList(selectedAlbum.wallpapers)
}
@@ -85,7 +85,6 @@ fun PaperizeApp(
albumsViewModel.onEvent(AlbumsEvent.DeleteAlbumWithWallpapers(albumWithWallpapers))
}
}
selectedState.value.selectedAlbum?.let { selectedAlbum ->
albumState.value.albumsWithWallpapers.find { it.album.initialAlbumName == selectedAlbum.album.initialAlbumName }?.let { foundAlbum ->
val albumNameHashCode = foundAlbum.album.initialAlbumName.hashCode()
@@ -107,7 +106,7 @@ fun PaperizeApp(
),
wallpapers = wallpapers
)
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(newSelectedAlbum))
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(newSelectedAlbum, null))
settingsViewModel.onEvent(SettingsEvent.SetWallpaperInterval(settingsState.value.interval))
if (settingsState.value.enableChanger) { // Not ideal but no other option to check if service is running (thanks Android)
val intent = Intent(context, WallpaperService::class.java).apply {
@@ -248,6 +247,15 @@ fun PaperizeApp(
context.startForegroundService(it)
}
}
},
onSelectAlbum = {album ->
settingsViewModel.onEvent(SettingsEvent.SetChangerToggle(true))
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(null, album))
val intent = Intent(context, WallpaperService::class.java).apply {
action = WallpaperService.Actions.START.toString()
putExtra("timeInMinutes", settingsState.value.interval)
}
context.startForegroundService(intent)
}
)
}
@@ -22,6 +22,7 @@ import androidx.compose.runtime.setValue
import androidx.compose.runtime.snapshotFlow
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp
import com.anthonyla.paperize.feature.wallpaper.domain.model.AlbumWithWallpaperAndFolder
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
import com.anthonyla.paperize.feature.wallpaper.presentation.add_album_screen.components.AddAlbumDialog
import com.anthonyla.paperize.feature.wallpaper.presentation.home_screen.components.HomeTopBar
@@ -37,6 +38,7 @@ fun HomeScreen(
onScheduleWallpaperChanger: (Int) -> Unit,
onSetLockWithHome: (Boolean) -> Unit,
onToggleChanger: (Boolean) -> Unit,
onSelectAlbum: (AlbumWithWallpaperAndFolder) -> Unit,
onStop: () -> Unit,
animate : Boolean,
interval: Int,
@@ -129,7 +131,8 @@ fun HomeScreen(
onSetLockWithHome = onSetLockWithHome,
selectedAlbum = selectedAlbum,
enableChanger = enableChanger,
onToggleChanger = onToggleChanger
onToggleChanger = onToggleChanger,
onSelectAlbum = onSelectAlbum
)
1 -> LibraryScreen(
onAddNewAlbumClick = { addAlbumDialog = true },
@@ -63,13 +63,7 @@ fun LibraryScreen(
AlbumItem(
album = album.album,
onAlbumViewClick = { onViewAlbum(album.album.initialAlbumName) },
modifier = Modifier.padding(4.dp).animateItem(
placementSpec = tween(
durationMillis = 800,
delayMillis = 0,
easing = FastOutSlowInEasing
),
),
modifier = Modifier.padding(4.dp),
animate = animate
)
}
@@ -33,6 +33,7 @@ import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.saveable.rememberSaveable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
@@ -129,6 +130,7 @@ fun NotificationScreen(
progress = { progress },
modifier = Modifier
.fillMaxHeight(0.5f)
.align(Alignment.CenterHorizontally)
.semantics {
contentDescription =
context.getString(R.string.notification_bell_animation)
@@ -92,6 +92,7 @@ fun StartupScreen(
progress = { progress },
modifier = Modifier
.fillMaxHeight(0.5f)
.align(Alignment.CenterHorizontally)
.semantics { contentDescription = context.getString(R.string.welcome_animation) },
safeMode = true,
enableMergePaths = true
@@ -1,9 +1,10 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen
import com.anthonyla.paperize.feature.wallpaper.domain.model.AlbumWithWallpaperAndFolder
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
sealed class WallpaperEvent {
data class UpdateSelectedAlbum(val selectedAlbum: SelectedAlbum): WallpaperEvent()
data class UpdateSelectedAlbum(val selectedAlbum: SelectedAlbum?, val album: AlbumWithWallpaperAndFolder?): WallpaperEvent()
object Reset : WallpaperEvent()
object Refresh : WallpaperEvent()
}
@@ -27,11 +27,9 @@ import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.anthonyla.paperize.R
import com.anthonyla.paperize.feature.wallpaper.domain.model.AlbumWithWallpaperAndFolder
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
import com.anthonyla.paperize.feature.wallpaper.presentation.album.AlbumsViewModel
import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.SettingsEvent
import com.anthonyla.paperize.feature.wallpaper.presentation.settings_screen.SettingsViewModel
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.AlbumBottomSheet
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.CurrentAndNextChange
import com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components.CurrentSelectedAlbum
@@ -42,11 +40,10 @@ import kotlinx.coroutines.launch
@Composable
fun WallpaperScreen(
albumsViewModel: AlbumsViewModel = hiltViewModel(),
wallpaperScreenViewModel: WallpaperScreenViewModel = hiltViewModel(),
settingsViewModel: SettingsViewModel = hiltViewModel(),
onScheduleWallpaperChanger: (Int) -> Unit,
onSetLockWithHome: (Boolean) -> Unit,
onToggleChanger: (Boolean) -> Unit,
onSelectAlbum: (AlbumWithWallpaperAndFolder) -> Unit,
onStop: () -> Unit,
animate: Boolean,
interval: Int,
@@ -199,28 +196,9 @@ fun WallpaperScreen(
currentSelectedAlbum = selectedAlbum,
onDismiss = { openBottomSheet = false },
onSelect = { album ->
val wallpapers: List<Wallpaper> = album.wallpapers + album.folders.asSequence().flatMap { folder ->
folder.wallpapers.asSequence().map { wallpaper ->
Wallpaper(
initialAlbumName = album.album.initialAlbumName,
wallpaperUri = wallpaper,
key = wallpaper.hashCode() + album.album.initialAlbumName.hashCode(),
)
}
}.toList()
val shuffledWallpapers = wallpapers.map { it.wallpaperUri }.shuffled()
val newSelectedAlbum = SelectedAlbum(
album = album.album.copy(
wallpapersInQueue = shuffledWallpapers,
currentWallpaper = shuffledWallpapers.firstOrNull()
),
wallpapers = wallpapers
)
settingsViewModel.onEvent(SettingsEvent.SetChangerToggle(true))
wallpaperScreenViewModel.onEvent(WallpaperEvent.UpdateSelectedAlbum(newSelectedAlbum))
openBottomSheet = false
onSelectAlbum(album)
onScheduleWallpaperChanger(interval)
onToggleChanger(true)
},
animate = animate
)
@@ -3,6 +3,8 @@ package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen
import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.viewModelScope
import com.anthonyla.paperize.feature.wallpaper.domain.model.SelectedAlbum
import com.anthonyla.paperize.feature.wallpaper.domain.model.Wallpaper
import com.anthonyla.paperize.feature.wallpaper.domain.repository.SelectedAlbumRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.Dispatchers
@@ -41,8 +43,39 @@ class WallpaperScreenViewModel @Inject constructor (
when (event) {
is WallpaperEvent.UpdateSelectedAlbum -> {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
repository.upsertSelectedAlbum(event.selectedAlbum)
if (event.selectedAlbum != null) {
_state.update {
it.copy(
selectedAlbum = event.selectedAlbum
)
}
repository.upsertSelectedAlbum(event.selectedAlbum)
}
else if (event.album != null) {
val wallpapers: List<Wallpaper> = event.album.wallpapers + event.album.folders.asSequence().flatMap { folder ->
folder.wallpapers.asSequence().map { wallpaper ->
Wallpaper(
initialAlbumName = event.album.album.initialAlbumName,
wallpaperUri = wallpaper,
key = wallpaper.hashCode() + event.album.album.initialAlbumName.hashCode(),
)
}
}.toList()
val shuffledWallpapers = wallpapers.map { it.wallpaperUri }.shuffled()
val newSelectedAlbum = SelectedAlbum(
album = event.album.album.copy(
wallpapersInQueue = shuffledWallpapers,
currentWallpaper = shuffledWallpapers.firstOrNull()
),
wallpapers = wallpapers
)
_state.update {
it.copy(
selectedAlbum = newSelectedAlbum
)
}
repository.upsertSelectedAlbum(newSelectedAlbum)
}
}
}
is WallpaperEvent.Refresh -> {
@@ -50,12 +83,12 @@ class WallpaperScreenViewModel @Inject constructor (
}
is WallpaperEvent.Reset -> {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
_state.update {
it.copy(
selectedAlbum = null
)
}
repository.deleteAll()
}
}
}
@@ -1,12 +1,13 @@
package com.anthonyla.paperize.feature.wallpaper.presentation.wallpaper_screen.components
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.animateContentSize
import androidx.compose.animation.core.LinearOutSlowInEasing
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.animation.expandVertically
import androidx.compose.animation.shrinkVertically
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.border
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
@@ -43,7 +44,16 @@ fun SetLockScreenSwitch(
.padding(PaddingValues(horizontal = 16.dp, vertical = 8.dp))
.clip(RoundedCornerShape(16.dp))
) {
Column (horizontalAlignment = Alignment.CenterHorizontally, verticalArrangement = Arrangement.Center) {
val columnModifier = if (animate) {
Modifier.animateContentSize(
animationSpec = tween(durationMillis = 300, easing = LinearOutSlowInEasing)
)
} else { Modifier }
Column (
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center,
modifier = columnModifier
) {
Row (horizontalArrangement = Arrangement.Center, verticalAlignment = Alignment.CenterVertically) {
Text(
text = stringResource(R.string.set_as_lock_screen),
@@ -59,7 +69,12 @@ fun SetLockScreenSwitch(
AnimatedVisibility(
visible = checked && albumUri != null,
enter = expandVertically(animationSpec = spring(dampingRatio = Spring.DampingRatioMediumBouncy, stiffness = Spring.StiffnessLow)),
exit = shrinkVertically(animationSpec = tween(durationMillis = 700, easing = LinearOutSlowInEasing))
exit = fadeOut(
animationSpec = tween(
durationMillis = 300,
easing = LinearOutSlowInEasing
)
)
) {
Row(Modifier.fillMaxWidth()) {
Column(
@@ -97,7 +97,7 @@ class WallpaperService: Service() {
}
}
}
handler.post(runnableCode)
handler.postDelayed(runnableCode, 3000)
}
Actions.STOP.toString() -> {
CoroutineScope(Dispatchers.IO).launch {
@@ -147,48 +147,18 @@ class WallpaperService: Service() {
settingsDataStoreImpl.putString(SettingsConstants.NEXT_SET_TIME, time.plusMinutes(timeInMinutes.toLong()).format(formatter))
val setLockWithHome = settingsDataStoreImpl.getBoolean(SettingsConstants.SET_LOCK_WITH_HOME) ?: false
val selectedAlbum = selectedRepository.getSelectedAlbum().first().firstOrNull()
selectedAlbum?.let { it ->
var wallpaper = it.album.wallpapersInQueue.firstOrNull()
// Pick the next wallpaper in the queue
if (wallpaper != null) {
val notification = createNotification(it.album.wallpapersInQueue.size, it.wallpapers.size)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
if (!setWallpaper(context, wallpaper.toUri(), setLockWithHome)) {
selectedAlbum.wallpapers.firstOrNull{ it.wallpaperUri == wallpaper }
?.let { it1 ->
selectedRepository.deleteWallpaper(it1)
selectedRepository.upsertSelectedAlbum(
it.copy(
album = it.album.copy(
wallpapersInQueue = it.album.wallpapersInQueue.drop(1),
currentWallpaper = null
),
wallpapers = it.wallpapers.filter { it == it1 },
)
)
albumRepository.deleteWallpaper(it1)
}
}
else {
selectedRepository.upsertSelectedAlbum(
it.copy(
album = it.album.copy(
wallpapersInQueue = it.album.wallpapersInQueue.drop(1),
currentWallpaper = wallpaper
)
)
)
}
}
// No more wallpapers in the queue -- reshuffle the wallpapers
else {
val notification = createNotification(it.wallpapers.size, it.wallpapers.size)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
val newWallpaperInQueue = it.wallpapers.map { it.wallpaperUri }.shuffled()
wallpaper = newWallpaperInQueue.firstOrNull()
if (selectedAlbum == null) {
onDestroy()
return
}
else {
selectedAlbum.let { it ->
var wallpaper = it.album.wallpapersInQueue.firstOrNull()
// Pick the next wallpaper in the queue
if (wallpaper != null) {
val notification = createNotification(it.album.wallpapersInQueue.size, it.wallpapers.size)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
if (!setWallpaper(context, wallpaper.toUri(), setLockWithHome)) {
selectedAlbum.wallpapers.firstOrNull{ it.wallpaperUri == wallpaper }
?.let { it1 ->
@@ -199,23 +169,57 @@ class WallpaperService: Service() {
wallpapersInQueue = it.album.wallpapersInQueue.drop(1),
currentWallpaper = null
),
wallpapers = it.wallpapers.filter { it == it1 }
wallpapers = it.wallpapers.filter { it == it1 },
)
)
albumRepository.deleteWallpaper(it1)
}
}
else {
} else {
selectedRepository.upsertSelectedAlbum(
it.copy(
album = it.album.copy(
wallpapersInQueue = newWallpaperInQueue.drop(1),
wallpapersInQueue = it.album.wallpapersInQueue.drop(1),
currentWallpaper = wallpaper
)
)
)
}
} else { onDestroy() }
}
// No more wallpapers in the queue -- reshuffle the wallpapers
else {
val notification = createNotification(it.wallpapers.size, it.wallpapers.size)
val notificationManager = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
notificationManager.notify(1, notification)
val newWallpaperInQueue = it.wallpapers.map { it.wallpaperUri }.shuffled()
wallpaper = newWallpaperInQueue.firstOrNull()
if (wallpaper != null) {
if (!setWallpaper(context, wallpaper.toUri(), setLockWithHome)) {
selectedAlbum.wallpapers.firstOrNull{ it.wallpaperUri == wallpaper }
?.let { it1 ->
selectedRepository.deleteWallpaper(it1)
selectedRepository.upsertSelectedAlbum(
it.copy(
album = it.album.copy(
wallpapersInQueue = it.album.wallpapersInQueue.drop(1),
currentWallpaper = null
),
wallpapers = it.wallpapers.filter { it == it1 }
)
)
albumRepository.deleteWallpaper(it1)
}
} else {
selectedRepository.upsertSelectedAlbum(
it.copy(
album = it.album.copy(
wallpapersInQueue = newWallpaperInQueue.drop(1),
currentWallpaper = wallpaper
)
)
)
}
} else { onDestroy() }
}
}
}
}