Skip to content

Commit

Permalink
bump libs
Browse files Browse the repository at this point in the history
  • Loading branch information
T8RIN committed Jan 15, 2025
1 parent 4f12d4e commit a68041b
Showing 1 changed file with 9 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,12 @@

package ru.tech.imageresizershrinker.feature.settings.data

import android.annotation.SuppressLint
import android.content.Context
import android.net.Uri
import androidx.datastore.preferences.PreferencesMapCompat
import androidx.datastore.preferences.core.PreferencesSerializer
import kotlinx.coroutines.coroutineScope
import okio.buffer
import okio.source
import ru.tech.imageresizershrinker.core.resources.R
import java.io.ByteArrayInputStream
import java.io.File
Expand All @@ -47,19 +48,20 @@ internal suspend fun Context.restoreDatastore(
}
}

@SuppressLint("RestrictedApi")
internal suspend fun Context.restoreDatastore(
fileName: String,
backupData: ByteArray,
onFailure: (Throwable) -> Unit,
onSuccess: suspend () -> Unit
) = coroutineScope {
runCatching {
runCatching {
PreferencesMapCompat.readFrom(ByteArrayInputStream(backupData))
}.onFailure {
throw Throwable(getString(R.string.corrupted_file_or_not_a_backup))
try {
PreferencesSerializer.readFrom(ByteArrayInputStream(backupData).source().buffer())
} catch (_: Throwable) {
onFailure(Throwable(getString(R.string.corrupted_file_or_not_a_backup)))
return@coroutineScope
}

File(
filesDir,
"datastore/${fileName}.preferences_pb"
Expand Down

0 comments on commit a68041b

Please sign in to comment.