From e42b25057a192b1505718c0bfa20cf83da0f6e5f Mon Sep 17 00:00:00 2001 From: Bnyro Date: Sat, 1 Jun 2024 18:21:24 +0200 Subject: [PATCH] feat: history for recently applied wallpapers --- .../com.bnyro.wallpaper.db.AppDatabase/1.json | 2 +- .../com.bnyro.wallpaper.db.AppDatabase/2.json | 103 ++++++++++++++++++ .../com/bnyro/wallpaper/db/AppDatabase.kt | 6 +- .../bnyro/wallpaper/db/dao/FavoritesDao.kt | 53 +++++++-- .../com/bnyro/wallpaper/db/obj/Wallpaper.kt | 5 +- .../ui/components/NavigationDrawer.kt | 1 - .../wallpaper/ui/components/NothingHere.kt | 45 ++++++++ .../wallpaper/ui/components/WallpaperGrid.kt | 6 +- .../ui/components/WallpaperModeDialog.kt | 8 +- .../wallpaper/ui/components/WallpaperView.kt | 13 +-- .../bnyro/wallpaper/ui/models/MainModel.kt | 9 +- .../com/bnyro/wallpaper/ui/nav/AppNavHost.kt | 5 + .../bnyro/wallpaper/ui/nav/DrawerScreens.kt | 22 +++- .../bnyro/wallpaper/ui/pages/FavoritesPage.kt | 39 +------ .../bnyro/wallpaper/ui/pages/HistoryPage.kt | 51 +++++++++ .../bnyro/wallpaper/ui/pages/SettingsPage.kt | 5 +- .../bnyro/wallpaper/util/BackgroundWorker.kt | 7 +- .../com/bnyro/wallpaper/util/BackupHelper.kt | 2 +- .../com/bnyro/wallpaper/util/Preferences.kt | 2 +- app/src/main/res/values/strings.xml | 3 + 20 files changed, 311 insertions(+), 76 deletions(-) create mode 100644 app/schemas/com.bnyro.wallpaper.db.AppDatabase/2.json create mode 100644 app/src/main/java/com/bnyro/wallpaper/ui/components/NothingHere.kt create mode 100644 app/src/main/java/com/bnyro/wallpaper/ui/pages/HistoryPage.kt diff --git a/app/schemas/com.bnyro.wallpaper.db.AppDatabase/1.json b/app/schemas/com.bnyro.wallpaper.db.AppDatabase/1.json index 5986f3df..2cd3a586 100644 --- a/app/schemas/com.bnyro.wallpaper.db.AppDatabase/1.json +++ b/app/schemas/com.bnyro.wallpaper.db.AppDatabase/1.json @@ -79,4 +79,4 @@ "INSERT OR REPLACE INTO room_master_table (id,identity_hash) VALUES(42, 'b8804b8b4eaa74f2f2977f3c05bc3500')" ] } -} \ No newline at end of file +} diff --git a/app/schemas/com.bnyro.wallpaper.db.AppDatabase/2.json b/app/schemas/com.bnyro.wallpaper.db.AppDatabase/2.json new file mode 100644 index 00000000..6fdae511 --- /dev/null +++ b/app/schemas/com.bnyro.wallpaper.db.AppDatabase/2.json @@ -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')" + ] + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/db/AppDatabase.kt b/app/src/main/java/com/bnyro/wallpaper/db/AppDatabase.kt index 0370cb1f..fb179413 100644 --- a/app/src/main/java/com/bnyro/wallpaper/db/AppDatabase.kt +++ b/app/src/main/java/com/bnyro/wallpaper/db/AppDatabase.kt @@ -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 ] diff --git a/app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt b/app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt index d1abfdc1..4bc40031 100644 --- a/app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt +++ b/app/src/main/java/com/bnyro/wallpaper/db/dao/FavoritesDao.kt @@ -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 @@ -13,21 +13,50 @@ interface FavoritesDao { @Query("SELECT * FROM favorites") fun getAll(): List - @Query("SELECT * FROM favorites") - fun getAllFlow(): Flow> + @Query("SELECT * FROM favorites WHERE favorite = 1") + fun getFavorites(): List + + @Query("SELECT * FROM favorites WHERE favorite = 1 ORDER BY timeAdded DESC") + fun getFavoritesFlow(): Flow> - @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> - @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) + + 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() } diff --git a/app/src/main/java/com/bnyro/wallpaper/db/obj/Wallpaper.kt b/app/src/main/java/com/bnyro/wallpaper/db/obj/Wallpaper.kt index 0d2ee966..b842e21e 100644 --- a/app/src/main/java/com/bnyro/wallpaper/db/obj/Wallpaper.kt +++ b/app/src/main/java/com/bnyro/wallpaper/db/obj/Wallpaper.kt @@ -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, ) diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt index d5ff4dc4..42cbbaf9 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/NavigationDrawer.kt @@ -124,7 +124,6 @@ fun NavigationDrawer( val MinimumDrawerWidth = 240.dp val MaximumDrawerWidth = 300.dp -@OptIn(ExperimentalMaterial3Api::class) @Composable private fun DrawerSheet( windowInsets: WindowInsets, diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/NothingHere.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/NothingHere.kt new file mode 100644 index 00000000..2af6715e --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/NothingHere.kt @@ -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 + ) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt index abb4f824..f2a2f8d5 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperGrid.kt @@ -68,7 +68,7 @@ fun WallpaperGrid( LaunchedEffect(true) { query { - wallpaper.imgSrc.let { src -> liked = Database.favoritesDao().exists(src) } + liked = Database.favoritesDao().isLiked(wallpaper.imgSrc) } } @@ -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) } } } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperModeDialog.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperModeDialog.kt index 87af8544..de7cc5ea 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperModeDialog.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperModeDialog.kt @@ -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( @@ -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) { diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperView.kt b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperView.kt index e9ef436a..d0828759 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperView.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/components/WallpaperView.kt @@ -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) } } @@ -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) } } }, @@ -189,10 +187,7 @@ fun WallpaperView( WallpaperModeDialog( wallpaper, wallpaperHelperModel, - onDismissRequest = { showModeSelection = false }, - onLike = { - liked = true - }) + onDismissRequest = { showModeSelection = false }) } MultiStateDialog( diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt b/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt index 004276e9..1c2e37f5 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/models/MainModel.kt @@ -35,7 +35,14 @@ class MainModel : ViewModel() { ) val favWallpapers: StateFlow> = - DatabaseHolder.Database.favoritesDao().getAllFlow().stateIn( + DatabaseHolder.Database.favoritesDao().getFavoritesFlow().stateIn( + scope = viewModelScope, + started = SharingStarted.WhileSubscribed(5000L), + initialValue = listOf() + ) + + val recentlyAppliedWallpapers: StateFlow> = + DatabaseHolder.Database.favoritesDao().getHistoryFlow().stateIn( scope = viewModelScope, started = SharingStarted.WhileSubscribed(5000L), initialValue = listOf() diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt b/app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt index ffc7a9d6..c64d0d04 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/nav/AppNavHost.kt @@ -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 @@ -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) diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt index 85d82440..5ebcd838 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/nav/DrawerScreens.kt @@ -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 @@ -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, @@ -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) + } } } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt index 299e7423..516dcb92 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/FavoritesPage.kt @@ -1,29 +1,19 @@ package com.bnyro.wallpaper.ui.pages -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.material.icons.Icons 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.collectAsState import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.remember import androidx.compose.runtime.setValue -import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.res.stringResource -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.ui.components.NothingHere import com.bnyro.wallpaper.ui.components.WallpaperGrid import com.bnyro.wallpaper.ui.components.WallpaperPageView import com.bnyro.wallpaper.ui.models.MainModel @@ -51,29 +41,10 @@ fun FavoritesPage(viewModel: MainModel) { } } } else { - Box( - modifier = Modifier.fillMaxSize() - ) { - Column( - modifier = Modifier.align(Alignment.Center), - horizontalAlignment = Alignment.CenterHorizontally, - verticalArrangement = Arrangement.Center - ) { - Icon( - Icons.Default.HeartBroken, - null, - Modifier.size(150.dp) - ) - Spacer( - modifier = Modifier.height(10.dp) - ) - Text( - stringResource(R.string.no_favorites), - fontSize = 20.sp, - fontWeight = FontWeight.Bold - ) - } - } + NothingHere( + text = stringResource(R.string.no_favorites), + icon = Icons.Default.HeartBroken + ) } } } diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/HistoryPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/HistoryPage.kt new file mode 100644 index 00000000..645cf316 --- /dev/null +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/HistoryPage.kt @@ -0,0 +1,51 @@ +package com.bnyro.wallpaper.ui.pages + +import androidx.compose.foundation.layout.Column +import androidx.compose.foundation.layout.fillMaxSize +import androidx.compose.material.icons.Icons +import androidx.compose.material.icons.filled.HeartBroken +import androidx.compose.material.icons.filled.History +import androidx.compose.runtime.Composable +import androidx.compose.runtime.collectAsState +import androidx.compose.runtime.getValue +import androidx.compose.runtime.mutableStateOf +import androidx.compose.runtime.remember +import androidx.compose.runtime.setValue +import androidx.compose.ui.Modifier +import androidx.compose.ui.res.stringResource +import com.bnyro.wallpaper.R +import com.bnyro.wallpaper.ui.components.NothingHere +import com.bnyro.wallpaper.ui.components.WallpaperGrid +import com.bnyro.wallpaper.ui.components.WallpaperPageView +import com.bnyro.wallpaper.ui.models.MainModel + +@Composable +fun HistoryPage(viewModel: MainModel) { + Column( + modifier = Modifier + .fillMaxSize() + ) { + val history by viewModel.recentlyAppliedWallpapers.collectAsState() + + var selectedIndex by remember { mutableStateOf(null) } + + if (history.isNotEmpty()) { + WallpaperGrid( + wallpapers = history, + onClickWallpaper = { + selectedIndex = it + } + ) + selectedIndex?.let { + WallpaperPageView(initialPage = it, wallpapers = history) { + selectedIndex = null + } + } + } else { + NothingHere( + text = stringResource(R.string.no_history), + icon = Icons.Default.History + ) + } + } +} diff --git a/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt b/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt index 05402323..f8e28b19 100644 --- a/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt +++ b/app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt @@ -87,8 +87,9 @@ fun SettingsPage( viewModel.themeMode = ThemeMode.values()[it.toInt()] } CheckboxPref( - prefKey = Preferences.autoAddToFavoritesKey, - title = stringResource(R.string.auto_add_to_favorites) + prefKey = Preferences.wallpaperHistory, + title = stringResource(R.string.wallpaper_history), + defaultValue = true ) } } diff --git a/app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt b/app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt index 8a03edb3..85c928bd 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/BackgroundWorker.kt @@ -56,8 +56,9 @@ class BackgroundWorker( }.getOrNull() ?: return@withContext null val bitmap = ImageHelper.getSuspend(applicationContext, url, true) - if (bitmap != null && Preferences.getBoolean(Preferences.autoAddToFavoritesKey, false)) { - DatabaseHolder.Database.favoritesDao().insertAll(Wallpaper(imgSrc = url)) + if (bitmap != null && Preferences.getBoolean(Preferences.wallpaperHistory, true)) { + val wallpaper = Wallpaper(imgSrc = url) + DatabaseHolder.Database.favoritesDao().insert(wallpaper, null, true) } bitmap @@ -66,7 +67,7 @@ class BackgroundWorker( private suspend fun getFavoritesWallpaper(): Bitmap? { val favoriteUrl = awaitQuery { - DatabaseHolder.Database.favoritesDao().getAll() + DatabaseHolder.Database.favoritesDao().getFavorites() }.randomOrNull()?.imgSrc return ImageHelper.getSuspend(applicationContext, favoriteUrl, true) } diff --git a/app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt b/app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt index 3e7fd80e..38492638 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt @@ -32,7 +32,7 @@ object BackupHelper { try { context.contentResolver.openInputStream(uri)?.use { val backupFile = RetrofitHelper.json.decodeFromStream(it) - DatabaseHolder.Database.favoritesDao().insertAll(*backupFile.favorites.toTypedArray()) + DatabaseHolder.Database.favoritesDao().insertAll(backupFile.favorites) } context.toastFromMainThread(context.getString(R.string.success)) } catch (e: Exception) { diff --git a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt index 93a4c7c2..0fbd3ab0 100644 --- a/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt +++ b/app/src/main/java/com/bnyro/wallpaper/util/Preferences.kt @@ -14,7 +14,7 @@ object Preferences { const val resizeMethodKey = "resizeMethod" const val diskCacheKey = "diskCache" const val themeModeKey = "themeModeKey" - const val autoAddToFavoritesKey = "autoAddToFavorites" + const val wallpaperHistory = "autoAddToFavorites" const val grayscaleKey = "grayscale" const val blurKey = "blur" const val startTabKey = "startTab" diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index cf66021f..acf67bb9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -11,6 +11,7 @@ Google Pixel Favorites + History Settings About @@ -83,7 +84,9 @@ Dark No favorites yet + No history yet Add to favorites when applying + Wallpaper history Version GitHub