replace the Shared Prefs way of storing directories by Room

This commit is contained in:
tibbi 2018-04-20 12:35:40 +02:00
parent 236ceafe46
commit 5b8143d41e
7 changed files with 21 additions and 25 deletions

View file

@ -274,9 +274,12 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
mIsGettingDirs = true mIsGettingDirs = true
val dirs = getCachedDirectories() if (!mLoadedInitialPhotos) {
if (dirs.isNotEmpty() && !mLoadedInitialPhotos) { getCachedDirectories {
gotDirectories(dirs, true) if (it.isNotEmpty()) {
gotDirectories(it, true)
}
}
} }
if (!mLoadedInitialPhotos) { if (!mLoadedInitialPhotos) {

View file

@ -40,9 +40,10 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri
activity.setupDialogStuff(view, this, R.string.select_destination) activity.setupDialogStuff(view, this, R.string.select_destination)
} }
val dirs = activity.getCachedDirectories() activity.getCachedDirectories {
if (dirs.isNotEmpty()) { if (it.isNotEmpty()) {
gotDirectories(activity.addTempFolderIfNeeded(dirs)) gotDirectories(activity.addTempFolderIfNeeded(it))
}
} }
GetDirectoriesAsynctask(activity, false, false) { GetDirectoriesAsynctask(activity, false, false) {

View file

@ -244,9 +244,10 @@ fun Activity.loadJpg(path: String, target: MySquareImageView, cropThumbnails: Bo
builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target) builder.apply(options).transition(DrawableTransitionOptions.withCrossFade()).into(target)
} }
fun Activity.getCachedDirectories(): ArrayList<Directory> { fun Activity.getCachedDirectories(callback: (ArrayList<Directory>) -> Unit) {
val token = object : TypeToken<List<Directory>>() {}.type Thread {
return Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList(1) callback(galleryDB.DirectoryDao().getAll() as ArrayList<Directory>)
}.start()
} }
fun Activity.getCachedMedia(path: String): ArrayList<Medium> { fun Activity.getCachedMedia(path: String): ArrayList<Medium> {

View file

@ -19,7 +19,6 @@ import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.databases.GalleryDataBase import com.simplemobiletools.gallery.databases.GalleryDataBase
import com.simplemobiletools.gallery.helpers.Config import com.simplemobiletools.gallery.helpers.Config
import com.simplemobiletools.gallery.helpers.NOMEDIA 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.helpers.SAVE_MEDIA_CNT
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
@ -163,8 +162,9 @@ fun Context.updateStoredDirectories() {
} }
fun Context.storeDirectoryItems(items: ArrayList<Directory>) { fun Context.storeDirectoryItems(items: ArrayList<Directory>) {
val subList = items.subList(0, Math.min(SAVE_DIRS_CNT, items.size)) Thread {
config.directories = Gson().toJson(subList) galleryDB.DirectoryDao().insertAll(items)
}.start()
} }
fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>): String { fun Context.checkAppendingHidden(path: String, hidden: String, includedFolders: MutableSet<String>): String {

View file

@ -239,10 +239,6 @@ class Config(context: Context) : BaseConfig(context) {
private fun getDefaultMediaColumnCount() = context.resources.getInteger(if (scrollHorizontally) R.integer.media_columns_horizontal_scroll private fun getDefaultMediaColumnCount() = context.resources.getInteger(if (scrollHorizontally) R.integer.media_columns_horizontal_scroll
else R.integer.media_columns_vertical_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 var albumCovers: String
get() = prefs.getString(ALBUM_COVERS, "") get() = prefs.getString(ALBUM_COVERS, "")
set(albumCovers) = prefs.edit().putString(ALBUM_COVERS, albumCovers).apply() set(albumCovers) = prefs.edit().putString(ALBUM_COVERS, albumCovers).apply()

View file

@ -67,7 +67,6 @@ const val MAX_COLUMN_COUNT = 20
const val SHOW_TEMP_HIDDEN_DURATION = 600000L const val SHOW_TEMP_HIDDEN_DURATION = 600000L
const val CLICK_MAX_DURATION = 150 const val CLICK_MAX_DURATION = 150
const val DRAG_THRESHOLD = 8 const val DRAG_THRESHOLD = 8
const val SAVE_DIRS_CNT = 60
const val SAVE_MEDIA_CNT = 80 const val SAVE_MEDIA_CNT = 80
const val DIRECTORY = "directory" 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_VIDEO_INTENT = "get_video_intent"
const val GET_ANY_INTENT = "get_any_intent" const val GET_ANY_INTENT = "get_any_intent"
const val SET_WALLPAPER_INTENT = "set_wallpaper_intent" const val SET_WALLPAPER_INTENT = "set_wallpaper_intent"
const val DIRECTORIES = "directories2"
const val IS_VIEW_INTENT = "is_view_intent" const val IS_VIEW_INTENT = "is_view_intent"
const val PICKED_PATHS = "picked_paths" const val PICKED_PATHS = "picked_paths"

View file

@ -3,16 +3,13 @@ package com.simplemobiletools.gallery.receivers
import android.content.BroadcastReceiver import android.content.BroadcastReceiver
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import com.google.gson.Gson
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.storeDirectoryItems
import com.simplemobiletools.gallery.helpers.SAVE_DIRS_CNT
class RefreshMediaReceiver : BroadcastReceiver() { class RefreshMediaReceiver : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) { override fun onReceive(context: Context, intent: Intent) {
GetDirectoriesAsynctask(context, false, false) { GetDirectoriesAsynctask(context, false, false) {
val subList = it.subList(0, Math.min(SAVE_DIRS_CNT, it.size)) context.storeDirectoryItems(it)
context.config.directories = Gson().toJson(subList)
}.execute() }.execute()
} }
} }