lets use Room for caching directories

This commit is contained in:
tibbi 2018-04-19 23:35:18 +02:00
parent d0ba3314ad
commit 30ce207b1f
8 changed files with 87 additions and 13 deletions

View file

@ -1,6 +1,7 @@
apply plugin: 'com.android.application' apply plugin: 'com.android.application'
apply plugin: 'kotlin-android' apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions' apply plugin: 'kotlin-android-extensions'
apply plugin: 'kotlin-kapt'
android { android {
compileSdkVersion 27 compileSdkVersion 27
@ -53,6 +54,10 @@ dependencies {
implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.12' implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.12'
implementation 'com.github.chrisbanes:PhotoView:2.1.3' implementation 'com.github.chrisbanes:PhotoView:2.1.3'
kapt "android.arch.persistence.room:compiler:1.0.0"
implementation "android.arch.persistence.room:runtime:1.0.0"
annotationProcessor "android.arch.persistence.room:compiler:1.0.0"
//implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0' //implementation 'com.davemorrissey.labs:subsampling-scale-image-view:3.9.0'
implementation 'com.github.tibbi:subsampling-scale-image-view:v3.10.0-fork' implementation 'com.github.tibbi:subsampling-scale-image-view:v3.10.0-fork'

View file

@ -29,6 +29,7 @@ import com.simplemobiletools.gallery.BuildConfig
import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.adapters.DirectoryAdapter
import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask
import com.simplemobiletools.gallery.databases.DirectoryDataBase
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
@ -62,6 +63,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
private var mTempShowHiddenHandler = Handler() private var mTempShowHiddenHandler = Handler()
private var mCurrAsyncTask: GetDirectoriesAsynctask? = null private var mCurrAsyncTask: GetDirectoriesAsynctask? = null
private var mZoomListener: MyRecyclerView.MyZoomListener? = null private var mZoomListener: MyRecyclerView.MyZoomListener? = null
private var mDirsDB: DirectoryDataBase? = null
private var mStoredAnimateGifs = true private var mStoredAnimateGifs = true
private var mStoredCropThumbnails = true private var mStoredCropThumbnails = true
@ -89,6 +91,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
removeTempFolder() removeTempFolder()
directories_refresh_layout.setOnRefreshListener { getDirectories() } directories_refresh_layout.setOnRefreshListener { getDirectories() }
mDirs = ArrayList() mDirs = ArrayList()
mDirsDB = DirectoryDataBase.getInstance(applicationContext)
storeStateVariables() storeStateVariables()
checkWhatsNewDialog() checkWhatsNewDialog()
@ -551,11 +554,14 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
} }
private fun gotDirectories(newDirs: ArrayList<Directory>, isFromCache: Boolean) { private fun gotDirectories(newDirs: ArrayList<Directory>, isFromCache: Boolean) {
/*if (!isFromCache) { if (!isFromCache) {
Thread { Thread {
checkFolderContentChange(newDirs) //checkFolderContentChange(newDirs)
newDirs.forEach {
mDirsDB!!.DirectoryDao().insert(it)
}
}.start() }.start()
}*/ }
val dirs = getSortedDirectories(newDirs) val dirs = getSortedDirectories(newDirs)
directories_refresh_layout.isRefreshing = false directories_refresh_layout.isRefreshing = false

View file

@ -38,7 +38,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val firstItem = curMedia.first() val firstItem = curMedia.first()
val lastItem = curMedia.last() val lastItem = curMedia.last()
val parentDir = if (hasOTG && firstItem.path.startsWith(OTG_PATH)) firstItem.path.getParentPath() else File(firstItem.path).parent ?: continue val parentDir = if (hasOTG && firstItem.path.startsWith(OTG_PATH)) {
firstItem.path.getParentPath()
} else {
File(firstItem.path).parent
} ?: continue
var thumbnail = curMedia.firstOrNull { context.getDoesFilePathExist(it.path) }?.path ?: "" var thumbnail = curMedia.firstOrNull { context.getDoesFilePathExist(it.path) }?.path ?: ""
if (thumbnail.startsWith(OTG_PATH)) { if (thumbnail.startsWith(OTG_PATH)) {
thumbnail = thumbnail.getOTGPublicPath(context) thumbnail = thumbnail.getOTGPublicPath(context)
@ -54,7 +59,7 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va
val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified) val lastModified = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.modified, lastItem.modified) else Math.min(firstItem.modified, lastItem.modified)
val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken) val dateTaken = if (config.directorySorting and SORT_DESCENDING > 0) Math.max(firstItem.taken, lastItem.taken) else Math.min(firstItem.taken, lastItem.taken)
val size = curMedia.sumByLong { it.size } val size = curMedia.sumByLong { it.size }
val directory = Directory(parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, context.isPathOnSD(parentDir)) val directory = Directory(null, parentDir, thumbnail, dirName, curMedia.size, lastModified, dateTaken, size, context.isPathOnSD(parentDir))
directories.add(directory) directories.add(directory)
} }

View file

@ -0,0 +1,31 @@
package com.simplemobiletools.gallery.databases
import android.arch.persistence.room.Database
import android.arch.persistence.room.Room
import android.arch.persistence.room.RoomDatabase
import android.content.Context
import com.simplemobiletools.gallery.interfaces.DirectoryDao
import com.simplemobiletools.gallery.models.Directory
@Database(entities = [(Directory::class)], version = 1)
abstract class DirectoryDataBase : RoomDatabase() {
abstract fun DirectoryDao(): DirectoryDao
companion object {
private var INSTANCE: DirectoryDataBase? = null
fun getInstance(context: Context): DirectoryDataBase {
if (INSTANCE == null) {
synchronized(DirectoryDataBase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, DirectoryDataBase::class.java, "directories.db").build()
}
}
return INSTANCE!!
}
fun destroyInstance() {
INSTANCE = null
}
}
}

View file

@ -211,7 +211,7 @@ fun BaseSimpleActivity.addTempFolderIfNeeded(dirs: ArrayList<Directory>): ArrayL
val directories = ArrayList<Directory>() val directories = ArrayList<Directory>()
val tempFolderPath = config.tempFolderPath val tempFolderPath = config.tempFolderPath
if (tempFolderPath.isNotEmpty()) { if (tempFolderPath.isNotEmpty()) {
val newFolder = Directory(tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath)) val newFolder = Directory(null, tempFolderPath, "", tempFolderPath.getFilenameFromPath(), 0, 0, 0, 0L, isPathOnSD(tempFolderPath))
directories.add(newFolder) directories.add(newFolder)
} }
directories.addAll(dirs) directories.addAll(dirs)

View file

@ -0,0 +1,16 @@
package com.simplemobiletools.gallery.interfaces
import android.arch.persistence.room.Dao
import android.arch.persistence.room.Insert
import android.arch.persistence.room.OnConflictStrategy.REPLACE
import android.arch.persistence.room.Query
import com.simplemobiletools.gallery.models.Directory
@Dao
interface DirectoryDao {
@Query("SELECT * from directories")
fun getAll(): List<Directory>
@Insert(onConflict = REPLACE)
fun insert(directory: Directory)
}

View file

@ -1,14 +1,28 @@
package com.simplemobiletools.gallery.models package com.simplemobiletools.gallery.models
import android.arch.persistence.room.ColumnInfo
import android.arch.persistence.room.Entity
import android.arch.persistence.room.Index
import android.arch.persistence.room.PrimaryKey
import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.formatSize import com.simplemobiletools.commons.extensions.formatSize
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import java.io.Serializable import java.io.Serializable
data class Directory(var path: String, var tmb: String, var name: String, var mediaCnt: Int, val modified: Long, val taken: Long, @Entity(tableName = "directories", indices = [Index(value = "path", unique = true)])
val size: Long, val isOnSDCard: Boolean) : Serializable, Comparable<Directory> { data class Directory(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "path") var path: String,
@ColumnInfo(name = "thumbnail") var tmb: String,
@ColumnInfo(name = "filename") var name: String,
@ColumnInfo(name = "media_count") var mediaCnt: Int,
@ColumnInfo(name = "last_modified") val modified: Long,
@ColumnInfo(name = "date_taken") val taken: Long,
@ColumnInfo(name = "size") val size: Long,
@ColumnInfo(name = "is_on_sd_card") val isOnSDCard: Boolean) : Serializable, Comparable<Directory> {
companion object { companion object {
private val serialVersionUID = -6553345863555455L private const val serialVersionUID = -6553345863555455L
var sorting: Int = 0 var sorting: Int = 0
} }

View file

@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.models
import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.formatSize import com.simplemobiletools.commons.extensions.formatSize
import com.simplemobiletools.commons.extensions.getMimeType
import com.simplemobiletools.commons.extensions.isDng import com.simplemobiletools.commons.extensions.isDng
import com.simplemobiletools.commons.helpers.* import com.simplemobiletools.commons.helpers.*
import com.simplemobiletools.gallery.helpers.TYPE_GIF import com.simplemobiletools.gallery.helpers.TYPE_GIF
@ -12,7 +11,7 @@ import java.io.Serializable
data class Medium(var name: String, var path: String, val modified: Long, val taken: Long, val size: Long, val type: Int) : Serializable, Comparable<Medium> { data class Medium(var name: String, var path: String, val modified: Long, val taken: Long, val size: Long, val type: Int) : Serializable, Comparable<Medium> {
companion object { companion object {
private val serialVersionUID = -6553149366975455L private const val serialVersionUID = -6553149366975455L
var sorting: Int = 0 var sorting: Int = 0
} }
@ -24,8 +23,6 @@ data class Medium(var name: String, var path: String, val modified: Long, val ta
fun isDng() = path.isDng() fun isDng() = path.isDng()
fun getMimeType() = path.getMimeType()
override fun compareTo(other: Medium): Int { override fun compareTo(other: Medium): Int {
var result: Int var result: Int
when { when {