From ce4ee915bb6dfbd8712a561f426d59c1fabc25ba Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 11:01:01 +0200 Subject: [PATCH] store media in a room db too --- .../gallery/activities/MainActivity.kt | 10 ++++------ .../gallery/activities/MediaActivity.kt | 10 +++++++++- .../gallery/activities/PhotoVideoActivity.kt | 2 +- ...irectoryDataBase.kt => GalleryDataBase.kt} | 18 +++++++++++------- .../gallery/helpers/MediaFetcher.kt | 4 ++-- .../gallery/interfaces/DirectoryDao.kt | 3 +++ .../gallery/interfaces/MediumDao.kt | 19 +++++++++++++++++++ .../gallery/models/Medium.kt | 15 ++++++++++++++- 8 files changed, 63 insertions(+), 18 deletions(-) rename app/src/main/kotlin/com/simplemobiletools/gallery/databases/{DirectoryDataBase.kt => GalleryDataBase.kt} (56%) create mode 100644 app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt 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 e685bac9b..8137f0ba5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -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() } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index cb26c7c38..507644f84 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -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 diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt index f3c4084bd..dd68c39c2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/PhotoVideoActivity.kt @@ -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) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/databases/GalleryDataBase.kt similarity index 56% rename from app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt rename to app/src/main/kotlin/com/simplemobiletools/gallery/databases/GalleryDataBase.kt index 102329dec..a011337e2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/databases/DirectoryDataBase.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/databases/GalleryDataBase.kt @@ -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!! diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt index b4289d92f..495c359f6 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/helpers/MediaFetcher.kt @@ -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) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt index 2761c1f66..fbec9b6fb 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/DirectoryDao.kt @@ -13,4 +13,7 @@ interface DirectoryDao { @Insert(onConflict = REPLACE) fun insert(directory: Directory) + + @Insert(onConflict = REPLACE) + fun insertAll(directories: List) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt new file mode 100644 index 000000000..d2ccd3aee --- /dev/null +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/interfaces/MediumDao.kt @@ -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 + + @Insert(onConflict = REPLACE) + fun insert(medium: Medium) + + @Insert(onConflict = REPLACE) + fun insertAll(media: List) +} 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 9f3efc09a..6fc3c5818 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/models/Medium.kt @@ -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 { +@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 { + companion object { private const val serialVersionUID = -6553149366975455L var sorting: Int = 0