Skip to content

Commit

Permalink
Merge pull request #201 from you-apps/history
Browse files Browse the repository at this point in the history
feat: history for recently applied wallpapers
  • Loading branch information
Bnyro authored Jun 4, 2024
2 parents 3e86e8b + e42b250 commit 07a2cd3
Show file tree
Hide file tree
Showing 20 changed files with 311 additions and 76 deletions.
2 changes: 1 addition & 1 deletion app/schemas/com.bnyro.wallpaper.db.AppDatabase/1.json
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,4 @@
"INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b8804b8b4eaa74f2f2977f3c05bc3500')"
]
}
}
}
103 changes: 103 additions & 0 deletions app/schemas/com.bnyro.wallpaper.db.AppDatabase/2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
{
"formatVersion": 1,
"database": {
"version": 2,
"identityHash": "bcfb14085e7abeb5d0f9c9bd5273164d",
"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, `favorite` INTEGER NOT NULL DEFAULT 1, `inHistory` INTEGER NOT NULL DEFAULT 0, `timeAdded` INTEGER NOT NULL DEFAULT 0, PRIMARY KEY(`imgSrc`))",
"fields": [
{
"fieldPath": "imgSrc",
"columnName": "imgSrc",
"affinity": "TEXT",
"notNull": true
},
{
"fieldPath": "title",
"columnName": "title",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "url",
"columnName": "url",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "author",
"columnName": "author",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "category",
"columnName": "category",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "resolution",
"columnName": "resolution",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "fileSize",
"columnName": "fileSize",
"affinity": "INTEGER",
"notNull": false
},
{
"fieldPath": "thumb",
"columnName": "thumb",
"affinity": "TEXT",
"notNull": false
},
{
"fieldPath": "creationDate",
"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"
},
{
"fieldPath": "timeAdded",
"columnName": "timeAdded",
"affinity": "INTEGER",
"notNull": true,
"defaultValue": "0"
}
],
"primaryKey": {
"autoGenerate": false,
"columnNames": [
"imgSrc"
]
},
"indices": [],
"foreignKeys": []
}
],
"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, 'bcfb14085e7abeb5d0f9c9bd5273164d')"
]
}
}
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
53 changes: 41 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,10 +1,10 @@
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
import androidx.room.Update
import com.bnyro.wallpaper.db.obj.Wallpaper
import kotlinx.coroutines.flow.Flow

Expand All @@ -13,21 +13,50 @@ 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 ORDER BY timeAdded DESC")
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 ORDER BY timeAdded DESC")
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?) {
val existingWallpaper = findByImgSrc(wallpaper.imgSrc)

wallpaper.favorite = isFavorite ?: existingWallpaper?.favorite ?: false
wallpaper.inHistory = isHistory ?: existingWallpaper?.inHistory ?: false

if (existingWallpaper != null) {
updateWallpaper(wallpaper)
} else {
wallpaper.timeAdded = System.currentTimeMillis()
insertAll(listOf(wallpaper))
}
}

@Update
fun updateWallpaper(wallpaper: 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()
}
5 changes: 4 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,8 @@ 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,
@ColumnInfo(defaultValue = "0") var timeAdded: Long = 0,
)
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 @@ -16,10 +16,10 @@ fun WallpaperModeDialog(
wallpaper: Wallpaper,
wallpaperHelperModel: WallpaperHelperModel,
onDismissRequest: () -> Unit,
onLike: () -> Unit = {},
applyFilter: Boolean = false
) {
val context = LocalContext.current

ListDialog(
title = stringResource(R.string.set_wallpaper),
items = listOf(
Expand All @@ -30,10 +30,10 @@ fun WallpaperModeDialog(
),
onDismissRequest = onDismissRequest,
onClick = { index ->
if (Preferences.getBoolean(Preferences.autoAddToFavoritesKey, false)) {
onLike.invoke()
if (Preferences.getBoolean(Preferences.wallpaperHistory, true)) {
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 All @@ -189,10 +187,7 @@ fun WallpaperView(
WallpaperModeDialog(
wallpaper,
wallpaperHelperModel,
onDismissRequest = { showModeSelection = false },
onLike = {
liked = true
})
onDismissRequest = { showModeSelection = false })
}

MultiStateDialog(
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
Loading

0 comments on commit 07a2cd3

Please sign in to comment.