From 8b37560ed4f30b0c4e4bb24cfb6614e3cc9265d3 Mon Sep 17 00:00:00 2001 From: tibbi Date: Sat, 14 Jan 2017 13:04:22 +0100 Subject: [PATCH] cache 30 images per folder for faster loading the second time --- .../gallery/activities/MainActivity.kt | 2 +- .../gallery/activities/MediaActivity.kt | 17 +++++++++++++++++ .../simplemobiletools/gallery/helpers/Config.kt | 6 ++++++ .../gallery/helpers/Constants.kt | 1 + 4 files changed, 25 insertions(+), 1 deletion(-) 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 00cdf9736..b7921e23c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -130,6 +130,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { if (mIsGettingDirs) return + mIsGettingDirs = true val token = object : TypeToken>() {}.type val dirs = Gson().fromJson>(config.directories, token) ?: ArrayList(1) if (dirs.size == 0) { @@ -138,7 +139,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { gotDirectories(dirs) } - mIsGettingDirs = true GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent) { gotDirectories(it) }.execute() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 069523b56..28c557bdb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -13,6 +13,8 @@ import android.view.MenuItem import com.bumptech.glide.Glide import com.bumptech.glide.request.animation.GlideAnimation import com.bumptech.glide.request.target.SimpleTarget +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MediaAdapter @@ -164,6 +166,14 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { return mIsGettingMedia = true + val token = object : TypeToken>() {}.type + val media = Gson().fromJson>(config.loadFolderMedia(mPath), token) ?: ArrayList(1) + if (media.size == 0) { + media_holder.isRefreshing = true + } else { + gotMedia(media) + } + GetMediaAsynctask(applicationContext, mPath, mIsGetVideoIntent, mIsGetImageIntent, mShowAll) { gotMedia(it) }.execute() @@ -277,6 +287,13 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mMedia = media initializeGallery() + storeFolder() + } + + private fun storeFolder() { + val subList = mMedia.subList(0, Math.min(30, mMedia.size)) + val json = Gson().toJson(subList) + config.saveFolderMedia(mPath, json) } override fun refreshItems() { 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 ee184ccc5..a48e45a92 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -68,6 +68,12 @@ class Config(context: Context) : BaseConfig(context) { fun getIsFolderHidden(path: String) = hiddenFolders.contains(path) + fun saveFolderMedia(path: String, json: String) { + prefs.edit().putString(SAVE_FOLDER_PREFIX + path, json).apply() + } + + fun loadFolderMedia(path: String) = prefs.getString(SAVE_FOLDER_PREFIX + path, "") + var hiddenFolders: MutableSet get() = prefs.getStringSet(HIDDEN_FOLDERS, HashSet()) set(hiddenFolders) = prefs.edit().remove(HIDDEN_FOLDERS).putStringSet(HIDDEN_FOLDERS, hiddenFolders).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 d35069abc..9f406f00f 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -14,6 +14,7 @@ val DIR_COLUMN_CNT = "dir_column_cnt" val MEDIA_COLUMN_CNT = "media_column_cnt" val SHOW_ALL = "show_all" // display images and videos from all folders together val SHOW_MEDIA = "show_media" +val SAVE_FOLDER_PREFIX = "folder_" // sorting val SORT_BY_NAME = 1