diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt index 5a0057a91..84d167b0a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/extensions/Activity.kt @@ -19,6 +19,7 @@ import android.provider.MediaStore.Images import android.util.DisplayMetrics import android.view.View import androidx.appcompat.app.AppCompatActivity +import androidx.lifecycle.lifecycleScope import com.bumptech.glide.Glide import com.bumptech.glide.load.DecodeFormat import com.bumptech.glide.load.engine.DiskCacheStrategy @@ -36,6 +37,9 @@ import com.simplemobiletools.gallery.pro.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.pro.helpers.RECYCLE_BIN import com.simplemobiletools.gallery.pro.models.DateTaken import com.squareup.picasso.Picasso +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.launch +import kotlinx.coroutines.withContext import java.io.File import java.io.FileOutputStream import java.io.InputStream @@ -424,7 +428,12 @@ fun Activity.hasNavBar(): Boolean { return (realDisplayMetrics.widthPixels - displayMetrics.widthPixels > 0) || (realDisplayMetrics.heightPixels - displayMetrics.heightPixels > 0) } -fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasRescanned: Boolean = false, callback: (() -> Unit)? = null) { +fun AppCompatActivity.fixDateTaken( + paths: ArrayList, + showToasts: Boolean, + hasRescanned: Boolean = false, + callback: (() -> Unit)? = null +) { val BATCH_SIZE = 50 if (showToasts) { toast(R.string.fixing) @@ -435,7 +444,7 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasResc var didUpdateFile = false val operations = ArrayList() - ensureBackgroundThread { + lifecycleScope.launch(Dispatchers.IO) { val dateTakens = ArrayList() for (path in paths) { @@ -479,10 +488,8 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasResc toast(R.string.no_date_takens_found) } - runOnUiThread { - callback?.invoke() - } - return@ensureBackgroundThread + withContext(Dispatchers.Main) { callback?.invoke() } + return@launch } val resultSize = contentResolver.applyBatch(MediaStore.AUTHORITY, operations).size @@ -495,7 +502,7 @@ fun Activity.fixDateTaken(paths: ArrayList, showToasts: Boolean, hasResc dateTakensDB.insertAll(dateTakens) } - runOnUiThread { + withContext(Dispatchers.Main) { if (showToasts) { toast(if (didUpdateFile) R.string.dates_fixed_successfully else R.string.unknown_error_occurred) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt index 4c4daa180..d45bcdc78 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/MediaFetcher.kt @@ -15,6 +15,8 @@ import com.simplemobiletools.gallery.pro.extensions.* import com.simplemobiletools.gallery.pro.models.Medium import com.simplemobiletools.gallery.pro.models.ThumbnailItem import com.simplemobiletools.gallery.pro.models.ThumbnailSection +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.runBlocking import java.io.File import java.util.* @@ -456,10 +458,12 @@ class MediaFetcher(val context: Context) { } val dateTakenValues = try { - if (folder == FAVORITES) { - context.dateTakensDB.getAllDateTakens() - } else { - context.dateTakensDB.getDateTakensFromPath(folder) + runBlocking { + if (folder == FAVORITES) { + context.dateTakensDB.getAllDateTakens() + } else { + context.dateTakensDB.getDateTakensFromPath(folder) + } } } catch (e: Exception) { return dateTakens @@ -493,7 +497,7 @@ class MediaFetcher(val context: Context) { } } - val dateTakenValues = context.dateTakensDB.getAllDateTakens() + val dateTakenValues = runBlocking(Dispatchers.IO) { context.dateTakensDB.getAllDateTakens() } dateTakenValues.forEach { dateTakens[it.fullPath] = it.taken diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DateTakensDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DateTakensDao.kt index 5041727e9..93b27b88c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DateTakensDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/interfaces/DateTakensDao.kt @@ -9,11 +9,11 @@ import com.simplemobiletools.gallery.pro.models.DateTaken @Dao interface DateTakensDao { @Insert(onConflict = OnConflictStrategy.REPLACE) - fun insertAll(dateTakens: List) + suspend fun insertAll(dateTakens: List) @Query("SELECT full_path, filename, parent_path, date_taken, last_fixed, last_modified FROM date_takens WHERE parent_path = :path COLLATE NOCASE") - fun getDateTakensFromPath(path: String): List + suspend fun getDateTakensFromPath(path: String): List @Query("SELECT full_path, filename, parent_path, date_taken, last_fixed, last_modified FROM date_takens") - fun getAllDateTakens(): List + suspend fun getAllDateTakens(): List }