rely on thumbnailItems at getting selected items at mediaadapter

This commit is contained in:
tibbi 2018-06-22 13:46:37 +02:00
parent 4617be2238
commit e759270019
5 changed files with 52 additions and 32 deletions

View file

@ -356,7 +356,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener {
} }
} }
private fun getBubbleTextItem(index: Int, sorting: Int) = getMediaAdapter()?.media?.getOrNull(index)?.getBubbleText(sorting) ?: "" private fun getBubbleTextItem(index: Int, sorting: Int) = getMediaAdapter()?.getItemBubbleText(index, sorting) ?: ""
private fun checkLastMediaChanged() { private fun checkLastMediaChanged() {
if (isActivityDestroyed()) if (isActivityDestroyed())

View file

@ -321,7 +321,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
R.id.menu_open_with -> openPath(getCurrentPath(), true) R.id.menu_open_with -> openPath(getCurrentPath(), true)
R.id.menu_hide -> toggleFileVisibility(true) R.id.menu_hide -> toggleFileVisibility(true)
R.id.menu_unhide -> toggleFileVisibility(false) R.id.menu_unhide -> toggleFileVisibility(false)
R.id.menu_share -> shareMedium(getCurrentMedium()!!) R.id.menu_share -> shareMediumPath(getCurrentPath())
R.id.menu_delete -> checkDeleteConfirmation() R.id.menu_delete -> checkDeleteConfirmation()
R.id.menu_rename -> renameFile() R.id.menu_rename -> renameFile()
R.id.menu_edit -> openEditor(getCurrentPath()) R.id.menu_edit -> openEditor(getCurrentPath())
@ -769,7 +769,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
} }
bottom_share.setOnClickListener { bottom_share.setOnClickListener {
shareMedium(getCurrentMedium()!!) shareMediumPath(getCurrentPath())
} }
bottom_delete.setOnClickListener { bottom_delete.setOnClickListener {

View file

@ -166,7 +166,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun checkHideBtnVisibility(menu: Menu) { private fun checkHideBtnVisibility(menu: Menu) {
var hiddenCnt = 0 var hiddenCnt = 0
var unhiddenCnt = 0 var unhiddenCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach { getSelectedMedia().forEach {
if (it.name.startsWith('.')) { if (it.name.startsWith('.')) {
hiddenCnt++ hiddenCnt++
} else { } else {
@ -181,7 +181,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun checkFavoriteBtnVisibility(menu: Menu) { private fun checkFavoriteBtnVisibility(menu: Menu) {
var favoriteCnt = 0 var favoriteCnt = 0
var nonFavoriteCnt = 0 var nonFavoriteCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach { getSelectedMedia().forEach {
if (it.isFavorite) { if (it.isFavorite) {
favoriteCnt++ favoriteCnt++
} else { } else {
@ -194,16 +194,14 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
} }
private fun confirmSelection() { private fun confirmSelection() {
val paths = getSelectedMedia().map { it.path } as ArrayList<String> listener?.selectedPaths(getSelectedPaths())
listener?.selectedPaths(paths)
} }
private fun showProperties() { private fun showProperties() {
if (selectedPositions.size <= 1) { if (selectedPositions.size <= 1) {
PropertiesDialog(activity, media[selectedPositions.first()].path, config.shouldShowHidden) PropertiesDialog(activity, (thumbnailItems[selectedPositions.first()] as ThumbnailMedium).path, config.shouldShowHidden)
} else { } else {
val paths = ArrayList<String>() val paths = getSelectedPaths()
selectedPositions.forEach { paths.add(media[it].path) }
PropertiesDialog(activity, paths, config.shouldShowHidden) PropertiesDialog(activity, paths, config.shouldShowHidden)
} }
} }
@ -255,17 +253,19 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun shareMedia() { private fun shareMedia() {
if (selectedPositions.size == 1 && selectedPositions.first() != -1) { if (selectedPositions.size == 1 && selectedPositions.first() != -1) {
activity.shareMedium(getSelectedMedia()[0]) activity.shareMediumPath(getSelectedMedia().first().path)
} else if (selectedPositions.size > 1) { } else if (selectedPositions.size > 1) {
activity.shareMedia(getSelectedMedia()) activity.shareMediaPaths(getSelectedPaths())
} }
} }
private fun copyMoveTo(isCopyOperation: Boolean) { private fun copyMoveTo(isCopyOperation: Boolean) {
val paths = ArrayList<String>() val paths = getSelectedPaths()
selectedPositions.forEach { paths.add(media[it].path) }
val fileDirItems = paths.map {
FileDirItem(it, it.getFilenameFromPath())
} as ArrayList
val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList
activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) { activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) {
config.tempFolderPath = "" config.tempFolderPath = ""
activity.applicationContext.rescanFolderMedia(it) activity.applicationContext.rescanFolderMedia(it)
@ -293,7 +293,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
} }
} }
private fun getCurrentPath() = media[selectedPositions.first()].path private fun getCurrentPath() = (thumbnailItems[selectedPositions.first()] as ThumbnailMedium).path
private fun deleteFiles() { private fun deleteFiles() {
if (selectedPositions.isEmpty()) { if (selectedPositions.isEmpty()) {
@ -301,33 +301,39 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
} }
val fileDirItems = ArrayList<FileDirItem>(selectedPositions.size) val fileDirItems = ArrayList<FileDirItem>(selectedPositions.size)
val removeMedia = ArrayList<Medium>(selectedPositions.size) val removeMedia = ArrayList<ThumbnailMedium>(selectedPositions.size)
if (media.size <= selectedPositions.first()) { if (thumbnailItems.size <= selectedPositions.first()) {
finishActMode() finishActMode()
return return
} }
val SAFPath = media[selectedPositions.first()].path val SAFPath = (thumbnailItems[selectedPositions.first()] as ThumbnailMedium).path
activity.handleSAFDialog(SAFPath) { activity.handleSAFDialog(SAFPath) {
selectedPositions.sortedDescending().forEach { selectedPositions.sortedDescending().forEach {
val medium = media[it] val thumbnailItem = thumbnailItems[it]
fileDirItems.add(FileDirItem(medium.path, medium.name)) if (thumbnailItem is ThumbnailMedium) {
removeMedia.add(medium) fileDirItems.add(FileDirItem(thumbnailItem.path, thumbnailItem.name))
removeMedia.add(thumbnailItem)
}
} }
media.removeAll(removeMedia) thumbnailItems.removeAll(removeMedia)
listener?.tryDeleteFiles(fileDirItems) listener?.tryDeleteFiles(fileDirItems)
removeSelectedItems() removeSelectedItems()
} }
} }
private fun getSelectedMedia(): List<Medium> { private fun getSelectedMedia(): List<ThumbnailMedium> {
val selectedMedia = ArrayList<Medium>(selectedPositions.size) val selectedMedia = ArrayList<ThumbnailMedium>(selectedPositions.size)
selectedPositions.forEach { selectedMedia.add(media[it]) } selectedPositions.forEach {
selectedMedia.add(thumbnailItems[it] as ThumbnailMedium)
}
return selectedMedia return selectedMedia
} }
private fun getSelectedPaths() = getSelectedMedia().map { it.path } as ArrayList<String>
fun updateMedia(newMedia: ArrayList<Medium>) { fun updateMedia(newMedia: ArrayList<Medium>) {
if (newMedia.hashCode() != currentMediaHash || currentGrouping != config.getFolderGrouping(path)) { if (newMedia.hashCode() != currentMediaHash || currentGrouping != config.getFolderGrouping(path)) {
currentMediaHash = newMedia.hashCode() currentMediaHash = newMedia.hashCode()
@ -432,6 +438,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
return activity.getString(stringId) return activity.getString(stringId)
} }
fun getItemBubbleText(position: Int, sorting: Int) = (thumbnailItems[position] as? ThumbnailMedium)?.getBubbleText(sorting)
private fun setupThumbnailMedium(view: View, medium: ThumbnailMedium) { private fun setupThumbnailMedium(view: View, medium: ThumbnailMedium) {
view.apply { view.apply {
play_outline.beVisibleIf(medium.isVideo()) play_outline.beVisibleIf(medium.isVideo())

View file

@ -15,7 +15,6 @@ import com.simplemobiletools.gallery.R
import com.simplemobiletools.gallery.activities.SimpleActivity import com.simplemobiletools.gallery.activities.SimpleActivity
import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog import com.simplemobiletools.gallery.dialogs.PickDirectoryDialog
import com.simplemobiletools.gallery.helpers.NOMEDIA import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.models.Medium
import java.io.File import java.io.File
import java.util.* import java.util.*
@ -27,12 +26,11 @@ fun Activity.sharePaths(paths: ArrayList<String>) {
sharePathsIntent(paths, BuildConfig.APPLICATION_ID) sharePathsIntent(paths, BuildConfig.APPLICATION_ID)
} }
fun Activity.shareMedium(medium: Medium) { fun Activity.shareMediumPath(path: String) {
sharePath(medium.path) sharePath(path)
} }
fun Activity.shareMedia(media: List<Medium>) { fun Activity.shareMediaPaths(paths: ArrayList<String>) {
val paths = media.map { it.path } as ArrayList
sharePaths(paths) sharePaths(paths)
} }

View file

@ -1,8 +1,22 @@
package com.simplemobiletools.gallery.models 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 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, data class ThumbnailMedium(val name: String, val path: String, val parentPath: String, val modified: Long, val taken: Long, val size: Long,
val type: Int, val isFavorite: Boolean) : ThumbnailItem() { val type: Int, var isFavorite: Boolean) : ThumbnailItem() {
fun isVideo() = type == TYPE_VIDEOS 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()
}
} }