Skip to content

Commit

Permalink
feat: history for recently applied wallpapers
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Jun 1, 2024
1 parent 422f916 commit 3e5faec
Show file tree
Hide file tree
Showing 19 changed files with 219 additions and 75 deletions.
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"formatVersion": 1,
"database": {
"version": 1,
"identityHash": "b8804b8b4eaa74f2f2977f3c05bc3500",
"version": 2,
"identityHash": "5649d298e25774a84b6587f2536bcf7a",
"entities": [
{
"tableName": "favorites",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`imgSrc` TEXT NOT NULL, `title` TEXT, `url` TEXT, `author` TEXT, `category` TEXT, `resolution` TEXT, `fileSize` INTEGER, `thumb` TEXT, `creationDate` TEXT, PRIMARY KEY(`imgSrc`))",
"createSql": "CREATE TABLE IF NOT EXISTS `${TABLE_NAME}` (`imgSrc` TEXT NOT NULL, `title` TEXT, `url` TEXT, `author` TEXT, `category` TEXT, `resolution` TEXT, `fileSize` INTEGER, `thumb` TEXT, `creationDate` TEXT, `favorite` INTEGER NOT NULL DEFAULT 1, `inHistory` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(`imgSrc`))",
"fields": [
{
"fieldPath": "imgSrc",
Expand Down Expand Up @@ -61,13 +61,27 @@
"columnName": "creationDate",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "favorite",
"columnName": "favorite",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "1"
},
{
"fieldPath": "inHistory",
"columnName": "inHistory",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"imgSrc"
],
"autoGenerate": false
]
},
"indices": [],
"foreignKeys": []
Expand All @@ -76,7 +90,7 @@
"views": [],
"setupQueries": [
"CREATE TABLE IF NOT EXISTS room_master_table (id INTEGER PRIMARY KEY,identity_hash TEXT)",
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b8804b8b4eaa74f2f2977f3c05bc3500')"
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, '5649d298e25774a84b6587f2536bcf7a')"
]
}
}
6 changes: 5 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/db/AppDatabase.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package com.bnyro.wallpaper.db

import androidx.room.AutoMigration
import androidx.room.Database
import androidx.room.RoomDatabase
import com.bnyro.wallpaper.db.dao.FavoritesDao
import com.bnyro.wallpaper.db.obj.Wallpaper

@Database(
version = 1,
version = 2,
autoMigrations = [
AutoMigration(1, 2)
],
entities = [
Wallpaper::class
]
Expand Down
48 changes: 36 additions & 12 deletions app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.bnyro.wallpaper.db.dao

import androidx.room.Dao
import androidx.room.Delete
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
Expand All @@ -13,21 +12,46 @@ interface FavoritesDao {
@Query("SELECT * FROM favorites")
fun getAll(): List<Wallpaper>

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

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

@Query("SELECT * FROM favorites WHERE imgSrc LIKE :imgSrc")
fun findBySrc(imgSrc: String): Wallpaper
@Query("SELECT * FROM favorites WHERE inHistory = 1")
fun getHistoryFlow(): Flow<List<Wallpaper>>

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

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

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

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

if (existingWallpaper != null) {
wallpaper.favorite = isFavorite ?: wallpaper.favorite
wallpaper.inHistory = isHistory ?: wallpaper.inHistory
}
}

insertAll(listOf(wallpaper))
}

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

@Delete
fun delete(wallpaper: Wallpaper)
cleanup()
}

@Query("DELETE FROM favorites")
fun deleteAll()
@Query("DELETE FROM favorites WHERE favorite = 0 and inHistory = 0")
fun cleanup()
}
4 changes: 3 additions & 1 deletion app/src/main/java/com/bnyro/wallpaper/db/obj/Wallpaper.kt
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,7 @@ data class Wallpaper(
@ColumnInfo val resolution: String? = null,
@ColumnInfo val fileSize: Long? = null,
@ColumnInfo val thumb: String? = null,
@ColumnInfo val creationDate: String? = null
@ColumnInfo val creationDate: String? = null,
@ColumnInfo(defaultValue = "1") var favorite: Boolean = false,
@ColumnInfo(defaultValue = "0") var inHistory: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,6 @@ fun NavigationDrawer(
val MinimumDrawerWidth = 240.dp
val MaximumDrawerWidth = 300.dp

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun DrawerSheet(
windowInsets: WindowInsets,
Expand Down
45 changes: 45 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ui/components/NothingHere.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.bnyro.wallpaper.ui.components

import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.size
import androidx.compose.material3.Icon
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp

@Composable
fun NothingHere(text: String, icon: ImageVector) {
Box(
modifier = Modifier.fillMaxSize()
) {
Column(
modifier = Modifier.align(Alignment.Center),
horizontalAlignment = Alignment.CenterHorizontally,
verticalArrangement = Arrangement.Center
) {
Icon(
icon,
null,
Modifier.size(150.dp)
)
Spacer(
modifier = Modifier.height(10.dp)
)
Text(
text,
fontSize = 20.sp,
fontWeight = FontWeight.Bold
)
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ fun WallpaperGrid(

LaunchedEffect(true) {
query {
wallpaper.imgSrc.let { src -> liked = Database.favoritesDao().exists(src) }
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
}
}

Expand Down Expand Up @@ -103,9 +103,9 @@ fun WallpaperGrid(
liked = !liked
query {
if (!liked) {
Database.favoritesDao().delete(wallpaper)
Database.favoritesDao().removeFromFavorites(wallpaper)
} else {
Database.favoritesDao().insertAll(wallpaper)
Database.favoritesDao().insert(wallpaper, true, null)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ fun WallpaperModeDialog(
applyFilter: Boolean = false
) {
val context = LocalContext.current

ListDialog(
title = stringResource(R.string.set_wallpaper),
items = listOf(
Expand All @@ -30,10 +31,11 @@ fun WallpaperModeDialog(
),
onDismissRequest = onDismissRequest,
onClick = { index ->
if (Preferences.getBoolean(Preferences.autoAddToFavoritesKey, false)) {
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
onLike.invoke()
awaitQuery {
DatabaseHolder.Database.favoritesDao().insertAll(wallpaper)
DatabaseHolder.Database.favoritesDao()
.insert(wallpaper, null, true)
}
}
if (index == 3) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,9 +63,7 @@ fun WallpaperView(
var liked by remember { mutableStateOf(false) }
LaunchedEffect(true) {
query {
wallpaper.imgSrc.let {
liked = Database.favoritesDao().exists(it)
}
liked = Database.favoritesDao().isLiked(wallpaper.imgSrc)
}
}

Expand Down Expand Up @@ -165,9 +163,9 @@ fun WallpaperView(
liked = !liked
query {
if (!liked) {
Database.favoritesDao().delete(wallpaper)
Database.favoritesDao().removeFromFavorites(wallpaper)
} else {
Database.favoritesDao().insertAll(wallpaper)
Database.favoritesDao().insert(wallpaper, true, null)
}
}
},
Expand Down
9 changes: 8 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 @@ -35,7 +35,14 @@ class MainModel : ViewModel() {
)

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

val recentlyAppliedWallpapers: StateFlow<List<Wallpaper>> =
DatabaseHolder.Database.favoritesDao().getHistoryFlow().stateIn(
scope = viewModelScope,
started = SharingStarted.WhileSubscribed(5000L),
initialValue = listOf()
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.ui.pages.AboutPage
import com.bnyro.wallpaper.ui.pages.FavoritesPage
import com.bnyro.wallpaper.ui.pages.HistoryPage
import com.bnyro.wallpaper.ui.pages.SettingsPage
import com.bnyro.wallpaper.ui.pages.WallpaperPage
import com.bnyro.wallpaper.util.Preferences
Expand Down Expand Up @@ -37,6 +38,10 @@ fun AppNavHost(
viewModel.titleResource = R.string.favorites
FavoritesPage(viewModel)
}
composable(DrawerScreens.History.route) {
viewModel.titleResource = DrawerScreens.History.titleResource
HistoryPage(viewModel)
}
composable(DrawerScreens.Settings.route) {
viewModel.titleResource = R.string.settings
SettingsPage(viewModel)
Expand Down
22 changes: 20 additions & 2 deletions app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.material.icons.filled.Air
import androidx.compose.material.icons.filled.Book
import androidx.compose.material.icons.filled.Favorite
import androidx.compose.material.icons.filled.Forum
import androidx.compose.material.icons.filled.History
import androidx.compose.material.icons.filled.Info
import androidx.compose.material.icons.filled.Landscape
import androidx.compose.material.icons.filled.Nightlight
Expand All @@ -13,6 +14,7 @@ import androidx.compose.material.icons.filled.Settings
import androidx.compose.material.icons.filled.WaterDrop
import androidx.compose.ui.graphics.vector.ImageVector
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.util.Preferences

sealed class DrawerScreens(
val titleResource: Int,
Expand All @@ -31,11 +33,27 @@ sealed class DrawerScreens(
object Lemmy : DrawerScreens(R.string.lemmy, "le", Icons.Default.Book)
object Pixel : DrawerScreens(R.string.pixel, "px", Icons.Default.Pix)
object Favorites : DrawerScreens(R.string.favorites, "favorites", Icons.Default.Favorite, true)
object History : DrawerScreens(R.string.history, "history", Icons.Default.History)
object Settings : DrawerScreens(R.string.settings, "settings", Icons.Default.Settings, true)
object About : DrawerScreens(R.string.about, "about", Icons.Default.Info)

companion object {
val apiScreens by lazy { listOf(Wallhaven, Unsplash, OWalls, Picsum, BingDaily, Reddit, Lemmy, Pixel) }
val screens by lazy { listOf(*apiScreens.toTypedArray(), Favorites, Settings, About) }
val apiScreens by lazy {
listOf(
Wallhaven,
Unsplash,
OWalls,
Picsum,
BingDaily,
Reddit,
Lemmy,
Pixel
)
}
val screens by lazy {
listOfNotNull(*apiScreens.toTypedArray(), Favorites, History.takeIf {
Preferences.getBoolean(Preferences.wallpaperHistory, true)
}, Settings, About)
}
}
}
Loading

0 comments on commit 3e5faec

Please sign in to comment.