From 409be79489f21bd810f8f2eca698b31da0db0c91 Mon Sep 17 00:00:00 2001 From: tibbi Date: Mon, 27 Jan 2020 15:17:46 +0100 Subject: [PATCH] migrate favorites in the new table on app upgrade --- .../gallery/pro/activities/SplashActivity.kt | 32 +++++++++++++++++++ .../gallery/pro/helpers/Config.kt | 4 +++ .../gallery/pro/helpers/Constants.kt | 1 + 3 files changed, 37 insertions(+) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SplashActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SplashActivity.kt index 20c9c9387..ede321fc2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SplashActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/activities/SplashActivity.kt @@ -2,9 +2,41 @@ package com.simplemobiletools.gallery.pro.activities import android.content.Intent import com.simplemobiletools.commons.activities.BaseSplashActivity +import com.simplemobiletools.commons.helpers.ensureBackgroundThread +import com.simplemobiletools.gallery.pro.extensions.config +import com.simplemobiletools.gallery.pro.extensions.favoritesDB +import com.simplemobiletools.gallery.pro.extensions.getFavoriteFromPath +import com.simplemobiletools.gallery.pro.extensions.mediaDB +import com.simplemobiletools.gallery.pro.models.Favorite class SplashActivity : BaseSplashActivity() { override fun initActivity() { + + // check if previously selected favorite items have been properly migrated into the new Favorites table + if (config.wereFavoritesMigrated) { + launchActivity() + } else { + if (config.appRunCount == 0) { + config.wereFavoritesMigrated = true + } else { + ensureBackgroundThread { + val favorites = ArrayList() + val favoritePaths = mediaDB.getFavorites().map { it.path }.toMutableList() as ArrayList + favoritePaths.forEach { + favorites.add(getFavoriteFromPath(it)) + } + favoritesDB.insertAll(favorites) + config.wereFavoritesMigrated = true + + runOnUiThread { + launchActivity() + } + } + } + } + } + + private fun launchActivity() { startActivity(Intent(this, MainActivity::class.java)) finish() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt index 4f8bce7c8..6769ec72d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Config.kt @@ -494,4 +494,8 @@ class Config(context: Context) : BaseConfig(context) { var editorBrushSize: Float get() = prefs.getFloat(EDITOR_BRUSH_SIZE, 0.05f) set(editorBrushSize) = prefs.edit().putFloat(EDITOR_BRUSH_SIZE, editorBrushSize).apply() + + var wereFavoritesMigrated: Boolean + get() = prefs.getBoolean(WERE_FAVORITES_MIGRATED, false) + set(wereFavoritesMigrated) = prefs.edit().putBoolean(WERE_FAVORITES_MIGRATED, wereFavoritesMigrated).apply() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt index 50ca3f376..9b549de78 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/pro/helpers/Constants.kt @@ -80,6 +80,7 @@ const val SHOW_THUMBNAIL_FILE_TYPES = "show_thumbnail_file_types" const val EDITOR_BRUSH_COLOR = "editor_brush_color" const val EDITOR_BRUSH_HARDNESS = "editor_brush_hardness" const val EDITOR_BRUSH_SIZE = "editor_brush_size" +const val WERE_FAVORITES_MIGRATED = "were_favorites_migrated" // slideshow const val SLIDESHOW_INTERVAL = "slideshow_interval"