store media in a room db too

This commit is contained in:
tibbi 2018-04-20 11:01:01 +02:00
parent 30ce207b1f
commit ce4ee915bb
8 changed files with 63 additions and 18 deletions

View file

@ -29,7 +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.databases.GalleryDataBase
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.extensions.*
@ -63,7 +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 mGalleryDB: GalleryDataBase? = null
private var mStoredAnimateGifs = true
private var mStoredCropThumbnails = true
@ -91,7 +91,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
removeTempFolder()
directories_refresh_layout.setOnRefreshListener { getDirectories() }
mDirs = ArrayList()
mDirsDB = DirectoryDataBase.getInstance(applicationContext)
mGalleryDB = GalleryDataBase.getInstance(applicationContext)
storeStateVariables()
checkWhatsNewDialog()
@ -557,9 +557,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
if (!isFromCache) {
Thread {
//checkFolderContentChange(newDirs)
newDirs.forEach {
mDirsDB!!.DirectoryDao().insert(it)
}
mGalleryDB!!.DirectoryDao().insertAll(newDirs)
}.start()
}

View file

@ -33,6 +33,7 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.databases.GalleryDataBase
import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog
import com.simplemobiletools.gallery.dialogs.ExcludeFolderDialog
import com.simplemobiletools.gallery.dialogs.FilterMediaDialog
@ -62,6 +63,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mZoomListener: MyRecyclerView.MyZoomListener? = null
private var mSearchMenuItem: MenuItem? = null
private var mGalleryDB: GalleryDataBase? = null
private var mStoredAnimateGifs = true
private var mStoredCropThumbnails = true
@ -94,8 +96,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
}
storeStateVariables()
if (mShowAll)
mGalleryDB = GalleryDataBase.getInstance(applicationContext)
if (mShowAll) {
supportActionBar?.setDisplayHomeAsUpEnabled(false)
}
media_empty_text.setOnClickListener {
showFilterMediaDialog()
@ -623,6 +628,9 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
Thread {
mLatestMediaId = getLatestMediaId()
mLatestMediaDateId = getLatestMediaByDateId()
if (!isFromCache) {
mGalleryDB!!.MediumDao().insertAll(media)
}
}.start()
mIsGettingMedia = false

View file

@ -90,7 +90,7 @@ open class PhotoVideoActivity : SimpleActivity(), ViewPagerFragment.FragmentList
else -> TYPE_GIF
}
mMedium = Medium(getFilenameFromUri(mUri!!), mUri.toString(), 0, 0, file.length(), type)
mMedium = Medium(null, getFilenameFromUri(mUri!!), mUri.toString(), 0, 0, file.length(), type)
supportActionBar?.title = mMedium!!.name
bundle.putSerializable(MEDIUM, mMedium)

View file

@ -5,20 +5,24 @@ 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.interfaces.MediumDao
import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium
@Database(entities = [(Directory::class)], version = 1)
abstract class DirectoryDataBase : RoomDatabase() {
@Database(entities = [(Directory::class), (Medium::class)], version = 1)
abstract class GalleryDataBase : RoomDatabase() {
abstract fun DirectoryDao(): DirectoryDao
companion object {
private var INSTANCE: DirectoryDataBase? = null
abstract fun MediumDao(): MediumDao
fun getInstance(context: Context): DirectoryDataBase {
companion object {
private var INSTANCE: GalleryDataBase? = null
fun getInstance(context: Context): GalleryDataBase {
if (INSTANCE == null) {
synchronized(DirectoryDataBase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, DirectoryDataBase::class.java, "directories.db").build()
synchronized(GalleryDataBase::class) {
INSTANCE = Room.databaseBuilder(context.applicationContext, GalleryDataBase::class.java, "gallery.db").build()
}
}
return INSTANCE!!

View file

@ -247,7 +247,7 @@ class MediaFetcher(val context: Context) {
else -> TYPE_GIF
}
val medium = Medium(filename, file.absolutePath, dateModified, dateTaken, size, type)
val medium = Medium(null, filename, file.absolutePath, dateModified, dateTaken, size, type)
curMedia.add(medium)
}
}
@ -296,7 +296,7 @@ class MediaFetcher(val context: Context) {
}
val path = Uri.decode(file.uri.toString().replaceFirst("${context.config.OTGBasePath}%3A", OTG_PATH))
val medium = Medium(filename, path, dateModified, dateTaken, size, type)
val medium = Medium(null, filename, path, dateModified, dateTaken, size, type)
curMedia.add(medium)
}
}

View file

@ -13,4 +13,7 @@ interface DirectoryDao {
@Insert(onConflict = REPLACE)
fun insert(directory: Directory)
@Insert(onConflict = REPLACE)
fun insertAll(directories: List<Directory>)
}

View file

@ -0,0 +1,19 @@
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.Medium
@Dao
interface MediumDao {
@Query("SELECT * from media")
fun getAll(): List<Medium>
@Insert(onConflict = REPLACE)
fun insert(medium: Medium)
@Insert(onConflict = REPLACE)
fun insertAll(media: List<Medium>)
}

View file

@ -1,5 +1,9 @@
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.extensions.isDng
@ -9,7 +13,16 @@ import com.simplemobiletools.gallery.helpers.TYPE_IMAGE
import com.simplemobiletools.gallery.helpers.TYPE_VIDEO
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> {
@Entity(tableName = "media", indices = [(Index(value = "path", unique = true))])
data class Medium(
@PrimaryKey(autoGenerate = true) var id: Long?,
@ColumnInfo(name = "filename") var name: String,
@ColumnInfo(name = "path") var path: String,
@ColumnInfo(name = "last_modified") val modified: Long,
@ColumnInfo(name = "date_taken") val taken: Long,
@ColumnInfo(name = "size") val size: Long,
@ColumnInfo(name = "type") val type: Int) : Serializable, Comparable<Medium> {
companion object {
private const val serialVersionUID = -6553149366975455L
var sorting: Int = 0