forked from recloudstream/cloudstream
Moved backup restore to IO thread.
This commit is contained in:
parent
9988753432
commit
2ff90c03ca
1 changed files with 25 additions and 23 deletions
|
@ -9,6 +9,7 @@ import android.provider.MediaStore
|
||||||
import android.widget.Toast
|
import android.widget.Toast
|
||||||
import androidx.activity.result.ActivityResultLauncher
|
import androidx.activity.result.ActivityResultLauncher
|
||||||
import androidx.activity.result.contract.ActivityResultContracts
|
import androidx.activity.result.contract.ActivityResultContracts
|
||||||
|
import androidx.annotation.WorkerThread
|
||||||
import androidx.fragment.app.FragmentActivity
|
import androidx.fragment.app.FragmentActivity
|
||||||
import com.fasterxml.jackson.annotation.JsonProperty
|
import com.fasterxml.jackson.annotation.JsonProperty
|
||||||
import com.fasterxml.jackson.module.kotlin.readValue
|
import com.fasterxml.jackson.module.kotlin.readValue
|
||||||
|
@ -27,6 +28,8 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_T
|
||||||
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY
|
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY
|
||||||
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY
|
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY
|
||||||
import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY
|
import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY
|
||||||
|
import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
|
||||||
|
import com.lagradost.cloudstream3.utils.Coroutines.main
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
|
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
|
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
|
||||||
import com.lagradost.cloudstream3.utils.DataStore.mapper
|
import com.lagradost.cloudstream3.utils.DataStore.mapper
|
||||||
|
@ -117,6 +120,7 @@ object BackupUtils {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@WorkerThread
|
||||||
fun Context.restore(
|
fun Context.restore(
|
||||||
backupFile: BackupFile,
|
backupFile: BackupFile,
|
||||||
restoreSettings: Boolean,
|
restoreSettings: Boolean,
|
||||||
|
@ -219,31 +223,29 @@ object BackupUtils {
|
||||||
try {
|
try {
|
||||||
restoreFileSelector =
|
restoreFileSelector =
|
||||||
registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
|
registerForActivityResult(ActivityResultContracts.OpenDocument()) { uri: Uri? ->
|
||||||
this.let { activity ->
|
if (uri == null) return@registerForActivityResult
|
||||||
uri?.let {
|
val activity = this
|
||||||
|
ioSafe {
|
||||||
try {
|
try {
|
||||||
val input =
|
val input = activity.contentResolver.openInputStream(uri)
|
||||||
activity.contentResolver.openInputStream(uri)
|
?: return@ioSafe
|
||||||
?: return@registerForActivityResult
|
|
||||||
|
|
||||||
val restoredValue =
|
val restoredValue =
|
||||||
mapper.readValue<BackupFile>(input)
|
mapper.readValue<BackupFile>(input)
|
||||||
|
|
||||||
activity.restore(
|
activity.restore(
|
||||||
restoredValue,
|
restoredValue,
|
||||||
restoreSettings = true,
|
restoreSettings = true,
|
||||||
restoreDataStore = true
|
restoreDataStore = true
|
||||||
)
|
)
|
||||||
activity.recreate()
|
activity.runOnUiThread { activity.recreate() }
|
||||||
} catch (e: Exception) {
|
} catch (e: Exception) {
|
||||||
logError(e)
|
logError(e)
|
||||||
try { // smth can fail in .format
|
main { // smth can fail in .format
|
||||||
showToast(
|
showToast(
|
||||||
activity,
|
activity,
|
||||||
getString(R.string.restore_failed_format).format(e.toString())
|
getString(R.string.restore_failed_format).format(e.toString())
|
||||||
)
|
)
|
||||||
} catch (e: Exception) {
|
|
||||||
logError(e)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue