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() {
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_hide -> toggleFileVisibility(true)
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_rename -> renameFile()
R.id.menu_edit -> openEditor(getCurrentPath())
@ -769,7 +769,7 @@ class ViewPagerActivity : SimpleActivity(), ViewPager.OnPageChangeListener, View
}
bottom_share.setOnClickListener {
shareMedium(getCurrentMedium()!!)
shareMediumPath(getCurrentPath())
}
bottom_delete.setOnClickListener {

View File

@ -166,7 +166,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun checkHideBtnVisibility(menu: Menu) {
var hiddenCnt = 0
var unhiddenCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach {
getSelectedMedia().forEach {
if (it.name.startsWith('.')) {
hiddenCnt++
} else {
@ -181,7 +181,7 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun checkFavoriteBtnVisibility(menu: Menu) {
var favoriteCnt = 0
var nonFavoriteCnt = 0
selectedPositions.mapNotNull { media.getOrNull(it) }.forEach {
getSelectedMedia().forEach {
if (it.isFavorite) {
favoriteCnt++
} else {
@ -194,16 +194,14 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
}
private fun confirmSelection() {
val paths = getSelectedMedia().map { it.path } as ArrayList<String>
listener?.selectedPaths(paths)
listener?.selectedPaths(getSelectedPaths())
}
private fun showProperties() {
if (selectedPositions.size <= 1) {
PropertiesDialog(activity, media[selectedPositions.first()].path, config.shouldShowHidden)
PropertiesDialog(activity, (thumbnailItems[selectedPositions.first()] as ThumbnailMedium).path, config.shouldShowHidden)
} else {
val paths = ArrayList<String>()
selectedPositions.forEach { paths.add(media[it].path) }
val paths = getSelectedPaths()
PropertiesDialog(activity, paths, config.shouldShowHidden)
}
}
@ -255,17 +253,19 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
private fun shareMedia() {
if (selectedPositions.size == 1 && selectedPositions.first() != -1) {
activity.shareMedium(getSelectedMedia()[0])
activity.shareMediumPath(getSelectedMedia().first().path)
} else if (selectedPositions.size > 1) {
activity.shareMedia(getSelectedMedia())
activity.shareMediaPaths(getSelectedPaths())
}
}
private fun copyMoveTo(isCopyOperation: Boolean) {
val paths = ArrayList<String>()
selectedPositions.forEach { paths.add(media[it].path) }
val paths = getSelectedPaths()
val fileDirItems = paths.map {
FileDirItem(it, it.getFilenameFromPath())
} as ArrayList
val fileDirItems = paths.map { FileDirItem(it, it.getFilenameFromPath()) } as ArrayList
activity.tryCopyMoveFilesTo(fileDirItems, isCopyOperation) {
config.tempFolderPath = ""
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() {
if (selectedPositions.isEmpty()) {
@ -301,33 +301,39 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
}
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()
return
}
val SAFPath = media[selectedPositions.first()].path
val SAFPath = (thumbnailItems[selectedPositions.first()] as ThumbnailMedium).path
activity.handleSAFDialog(SAFPath) {
selectedPositions.sortedDescending().forEach {
val medium = media[it]
fileDirItems.add(FileDirItem(medium.path, medium.name))
removeMedia.add(medium)
val thumbnailItem = thumbnailItems[it]
if (thumbnailItem is ThumbnailMedium) {
fileDirItems.add(FileDirItem(thumbnailItem.path, thumbnailItem.name))
removeMedia.add(thumbnailItem)
}
}
media.removeAll(removeMedia)
thumbnailItems.removeAll(removeMedia)
listener?.tryDeleteFiles(fileDirItems)
removeSelectedItems()
}
}
private fun getSelectedMedia(): List<Medium> {
val selectedMedia = ArrayList<Medium>(selectedPositions.size)
selectedPositions.forEach { selectedMedia.add(media[it]) }
private fun getSelectedMedia(): List<ThumbnailMedium> {
val selectedMedia = ArrayList<ThumbnailMedium>(selectedPositions.size)
selectedPositions.forEach {
selectedMedia.add(thumbnailItems[it] as ThumbnailMedium)
}
return selectedMedia
}
private fun getSelectedPaths() = getSelectedMedia().map { it.path } as ArrayList<String>
fun updateMedia(newMedia: ArrayList<Medium>) {
if (newMedia.hashCode() != currentMediaHash || currentGrouping != config.getFolderGrouping(path)) {
currentMediaHash = newMedia.hashCode()
@ -432,6 +438,8 @@ class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList<Medium>,
return activity.getString(stringId)
}
fun getItemBubbleText(position: Int, sorting: Int) = (thumbnailItems[position] as? ThumbnailMedium)?.getBubbleText(sorting)
private fun setupThumbnailMedium(view: View, medium: ThumbnailMedium) {
view.apply {
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.dialogs.PickDirectoryDialog
import com.simplemobiletools.gallery.helpers.NOMEDIA
import com.simplemobiletools.gallery.models.Medium
import java.io.File
import java.util.*
@ -27,12 +26,11 @@ fun Activity.sharePaths(paths: ArrayList<String>) {
sharePathsIntent(paths, BuildConfig.APPLICATION_ID)
}
fun Activity.shareMedium(medium: Medium) {
sharePath(medium.path)
fun Activity.shareMediumPath(path: String) {
sharePath(path)
}
fun Activity.shareMedia(media: List<Medium>) {
val paths = media.map { it.path } as ArrayList
fun Activity.shareMediaPaths(paths: ArrayList<String>) {
sharePaths(paths)
}

View File

@ -1,8 +1,22 @@
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, val isFavorite: Boolean) : ThumbnailItem() {
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()
}
}