Skip to content

Commit

Permalink
Fixed animations
Browse files Browse the repository at this point in the history
Fixed an issue where selecting an album and then canceling it immediately would sometimes occur out of order due to async nature
  • Loading branch information
Anthonyy232 committed May 14, 2024
1 parent 742d14a commit 626b0a4
Show file tree
Hide file tree
Showing 11 changed files with 129 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class SelectedAlbumRepositoryImpl(
}

override suspend fun upsertSelectedAlbum(selectedAlbum: SelectedAlbum) {
dao.deleteAll()
dao.upsertAlbum(selectedAlbum.album)
dao.upsertWallpaperList(selectedAlbum.wallpapers)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
}
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -37,6 +38,7 @@ fun HomeScreen(
onScheduleWallpaperChanger: (Int) -> Unit,
onSetLockWithHome: (Boolean) -> Unit,
onToggleChanger: (Boolean) -> Unit,
onSelectAlbum: (AlbumWithWallpaperAndFolder) -> Unit,
onStop: () -> Unit,
animate : Boolean,
interval: Int,
Expand Down Expand Up @@ -129,7 +131,8 @@ fun HomeScreen(
onSetLockWithHome = onSetLockWithHome,
selectedAlbum = selectedAlbum,
enableChanger = enableChanger,
onToggleChanger = onToggleChanger
onToggleChanger = onToggleChanger,
onSelectAlbum = onSelectAlbum
)
1 -> LibraryScreen(
onAddNewAlbumClick = { addAlbumDialog = true },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -41,21 +43,52 @@ 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 -> {
refreshAlbums()
}
is WallpaperEvent.Reset -> {
viewModelScope.launch(Dispatchers.IO) {
repository.deleteAll()
_state.update {
it.copy(
selectedAlbum = null
)
}
repository.deleteAll()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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),
Expand All @@ -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(
Expand Down
Loading

0 comments on commit 626b0a4

Please sign in to comment.