Skip to content

Commit 2ff90c0

Browse files
author
no-commit
committed
Moved backup restore to IO thread.
1 parent 9988753 commit 2ff90c0

File tree

1 file changed

+25
-23
lines changed

1 file changed

+25
-23
lines changed

app/src/main/java/com/lagradost/cloudstream3/utils/BackupUtils.kt

Lines changed: 25 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import android.provider.MediaStore
99
import android.widget.Toast
1010
import androidx.activity.result.ActivityResultLauncher
1111
import androidx.activity.result.contract.ActivityResultContracts
12+
import androidx.annotation.WorkerThread
1213
import androidx.fragment.app.FragmentActivity
1314
import com.fasterxml.jackson.annotation.JsonProperty
1415
import com.fasterxml.jackson.module.kotlin.readValue
@@ -27,6 +28,8 @@ import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_T
2728
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_UNIXTIME_KEY
2829
import com.lagradost.cloudstream3.syncproviders.providers.MALApi.Companion.MAL_USER_KEY
2930
import 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
3033
import com.lagradost.cloudstream3.utils.DataStore.getDefaultSharedPrefs
3134
import com.lagradost.cloudstream3.utils.DataStore.getSharedPrefs
3235
import 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

Comments
 (0)