Skip to content

Commit

Permalink
refactor: use kotlin flows for favourite wallpapers
Browse files Browse the repository at this point in the history
  • Loading branch information
SuhasDissa committed Apr 9, 2024
1 parent 1699c55 commit beae674
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 20 deletions.
4 changes: 4 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.bnyro.wallpaper.db.obj.Wallpaper
import kotlinx.coroutines.flow.Flow

@Dao
interface FavoritesDao {
@Query("SELECT * FROM favorites")
fun getAll(): List<Wallpaper>

@Query("SELECT * FROM favorites")
fun getAllFlow(): Flow<List<Wallpaper>>

@Query("SELECT * FROM favorites WHERE imgSrc LIKE :imgSrc")
fun findBySrc(imgSrc: String): Wallpaper

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.bnyro.wallpaper.ui.activities

import android.os.Bundle
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
Expand Down Expand Up @@ -36,7 +37,7 @@ class MainActivity : BaseActivity() {
super.onCreate(savedInstanceState)

val viewModel: MainModel = ViewModelProvider(this).get()

enableEdgeToEdge()
showContent {
MainContent(viewModel)
}
Expand Down
13 changes: 12 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,15 @@ import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.api.Api
import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.enums.ThemeMode
import com.bnyro.wallpaper.ui.nav.DrawerScreens
import com.bnyro.wallpaper.util.Preferences
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.stateIn
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

Expand All @@ -30,6 +33,14 @@ class MainModel : ViewModel() {
var wallpapers by mutableStateOf(
listOf<Wallpaper>()
)

val favWallpapers: StateFlow<List<Wallpaper>> =
DatabaseHolder.Database.favoritesDao().getAllFlow().stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = listOf()
)

var titleResource by mutableIntStateOf(R.string.app_name)

var page: Int = 1
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fun AppNavHost(
}
composable(DrawerScreens.Favorites.route) {
viewModel.titleResource = R.string.favorites
FavoritesPage()
FavoritesPage(viewModel)
}
composable(DrawerScreens.Settings.route) {
viewModel.titleResource = R.string.settings
Expand Down
18 changes: 4 additions & 14 deletions app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import androidx.compose.material.icons.filled.HeartBroken
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
Expand All @@ -24,30 +24,20 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.db.DatabaseHolder.Database
import com.bnyro.wallpaper.db.obj.Wallpaper
import com.bnyro.wallpaper.ext.query
import com.bnyro.wallpaper.ui.components.WallpaperGrid
import com.bnyro.wallpaper.ui.components.WallpaperPageView
import com.bnyro.wallpaper.ui.models.MainModel

@Composable
fun FavoritesPage() {
fun FavoritesPage(viewModel: MainModel) {
Column(
modifier = Modifier
.fillMaxSize()
) {
var favorites by remember {
mutableStateOf(listOf<Wallpaper>())
}
val favorites by viewModel.favWallpapers.collectAsState()

var selectedIndex by remember { mutableStateOf<Int?>(null) }

LaunchedEffect(true) {
query {
favorites = Database.favoritesDao().getAll()
}
}

if (favorites.isNotEmpty()) {
WallpaperGrid(
wallpapers = favorites,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import androidx.compose.material3.lightColorScheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.SideEffect
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalView
import androidx.core.view.WindowCompat
Expand Down Expand Up @@ -47,6 +46,7 @@ fun WallYouTheme(
val context = LocalContext.current
if (darkTheme) dynamicDarkColorScheme(context) else dynamicLightColorScheme(context)
}

darkTheme -> DarkColorScheme
else -> LightColorScheme
}
Expand All @@ -55,8 +55,6 @@ fun WallYouTheme(
SideEffect {
val activity = view.context as Activity
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
activity.window.navigationBarColor = colorScheme.background.toArgb()
activity.window.statusBarColor = colorScheme.background.toArgb()
WindowCompat.getInsetsController(
activity.window,
view
Expand Down

0 comments on commit beae674

Please sign in to comment.