From b0afe92afbad887a16920714573f850e2d05e9e8 Mon Sep 17 00:00:00 2001 From: tibbi Date: Fri, 20 Apr 2018 11:52:26 +0200 Subject: [PATCH] move the gallery database fetcher in a context extension --- .../com/simplemobiletools/gallery/activities/MainActivity.kt | 5 +---- .../simplemobiletools/gallery/activities/MediaActivity.kt | 5 +---- .../com/simplemobiletools/gallery/extensions/Context.kt | 3 +++ 3 files changed, 5 insertions(+), 8 deletions(-) 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 8137f0ba5..c56c0ebae 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,6 @@ 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.GalleryDataBase import com.simplemobiletools.gallery.dialogs.ChangeSortingDialog import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* @@ -63,7 +62,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { private var mTempShowHiddenHandler = Handler() private var mCurrAsyncTask: GetDirectoriesAsynctask? = null private var mZoomListener: MyRecyclerView.MyZoomListener? = null - private var mGalleryDB: GalleryDataBase? = null private var mStoredAnimateGifs = true private var mStoredCropThumbnails = true @@ -91,7 +89,6 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { removeTempFolder() directories_refresh_layout.setOnRefreshListener { getDirectories() } mDirs = ArrayList() - mGalleryDB = GalleryDataBase.getInstance(applicationContext) storeStateVariables() checkWhatsNewDialog() @@ -557,7 +554,7 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { if (!isFromCache) { Thread { //checkFolderContentChange(newDirs) - mGalleryDB!!.DirectoryDao().insertAll(newDirs) + galleryDB.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 507644f84..547cf7d37 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -33,7 +33,6 @@ 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 @@ -63,7 +62,6 @@ 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 @@ -96,7 +94,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } storeStateVariables() - mGalleryDB = GalleryDataBase.getInstance(applicationContext) if (mShowAll) { supportActionBar?.setDisplayHomeAsUpEnabled(false) @@ -629,7 +626,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { mLatestMediaId = getLatestMediaId() mLatestMediaDateId = getLatestMediaByDateId() if (!isFromCache) { - mGalleryDB!!.MediumDao().insertAll(media) + galleryDB.MediumDao().insertAll(media) } }.start() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt index 66a7fbde5..33c0cf0d5 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -16,6 +16,7 @@ import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.activities.SettingsActivity import com.simplemobiletools.gallery.asynctasks.GetDirectoriesAsynctask 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 @@ -65,6 +66,8 @@ fun Context.launchSettings() { val Context.config: Config get() = Config.newInstance(applicationContext) +val Context.galleryDB: GalleryDataBase get() = GalleryDataBase.getInstance(applicationContext) + fun Context.movePinnedDirectoriesToFront(dirs: ArrayList): ArrayList { val foundFolders = ArrayList() val pinnedFolders = config.pinnedFolders