remove ThumbnailMedium, make Medium itself implement ThumbnailItem

This commit is contained in:
tibbi 2018-06-22 19:59:56 +02:00
parent 2decc73e39
commit 51182c5563
5 changed files with 29 additions and 62 deletions

View file

@ -40,7 +40,6 @@ 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.ThumbnailMedium
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
@ -319,10 +318,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
mGroupedMedia.clear() mGroupedMedia.clear()
val groupedMedia = MediaFetcher(applicationContext).groupMedia(mMedia.clone() as ArrayList<Medium>, mPath) val groupedMedia = MediaFetcher(applicationContext).groupMedia(mMedia.clone() as ArrayList<Medium>, mPath)
groupedMedia.filter { it is ThumbnailMedium }.forEach { groupedMedia.filter { it is Medium }.forEach {
it as ThumbnailMedium mGroupedMedia.add(it as Medium)
val medium = Medium(0L, it.name, it.path, it.parentPath, it.modified, it.taken, it.size, it.type, it.isFavorite)
mGroupedMedia.add(medium)
} }
val currAdapter = media_grid.adapter val currAdapter = media_grid.adapter
@ -330,7 +327,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
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, groupedMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid, fastscroller) {
itemClicked((it as ThumbnailMedium).path) itemClicked((it as Medium).path)
}.apply { }.apply {
setupZoomListener(mZoomListener) setupZoomListener(mZoomListener)
media_grid.adapter = this media_grid.adapter = this

View file

@ -19,8 +19,8 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog import com.simplemobiletools.gallery.dialogs.DeleteWithRememberDialog
import com.simplemobiletools.gallery.extensions.* import com.simplemobiletools.gallery.extensions.*
import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST import com.simplemobiletools.gallery.helpers.VIEW_TYPE_LIST
import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.models.ThumbnailItem
import com.simplemobiletools.gallery.models.ThumbnailMedium
import com.simplemobiletools.gallery.models.ThumbnailSection import com.simplemobiletools.gallery.models.ThumbnailSection
import kotlinx.android.synthetic.main.photo_video_item_grid.view.* import kotlinx.android.synthetic.main.photo_video_item_grid.view.*
import kotlinx.android.synthetic.main.thumbnail_section.view.* import kotlinx.android.synthetic.main.thumbnail_section.view.*
@ -77,15 +77,15 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) {
val tmbItem = media.getOrNull(position) ?: return val tmbItem = media.getOrNull(position) ?: return
if (tmbItem is ThumbnailMedium) { if (tmbItem is Medium) {
visibleItemPaths.add(tmbItem.path) visibleItemPaths.add(tmbItem.path)
} }
val view = holder.bindView(tmbItem, !allowMultiplePicks) { itemView, adapterPosition -> val view = holder.bindView(tmbItem, !allowMultiplePicks) { itemView, adapterPosition ->
if (tmbItem is ThumbnailMedium) { if (tmbItem is Medium) {
setupThumbnailMedium(itemView, tmbItem) setupThumbnail(itemView, tmbItem)
} else { } else {
setupThumbnailSection(itemView, tmbItem as ThumbnailSection) setupSection(itemView, tmbItem as ThumbnailSection)
} }
} }
bindViewHolder(holder, position, view) bindViewHolder(holder, position, view)
@ -137,7 +137,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
} }
override fun getSelectableItemCount() = media.filter { it is ThumbnailMedium }.size override fun getSelectableItemCount() = media.filter { it is Medium }.size
override fun getIsItemSelectable(position: Int) = !isASectionTitle(position) override fun getIsItemSelectable(position: Int) = !isASectionTitle(position)
@ -191,7 +191,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
private fun showProperties() { private fun showProperties() {
if (selectedPositions.size <= 1) { if (selectedPositions.size <= 1) {
PropertiesDialog(activity, (media[selectedPositions.first()] as ThumbnailMedium).path, config.shouldShowHidden) PropertiesDialog(activity, (media[selectedPositions.first()] as Medium).path, config.shouldShowHidden)
} else { } else {
val paths = getSelectedPaths() val paths = getSelectedPaths()
PropertiesDialog(activity, paths, config.shouldShowHidden) PropertiesDialog(activity, paths, config.shouldShowHidden)
@ -285,7 +285,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
} }
private fun getCurrentPath() = (media[selectedPositions.first()] as ThumbnailMedium).path private fun getCurrentPath() = (media[selectedPositions.first()] as Medium).path
private fun deleteFiles() { private fun deleteFiles() {
if (selectedPositions.isEmpty()) { if (selectedPositions.isEmpty()) {
@ -293,18 +293,18 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
val fileDirItems = ArrayList<FileDirItem>(selectedPositions.size) val fileDirItems = ArrayList<FileDirItem>(selectedPositions.size)
val removeMedia = ArrayList<ThumbnailMedium>(selectedPositions.size) val removeMedia = ArrayList<Medium>(selectedPositions.size)
if (media.size <= selectedPositions.first()) { if (media.size <= selectedPositions.first()) {
finishActMode() finishActMode()
return return
} }
val SAFPath = (media[selectedPositions.first()] as ThumbnailMedium).path val SAFPath = (media[selectedPositions.first()] as Medium).path
activity.handleSAFDialog(SAFPath) { activity.handleSAFDialog(SAFPath) {
selectedPositions.sortedDescending().forEach { selectedPositions.sortedDescending().forEach {
val thumbnailItem = media[it] val thumbnailItem = media[it]
if (thumbnailItem is ThumbnailMedium) { if (thumbnailItem is Medium) {
fileDirItems.add(FileDirItem(thumbnailItem.path, thumbnailItem.name)) fileDirItems.add(FileDirItem(thumbnailItem.path, thumbnailItem.name))
removeMedia.add(thumbnailItem) removeMedia.add(thumbnailItem)
} }
@ -316,10 +316,10 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
} }
private fun getSelectedMedia(): List<ThumbnailMedium> { private fun getSelectedMedia(): List<Medium> {
val selectedMedia = ArrayList<ThumbnailMedium>(selectedPositions.size) val selectedMedia = ArrayList<Medium>(selectedPositions.size)
selectedPositions.forEach { selectedPositions.forEach {
selectedMedia.add(media[it] as ThumbnailMedium) selectedMedia.add(media[it] as Medium)
} }
return selectedMedia return selectedMedia
} }
@ -367,29 +367,29 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
}, INSTANT_LOAD_DURATION) }, INSTANT_LOAD_DURATION)
} }
fun getItemBubbleText(position: Int, sorting: Int) = (media[position] as? ThumbnailMedium)?.getBubbleText(sorting) fun getItemBubbleText(position: Int, sorting: Int) = (media[position] as? Medium)?.getBubbleText(sorting)
private fun setupThumbnailMedium(view: View, medium: ThumbnailMedium) { private fun setupThumbnail(view: View, medium: Medium) {
view.apply { view.apply {
play_outline.beVisibleIf(medium.isVideo()) play_outline.beVisibleIf(medium.isVideo())
photo_name.beVisibleIf(displayFilenames || isListViewType) photo_name.beVisibleIf(displayFilenames || isListViewType)
photo_name.text = medium.name photo_name.text = medium.name
photo_name.tag = medium.path photo_name.tag = medium.path
var thumbnailPath = medium.path var path = medium.path
if (hasOTGConnected && thumbnailPath.startsWith(OTG_PATH)) { if (hasOTGConnected && path.startsWith(OTG_PATH)) {
thumbnailPath = thumbnailPath.getOTGPublicPath(context) path = path.getOTGPublicPath(context)
} }
if (loadImageInstantly) { if (loadImageInstantly) {
activity.loadImage(medium.type, thumbnailPath, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails)
} else { } else {
medium_thumbnail.setImageDrawable(null) medium_thumbnail.setImageDrawable(null)
medium_thumbnail.isHorizontalScrolling = scrollHorizontally medium_thumbnail.isHorizontalScrolling = scrollHorizontally
delayHandler.postDelayed({ delayHandler.postDelayed({
val isVisible = visibleItemPaths.contains(medium.path) val isVisible = visibleItemPaths.contains(medium.path)
if (isVisible) { if (isVisible) {
activity.loadImage(medium.type, thumbnailPath, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) activity.loadImage(medium.type, path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails)
} }
}, IMAGE_LOAD_DELAY) }, IMAGE_LOAD_DELAY)
} }
@ -401,7 +401,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Thumbnai
} }
} }
private fun setupThumbnailSection(view: View, section: ThumbnailSection) { private fun setupSection(view: View, section: ThumbnailSection) {
view.apply { view.apply {
thumbnail_section.text = section.title thumbnail_section.text = section.title
thumbnail_section.setTextColor(textColor) thumbnail_section.setTextColor(textColor)

View file

@ -14,7 +14,6 @@ import com.simplemobiletools.gallery.extensions.getOTGFolderChildren
import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible import com.simplemobiletools.gallery.extensions.shouldFolderBeVisible
import com.simplemobiletools.gallery.models.Medium import com.simplemobiletools.gallery.models.Medium
import com.simplemobiletools.gallery.models.ThumbnailItem import com.simplemobiletools.gallery.models.ThumbnailItem
import com.simplemobiletools.gallery.models.ThumbnailMedium
import com.simplemobiletools.gallery.models.ThumbnailSection import com.simplemobiletools.gallery.models.ThumbnailSection
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -337,15 +336,10 @@ class MediaFetcher(val context: Context) {
} }
fun groupMedia(media: ArrayList<Medium>, path: String): ArrayList<ThumbnailItem> { fun groupMedia(media: ArrayList<Medium>, path: String): ArrayList<ThumbnailItem> {
val thumbnailItems = ArrayList<ThumbnailItem>()
val mediumGroups = LinkedHashMap<String, ArrayList<Medium>>() val mediumGroups = LinkedHashMap<String, ArrayList<Medium>>()
val currentGrouping = context.config.getFolderGrouping(path) val currentGrouping = context.config.getFolderGrouping(path)
if (currentGrouping and GROUP_BY_NONE != 0) { if (currentGrouping and GROUP_BY_NONE != 0) {
media.forEach { return media as ArrayList<ThumbnailItem>
val thumbnailMedium = ThumbnailMedium(it.name, it.path, it.parentPath, it.modified, it.taken, it.size, it.type, it.isFavorite)
thumbnailItems.add(thumbnailMedium)
}
return thumbnailItems
} }
media.forEach { media.forEach {
@ -363,12 +357,10 @@ class MediaFetcher(val context: Context) {
mediumGroups[key] = value mediumGroups[key] = value
} }
val thumbnailItems = ArrayList<ThumbnailItem>()
for ((key, value) in mediumGroups) { for ((key, value) in mediumGroups) {
thumbnailItems.add(ThumbnailSection(getFormattedKey(key, currentGrouping))) thumbnailItems.add(ThumbnailSection(getFormattedKey(key, currentGrouping)))
value.forEach { thumbnailItems.addAll(value)
val thumbnailMedium = ThumbnailMedium(it.name, it.path, it.parentPath, it.modified, it.taken, it.size, it.type, it.isFavorite)
thumbnailItems.add(thumbnailMedium)
}
} }
return thumbnailItems return thumbnailItems

View file

@ -25,7 +25,7 @@ data class Medium(
@ColumnInfo(name = "date_taken") var taken: Long, @ColumnInfo(name = "date_taken") var taken: Long,
@ColumnInfo(name = "size") val size: Long, @ColumnInfo(name = "size") val size: Long,
@ColumnInfo(name = "type") val type: Int, @ColumnInfo(name = "type") val type: Int,
@ColumnInfo(name = "is_favorite") var isFavorite: Boolean) : Serializable { @ColumnInfo(name = "is_favorite") var isFavorite: Boolean) : Serializable, ThumbnailItem() {
companion object { companion object {
private const val serialVersionUID = -6553149366975455L private const val serialVersionUID = -6553149366975455L

View file

@ -1,22 +0,0 @@
package com.simplemobiletools.gallery.models
import com.simplemobiletools.commons.extensions.formatDate
import com.simplemobiletools.commons.extensions.formatSize
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_VIDEOS
data class ThumbnailMedium(val name: String, val path: String, val parentPath: String, val modified: Long, val taken: Long, val size: Long,
val type: Int, var isFavorite: Boolean) : ThumbnailItem() {
fun isVideo() = type == TYPE_VIDEOS
fun getBubbleText(sorting: Int) = when {
sorting and SORT_BY_NAME != 0 -> name
sorting and SORT_BY_PATH != 0 -> path
sorting and SORT_BY_SIZE != 0 -> size.formatSize()
sorting and SORT_BY_DATE_MODIFIED != 0 -> modified.formatDate()
else -> taken.formatDate()
}
}