From a1ad3391c5313df6118fc368fb3766c94507e327 Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Tue, 3 Nov 2020 19:21:23 +0900 Subject: [PATCH 1/2] Show delete button when long-pressing a slide --- .../stories/compose/ComposeLoopFrameActivity.kt | 13 +++++++++++++ .../compose/story/StoryFrameSelectorAdapter.kt | 6 ++++++ .../compose/story/StoryFrameSelectorFragment.kt | 6 ++++++ .../stories/compose/story/StoryViewModel.kt | 13 +++++++++++++ 4 files changed, 38 insertions(+) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index 25098796a..de9ce696e 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1900,6 +1900,19 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec } override fun onCurrentFrameTapped() { + toggleDeleteSlideMode() + } + + override fun onStoryFrameLongPressed(oldIndex: Int, newIndex: Int) { + // On long press we want to switch to that frame and show the delete slide move all together. + if (oldIndex != newIndex) { + // The long-pressed frame was not the one already in focus - switch to it first. + onStoryFrameSelected(oldIndex, newIndex) + } + toggleDeleteSlideMode() + } + + private fun toggleDeleteSlideMode() { if (delete_slide_view.visibility == View.VISIBLE) { disableDeleteSlideMode() } else { diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorAdapter.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorAdapter.kt index a647d3062..0076a81a2 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorAdapter.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorAdapter.kt @@ -80,15 +80,21 @@ class StoryFrameSelectorAdapter : RecyclerView.Adapter Unit)? = null + private var onFrameLongPressed: (() -> Unit)? = null init { clickableView.setOnClickListener { onFrameSelected?.invoke() } + clickableView.setOnLongClickListener { + onFrameLongPressed?.invoke() + true + } } override fun onBind(uiState: StoryFrameListItemUiState) { onFrameSelected = requireNotNull(uiState.onItemTapped) { "OnItemTapped is required." } + onFrameLongPressed = requireNotNull(uiState.onItemLongPressed) { "OnItemLongPressed is required." } uiState as StoryFrameListItemUiStateFrame val loadThumbnailImage = { diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt index c31289e95..863ef8611 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt @@ -24,6 +24,7 @@ interface OnStoryFrameSelectorTappedListener { fun onStoryFrameSelected(oldIndex: Int, newIndex: Int) fun onStoryFrameAddTapped() fun onCurrentFrameTapped() + fun onStoryFrameLongPressed(oldIndex: Int, newIndex: Int) } class StoryFrameSelectorFragment : Fragment() { @@ -51,6 +52,11 @@ class StoryFrameSelectorFragment : Fragment() { storyFrameTappedListener?.onCurrentFrameTapped() }) + storyViewModel.onUserLongPressedFrame.observe(this, Observer { selectedFrameIndexChange -> + storyFrameTappedListener?.onStoryFrameLongPressed( + selectedFrameIndexChange.first, selectedFrameIndexChange.second) + }) + storyViewModel.onFrameIndexMoved.observe(this, Observer> { positionFrameIndexChange -> updateContentUiStateMovedIndex(positionFrameIndexChange.first, positionFrameIndexChange.second) }) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt index 9d9324fd6..194c095dc 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt @@ -47,6 +47,9 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St private val _onUserTappedCurrentFrame = SingleLiveEvent() val onUserTappedCurrentFrame = _onUserTappedCurrentFrame + private val _onUserLongPressedFrame = SingleLiveEvent>() + val onUserLongPressedFrame = _onUserLongPressedFrame + fun createNewStory() { loadStory(StoryRepository.DEFAULT_NONE_SELECTED) } @@ -104,6 +107,12 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St } } + private fun setLongPressedFrame(index: Int) { + val oldIndex = currentSelectedFrameIndex + setSelectedFrame(index) + _onUserLongPressedFrame.value = Pair(oldIndex, index) + } + fun updateCurrentSelectedFrameOnRetryResult(frameSaveResult: FrameSaveResult) { repository.updateCurrentStorySaveResultOnFrame( currentSelectedFrameIndex, @@ -291,6 +300,9 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St oneFrameUiState.onItemTapped = { setSelectedFrameByUser(index, userInitiated = true) } + oneFrameUiState.onItemLongPressed = { + setLongPressedFrame(index) + } uiStateItems.add(oneFrameUiState) } return newUiState @@ -300,6 +312,7 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St sealed class StoryFrameListItemUiState() { var onItemTapped: (() -> Unit)? = null + var onItemLongPressed: (() -> Unit)? = null data class StoryFrameListItemUiStateFrame( var selected: Boolean = false, From cf3b754895d71164ec6cf7060141fa622e792b4f Mon Sep 17 00:00:00 2001 From: Alex Forcier Date: Thu, 5 Nov 2020 09:41:08 +0900 Subject: [PATCH 2/2] Hide delete button after swapping slide positions --- .../wordpress/stories/compose/ComposeLoopFrameActivity.kt | 4 ++++ .../stories/compose/story/StoryFrameSelectorFragment.kt | 5 +++++ .../com/wordpress/stories/compose/story/StoryViewModel.kt | 8 ++++++++ 3 files changed, 17 insertions(+) diff --git a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt index de9ce696e..b3a919bf4 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/ComposeLoopFrameActivity.kt @@ -1912,6 +1912,10 @@ abstract class ComposeLoopFrameActivity : AppCompatActivity(), OnStoryFrameSelec toggleDeleteSlideMode() } + override fun onStoryFrameMovedLongPressed() { + disableDeleteSlideMode() + } + private fun toggleDeleteSlideMode() { if (delete_slide_view.visibility == View.VISIBLE) { disableDeleteSlideMode() diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt index 863ef8611..22d8d2210 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryFrameSelectorFragment.kt @@ -25,6 +25,7 @@ interface OnStoryFrameSelectorTappedListener { fun onStoryFrameAddTapped() fun onCurrentFrameTapped() fun onStoryFrameLongPressed(oldIndex: Int, newIndex: Int) + fun onStoryFrameMovedLongPressed() } class StoryFrameSelectorFragment : Fragment() { @@ -57,6 +58,10 @@ class StoryFrameSelectorFragment : Fragment() { selectedFrameIndexChange.first, selectedFrameIndexChange.second) }) + storyViewModel.onUserMovedLongPressedFrame.observe(this, Observer { + storyFrameTappedListener?.onStoryFrameMovedLongPressed() + }) + storyViewModel.onFrameIndexMoved.observe(this, Observer> { positionFrameIndexChange -> updateContentUiStateMovedIndex(positionFrameIndexChange.first, positionFrameIndexChange.second) }) diff --git a/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt b/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt index 194c095dc..90c08774a 100644 --- a/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt +++ b/stories/src/main/java/com/wordpress/stories/compose/story/StoryViewModel.kt @@ -50,6 +50,9 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St private val _onUserLongPressedFrame = SingleLiveEvent>() val onUserLongPressedFrame = _onUserLongPressedFrame + private val _onUserMovedLongPressedFrame = SingleLiveEvent() + val onUserMovedLongPressedFrame = _onUserMovedLongPressedFrame + fun createNewStory() { loadStory(StoryRepository.DEFAULT_NONE_SELECTED) } @@ -113,6 +116,10 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St _onUserLongPressedFrame.value = Pair(oldIndex, index) } + private fun setMovedLongPressedFrame() { + _onUserMovedLongPressedFrame.call() + } + fun updateCurrentSelectedFrameOnRetryResult(frameSaveResult: FrameSaveResult) { repository.updateCurrentStorySaveResultOnFrame( currentSelectedFrameIndex, @@ -221,6 +228,7 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St } fun swapItemsInPositions(pos1: Int, pos2: Int) { + setMovedLongPressedFrame() repository.swapItemsInPositions(pos1, pos2) // adjust currentSelectedFrameIndex so it reflects the movement only // if the movement occurred entierly to the left of the selection, don't update it