diff --git a/app/build.gradle b/app/build.gradle index a4999534a..a2345d9be 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,6 +1,7 @@ apply plugin: 'com.android.application' apply plugin: 'kotlin-android' apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-kapt' android { compileSdkVersion 27 @@ -53,6 +54,10 @@ dependencies { implementation 'pl.droidsonroids.gif:android-gif-drawable:1.2.12' 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.github.tibbi:subsampling-scale-image-view:v3.10.0-fork' 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 677b1caab..e685bac9b 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -29,6 +29,7 @@ import com.simplemobiletools.gallery.BuildConfig import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.DirectoryAdapter import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask +import com.simplemobiletools.gallery.databases.DirectoryDataBase import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* @@ -62,6 +63,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private var mTempShowHiddenHandler = Handler() private var mCurrAsyncTask: GetDirectoriesAsynctask? = null private var mZoomListener: MyRecyclerView.MyZoomListener? = null + private var mDirsDB: DirectoryDataBase? = null private var mStoredAnimateGifs = true private var mStoredCropThumbnails = true @@ -89,6 +91,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { removeTempFolder() directories_refresh_layout.setOnRefreshListener { getDirectories() } mDirs = ArrayList() + mDirsDB = DirectoryDataBase.getInstance(applicationContext) storeStateVariables() checkWhatsNewDialog() @@ -551,11 +554,14 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } private fun gotDirectories(newDirs: ArrayList, isFromCache: Boolean) { - /*if (!isFromCache) { + if (!isFromCache) { Thread { - checkFolderContentChange(newDirs) + //checkFolderContentChange(newDirs) + newDirs.forEach { + mDirsDB!!.DirectoryDao().insert(it) + } }.start() - }*/ + } val dirs = getSortedDirectories(newDirs) directories_refresh_layout.isRefreshing = false diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt index 19f73ddde..3f19087c0 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetDirectoriesAsynctask.kt @@ -38,7 +38,12 @@ class GetDirectoriesAsynctask(val context: Context, val isPickVideo: Boolean, va val firstItem = curMedia.first() 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 ?: "" if (thumbnail.startsWith(OTG_PATH)) { 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 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 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) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt new file mode 100644 index 000000000..102329dec --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt @@ -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 + } + } +} diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt index f7073185f..d887a097d 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Activity.kt @@ -211,7 +211,7 @@ fun BaseSimpleActivity.addTempFolderIfNeeded(dirs: ArrayList): ArrayL val directories = ArrayList() val tempFolderPath = config.tempFolderPath 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.addAll(dirs) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt new file mode 100644 index 000000000..2761c1f66 --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt @@ -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 + + @Insert(onConflict = REPLACE) + fun insert(directory: Directory) +} 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 3bb84637e..02b18d8d8 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Directory.kt @@ -1,14 +1,28 @@ 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.formatSize import com.simplemobiletools.commons.helpers.* 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, - val size: Long, val isOnSDCard: Boolean) : Serializable, Comparable { +@Entity(tableName = "directories", indices = [Index(value = "path", unique = true)]) +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 { + companion object { - private val serialVersionUID = -6553345863555455L + private const val serialVersionUID = -6553345863555455L var sorting: Int = 0 } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt index f00aedda9..9f3efc09a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -2,7 +2,6 @@ package com.simplemobiletools.gallery.models import com.simplemobiletools.commons.extensions.formatDate import com.simplemobiletools.commons.extensions.formatSize -import com.simplemobiletools.commons.extensions.getMimeType import com.simplemobiletools.commons.extensions.isDng import com.simplemobiletools.commons.helpers.* 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 { companion object { - private val serialVersionUID = -6553149366975455L + private const val serialVersionUID = -6553149366975455L 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 getMimeType() = path.getMimeType() - override fun compareTo(other: Medium): Int { var result: Int when {