Skip to content

Commit

Permalink
Merge pull request #1318 from wakmusic/implement-swipe-delete
Browse files Browse the repository at this point in the history
πŸ”€ :: (#1310) μž¬μƒλͺ©λ‘, λ‚˜μ˜ ν”Œλ¦¬ swipe delete κ΅¬ν˜„
  • Loading branch information
yongbeomkwak authored Oct 25, 2024
2 parents 4008c69 + 3d436c3 commit 32265c4
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ final class MyPlaylistDetailReactor: Reactor {
case changeImageData(PlaylistImageKind)
case shareButtonDidTap
case moreButtonDidTap
case didTapSwippedRemoveButton(IndexPath)
}

enum Mutation {
Expand Down Expand Up @@ -151,14 +152,20 @@ final class MyPlaylistDetailReactor: Reactor {
return deselectAll()

case .removeSongs:
return removeSongs()
return removeSongs(
targets: currentState.playlistModels.filter { $0.isSelected }
)

case let .changeImageData(imageData):
return updateImageData(imageData: imageData)
case .shareButtonDidTap:
return updateShareLink()
case .moreButtonDidTap:
return updateShowEditSheet(flag: !self.currentState.showEditSheet)

case let .didTapSwippedRemoveButton(index):
let target = currentState.playlistModels[index.row]
return removeSongs(targets: [target])
}
}

Expand Down Expand Up @@ -462,12 +469,9 @@ private extension MyPlaylistDetailReactor {
])
}

func removeSongs() -> Observable<Mutation> {
let state = currentState
let playlists = state.playlistModels

let remainSongs = playlists.filter { !$0.isSelected }
let removeSongs = playlists.filter { $0.isSelected }.map { $0.id }
func removeSongs(targets: [PlaylistItemModel]) -> Observable<Mutation> {
let remainSongs = currentState.playlistModels.filter { !targets.contains($0) }
let removeSongs = targets.map { $0.id }
var prevHeader = currentState.header
prevHeader.updateSongCount(remainSongs.count)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,23 @@ extension MyPlaylistDetailViewController: UITableViewDelegate, UITableViewDragDe
return false // νŽΈμ§‘λͺ¨λ“œ μ‹œ μ…€μ˜ λ“€μ—¬μ“°κΈ°λ₯Ό μ—†μ• λ €λ©΄ falseλ₯Ό λ¦¬ν„΄ν•©λ‹ˆλ‹€.
}

public func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "μ‚­μ œ") { [weak self] _, _, _ in
guard let self else { return }
self.reactor?.action.onNext(.didTapSwippedRemoveButton(indexPath))
}

deleteAction.backgroundColor = DesignSystemAsset.PrimaryColorV2.increase.color

let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false

return configuration
}

public func tableView(
_ tableView: UITableView,
itemsForBeginning session: any UIDragSession,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,23 @@ extension PlaylistViewController: UITableViewDelegate, UITableViewDragDelegate {
return true // λͺ¨λ“  Cell 을 이동 κ°€λŠ₯ν•˜κ²Œ μ„€μ •ν•©λ‹ˆλ‹€.
}

public func tableView(
_ tableView: UITableView,
trailingSwipeActionsConfigurationForRowAt indexPath: IndexPath
) -> UISwipeActionsConfiguration? {
let deleteAction = UIContextualAction(style: .destructive, title: "μ‚­μ œ") { [weak self] _, _, _ in
guard let self else { return }
self.didTapSwippedRemoveSongSubject.onNext(indexPath.row)
}

deleteAction.backgroundColor = DesignSystemAsset.PrimaryColorV2.increase.color

let configuration = UISwipeActionsConfiguration(actions: [deleteAction])
configuration.performsFirstActionWithFullSwipe = false

return configuration
}

public func tableView(
_ tableView: UITableView,
targetIndexPathForMoveFromRowAt sourceIndexPath: IndexPath,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public final class PlaylistViewController: UIViewController, SongCartViewType {
var tappedAddPlaylist = PublishSubject<Void>()
var tappedRemoveSongs = PublishSubject<Void>()
var didLongPressedSongSubject = PublishSubject<Int>()
var didTapSwippedRemoveSongSubject = PublishSubject<Int>()

private(set) var containSongsFactory: any ContainSongsFactory
private(set) var songDetailPresenter: any SongDetailPresentable
Expand All @@ -50,7 +51,8 @@ public final class PlaylistViewController: UIViewController, SongCartViewType {
addPlaylistButtonDidTapEvent: tappedAddPlaylist.asObservable(),
removeSongsButtonDidTapEvent: tappedRemoveSongs.asObservable(),
itemMovedEvent: playlistView.playlistTableView.rx.itemMoved.asObservable(),
didLongPressedSongEvent: didLongPressedSongSubject
didLongPressedSongEvent: didLongPressedSongSubject,
didTapSwippedRemoveButtonEvent: didTapSwippedRemoveSongSubject
)
lazy var output = self.viewModel.transform(from: input)

Expand Down Expand Up @@ -95,7 +97,6 @@ public final class PlaylistViewController: UIViewController, SongCartViewType {
playlistView.playlistTableView.dragDelegate = self
// playlistView.playlistTableView.dragInteractionEnabled = true
bindViewModel()
bindActions()
}

override public func viewDidAppear(_ animated: Bool) {
Expand Down Expand Up @@ -126,8 +127,6 @@ private extension PlaylistViewController {
case .changed:
let distanceY = max(distance.y, 0)
view.frame = CGRect(x: 0, y: distanceY, width: view.frame.width, height: screenHeight)
// let opacity = 1 - (distanceY / screenHeight)
// updateOpacity(value: Float(opacity))

case .ended:
let velocity = gestureRecognizer.velocity(in: self.view)
Expand All @@ -144,7 +143,6 @@ private extension PlaylistViewController {
options: [.curveEaseInOut],
animations: {
self.view.frame = CGRect(x: 0, y: 0, width: self.view.frame.width, height: screenHeight)
self.updateOpacity(value: 1)
}
)
}
Expand All @@ -153,10 +151,6 @@ private extension PlaylistViewController {
break
}
}

func updateOpacity(value: Float) {
playlistView.layer.opacity = value
}
}

private extension PlaylistViewController {
Expand Down Expand Up @@ -291,10 +285,6 @@ private extension PlaylistViewController {
}
}

private extension PlaylistViewController {
private func bindActions() {}
}

extension PlaylistViewController {
private func createDatasources(
output: PlaylistViewModel
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ final class PlaylistViewModel: ViewModelType {
let removeSongsButtonDidTapEvent: Observable<Void>
let itemMovedEvent: Observable<(sourceIndex: IndexPath, destinationIndex: IndexPath)>
let didLongPressedSongEvent: Observable<Int>
let didTapSwippedRemoveButtonEvent: Observable<Int>
}

struct Output {
Expand Down Expand Up @@ -149,6 +150,17 @@ final class PlaylistViewModel: ViewModelType {
output.countOfSongs.send(output.playlists.value.count)
}).disposed(by: disposeBag)

input.didTapSwippedRemoveButtonEvent
.bind(with: self, onNext: { owner, index in
var mutablePlaylist = output.playlists.value
mutablePlaylist.remove(at: index)
output.playlists.accept(mutablePlaylist)
output.countOfSongs.send(mutablePlaylist.count)

owner.playState.remove(indexs: [index])
})
.disposed(by: disposeBag)

input.itemMovedEvent
.map { (sourceIndex: $0.row, destIndex: $1.row) }
.subscribe { (sourceIndex: Int, destIndex: Int) in
Expand Down

0 comments on commit 32265c4

Please sign in to comment.