Skip to content

Commit

Permalink
Merge pull request #602 from Automattic/issue/583-long-press-delete-s…
Browse files Browse the repository at this point in the history
…lide

Show delete button when long pressing a slide
  • Loading branch information
mzorz authored Nov 5, 2020
2 parents 8093946 + cf3b754 commit df51ba7
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1900,6 +1900,23 @@ 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()
}

override fun onStoryFrameMovedLongPressed() {
disableDeleteSlideMode()
}

private fun toggleDeleteSlideMode() {
if (delete_slide_view.visibility == View.VISIBLE) {
disableDeleteSlideMode()
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,15 +80,21 @@ class StoryFrameSelectorAdapter : RecyclerView.Adapter<StoryFrameSelectorAdapter

class StoryFrameHolderItem(v: View) : StoryFrameHolder(v) {
private var onFrameSelected: (() -> 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 = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ interface OnStoryFrameSelectorTappedListener {
fun onStoryFrameSelected(oldIndex: Int, newIndex: Int)
fun onStoryFrameAddTapped()
fun onCurrentFrameTapped()
fun onStoryFrameLongPressed(oldIndex: Int, newIndex: Int)
fun onStoryFrameMovedLongPressed()
}

class StoryFrameSelectorFragment : Fragment() {
Expand Down Expand Up @@ -51,6 +53,15 @@ class StoryFrameSelectorFragment : Fragment() {
storyFrameTappedListener?.onCurrentFrameTapped()
})

storyViewModel.onUserLongPressedFrame.observe(this, Observer { selectedFrameIndexChange ->
storyFrameTappedListener?.onStoryFrameLongPressed(
selectedFrameIndexChange.first, selectedFrameIndexChange.second)
})

storyViewModel.onUserMovedLongPressedFrame.observe(this, Observer {
storyFrameTappedListener?.onStoryFrameMovedLongPressed()
})

storyViewModel.onFrameIndexMoved.observe(this, Observer<Pair<Int, Int>> { positionFrameIndexChange ->
updateContentUiStateMovedIndex(positionFrameIndexChange.first, positionFrameIndexChange.second)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,12 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St
private val _onUserTappedCurrentFrame = SingleLiveEvent<Unit>()
val onUserTappedCurrentFrame = _onUserTappedCurrentFrame

private val _onUserLongPressedFrame = SingleLiveEvent<Pair<Int, Int>>()
val onUserLongPressedFrame = _onUserLongPressedFrame

private val _onUserMovedLongPressedFrame = SingleLiveEvent<Unit>()
val onUserMovedLongPressedFrame = _onUserMovedLongPressedFrame

fun createNewStory() {
loadStory(StoryRepository.DEFAULT_NONE_SELECTED)
}
Expand Down Expand Up @@ -104,6 +110,16 @@ class StoryViewModel(private val repository: StoryRepository, val storyIndex: St
}
}

private fun setLongPressedFrame(index: Int) {
val oldIndex = currentSelectedFrameIndex
setSelectedFrame(index)
_onUserLongPressedFrame.value = Pair(oldIndex, index)
}

private fun setMovedLongPressedFrame() {
_onUserMovedLongPressedFrame.call()
}

fun updateCurrentSelectedFrameOnRetryResult(frameSaveResult: FrameSaveResult) {
repository.updateCurrentStorySaveResultOnFrame(
currentSelectedFrameIndex,
Expand Down Expand Up @@ -212,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
Expand Down Expand Up @@ -291,6 +308,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
Expand All @@ -300,6 +320,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,
Expand Down

0 comments on commit df51ba7

Please sign in to comment.