Skip to content

Commit 8b59e41

Browse files
committed
refactor: use suspend functions for database actions
1 parent 07a2cd3 commit 8b59e41

7 files changed

Lines changed: 44 additions & 46 deletions

File tree

‎app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt‎

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import kotlinx.coroutines.flow.Flow
1111
@Dao
1212
interface FavoritesDao {
1313
@Query("SELECT * FROM favorites")
14-
fun getAll(): List<Wallpaper>
14+
suspend fun getAll(): List<Wallpaper>
1515

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

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

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

2828
@Query("SELECT * FROM favorites WHERE imgSrc = :imgSrc")
29-
fun findByImgSrc(imgSrc: String): Wallpaper?
29+
suspend fun findByImgSrc(imgSrc: String): Wallpaper?
3030

3131
/**
3232
* Do not use this method directly unless when restoring backups
3333
*/
3434
@Insert(onConflict = OnConflictStrategy.REPLACE)
35-
fun insertAll(wallpapers: List<Wallpaper>)
35+
suspend fun insertAll(wallpapers: List<Wallpaper>)
3636

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

4040
wallpaper.favorite = isFavorite ?: existingWallpaper?.favorite ?: false
@@ -49,14 +49,14 @@ interface FavoritesDao {
4949
}
5050

5151
@Update
52-
fun updateWallpaper(wallpaper: Wallpaper)
52+
suspend fun updateWallpaper(wallpaper: Wallpaper)
5353

54-
fun removeFromFavorites(wallpaper: Wallpaper) {
54+
suspend fun removeFromFavorites(wallpaper: Wallpaper) {
5555
insertAll(listOf(wallpaper.copy(favorite = false)))
5656

5757
cleanup()
5858
}
5959

6060
@Query("DELETE FROM favorites WHERE favorite = 0 and inHistory = 0")
61-
fun cleanup()
61+
suspend fun cleanup()
6262
}

‎app/src/main/java/com/bnyro/wallpaper/ext/AwaitQuery.kt‎

Lines changed: 0 additions & 12 deletions
This file was deleted.

‎app/src/main/java/com/bnyro/wallpaper/ext/Query.kt‎

Lines changed: 0 additions & 5 deletions
This file was deleted.

‎app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import androidx.compose.runtime.derivedStateOf
2525
import androidx.compose.runtime.getValue
2626
import androidx.compose.runtime.mutableStateOf
2727
import androidx.compose.runtime.remember
28+
import androidx.compose.runtime.rememberCoroutineScope
2829
import androidx.compose.runtime.setValue
2930
import androidx.compose.ui.Alignment
3031
import androidx.compose.ui.Modifier
@@ -34,7 +35,9 @@ import androidx.compose.ui.unit.dp
3435
import coil.compose.AsyncImage
3536
import com.bnyro.wallpaper.db.DatabaseHolder.Database
3637
import com.bnyro.wallpaper.db.obj.Wallpaper
37-
import com.bnyro.wallpaper.ext.query
38+
import kotlinx.coroutines.Dispatchers
39+
import kotlinx.coroutines.launch
40+
import kotlinx.coroutines.withContext
3841

3942
@Composable
4043
fun WallpaperGrid(
@@ -67,7 +70,7 @@ fun WallpaperGrid(
6770
}
6871

6972
LaunchedEffect(true) {
70-
query {
73+
withContext(Dispatchers.IO) {
7174
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
7275
}
7376
}
@@ -96,12 +99,14 @@ fun WallpaperGrid(
9699
.clip(CircleShape)
97100
.background(MaterialTheme.colorScheme.primaryContainer)
98101
) {
102+
val scope = rememberCoroutineScope()
103+
99104
ButtonWithIcon(
100105
modifier = Modifier,
101106
if (liked) Icons.Default.Favorite else Icons.Default.FavoriteBorder
102107
) {
103108
liked = !liked
104-
query {
109+
scope.launch(Dispatchers.IO) {
105110
if (!liked) {
106111
Database.favoritesDao().removeFromFavorites(wallpaper)
107112
} else {
Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,18 @@
11
package com.bnyro.wallpaper.ui.components
22

33
import androidx.compose.runtime.Composable
4+
import androidx.compose.runtime.rememberCoroutineScope
45
import androidx.compose.ui.platform.LocalContext
56
import androidx.compose.ui.res.stringResource
67
import com.bnyro.wallpaper.R
78
import com.bnyro.wallpaper.db.DatabaseHolder
89
import com.bnyro.wallpaper.db.obj.Wallpaper
9-
import com.bnyro.wallpaper.ext.awaitQuery
1010
import com.bnyro.wallpaper.ui.components.dialogs.ListDialog
1111
import com.bnyro.wallpaper.ui.models.WallpaperHelperModel
1212
import com.bnyro.wallpaper.util.Preferences
13+
import kotlinx.coroutines.Dispatchers
14+
import kotlinx.coroutines.launch
15+
import kotlinx.coroutines.withContext
1316

1417
@Composable
1518
fun WallpaperModeDialog(
@@ -19,6 +22,7 @@ fun WallpaperModeDialog(
1922
applyFilter: Boolean = false
2023
) {
2124
val context = LocalContext.current
25+
val scope = rememberCoroutineScope()
2226

2327
ListDialog(
2428
title = stringResource(R.string.set_wallpaper),
@@ -30,22 +34,24 @@ fun WallpaperModeDialog(
3034
),
3135
onDismissRequest = onDismissRequest,
3236
onClick = { index ->
33-
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
34-
awaitQuery {
37+
scope.launch(Dispatchers.IO) {
38+
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
3539
DatabaseHolder.Database.favoritesDao()
3640
.insert(wallpaper, null, true)
3741
}
38-
}
39-
if (index == 3) {
40-
wallpaperHelperModel.setWallpaperWith(context, wallpaper)
41-
} else {
42-
if (applyFilter) {
43-
wallpaperHelperModel.setWallpaperWithFilter(wallpaper = wallpaper, index)
44-
} else {
45-
wallpaperHelperModel.setWallpaper(wallpaper = wallpaper, index)
42+
withContext(Dispatchers.Main) {
43+
if (index == 3) {
44+
wallpaperHelperModel.setWallpaperWith(context, wallpaper)
45+
} else {
46+
if (applyFilter) {
47+
wallpaperHelperModel.setWallpaperWithFilter(wallpaper = wallpaper, index)
48+
} else {
49+
wallpaperHelperModel.setWallpaper(wallpaper = wallpaper, index)
50+
}
51+
}
52+
onDismissRequest.invoke()
4653
}
4754
}
48-
onDismissRequest.invoke()
4955
}
5056
)
5157
}

‎app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperView.kt‎

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import androidx.compose.runtime.MutableState
2020
import androidx.compose.runtime.getValue
2121
import androidx.compose.runtime.mutableStateOf
2222
import androidx.compose.runtime.remember
23+
import androidx.compose.runtime.rememberCoroutineScope
2324
import androidx.compose.runtime.setValue
2425
import androidx.compose.ui.Alignment
2526
import androidx.compose.ui.Modifier
@@ -38,7 +39,6 @@ import com.bnyro.wallpaper.R
3839
import com.bnyro.wallpaper.db.DatabaseHolder.Database
3940
import com.bnyro.wallpaper.db.obj.Wallpaper
4041
import com.bnyro.wallpaper.enums.MultiState
41-
import com.bnyro.wallpaper.ext.query
4242
import com.bnyro.wallpaper.ui.components.bottombar.BottomBar
4343
import com.bnyro.wallpaper.ui.components.bottombar.WallpaperViewTopBar
4444
import com.bnyro.wallpaper.ui.components.dialogs.MultiStateDialog
@@ -47,6 +47,9 @@ import com.bnyro.wallpaper.ui.models.WallpaperHelperModel
4747
import com.bnyro.wallpaper.ext.rememberZoomState
4848
import com.bnyro.wallpaper.ext.zoomArea
4949
import com.bnyro.wallpaper.ext.zoomImage
50+
import kotlinx.coroutines.Dispatchers
51+
import kotlinx.coroutines.launch
52+
import kotlinx.coroutines.withContext
5053
import java.time.Instant
5154

5255
@Composable
@@ -62,7 +65,7 @@ fun WallpaperView(
6265
var showModeSelection by remember { mutableStateOf(false) }
6366
var liked by remember { mutableStateOf(false) }
6467
LaunchedEffect(true) {
65-
query {
68+
withContext(Dispatchers.IO) {
6669
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
6770
}
6871
}
@@ -139,6 +142,8 @@ fun WallpaperView(
139142
.align(Alignment.BottomCenter),
140143
horizontalAlignment = Alignment.CenterHorizontally
141144
) {
145+
val scope = rememberCoroutineScope()
146+
142147
AnimatedVisibility(
143148
visible = showUi
144149
) {
@@ -161,7 +166,7 @@ fun WallpaperView(
161166
},
162167
onClickFavourite = {
163168
liked = !liked
164-
query {
169+
scope.launch(Dispatchers.IO) {
165170
if (!liked) {
166171
Database.favoritesDao().removeFromFavorites(wallpaper)
167172
} else {

‎app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt‎

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import com.bnyro.wallpaper.db.DatabaseHolder
88
import com.bnyro.wallpaper.db.obj.Wallpaper
99
import com.bnyro.wallpaper.obj.WallpaperConfig
1010
import com.bnyro.wallpaper.enums.WallpaperSource
11-
import com.bnyro.wallpaper.ext.awaitQuery
1211
import kotlinx.coroutines.Dispatchers
1312
import kotlinx.coroutines.withContext
1413

@@ -66,7 +65,7 @@ class BackgroundWorker(
6665
}
6766

6867
private suspend fun getFavoritesWallpaper(): Bitmap? {
69-
val favoriteUrl = awaitQuery {
68+
val favoriteUrl = withContext(Dispatchers.IO) {
7069
DatabaseHolder.Database.favoritesDao().getFavorites()
7170
}.randomOrNull()?.imgSrc
7271
return ImageHelper.getSuspend(applicationContext, favoriteUrl, true)

0 commit comments

Comments
 (0)