-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support for exporting and importing favorites
- Loading branch information
Showing
6 changed files
with
111 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
app/src/main/java/com/bnyro/wallpaper/util/BackupHelper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters