From 5b8143d41e3a30731970410c08543aa6419cb5bb Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 12:35:40 +0200 Subject: [PATCH] replace the Shared Prefs way of storing directories by Room --- .../gallery/activities/MainActivity.kt | 9 ++++++--- .../gallery/dialogs/PickDirectoryDialog.kt | 11 ++++++----- .../simplemobiletools/gallery/extensions/Activity.kt | 7 ++++--- .../simplemobiletools/gallery/extensions/Context.kt | 6 +++--- .../com/simplemobiletools/gallery/helpers/Config.kt | 4 ---- .../simplemobiletools/gallery/helpers/Constants.kt | 2 -- .../gallery/receivers/RefreshMediaReceiver.kt | 7 ++----- 7 files changed, 21 insertions(+), 25 deletions(-) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index c56c0ebae..1954b593d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -274,9 +274,12 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } mIsGettingDirs = true - val dirs = getCachedDirectories() - if (dirs.isNotEmpty() && !mLoadedInitialPhotos) { - gotDirectories(dirs, true) + if (!mLoadedInitialPhotos) { + getCachedDirectories { + if (it.isNotEmpty()) { + gotDirectories(it, true) + } + } } if (!mLoadedInitialPhotos) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt index 471237111..e31b4b260 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickDirectoryDialog.kt @@ -37,12 +37,13 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri .setNegativeButton(R.string.cancel, null) .setNeutralButton(R.string.other_folder, { dialogInterface, i -> showOtherFolder() }) .create().apply { - activity.setupDialogStuff(view, this, R.string.select_destination) - } + activity.setupDialogStuff(view, this, R.string.select_destination) + } - val dirs = activity.getCachedDirectories() - if (dirs.isNotEmpty()) { - gotDirectories(activity.addTempFolderIfNeeded(dirs)) + activity.getCachedDirectories { + if (it.isNotEmpty()) { + gotDirectories(activity.addTempFolderIfNeeded(it)) + } } GetDirectoriesAsynctask(activity, false, false) { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index d887a097d..78d66eb98 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -244,9 +244,10 @@ fun Activity.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Bo builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) } -fun Activity.getCachedDirectories(): ArrayList { - val token = object : TypeToken>() {}.type - return Gson().fromJson>(config.directories, token) ?: ArrayList(1) +fun Activity.getCachedDirectories(callback: (ArrayList) -> Unit) { + Thread { + callback(galleryDB.DirectoryDao().getAll() as ArrayList) + }.start() } fun Activity.getCachedMedia(path: String): ArrayList { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 33c0cf0d5..75b6536bb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -19,7 +19,6 @@ import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.databases.GalleryDataBase import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.NOMEDIA -import com.simplemobiletools.gallery.helpers.SAVE_DIRS_CNT import com.simplemobiletools.gallery.helpers.SAVE_MEDIA_CNT import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium @@ -163,8 +162,9 @@ fun Context.updateStoredDirectories() { } fun Context.storeDirectoryItems(items: ArrayList) { - val subList = items.subList(0, Math.min(SAVE_DIRS_CNT, items.size)) - config.directories = Gson().toJson(subList) + Thread { + galleryDB.DirectoryDao().insertAll(items) + }.start() } fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet): String { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt index 428c214b7..209ff4f6c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -239,10 +239,6 @@ class Config(context: Context) : BaseConfig(context) { private fun getDefaultMediaColumnCount() = context.resources.getInteger(if (scrollHorizontally) R.integer.media_columns_horizontal_scroll else R.integer.media_columns_vertical_scroll) - var directories: String - get() = prefs.getString(DIRECTORIES, "") - set(directories) = prefs.edit().putString(DIRECTORIES, directories).apply() - var albumCovers: String get() = prefs.getString(ALBUM_COVERS, "") set(albumCovers) = prefs.edit().putString(ALBUM_COVERS, albumCovers).apply() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt index c0431b69c..37cf9c4c1 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -67,7 +67,6 @@ const val MAX_COLUMN_COUNT = 20 const val SHOW_TEMP_HIDDEN_DURATION = 600000L const val CLICK_MAX_DURATION = 150 const val DRAG_THRESHOLD = 8 -const val SAVE_DIRS_CNT = 60 const val SAVE_MEDIA_CNT = 80 const val DIRECTORY = "directory" @@ -77,7 +76,6 @@ const val GET_IMAGE_INTENT = "get_image_intent" const val GET_VIDEO_INTENT = "get_video_intent" const val GET_ANY_INTENT = "get_any_intent" const val SET_WALLPAPER_INTENT = "set_wallpaper_intent" -const val DIRECTORIES = "directories2" const val IS_VIEW_INTENT = "is_view_intent" const val PICKED_PATHS = "picked_paths" diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt index e2c2731d5..7163a5af5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/receivers/RefreshMediaReceiver.kt @@ -3,16 +3,13 @@ package com.simplemobiletools.gallery.receivers import android.content.BroadcastReceiver import android.content.Context import android.content.Intent -import com.google.gson.Gson import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask -import com.simplemobiletools.gallery.extensions.config -import com.simplemobiletools.gallery.helpers.SAVE_DIRS_CNT +import com.simplemobiletools.gallery.extensions.storeDirectoryItems class RefreshMediaReceiver : BroadcastReceiver() { override fun onReceive(context: Context, intent: Intent) { GetDirectoriesAsynctask(context, false, false) { - val subList = it.subList(0, Math.min(SAVE_DIRS_CNT, it.size)) - context.config.directories = Gson().toJson(subList) + context.storeDirectoryItems(it) }.execute() } }