@@ -9,6 +9,7 @@ import android.provider.MediaStore
99import android.widget.Toast
1010import androidx.activity.result.ActivityResultLauncher
1111import androidx.activity.result.contract.ActivityResultContracts
12+ import androidx.annotation.WorkerThread
1213import androidx.fragment.app.FragmentActivity
1314import com.fasterxml.jackson.annotation.JsonProperty
1415import com.fasterxml.jackson.module.kotlin.readValue
@@ -27,6 +28,8 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_T
2728import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY
2829import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY
2930import com.lagradost.cloudstream3.syncproviders.providers.OpenSubtitlesApi.Companion.OPEN_SUBTITLES_USER_KEY
31+ import com.lagradost.cloudstream3.utils.Coroutines.ioSafe
32+ import com.lagradost.cloudstream3.utils.Coroutines.main
3033import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
3134import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
3235import com.lagradost.cloudstream3.utils.DataStore.mapper
@@ -117,6 +120,7 @@ object BackupUtils {
117120 )
118121 }
119122
123+ @WorkerThread
120124 fun Context.restore (
121125 backupFile : BackupFile ,
122126 restoreSettings : Boolean ,
@@ -219,31 +223,29 @@ object BackupUtils {
219223 try {
220224 restoreFileSelector =
221225 registerForActivityResult(ActivityResultContracts .OpenDocument ()) { uri: Uri ? ->
222- this . let { activity ->
223- uri?. let {
224- try {
225- val input =
226- activity.contentResolver.openInputStream(uri)
227- ? : return @registerForActivityResult
226+ if (uri == null ) return @registerForActivityResult
227+ val activity = this
228+ ioSafe {
229+ try {
230+ val input = activity.contentResolver.openInputStream(uri)
231+ ? : return @ioSafe
228232
229- val restoredValue =
230- mapper.readValue<BackupFile >(input)
231- activity.restore(
232- restoredValue,
233- restoreSettings = true ,
234- restoreDataStore = true
233+ val restoredValue =
234+ mapper.readValue<BackupFile >(input)
235+
236+ activity.restore(
237+ restoredValue,
238+ restoreSettings = true ,
239+ restoreDataStore = true
240+ )
241+ activity.runOnUiThread { activity.recreate() }
242+ } catch (e: Exception ) {
243+ logError(e)
244+ main { // smth can fail in .format
245+ showToast(
246+ activity,
247+ getString(R .string.restore_failed_format).format(e.toString())
235248 )
236- activity.recreate()
237- } catch (e: Exception ) {
238- logError(e)
239- try { // smth can fail in .format
240- showToast(
241- activity,
242- getString(R .string.restore_failed_format).format(e.toString())
243- )
244- } catch (e: Exception ) {
245- logError(e)
246- }
247249 }
248250 }
249251 }
0 commit comments