Skip to content

Commit

Permalink
Fix back up and restore
Browse files Browse the repository at this point in the history
  • Loading branch information
maxrave-dev committed Oct 7, 2023
1 parent 1de7ce4 commit a7bf7d6
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -278,19 +278,21 @@ class SettingsViewModel @Inject constructor(
@UnstableApi
fun restore(context: Context, uri: Uri) {
runCatching {
runBlocking { dataStoreManager.restore(true)}
context.applicationContext.contentResolver.openInputStream(uri)?.use {
it.zipInputStream().use { inputStream ->
var entry = inputStream.nextEntry
while (entry != null) {
var count = 0
while (entry != null && count < 2) {
when (entry.name) {
"$SETTINGS_FILENAME.preferences_pb" -> {
(context.filesDir /"datastore"/"$SETTINGS_FILENAME.preferences_pb").outputStream().use { outputStream ->
(context.filesDir/"datastore"/"$SETTINGS_FILENAME.preferences_pb").outputStream().use { outputStream ->
inputStream.copyTo(outputStream)
}
}

DB_NAME -> {
runBlocking((Dispatchers.Main)) {
runBlocking(Dispatchers.IO) {
databaseDao.checkpoint()
}
database.close()
Expand All @@ -299,16 +301,14 @@ class SettingsViewModel @Inject constructor(
}
}
}
count++
entry = inputStream.nextEntry
}
}
}
runBlocking { dataStoreManager.restore(true)}
context.stopService(Intent(context, SimpleMediaService::class.java))
context.startActivity(Intent(context, MainActivity::class.java))
exitProcess(0)
}.onSuccess {
Toast.makeText(context, context.getString(R.string.restore_success), Toast.LENGTH_SHORT).show()
}.onFailure {
it.printStackTrace()
Toast.makeText(context, context.getString(R.string.restore_failed), Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,7 @@ class SharedViewModel @Inject constructor(private var dataStoreManager: DataStor
isRestoring.value = restoring == TRUE
isRestoring.collect() { it ->
if (it) {
Toast.makeText(context, context.getString(R.string.restore_success), Toast.LENGTH_SHORT).show()
mainRepository.getDownloadedSongs().collect { songs ->
songs.forEach { song ->
if (!downloadedCache.keys.contains(song.videoId)) {
Expand Down

0 comments on commit a7bf7d6

Please sign in to comment.