Skip to content

Commit

Permalink
refactor: use suspend functions for database actions
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jun 12, 2024
1 parent 07a2cd3 commit 8b59e41
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 46 deletions.
18 changes: 9 additions & 9 deletions app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ import kotlinx.coroutines.flow.Flow
@Dao
interface FavoritesDao {
@Query("SELECT * FROM favorites")
fun getAll(): List<Wallpaper>
suspend fun getAll(): List<Wallpaper>

@Query("SELECT * FROM favorites WHERE favorite = 1")
fun getFavorites(): List<Wallpaper>
suspend fun getFavorites(): List<Wallpaper>

@Query("SELECT * FROM favorites WHERE favorite = 1 ORDER BY timeAdded DESC")
fun getFavoritesFlow(): Flow<List<Wallpaper>>
Expand All @@ -23,18 +23,18 @@ interface FavoritesDao {
fun getHistoryFlow(): Flow<List<Wallpaper>>

@Query("SELECT EXISTS (SELECT 1 FROM favorites WHERE favorite = 1 AND imgSrc = :imgSrc)")
fun isLiked(imgSrc: String): Boolean
suspend fun isLiked(imgSrc: String): Boolean

@Query("SELECT * FROM favorites WHERE imgSrc = :imgSrc")
fun findByImgSrc(imgSrc: String): Wallpaper?
suspend fun findByImgSrc(imgSrc: String): Wallpaper?

/**
* Do not use this method directly unless when restoring backups
*/
@Insert(onConflict = OnConflictStrategy.REPLACE)
fun insertAll(wallpapers: List<Wallpaper>)
suspend fun insertAll(wallpapers: List<Wallpaper>)

fun insert(wallpaper: Wallpaper, isFavorite: Boolean?, isHistory: Boolean?) {
suspend fun insert(wallpaper: Wallpaper, isFavorite: Boolean?, isHistory: Boolean?) {
val existingWallpaper = findByImgSrc(wallpaper.imgSrc)

wallpaper.favorite = isFavorite ?: existingWallpaper?.favorite ?: false
Expand All @@ -49,14 +49,14 @@ interface FavoritesDao {
}

@Update
fun updateWallpaper(wallpaper: Wallpaper)
suspend fun updateWallpaper(wallpaper: Wallpaper)

fun removeFromFavorites(wallpaper: Wallpaper) {
suspend fun removeFromFavorites(wallpaper: Wallpaper) {
insertAll(listOf(wallpaper.copy(favorite = false)))

cleanup()
}

@Query("DELETE FROM favorites WHERE favorite = 0 and inHistory = 0")
fun cleanup()
suspend fun cleanup()
}
12 changes: 0 additions & 12 deletions app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt

This file was deleted.

5 changes: 0 additions & 5 deletions app/src/main/java/com/bnyro/wallpaper/ext/Query.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import androidx.compose.runtime.derivedStateOf
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -34,7 +35,9 @@ import androidx.compose.ui.unit.dp
import coil.compose.AsyncImage
import com.bnyro.wallpaper.db.DatabaseHolder.Database
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.ext.query
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Composable
fun WallpaperGrid(
Expand Down Expand Up @@ -67,7 +70,7 @@ fun WallpaperGrid(
}

LaunchedEffect(true) {
query {
withContext(Dispatchers.IO) {
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
}
}
Expand Down Expand Up @@ -96,12 +99,14 @@ fun WallpaperGrid(
.clip(CircleShape)
.background(MaterialTheme.colorScheme.primaryContainer)
) {
val scope = rememberCoroutineScope()

ButtonWithIcon(
modifier = Modifier,
if (liked) Icons.Default.Favorite else Icons.Default.FavoriteBorder
) {
liked = !liked
query {
scope.launch(Dispatchers.IO) {
if (!liked) {
Database.favoritesDao().removeFromFavorites(wallpaper)
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
package com.bnyro.wallpaper.ui.components

import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.ext.awaitQuery
import com.bnyro.wallpaper.ui.components.dialogs.ListDialog
import com.bnyro.wallpaper.ui.models.WallpaperHelperModel
import com.bnyro.wallpaper.util.Preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

@Composable
fun WallpaperModeDialog(
Expand All @@ -19,6 +22,7 @@ fun WallpaperModeDialog(
applyFilter: Boolean = false
) {
val context = LocalContext.current
val scope = rememberCoroutineScope()

ListDialog(
title = stringResource(R.string.set_wallpaper),
Expand All @@ -30,22 +34,24 @@ fun WallpaperModeDialog(
),
onDismissRequest = onDismissRequest,
onClick = { index ->
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
awaitQuery {
scope.launch(Dispatchers.IO) {
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
DatabaseHolder.Database.favoritesDao()
.insert(wallpaper, null, true)
}
}
if (index == 3) {
wallpaperHelperModel.setWallpaperWith(context, wallpaper)
} else {
if (applyFilter) {
wallpaperHelperModel.setWallpaperWithFilter(wallpaper = wallpaper, index)
} else {
wallpaperHelperModel.setWallpaper(wallpaper = wallpaper, index)
withContext(Dispatchers.Main) {
if (index == 3) {
wallpaperHelperModel.setWallpaperWith(context, wallpaper)
} else {
if (applyFilter) {
wallpaperHelperModel.setWallpaperWithFilter(wallpaper = wallpaper, index)
} else {
wallpaperHelperModel.setWallpaper(wallpaper = wallpaper, index)
}
}
onDismissRequest.invoke()
}
}
onDismissRequest.invoke()
}
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import androidx.compose.runtime.MutableState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
Expand All @@ -38,7 +39,6 @@ import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.db.DatabaseHolder.Database
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.enums.MultiState
import com.bnyro.wallpaper.ext.query
import com.bnyro.wallpaper.ui.components.bottombar.BottomBar
import com.bnyro.wallpaper.ui.components.bottombar.WallpaperViewTopBar
import com.bnyro.wallpaper.ui.components.dialogs.MultiStateDialog
Expand All @@ -47,6 +47,9 @@ import com.bnyro.wallpaper.ui.models.WallpaperHelperModel
import com.bnyro.wallpaper.ext.rememberZoomState
import com.bnyro.wallpaper.ext.zoomArea
import com.bnyro.wallpaper.ext.zoomImage
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.time.Instant

@Composable
Expand All @@ -62,7 +65,7 @@ fun WallpaperView(
var showModeSelection by remember { mutableStateOf(false) }
var liked by remember { mutableStateOf(false) }
LaunchedEffect(true) {
query {
withContext(Dispatchers.IO) {
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
}
}
Expand Down Expand Up @@ -139,6 +142,8 @@ fun WallpaperView(
.align(Alignment.BottomCenter),
horizontalAlignment = Alignment.CenterHorizontally
) {
val scope = rememberCoroutineScope()

AnimatedVisibility(
visible = showUi
) {
Expand All @@ -161,7 +166,7 @@ fun WallpaperView(
},
onClickFavourite = {
liked = !liked
query {
scope.launch(Dispatchers.IO) {
if (!liked) {
Database.favoritesDao().removeFromFavorites(wallpaper)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.obj.WallpaperConfig
import com.bnyro.wallpaper.enums.WallpaperSource
import com.bnyro.wallpaper.ext.awaitQuery
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

Expand Down Expand Up @@ -66,7 +65,7 @@ class BackgroundWorker(
}

private suspend fun getFavoritesWallpaper(): Bitmap? {
val favoriteUrl = awaitQuery {
val favoriteUrl = withContext(Dispatchers.IO) {
DatabaseHolder.Database.favoritesDao().getFavorites()
}.randomOrNull()?.imgSrc
return ImageHelper.getSuspend(applicationContext, favoriteUrl, true)
Expand Down

0 comments on commit 8b59e41

Please sign in to comment.