From e3779f632113f692d3750612ec38d2dd8d022161 Mon Sep 17 00:00:00 2001 From: tibbi Date: Wed, 4 Jan 2017 22:06:03 +0100 Subject: [PATCH] cache the directories to show instantly --- .../gallery/activities/MainActivity.kt | 13 +++++++++++++ .../com/simplemobiletools/gallery/helpers/Config.kt | 4 ++++ .../simplemobiletools/gallery/helpers/Constants.kt | 3 +-- .../simplemobiletools/gallery/models/Directory.kt | 4 +++- 4 files changed, 21 insertions(+), 3 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 05efd993a..ae24b50c4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -14,6 +14,8 @@ import android.support.v7.widget.GridLayoutManager import android.view.Menu import android.view.MenuItem import android.view.View +import com.google.gson.Gson +import com.google.gson.reflect.TypeToken import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.models.Release import com.simplemobiletools.gallery.BuildConfig @@ -97,6 +99,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { override fun onPause() { super.onPause() deleteDirs() + storeDirectories() } override fun onDestroy() { @@ -135,6 +138,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { if (mIsGettingDirs) return + val token = object : TypeToken>() {}.type + val dirs = Gson().fromJson>(config.directories, token) ?: ArrayList(1) + gotDirectories(dirs) + mIsGettingDirs = true GetDirectoriesAsynctask(applicationContext, mIsPickVideoIntent || mIsGetVideoContentIntent, mIsPickImageIntent || mIsGetImageContentIntent, mToBeDeleted) { gotDirectories(it) @@ -329,6 +336,12 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { mDirs = dirs setupAdapter() + storeDirectories() + } + + fun storeDirectories() { + val directories = Gson().toJson(mDirs) + config.directories = directories } private fun setupAdapter() { 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 a0534c128..ee184ccc5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Config.kt @@ -95,4 +95,8 @@ class Config(context: Context) : BaseConfig(context) { var mediaColumnCnt: Int get() = prefs.getInt(MEDIA_COLUMN_CNT, context.resources.getInteger(R.integer.media_columns)) set(mediaColumnCnt) = prefs.edit().putInt(MEDIA_COLUMN_CNT, mediaColumnCnt).apply() + + var directories: String + get() = prefs.getString(DIRECTORIES, "") + set(directories) = prefs.edit().putString(DIRECTORIES, directories).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 e72958f06..d35069abc 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/Constants.kt @@ -1,7 +1,6 @@ package com.simplemobiletools.gallery.helpers // shared preferences -val PREFS_KEY = "Gallery" val IS_SAME_SORTING = "is_same_sorting" val SORT_ORDER = "sort_order" val DIRECTORY_SORT_ORDER = "directory_sort_order" @@ -23,13 +22,13 @@ val SORT_BY_SIZE = 4 val SORT_BY_DATE_TAKEN = 8 val SORT_DESCENDING = 1024 - val DIRECTORY = "directory" val MEDIUM = "medium" val GET_IMAGE_INTENT = "get_image_intent" val GET_VIDEO_INTENT = "get_video_intent" val GET_ANY_INTENT = "get_any_intent" val SET_WALLPAPER_INTENT = "set_wallpaper_intent" +val DIRECTORIES = "directories" val REQUEST_EDIT_IMAGE = 1 val REQUEST_SET_WALLPAPER = 2 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt index a87dd4737..ae46c83f4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -4,10 +4,12 @@ import com.simplemobiletools.gallery.helpers.SORT_BY_DATE_MODIFIED import com.simplemobiletools.gallery.helpers.SORT_BY_NAME import com.simplemobiletools.gallery.helpers.SORT_BY_SIZE import com.simplemobiletools.gallery.helpers.SORT_DESCENDING +import java.io.Serializable data class Directory(val path: String, val thumbnail: String, val name: String, var mediaCnt: Int, val date_modified: Long, val date_taken: Long, - var size: Long) : Comparable { + var size: Long) : Serializable, Comparable { companion object { + private val serialVersionUID = -6553649863575455L var sorting: Int = 0 }