From 6f78a6e9ce898d12baa880a574ce3bca6c4977fb Mon Sep 17 00:00:00 2001 From: tibbi Date: Thu, 16 Nov 2017 14:44:39 +0100 Subject: [PATCH] rewriting MediaAdapter to the Commons MyRecyclerViewAdapter --- app/build.gradle | 2 +- .../gallery/activities/MainActivity.kt | 7 +- .../gallery/activities/MediaActivity.kt | 42 +-- .../gallery/adapters/DirectoryAdapter.kt | 12 +- .../gallery/adapters/MediaAdapter.kt | 318 +++++------------- .../gallery/dialogs/PickMediumDialog.kt | 4 +- 6 files changed, 124 insertions(+), 261 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index b076fd8a1..80fe23f3e 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -47,7 +47,7 @@ ext { } dependencies { - compile 'com.simplemobiletools:commons:2.39.7' + compile 'com.simplemobiletools:commons:2.39.8' compile 'com.davemorrissey.labs:subsampling-scale-image-view:3.7.2' compile 'com.theartofdev.edmodo:android-image-cropper:2.4.0' compile 'com.android.support:multidex:1.0.2' diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt index e2884e12e..25806c41e 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MainActivity.kt @@ -340,8 +340,11 @@ class MainActivity : SimpleActivity(), DirectoryAdapter.DirOperationsListener { } else { setupListLayoutManager() } - getDirectoryAdapter()?.setupZoomListener(mZoomListener) - getDirectoryAdapter()?.setupDragListener(true) + + getDirectoryAdapter()?.apply { + setupZoomListener(mZoomListener) + setupDragListener(true) + } } private fun setupGridLayoutManager() { diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt index 076541e2b..d4fdd4955 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/activities/MediaActivity.kt @@ -23,6 +23,7 @@ import com.simplemobiletools.commons.extensions.* import com.simplemobiletools.commons.helpers.PERMISSION_WRITE_STORAGE import com.simplemobiletools.commons.helpers.REQUEST_EDIT_IMAGE import com.simplemobiletools.commons.models.RadioItem +import com.simplemobiletools.commons.views.MyRecyclerView import com.simplemobiletools.gallery.R import com.simplemobiletools.gallery.adapters.MediaAdapter import com.simplemobiletools.gallery.asynctasks.GetMediaAsynctask @@ -57,6 +58,7 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private var mLatestMediaId = 0L private var mLastMediaHandler = Handler() private var mCurrAsyncTask: GetMediaAsynctask? = null + private var mZoomListener: MyRecyclerView.MyZoomListener? = null companion object { var mMedia = ArrayList() @@ -174,8 +176,8 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { val currAdapter = media_grid.adapter if (currAdapter == null) { - media_grid.adapter = MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple) { - itemClicked(it.path) + media_grid.adapter = MediaAdapter(this, mMedia, this, mIsGetImageIntent || mIsGetVideoIntent || mIsGetAnyIntent, mAllowPickingMultiple, media_grid) { + itemClicked((it as Medium).path) } } else { (currAdapter as MediaAdapter).updateMedia(mMedia) @@ -395,10 +397,16 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { private fun getRecyclerAdapter() = (media_grid.adapter as MediaAdapter) private fun setupLayoutManager() { - if (config.viewTypeFiles == VIEW_TYPE_GRID) + if (config.viewTypeFiles == VIEW_TYPE_GRID) { setupGridLayoutManager() - else + } else { setupListLayoutManager() + } + + getMediaAdapter()?.apply { + setupZoomListener(mZoomListener) + setupDragListener(true) + } } private fun setupGridLayoutManager() { @@ -411,42 +419,30 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) } - /*media_grid.isDragSelectionEnabled = true - media_grid.isZoomingEnabled = true layoutManager.spanCount = config.mediaColumnCnt - media_grid.listener = object : MyScalableRecyclerView.MyScalableRecyclerViewListener { + mZoomListener = object : MyRecyclerView.MyZoomListener { override fun zoomIn() { if (layoutManager.spanCount > 1) { reduceColumnCount() - getRecyclerAdapter().actMode?.finish() + getRecyclerAdapter().finishActMode() } } override fun zoomOut() { if (layoutManager.spanCount < MAX_COLUMN_COUNT) { increaseColumnCount() - getRecyclerAdapter().actMode?.finish() + getRecyclerAdapter().finishActMode() } } - - override fun selectItem(position: Int) { - getRecyclerAdapter().selectItem(position) - } - - override fun selectRange(initialSelection: Int, lastDraggedIndex: Int, minReached: Int, maxReached: Int) { - getRecyclerAdapter().selectRange(initialSelection, lastDraggedIndex, minReached, maxReached) - } - }*/ + } } private fun setupListLayoutManager() { - //media_grid.isDragSelectionEnabled = true - //media_grid.isZoomingEnabled = false - val layoutManager = media_grid.layoutManager as GridLayoutManager layoutManager.spanCount = 1 layoutManager.orientation = GridLayoutManager.VERTICAL media_refresh_layout.layoutParams = FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + mZoomListener = null } private fun increaseColumnCount() { @@ -572,10 +568,6 @@ class MediaActivity : SimpleActivity(), MediaAdapter.MediaOperationsListener { }, 1000) } - override fun itemLongClicked(position: Int) { - media_grid.setDragSelectActive(position) - } - override fun selectedPaths(paths: ArrayList) { Intent().apply { putExtra(PICKED_PATHS, paths) diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt index 8042ff84f..6e1ffed01 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/adapters/DirectoryAdapter.kt @@ -58,7 +58,7 @@ class DirectoryAdapter(activity: BaseSimpleActivity, var dirs: MutableList, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, - val allowMultiplePicks: Boolean, val itemClick: (Medium) -> Unit) : RecyclerView.Adapter() { +class MediaAdapter(activity: BaseSimpleActivity, var media: MutableList, val listener: MediaOperationsListener?, val isAGetIntent: Boolean, + val allowMultiplePicks: Boolean, recyclerView: MyRecyclerView, itemClick: (Any) -> Unit) : MyRecyclerViewAdapter(activity, recyclerView, itemClick) { private val config = activity.config - var actMode: ActionMode? = null - var primaryColor = config.primaryColor - - private val multiSelector = MultiSelector() private val isListViewType = config.viewTypeFiles == VIEW_TYPE_LIST private var skipConfirmationDialog = false - private var itemViews = SparseArray() - private val selectedPositions = HashSet() private var scrollHorizontally = config.scrollHorizontally private var animateGifs = config.animateGifs private var cropThumbnails = config.cropThumbnails - private var textColor = config.textColor private var displayFilenames = config.displayFileNames - fun toggleItemSelection(select: Boolean, pos: Int) { - if (select) { - if (itemViews[pos] != null) { - itemViews[pos].medium_check?.background?.applyColorFilter(primaryColor) - selectedPositions.add(pos) - } - } else { - selectedPositions.remove(pos) - } - - itemViews[pos]?.medium_check?.beVisibleIf(select) - - if (selectedPositions.isEmpty()) { - actMode?.finish() - return - } - - updateTitle(selectedPositions.size) + init { + selectableItemCount = media.count() } - private fun updateTitle(cnt: Int) { - actMode?.title = "$cnt / ${media.size}" - actMode?.invalidate() + override fun getActionMenuId() = R.menu.cab_media + + override fun prepareItemSelection(view: View) { + view.medium_check?.background?.applyColorFilter(primaryColor) } - private val adapterListener = object : MyAdapterListener { - override fun toggleItemSelectionAdapter(select: Boolean, position: Int) { - toggleItemSelection(select, position) - } - - override fun getSelectedPositions(): HashSet = selectedPositions - - override fun itemLongClicked(position: Int) {} + override fun markItemSelection(select: Boolean, view: View?) { + view?.medium_check?.beVisibleIf(select) } - private val multiSelectorMode = object : ModalMultiSelectorCallback(multiSelector) { - override fun onActionItemClicked(mode: ActionMode, item: MenuItem): Boolean { - when (item.itemId) { - R.id.cab_confirm_selection -> confirmSelection() - R.id.cab_properties -> showProperties() - R.id.cab_rename -> renameFile() - R.id.cab_edit -> editFile() - R.id.cab_hide -> toggleFileVisibility(true) - R.id.cab_unhide -> toggleFileVisibility(false) - R.id.cab_share -> shareMedia() - R.id.cab_copy_to -> copyMoveTo(true) - R.id.cab_move_to -> copyMoveTo(false) - R.id.cab_select_all -> selectAll() - R.id.cab_open_with -> activity.openFile(Uri.fromFile(getCurrentFile()), true) - R.id.cab_set_as -> activity.setAs(Uri.fromFile(getCurrentFile())) - R.id.cab_delete -> checkDeleteConfirmation() - else -> return false + override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder { + val layoutType = if (isListViewType) R.layout.photo_video_item_list else R.layout.photo_video_item_grid + val view = activity.layoutInflater.inflate(layoutType, parent, false) + return createViewHolder(view) + } + + override fun onBindViewHolder(holder: MyRecyclerViewAdapter.ViewHolder, position: Int) { + val medium = media[position] + val view = holder.bindView(medium, !allowMultiplePicks) { + setupView(it, medium) + } + itemViews.put(position, view) + toggleItemSelection(selectedPositions.contains(position), position) + holder.itemView.tag = holder + } + + override fun getItemCount() = media.size + + override fun actionItemPressed(id: Int) { + when (id) { + R.id.cab_confirm_selection -> confirmSelection() + R.id.cab_properties -> showProperties() + R.id.cab_rename -> renameFile() + R.id.cab_edit -> editFile() + R.id.cab_hide -> toggleFileVisibility(true) + R.id.cab_unhide -> toggleFileVisibility(false) + R.id.cab_share -> shareMedia() + R.id.cab_copy_to -> copyMoveTo(true) + R.id.cab_move_to -> copyMoveTo(false) + R.id.cab_select_all -> selectAll() + R.id.cab_open_with -> activity.openFile(Uri.fromFile(getCurrentFile()), true) + R.id.cab_set_as -> activity.setAs(Uri.fromFile(getCurrentFile())) + R.id.cab_delete -> checkDeleteConfirmation() + } + } + + override fun prepareActionMode(menu: Menu) { + menu.apply { + findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1 + findItem(R.id.cab_open_with).isVisible = selectedPositions.size == 1 + findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedPositions.size > 0 + + checkHideBtnVisibility(this) + } + } + + private fun checkHideBtnVisibility(menu: Menu) { + var hiddenCnt = 0 + var unhiddenCnt = 0 + selectedPositions.mapNotNull { media.getOrNull(it) }.forEach { + if (it.name.startsWith('.')) { + hiddenCnt++ + } else { + unhiddenCnt++ } - return true } - override fun onCreateActionMode(actionMode: ActionMode?, menu: Menu?): Boolean { - super.onCreateActionMode(actionMode, menu) - actMode = actionMode - activity.menuInflater.inflate(R.menu.cab_media, menu) - return true - } - - override fun onPrepareActionMode(actionMode: ActionMode?, menu: Menu): Boolean { - menu.findItem(R.id.cab_rename).isVisible = selectedPositions.size == 1 - menu.findItem(R.id.cab_open_with).isVisible = selectedPositions.size == 1 - menu.findItem(R.id.cab_confirm_selection).isVisible = isAGetIntent && allowMultiplePicks && selectedPositions.size > 0 - - checkHideBtnVisibility(menu) - - return true - } - - override fun onDestroyActionMode(actionMode: ActionMode?) { - super.onDestroyActionMode(actionMode) - selectedPositions.forEach { - itemViews[it]?.medium_check?.beGone() - } - selectedPositions.clear() - actMode = null - } - - fun checkHideBtnVisibility(menu: Menu) { - var hiddenCnt = 0 - var unhiddenCnt = 0 - selectedPositions.mapNotNull { media.getOrNull(it) }.forEach { - if (it.name.startsWith('.')) { - hiddenCnt++ - } else { - unhiddenCnt++ - } - } - - menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0 - menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 - } + menu.findItem(R.id.cab_hide).isVisible = unhiddenCnt > 0 + menu.findItem(R.id.cab_unhide).isVisible = hiddenCnt > 0 } private fun confirmSelection() { @@ -163,14 +130,14 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList(selectedPositions.size) if (media.size <= selectedPositions.first()) { - actMode?.finish() + finishActMode() return } @@ -269,7 +226,8 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList newItems.put(curIndex, itemViews[i]) } itemViews = newItems - actMode?.finish() + selectableItemCount = media.size + finishActMode() } } @@ -279,29 +237,18 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList) { media = newMedia + selectableItemCount = media.size notifyDataSetChanged() - actMode?.finish() + finishActMode() } fun updateDisplayFilenames(displayFilenames: Boolean) { @@ -324,96 +271,17 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList -1 && min < to) { - (min until to).filter { it != from } - .forEach { toggleItemSelection(false, it) } + if (isListViewType) { + photo_name.setTextColor(textColor) + play_outline.setColorFilter(textColor, PorterDuff.Mode.SRC_IN) } - if (max > -1) { - for (i in from + 1..max) - toggleItemSelection(false, i) - } - } else { - for (i in from..to) - toggleItemSelection(true, i) - - if (max > -1 && max > to) { - (to + 1..max).filter { it != from } - .forEach { toggleItemSelection(false, it) } - } - - if (min > -1) { - for (i in min until from) - toggleItemSelection(false, i) - } - } - } - - class ViewHolder(val view: View, val adapterListener: MyAdapterListener, val activity: BaseSimpleActivity, val multiSelectorCallback: ModalMultiSelectorCallback, - val multiSelector: MultiSelector, val listener: MediaOperationsListener?, val allowMultiplePicks: Boolean, - val itemClick: (Medium) -> (Unit)) : - SwappingHolder(view, MultiSelector()) { - fun bindView(medium: Medium, displayFilenames: Boolean, scrollHorizontally: Boolean, isListViewType: Boolean, textColor: Int, - animateGifs: Boolean, cropThumbnails: Boolean): View { - itemView.apply { - play_outline.beVisibleIf(medium.video) - photo_name.beVisibleIf(displayFilenames || isListViewType) - photo_name.text = medium.name - activity.loadImage(medium.path, medium_thumbnail, scrollHorizontally, animateGifs, cropThumbnails) - - if (isListViewType) { - photo_name.setTextColor(textColor) - play_outline.setColorFilter(textColor, PorterDuff.Mode.SRC_IN) - } - - setOnClickListener { viewClicked(medium) } - setOnLongClickListener { if (allowMultiplePicks) viewLongClicked() else viewClicked(medium); true } - } - return itemView - } - - private fun viewClicked(medium: Medium) { - if (multiSelector.isSelectable) { - val isSelected = adapterListener.getSelectedPositions().contains(adapterPosition) - adapterListener.toggleItemSelectionAdapter(!isSelected, adapterPosition) - } else { - itemClick(medium) - } - } - - private fun viewLongClicked() { - if (listener != null) { - if (!multiSelector.isSelectable) { - activity.startSupportActionMode(multiSelectorCallback) - adapterListener.toggleItemSelectionAdapter(true, adapterPosition) - } - - listener.itemLongClicked(adapterPosition) - } - } - - fun stopLoad() { - if (!activity.isActivityDestroyed()) - Glide.with(activity).clear(view.medium_thumbnail) } } @@ -422,8 +290,6 @@ class MediaAdapter(val activity: BaseSimpleActivity, var media: MutableList) - fun itemLongClicked(position: Int) - fun selectedPaths(paths: ArrayList) } } diff --git a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt index 1903462c4..ad579e02c 100644 --- a/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt +++ b/app/src/main/kotlin/com/simplemobiletools/gallery/dialogs/PickMediumDialog.kt @@ -58,8 +58,8 @@ class PickMediumDialog(val activity: BaseSimpleActivity, val path: String, val c return shownMedia = media - val adapter = MediaAdapter(activity, media, null, true, false) { - callback(it.path) + val adapter = MediaAdapter(activity, media, null, true, false, view.media_grid) { + callback((it as Medium).path) dialog.dismiss() }