handle medium grouping at the MediaAdapter

This commit is contained in:
tibbi 2018-06-21 23:00:59 +02:00
parent 1a04a98040
commit 9eedf64c1b
4 changed files with 37 additions and 7 deletions

View file

@ -319,7 +319,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
if (currAdapter == null) {
initZoomListener()
val fastscroller = if (config.scrollHorizontally) media_horizontal_fastscroller else media_vertical_fastscroller
MediaAdapter(this, media, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) {
MediaAdapter(this, media, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller, mPath) {
itemClicked((it as Medium).path)
}.apply {
setupZoomListener(mZoomListener)

View file

@ -18,12 +18,15 @@ import com.simplemobiletools.commons.views.MyRecyclerView
import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog
import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.GROUP_BY_NONE
import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST
import com.simplemobiletools.gallery.models.Medium
import kotlinx.android.synthetic.main.photo_video_item_grid.view.*
import java.util.HashMap
import kotlin.collections.ArrayList
class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>, val listener: MediaOperationsListener?, val isAGetIntent: Boolean,
val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, fastScroller: FastScroller? = null,
val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, fastScroller: FastScroller? = null, val path: String,
itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, fastScroller, itemClick) {
private val INSTANT_LOAD_DURATION = 2000L
@ -36,6 +39,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private var delayHandler = Handler(Looper.getMainLooper())
private var currentMediaHash = media.hashCode()
private val hasOTGConnected = activity.hasOTGConnected()
private var mediumGroups = HashMap<String, ArrayList<Medium>>()
private var scrollHorizontally = config.scrollHorizontally
private var animateGifs = config.animateGifs
@ -44,6 +48,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
init {
setupDragListener(true)
groupMedia()
enableInstantLoad()
}
@ -289,6 +294,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
currentMediaHash = newMedia.hashCode()
Handler().postDelayed({
media = newMedia
groupMedia()
enableInstantLoad()
notifyDataSetChanged()
finishActMode()
@ -325,6 +331,21 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
}, INSTANT_LOAD_DURATION)
}
private fun groupMedia() {
val grouping = activity.config.getFolderGrouping(path)
if (grouping and GROUP_BY_NONE != 0) {
return
}
media.forEach {
val key = it.getGroupingKey(grouping)
if (!mediumGroups.containsKey(key)) {
mediumGroups[key] = ArrayList()
}
mediumGroups[key]!!.add(it)
}
}
private fun setupView(view: View, medium: Medium) {
view.apply {
play_outline.beVisibleIf(medium.isVideo())

View file

@ -62,7 +62,7 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c
return
shownMedia = media
val adapter = MediaAdapter(activity, media, null, true, false, view.media_grid) {
val adapter = MediaAdapter(activity, media, null, true, false, view.media_grid, null, path) {
callback((it as Medium).path)
dialog.dismiss()
}

View file

@ -6,14 +6,12 @@ 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.getFilenameExtension
import com.simplemobiletools.commons.helpers.SORT_BY_DATE_MODIFIED
import com.simplemobiletools.commons.helpers.SORT_BY_NAME
import com.simplemobiletools.commons.helpers.SORT_BY_PATH
import com.simplemobiletools.commons.helpers.SORT_BY_SIZE
import com.simplemobiletools.gallery.helpers.TYPE_GIFS
import com.simplemobiletools.gallery.helpers.TYPE_IMAGES
import com.simplemobiletools.gallery.helpers.TYPE_RAWS
import com.simplemobiletools.gallery.helpers.TYPE_VIDEOS
import com.simplemobiletools.gallery.helpers.*
import java.io.Serializable
@Entity(tableName = "media", indices = [(Index(value = "full_path", unique = true))])
@ -47,4 +45,15 @@ data class Medium(
sorting and SORT_BY_DATE_MODIFIED != 0 -> modified.formatDate()
else -> taken.formatDate()
}
fun getGroupingKey(groupBy: Int): String {
return when {
groupBy and GROUP_BY_LAST_MODIFIED != 0 -> modified.toString()
groupBy and GROUP_BY_DATE_TAKEN != 0 -> taken.toString()
groupBy and GROUP_BY_FILE_TYPE != 0 -> type.toString()
groupBy and GROUP_BY_EXTENSION != 0 -> name.getFilenameExtension().toLowerCase()
groupBy and GROUP_BY_FOLDER != 0 -> parentPath
else -> ""
}
}
}