group media right after fetching, while still on a background thread

This commit is contained in:
tibbi 2018-06-22 20:33:35 +02:00
parent 51182c5563
commit 12ed9a66c1
6 changed files with 45 additions and 40 deletions

View file

@ -648,7 +648,10 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener {
getCachedMedia(directory.path, getVideosOnly, getImagesOnly) { getCachedMedia(directory.path, getVideosOnly, getImagesOnly) {
it.forEach { it.forEach {
if (!curMedia.contains(it)) { if (!curMedia.contains(it)) {
mediumDao.deleteMediumPath(it.path) val path = (it as? Medium)?.path
if (path != null) {
mediumDao.deleteMediumPath(path)
}
} }
} }
} }

View file

@ -40,6 +40,7 @@ import com.simplemobiletools.gallery.dialogs.FilterMediaDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem
import kotlinx.android.synthetic.main.activity_media.* import kotlinx.android.synthetic.main.activity_media.*
import java.io.File import java.io.File
import java.io.IOException import java.io.IOException
@ -64,7 +65,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mCurrAsyncTask: GetMediaAsynctask? = null private var mCurrAsyncTask: GetMediaAsynctask? = null
private var mZoomListener: MyRecyclerView.MyZoomListener? = null private var mZoomListener: MyRecyclerView.MyZoomListener? = null
private var mSearchMenuItem: MenuItem? = null private var mSearchMenuItem: MenuItem? = null
private var mMedia = ArrayList<Medium>()
private var mStoredAnimateGifs = true private var mStoredAnimateGifs = true
private var mStoredCropThumbnails = true private var mStoredCropThumbnails = true
@ -74,7 +74,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private var mStoredPrimaryColor = 0 private var mStoredPrimaryColor = 0
companion object { companion object {
var mGroupedMedia = ArrayList<Medium>() // basically mMedia items reordered depending on the grouping var mMedia = ArrayList<ThumbnailItem>()
} }
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
@ -281,11 +281,10 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
private fun searchQueryChanged(text: String) { private fun searchQueryChanged(text: String) {
Thread { Thread {
val filtered = mMedia.filter { it.name.contains(text, true) } as ArrayList val filtered = mMedia.filter { it is Medium && it.name.contains(text, true) } as ArrayList
filtered.sortBy { !it.name.startsWith(text, true) } filtered.sortBy { it is Medium && !it.name.startsWith(text, true) }
val groupedMedia = MediaFetcher(applicationContext).groupMedia(filtered, mPath)
runOnUiThread { runOnUiThread {
getMediaAdapter()?.updateMedia(groupedMedia) getMediaAdapter()?.updateMedia(filtered)
} }
}.start() }.start()
} }
@ -316,17 +315,11 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
return return
} }
mGroupedMedia.clear()
val groupedMedia = MediaFetcher(applicationContext).groupMedia(mMedia.clone() as ArrayList<Medium>, mPath)
groupedMedia.filter { it is Medium }.forEach {
mGroupedMedia.add(it as Medium)
}
val currAdapter = media_grid.adapter val currAdapter = media_grid.adapter
if (currAdapter == null) { if (currAdapter == null) {
initZoomListener() initZoomListener()
val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller 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) itemClicked((it as Medium).path)
}.apply { }.apply {
setupZoomListener(mZoomListener) setupZoomListener(mZoomListener)
@ -334,7 +327,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
setupLayoutManager() setupLayoutManager()
} else { } else {
(currAdapter as MediaAdapter).updateMedia(groupedMedia) (currAdapter as MediaAdapter).updateMedia(mMedia)
} }
setupScrollDirection() setupScrollDirection()
@ -682,12 +675,12 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
} }
private fun gotMedia(media: ArrayList<Medium>, isFromCache: Boolean = false) { private fun gotMedia(media: ArrayList<ThumbnailItem>, isFromCache: Boolean = false) {
val mediaToInsert = media.clone() as ArrayList<Medium>
Thread { Thread {
mLatestMediaId = getLatestMediaId() mLatestMediaId = getLatestMediaId()
mLatestMediaDateId = getLatestMediaByDateId() mLatestMediaDateId = getLatestMediaByDateId()
if (!isFromCache) { if (!isFromCache) {
val mediaToInsert = (mMedia.clone() as ArrayList<ThumbnailItem>).filter { it is Medium }.map { it as Medium }
galleryDB.MediumDao().insertAll(mediaToInsert) galleryDB.MediumDao().insertAll(mediaToInsert)
} }
}.start() }.start()
@ -718,7 +711,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
return@deleteFiles return@deleteFiles
} }
mMedia.removeAll { filtered.map { it.path }.contains(it.path) } mMedia.removeAll { filtered.map { it.path }.contains((it as? Medium)?.path) }
Thread { Thread {
val mediumDao = galleryDB.MediumDao() val mediumDao = galleryDB.MediumDao()

View file

@ -44,6 +44,7 @@ import com.simplemobiletools.gallery.fragments.VideoFragment
import com.simplemobiletools.gallery.fragments.ViewPagerFragment import com.simplemobiletools.gallery.fragments.ViewPagerFragment
import com.simplemobiletools.gallery.helpers.* import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem
import kotlinx.android.synthetic.main.activity_medium.* import kotlinx.android.synthetic.main.activity_medium.*
import kotlinx.android.synthetic.main.bottom_actions.* import kotlinx.android.synthetic.main.bottom_actions.*
import java.io.File import java.io.File
@ -84,8 +85,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
setContentView(R.layout.activity_medium) setContentView(R.layout.activity_medium)
mMediaFiles = MediaActivity.mGroupedMedia.clone() as ArrayList<Medium> (MediaActivity.mMedia.clone() as ArrayList<ThumbnailItem>).filter { it is Medium }.mapTo(mMediaFiles) { it as Medium }
mIsShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false) mIsShowingFavorites = intent.getBooleanExtra(SHOW_FAVORITES, false)
handlePermission(PERMISSION_WRITE_STORAGE) { handlePermission(PERMISSION_WRITE_STORAGE) {
@ -225,7 +225,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
view_pager.onGlobalLayout { view_pager.onGlobalLayout {
if (!isActivityDestroyed()) { if (!isActivityDestroyed()) {
if (mMediaFiles.isNotEmpty()) { if (mMediaFiles.isNotEmpty()) {
gotMedia(mMediaFiles) gotMedia(mMediaFiles as ArrayList<ThumbnailItem>)
} }
} }
} }
@ -884,7 +884,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
}.execute() }.execute()
} }
private fun gotMedia(media: ArrayList<Medium>) { private fun gotMedia(thumbnailItems: ArrayList<ThumbnailItem>) {
val media = thumbnailItems.filter { it is Medium }.map { it as Medium } as ArrayList<Medium>
if (isDirEmpty(media) || media.hashCode() == mPrevHashcode) { if (isDirEmpty(media) || media.hashCode() == mPrevHashcode) {
return return
} }

View file

@ -7,17 +7,18 @@ import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getFavoritePaths import com.simplemobiletools.gallery.extensions.getFavoritePaths
import com.simplemobiletools.gallery.helpers.MediaFetcher import com.simplemobiletools.gallery.helpers.MediaFetcher
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem
import java.util.* import java.util.*
class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage: Boolean = false, val isPickVideo: Boolean = false, class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage: Boolean = false, val isPickVideo: Boolean = false,
val showAll: Boolean, val callback: (media: ArrayList<Medium>) -> Unit) : val showAll: Boolean, val callback: (media: ArrayList<ThumbnailItem>) -> Unit) :
AsyncTask<Void, Void, ArrayList<Medium>>() { AsyncTask<Void, Void, ArrayList<ThumbnailItem>>() {
private val mediaFetcher = MediaFetcher(context) private val mediaFetcher = MediaFetcher(context)
override fun doInBackground(vararg params: Void): ArrayList<Medium> { override fun doInBackground(vararg params: Void): ArrayList<ThumbnailItem> {
val getProperDateTaken = context.config.getFileSorting(mPath) and SORT_BY_DATE_TAKEN != 0 val getProperDateTaken = context.config.getFileSorting(mPath) and SORT_BY_DATE_TAKEN != 0
val favoritePaths = context.getFavoritePaths() val favoritePaths = context.getFavoritePaths()
return if (showAll) { val media = if (showAll) {
val foldersToScan = mediaFetcher.getFoldersToScan() val foldersToScan = mediaFetcher.getFoldersToScan()
val media = ArrayList<Medium>() val media = ArrayList<Medium>()
foldersToScan.forEach { foldersToScan.forEach {
@ -25,14 +26,15 @@ class GetMediaAsynctask(val context: Context, val mPath: String, val isPickImage
media.addAll(newMedia) media.addAll(newMedia)
} }
MediaFetcher(context).sortMedia(media, context.config.getFileSorting("")) mediaFetcher.sortMedia(media, context.config.getFileSorting(""))
media media
} else { } else {
mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths) mediaFetcher.getFilesFrom(mPath, isPickImage, isPickVideo, getProperDateTaken, favoritePaths)
} }
return mediaFetcher.groupMedia(media, mPath)
} }
override fun onPostExecute(media: ArrayList<Medium>) { override fun onPostExecute(media: ArrayList<ThumbnailItem>) {
super.onPostExecute(media) super.onPostExecute(media)
callback(media) callback(media)
} }

View file

@ -12,7 +12,6 @@ import com.simplemobiletools.gallery.adapters.MediaAdapter
import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask
import com.simplemobiletools.gallery.extensions.config import com.simplemobiletools.gallery.extensions.config
import com.simplemobiletools.gallery.extensions.getCachedMedia import com.simplemobiletools.gallery.extensions.getCachedMedia
import com.simplemobiletools.gallery.helpers.MediaFetcher
import com.simplemobiletools.gallery.helpers.VIEW_TYPE_GRID import com.simplemobiletools.gallery.helpers.VIEW_TYPE_GRID
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.models.ThumbnailItem
@ -39,7 +38,7 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c
} }
activity.getCachedMedia(path) { 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()) { if (media.isNotEmpty()) {
activity.runOnUiThread { activity.runOnUiThread {
gotMedia(media) gotMedia(media)
@ -59,11 +58,11 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c
} }
} }
private fun gotMedia(media: ArrayList<Medium>) { private fun gotMedia(media: ArrayList<ThumbnailItem>) {
if (media.hashCode() == shownMedia.hashCode()) if (media.hashCode() == shownMedia.hashCode())
return return
shownMedia = MediaFetcher(activity).groupMedia(media, path) shownMedia = media
val adapter = MediaAdapter(activity, shownMedia, null, true, false, view.media_grid, null) { val adapter = MediaAdapter(activity, shownMedia, null, true, false, view.media_grid, null) {
callback((it as Medium).path) callback((it as Medium).path)
dialog.dismiss() dialog.dismiss()
@ -83,12 +82,12 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c
if (scrollHorizontally) { if (scrollHorizontally) {
media_horizontal_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_horizontal_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble
media_horizontal_fastscroller.setViews(media_grid) { 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 { } else {
media_vertical_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble media_vertical_fastscroller.allowBubbleDisplay = activity.config.showInfoBubble
media_vertical_fastscroller.setViews(media_grid) { media_vertical_fastscroller.setViews(media_grid) {
media_vertical_fastscroller.updateBubbleText(media[it].getBubbleText(sorting)) media_vertical_fastscroller.updateBubbleText((media[it] as? Medium)?.getBubbleText(sorting) ?: "")
} }
} }
} }

View file

@ -26,6 +26,7 @@ import com.simplemobiletools.gallery.helpers.*
import com.simplemobiletools.gallery.interfaces.DirectoryDao import com.simplemobiletools.gallery.interfaces.DirectoryDao
import com.simplemobiletools.gallery.models.Directory import com.simplemobiletools.gallery.models.Directory
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem
import com.simplemobiletools.gallery.views.MySquareImageView import com.simplemobiletools.gallery.views.MySquareImageView
import pl.droidsonroids.gif.GifDrawable import pl.droidsonroids.gif.GifDrawable
import java.io.File import java.io.File
@ -163,11 +164,15 @@ fun Context.rescanFolderMediaSync(path: String) {
Thread { Thread {
val newMedia = it val newMedia = it
val mediumDao = galleryDB.MediumDao() val mediumDao = galleryDB.MediumDao()
mediumDao.insertAll(newMedia) val media = newMedia.filter { it is Medium } as ArrayList<Medium>
mediumDao.insertAll(media)
cached.forEach { cached.forEach {
if (!newMedia.contains(it)) { if (!newMedia.contains(it)) {
mediumDao.deleteMediumPath(it.path) val mediumPath = (it as? Medium)?.path
if (mediumPath != null) {
mediumDao.deleteMediumPath(mediumPath)
}
} }
} }
}.start() }.start()
@ -315,10 +320,11 @@ fun Context.getCachedDirectories(getVideosOnly: Boolean = false, getImagesOnly:
}.start() }.start()
} }
fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList<Medium>) -> Unit) { fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList<ThumbnailItem>) -> Unit) {
Thread { Thread {
val mediaFetcher = MediaFetcher(this)
val mediumDao = galleryDB.MediumDao() 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<Medium>() var media = ArrayList<Medium>()
if (path == FAVORITES) { if (path == FAVORITES) {
media.addAll(mediumDao.getFavorites()) media.addAll(mediumDao.getFavorites())
@ -349,8 +355,9 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
} }
}) as ArrayList<Medium> }) as ArrayList<Medium>
MediaFetcher(this).sortMedia(media, config.getFileSorting(path)) mediaFetcher.sortMedia(media, config.getFileSorting(path))
callback(media.clone() as ArrayList<Medium>) val grouped = mediaFetcher.groupMedia(media, path)
callback(grouped.clone() as ArrayList<ThumbnailItem>)
media.filter { !getDoesFilePathExist(it.path) }.forEach { media.filter { !getDoesFilePathExist(it.path) }.forEach {
mediumDao.deleteMediumPath(it.path) mediumDao.deleteMediumPath(it.path)