Skip to content

Commit

Permalink
feat: support for exporting and importing favorites
Browse files Browse the repository at this point in the history
  • Loading branch information
Bnyro committed Oct 23, 2023
1 parent d6b33cf commit b579aa1
Show file tree
Hide file tree
Showing 6 changed files with 111 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/db/obj/BackupFile.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.bnyro.wallpaper.db.obj

data class BackupFile(
val favorites: List<Wallpaper> = emptyList()
)
2 changes: 1 addition & 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 @@ -6,7 +6,7 @@ import androidx.room.PrimaryKey

@Entity(tableName = "favorites")
data class Wallpaper(
@PrimaryKey val imgSrc: String,
@PrimaryKey val imgSrc: String = "",
@ColumnInfo val title: String? = null,
@ColumnInfo val url: String? = null,
@ColumnInfo var author: String? = null,
Expand Down
11 changes: 11 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ext/Context.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.bnyro.wallpaper.ext

import android.content.Context
import android.widget.Toast
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext

suspend fun Context.toastFromMainThread(text: String?, length: Int = Toast.LENGTH_SHORT)
= withContext(Dispatchers.Main) {
Toast.makeText(this@toastFromMainThread, text, length).show()
}
48 changes: 48 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/ui/pages/SettingsPage.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
package com.bnyro.wallpaper.ui.pages

import androidx.activity.compose.rememberLauncherForActivityResult
import androidx.activity.result.contract.ActivityResultContracts
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.runtime.toMutableStateList
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
Expand All @@ -25,8 +32,11 @@ import com.bnyro.wallpaper.ui.components.prefs.CheckboxPref
import com.bnyro.wallpaper.ui.components.prefs.ListPreference
import com.bnyro.wallpaper.ui.components.prefs.SettingsCategory
import com.bnyro.wallpaper.ui.models.MainModel
import com.bnyro.wallpaper.util.BackupHelper
import com.bnyro.wallpaper.util.Preferences
import com.bnyro.wallpaper.util.WorkerHelper
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch

@Composable
fun SettingsPage(
Expand All @@ -36,6 +46,20 @@ fun SettingsPage(
val wallpaperConfigs = remember {
Preferences.getWallpaperConfigs().toMutableStateList()
}
val scope = rememberCoroutineScope()

val createFavoritesBackup =
rememberLauncherForActivityResult(ActivityResultContracts.CreateDocument(BackupHelper.JSON_MIME)) {
scope.launch(Dispatchers.IO) {
BackupHelper.backupFavorites(it ?: return@launch, context)
}
}
val restoreFavoritesBackup =
rememberLauncherForActivityResult(ActivityResultContracts.OpenDocument()) {
scope.launch(Dispatchers.IO) {
BackupHelper.restoreFavorites(it ?: return@launch, context)
}
}

val scrollState = rememberScrollState()
Column(
Expand Down Expand Up @@ -169,5 +193,29 @@ fun SettingsPage(
}
}
}

AboutContainer {
SettingsCategory(
title = stringResource(R.string.import_export)
)
Spacer(modifier = Modifier.height(6.dp))
Row {
Button(
onClick = {
restoreFavoritesBackup.launch(arrayOf(BackupHelper.JSON_MIME))
}
) {
Text(stringResource(R.string.import_favorites))
}
Spacer(modifier = Modifier.width(6.dp))
Button(
onClick = {
createFavoritesBackup.launch("wall_you_favorites_backup.json")
}
) {
Text(stringResource(R.string.export_favorites))
}
}
}
}
}
42 changes: 42 additions & 0 deletions app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.bnyro.wallpaper.util

import android.content.Context
import android.net.Uri
import com.bnyro.wallpaper.R
import com.bnyro.wallpaper.db.DatabaseHolder
import com.bnyro.wallpaper.db.obj.BackupFile
import com.bnyro.wallpaper.ext.toastFromMainThread
import com.fasterxml.jackson.databind.ObjectMapper

object BackupHelper {
const val JSON_MIME = "application/json"
private val mapper by lazy {
ObjectMapper()
}

suspend fun backupFavorites(uri: Uri, context: Context) {
try {
val favorites = DatabaseHolder.Database.favoritesDao().getAll()
val backupFile = BackupFile(favorites = favorites)
context.contentResolver.openOutputStream(uri)?.use {
val favoritesJson = mapper.writeValueAsBytes(backupFile)
it.write(favoritesJson)
}
context.toastFromMainThread(context.getString(R.string.success))
} catch (e: Exception) {
context.toastFromMainThread(e.localizedMessage)
}
}

suspend fun restoreFavorites(uri: Uri, context: Context) {
try {
context.contentResolver.openInputStream(uri)?.use {
val backupFile = mapper.readValue(it.readBytes(), BackupFile::class.java)
DatabaseHolder.Database.favoritesDao().insertAll(*backupFile.favorites.toTypedArray())
}
context.toastFromMainThread(context.getString(R.string.success))
} catch (e: Exception) {
context.toastFromMainThread(e.localizedMessage)
}
}
}
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@
<string name="combine_wallpaper_changers">Combined wallpaper changer</string>
<string name="combine_wallpaper_changers_summary">Use the same wallpaper for home and lockscreen.</string>
<string name="color_palette">Show color palette</string>
<string name="import_export">Import &amp; export</string>
<string name="import_favorites">Import favorites</string>
<string name="export_favorites">Export favorites</string>
<string name="success">Success!</string>
<!-- Wallpaper changer source -->
<string name="online">Online</string>
<string name="local">Local</string>
Expand Down

0 comments on commit b579aa1

Please sign in to comment.