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
val dirs = getCachedDirectories()
if (dirs.isNotEmpty() && !mLoadedInitialPhotos) {
gotDirectories(dirs, true)
if (!mLoadedInitialPhotos) {
getCachedDirectories {
if (it.isNotEmpty()) {
gotDirectories(it, true)
}
}
}
if (!mLoadedInitialPhotos) {

View file

@ -40,9 +40,10 @@ class PickDirectoryDialog(val activity: BaseSimpleActivity, val sourcePath: Stri
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) {

View file

@ -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<Directory> {
val token = object : TypeToken<List<Directory>>() {}.type
return Gson().fromJson<ArrayList<Directory>>(config.directories, token) ?: ArrayList(1)
fun Activity.getCachedDirectories(callback: (ArrayList<Directory>) -> Unit) {
Thread {
callback(galleryDB.DirectoryDao().getAll() as ArrayList<Directory>)
}.start()
}
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.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<Directory>) {
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>): 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
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()

View file

@ -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"

View file

@ -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()
}
}