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 c6d324019..9a6ab9cb4 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -648,7 +648,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { it.forEach { if (!curMedia.contains(it)) { - mediumDao.deleteMediumPath(it.path) + val path = (it as? Medium)?.path + if (path != null) { + mediumDao.deleteMediumPath(path) + } } } } 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 eb43adbfb..8578357b2 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -40,6 +40,7 @@ import com.simplemobiletools.gallery.dialogs.FilterMediaDialog import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.models.ThumbnailItem import kotlinx.android.synthetic.main.activity_media.* import java.io.File import java.io.IOException @@ -64,7 +65,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private var mCurrAsyncTask: GetMediaAsynctask? = null private var mZoomListener: MyRecyclerView.MyZoomListener? = null private var mSearchMenuItem: MenuItem? = null - private var mMedia = ArrayList() private var mStoredAnimateGifs = true private var mStoredCropThumbnails = true @@ -74,7 +74,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private var mStoredPrimaryColor = 0 companion object { - var mGroupedMedia = ArrayList() // basically mMedia items reordered depending on the grouping + var mMedia = ArrayList() } override fun onCreate(savedInstanceState: Bundle?) { @@ -281,11 +281,10 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun searchQueryChanged(text: String) { Thread { - val filtered = mMedia.filter { it.name.contains(text, true) } as ArrayList - filtered.sortBy { !it.name.startsWith(text, true) } - val groupedMedia = MediaFetcher(applicationContext).groupMedia(filtered, mPath) + val filtered = mMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList + filtered.sortBy { it is Medium && !it.name.startsWith(text, true) } runOnUiThread { - getMediaAdapter()?.updateMedia(groupedMedia) + getMediaAdapter()?.updateMedia(filtered) } }.start() } @@ -316,17 +315,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { return } - mGroupedMedia.clear() - val groupedMedia = MediaFetcher(applicationContext).groupMedia(mMedia.clone() as ArrayList, mPath) - groupedMedia.filter { it is Medium }.forEach { - mGroupedMedia.add(it as Medium) - } - val currAdapter = media_grid.adapter if (currAdapter == null) { initZoomListener() val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller - MediaAdapter(this, groupedMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) { + MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) { itemClicked((it as Medium).path) }.apply { setupZoomListener(mZoomListener) @@ -334,7 +327,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } setupLayoutManager() } else { - (currAdapter as MediaAdapter).updateMedia(groupedMedia) + (currAdapter as MediaAdapter).updateMedia(mMedia) } setupScrollDirection() @@ -682,12 +675,12 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { } } - private fun gotMedia(media: ArrayList, isFromCache: Boolean = false) { - val mediaToInsert = media.clone() as ArrayList + private fun gotMedia(media: ArrayList, isFromCache: Boolean = false) { Thread { mLatestMediaId = getLatestMediaId() mLatestMediaDateId = getLatestMediaByDateId() if (!isFromCache) { + val mediaToInsert = (mMedia.clone() as ArrayList).filter { it is Medium }.map { it as Medium } galleryDB.MediumDao().insertAll(mediaToInsert) } }.start() @@ -718,7 +711,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { return@deleteFiles } - mMedia.removeAll { filtered.map { it.path }.contains(it.path) } + mMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) } Thread { val mediumDao = galleryDB.MediumDao() diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt index c0fa82e95..015eafa9a 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/ViewPagerActivity.kt @@ -44,6 +44,7 @@ import com.simplemobiletools.gallery.fragments.VideoFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.models.ThumbnailItem import kotlinx.android.synthetic.main.activity_medium.* import kotlinx.android.synthetic.main.bottom_actions.* import java.io.File @@ -84,8 +85,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_medium) - mMediaFiles = MediaActivity.mGroupedMedia.clone() as ArrayList - + (MediaActivity.mMedia.clone() as ArrayList).filter { it is Medium }.mapTo(mMediaFiles) { it as Medium } mIsShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false) handlePermission(PERMISSION_WRITE_STORAGE) { @@ -225,7 +225,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View view_pager.onGlobalLayout { if (!isActivityDestroyed()) { if (mMediaFiles.isNotEmpty()) { - gotMedia(mMediaFiles) + gotMedia(mMediaFiles as ArrayList) } } } @@ -884,7 +884,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View }.execute() } - private fun gotMedia(media: ArrayList) { + private fun gotMedia(thumbnailItems: ArrayList) { + val media = thumbnailItems.filter { it is Medium }.map { it as Medium } as ArrayList if (isDirEmpty(media) || media.hashCode() == mPrevHashcode) { return } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt index 07b9bb630..d99d5f370 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/asynctasks/GetMediaAsynctask.kt @@ -7,17 +7,18 @@ import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getFavoritePaths import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.models.ThumbnailItem import java.util.* class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage: Boolean = false, val isPickVideo: Boolean = false, - val showAll: Boolean, val callback: (media: ArrayList) -> Unit) : - AsyncTask>() { + val showAll: Boolean, val callback: (media: ArrayList) -> Unit) : + AsyncTask>() { private val mediaFetcher = MediaFetcher(context) - override fun doInBackground(vararg params: Void): ArrayList { + override fun doInBackground(vararg params: Void): ArrayList { val getProperDateTaken = context.config.getFileSorting(mPath) and SORT_BY_DATE_TAKEN != 0 val favoritePaths = context.getFavoritePaths() - return if (showAll) { + val media = if (showAll) { val foldersToScan = mediaFetcher.getFoldersToScan() val media = ArrayList() foldersToScan.forEach { @@ -25,14 +26,15 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage media.addAll(newMedia) } - MediaFetcher(context).sortMedia(media, context.config.getFileSorting("")) + mediaFetcher.sortMedia(media, context.config.getFileSorting("")) media } else { mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) } + return mediaFetcher.groupMedia(media, mPath) } - override fun onPostExecute(media: ArrayList) { + override fun onPostExecute(media: ArrayList) { super.onPostExecute(media) callback(media) } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt index 58d18bcf4..dbdd77856 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt @@ -12,7 +12,6 @@ import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.getCachedMedia -import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.helpers.VIEW_TYPE_GRID import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.ThumbnailItem @@ -39,7 +38,7 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c } activity.getCachedMedia(path) { - val media = it.filter { !it.isVideo() } as ArrayList + val media = it.filter { it is Medium && !it.isVideo() } as ArrayList if (media.isNotEmpty()) { activity.runOnUiThread { gotMedia(media) @@ -59,11 +58,11 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c } } - private fun gotMedia(media: ArrayList) { + private fun gotMedia(media: ArrayList) { if (media.hashCode() == shownMedia.hashCode()) return - shownMedia = MediaFetcher(activity).groupMedia(media, path) + shownMedia = media val adapter = MediaAdapter(activity, shownMedia, null, true, false, view.media_grid, null) { callback((it as Medium).path) dialog.dismiss() @@ -83,12 +82,12 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c if (scrollHorizontally) { media_horizontal_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_horizontal_fastscroller.setViews(media_grid) { - media_horizontal_fastscroller.updateBubbleText(media[it].getBubbleText(sorting)) + media_horizontal_fastscroller.updateBubbleText((media[it] as? Medium)?.getBubbleText(sorting) ?: "") } } else { media_vertical_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_vertical_fastscroller.setViews(media_grid) { - media_vertical_fastscroller.updateBubbleText(media[it].getBubbleText(sorting)) + media_vertical_fastscroller.updateBubbleText((media[it] as? Medium)?.getBubbleText(sorting) ?: "") } } } 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 c4049327e..368b97ffe 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/extensions/Context.kt @@ -26,6 +26,7 @@ import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.interfaces.DirectoryDao import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Medium +import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.views.MySquareImageView import pl.droidsonroids.gif.GifDrawable import java.io.File @@ -163,11 +164,15 @@ fun Context.rescanFolderMediaSync(path: String) { Thread { val newMedia = it val mediumDao = galleryDB.MediumDao() - mediumDao.insertAll(newMedia) + val media = newMedia.filter { it is Medium } as ArrayList + mediumDao.insertAll(media) cached.forEach { if (!newMedia.contains(it)) { - mediumDao.deleteMediumPath(it.path) + val mediumPath = (it as? Medium)?.path + if (mediumPath != null) { + mediumDao.deleteMediumPath(mediumPath) + } } } }.start() @@ -315,10 +320,11 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly: }.start() } -fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { +fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList) -> Unit) { Thread { + val mediaFetcher = MediaFetcher(this) val mediumDao = galleryDB.MediumDao() - val foldersToScan = if (path.isEmpty()) MediaFetcher(this).getFoldersToScan() else arrayListOf(path) + val foldersToScan = if (path.isEmpty()) mediaFetcher.getFoldersToScan() else arrayListOf(path) var media = ArrayList() if (path == FAVORITES) { media.addAll(mediumDao.getFavorites()) @@ -349,8 +355,9 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag } }) as ArrayList - MediaFetcher(this).sortMedia(media, config.getFileSorting(path)) - callback(media.clone() as ArrayList) + mediaFetcher.sortMedia(media, config.getFileSorting(path)) + val grouped = mediaFetcher.groupMedia(media, path) + callback(grouped.clone() as ArrayList) media.filter { !getDoesFilePathExist(it.path) }.forEach { mediumDao.deleteMediumPath(it.path)