Refactor db import with resourceDSL
This commit is contained in:
1 parent
3a2ccf86fd
commit
90ed83efd5
2 files changed
+17
-21
No files matched your search
@@ -21,6 +21,8 @@ import android.os.ParcelFileDescriptor
|
||||
import android.os.ParcelFileDescriptor.MODE_READ_ONLY
|
||||
import androidx.paging.PagingSource
|
||||
import androidx.room.Room.databaseBuilder
|
||||
import arrow.fx.coroutines.release
|
||||
import arrow.fx.coroutines.resource
|
||||
import com.hippo.ehviewer.EhApplication.Companion.ehDatabase
|
||||
import com.hippo.ehviewer.client.data.GalleryInfo
|
||||
import com.hippo.ehviewer.dao.BasicDao
|
||||
@@ -391,18 +393,15 @@ object EhDB {
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* @return error string, null for no error
|
||||
*/
|
||||
@Synchronized
|
||||
fun importDB(context: Context, uri: Uri): String? {
|
||||
runCatching {
|
||||
val tempDBName = "tmp.db"
|
||||
suspend fun importDB(context: Context, uri: Uri) {
|
||||
val tempDBName = "tmp.db"
|
||||
resource {
|
||||
context.deleteDatabase(tempDBName)
|
||||
val oldDB = databaseBuilder(context, EhDatabase::class.java, tempDBName)
|
||||
.createFromInputStream { context.contentResolver.openInputStream(uri) }
|
||||
.build()
|
||||
|
||||
databaseBuilder(context, EhDatabase::class.java, tempDBName).createFromInputStream { context.contentResolver.openInputStream(uri) }.build()
|
||||
} release {
|
||||
it.close()
|
||||
context.deleteDatabase(tempDBName)
|
||||
} use { oldDB ->
|
||||
// Download label
|
||||
val manager = DownloadManager
|
||||
runCatching {
|
||||
@@ -454,11 +453,6 @@ object EhDB {
|
||||
if (it !in currentFilterList) addFilter(it)
|
||||
}
|
||||
}
|
||||
oldDB.close()
|
||||
}.onFailure {
|
||||
it.printStackTrace()
|
||||
return context.getString(R.string.cant_read_the_file)
|
||||
}
|
||||
return null
|
||||
}
|
||||
}
|
||||
@@ -207,16 +207,18 @@ class AdvancedFragment : BasePreferenceFragment() {
|
||||
.setView(R.layout.preference_dialog_task)
|
||||
.show()
|
||||
lifecycleScope.launchIO {
|
||||
val error = EhDB.importDB(requireActivity(), uri)
|
||||
val error = runSuspendCatching {
|
||||
EhDB.importDB(requireActivity(), uri)
|
||||
}.exceptionOrNull()?.run {
|
||||
printStackTrace()
|
||||
getString(R.string.cant_read_the_file)
|
||||
}
|
||||
withUIContext {
|
||||
if (alertDialog.isShowing) {
|
||||
alertDialog.dismiss()
|
||||
}
|
||||
if (null == error) {
|
||||
showTip(
|
||||
getString(R.string.settings_advanced_import_data_successfully),
|
||||
BaseScene.LENGTH_SHORT,
|
||||
)
|
||||
showTip(getString(R.string.settings_advanced_import_data_successfully), BaseScene.LENGTH_SHORT)
|
||||
} else {
|
||||
showTip(error, BaseScene.LENGTH_SHORT)
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user