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) {
it.forEach {
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.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<Medium>()
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<Medium>() // basically mMedia items reordered depending on the grouping
var mMedia = ArrayList<ThumbnailItem>()
}
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<Medium>, 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<Medium>, isFromCache: Boolean = false) {
val mediaToInsert = media.clone() as ArrayList<Medium>
private fun gotMedia(media: ArrayList<ThumbnailItem>, isFromCache: Boolean = false) {
Thread {
mLatestMediaId = getLatestMediaId()
mLatestMediaDateId = getLatestMediaByDateId()
if (!isFromCache) {
val mediaToInsert = (mMedia.clone() as ArrayList<ThumbnailItem>).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()

View File

@ -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<Medium>
(MediaActivity.mMedia.clone() as ArrayList<ThumbnailItem>).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<ThumbnailItem>)
}
}
}
@ -884,7 +884,8 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
}.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) {
return
}

View File

@ -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<Medium>) -> Unit) :
AsyncTask<Void, Void, ArrayList<Medium>>() {
val showAll: Boolean, val callback: (media: ArrayList<ThumbnailItem>) -> Unit) :
AsyncTask<Void, Void, ArrayList<ThumbnailItem>>() {
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 favoritePaths = context.getFavoritePaths()
return if (showAll) {
val media = if (showAll) {
val foldersToScan = mediaFetcher.getFoldersToScan()
val media = ArrayList<Medium>()
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<Medium>) {
override fun onPostExecute(media: ArrayList<ThumbnailItem>) {
super.onPostExecute(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.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<Medium>) {
private fun gotMedia(media: ArrayList<ThumbnailItem>) {
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) ?: "")
}
}
}

View File

@ -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<Medium>
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<Medium>) -> Unit) {
fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImagesOnly: Boolean = false, callback: (ArrayList<ThumbnailItem>) -> 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<Medium>()
if (path == FAVORITES) {
media.addAll(mediumDao.getFavorites())
@ -349,8 +355,9 @@ fun Context.getCachedMedia(path: String, getVideosOnly: Boolean = false, getImag
}
}) as ArrayList<Medium>
MediaFetcher(this).sortMedia(media, config.getFileSorting(path))
callback(media.clone() as ArrayList<Medium>)
mediaFetcher.sortMedia(media, config.getFileSorting(path))
val grouped = mediaFetcher.groupMedia(media, path)
callback(grouped.clone() as ArrayList<ThumbnailItem>)
media.filter { !getDoesFilePathExist(it.path) }.forEach {
mediumDao.deleteMediumPath(it.path)