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