From 0b862d826fd50e676d8e2d5d97b2684f0f3d9b13 Mon Sep 17 00:00:00 2001 From: baegteun Date: Wed, 9 Oct 2024 20:43:56 +0900 Subject: [PATCH] =?UTF-8?q?:sparkles:=20::=20[#1306]=20=ED=94=8C=EB=A0=88?= =?UTF-8?q?=EC=9D=B4=EB=A6=AC=EC=8A=A4=ED=8A=B8=20=ED=99=94=EB=A9=B4=20Lon?= =?UTF-8?q?g=20Press=20=ED=8E=B8=EC=A7=91=EB=AA=A8=EB=93=9C=20=EC=A7=84?= =?UTF-8?q?=EC=9E=85=20=EC=B6=94=EA=B0=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../PlaylistViewController+Delegate.swift | 19 +++++++--- .../PlaylistViewController.swift | 35 ++++++++++++------- .../ViewModels/PlaylistViewModel.swift | 18 ++++++++++ .../Sources/Views/PlayListView.swift | 1 + .../Sources/Views/PlaylistTableViewCell.swift | 15 -------- 5 files changed, 57 insertions(+), 31 deletions(-) diff --git a/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController+Delegate.swift b/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController+Delegate.swift index 87eea374e..6e8f3aeef 100644 --- a/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController+Delegate.swift +++ b/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController+Delegate.swift @@ -120,6 +120,21 @@ extension PlaylistViewController: UITableViewDelegate { } return proposedDestinationIndexPath } + + public func tableView( + _ tableView: UITableView, + contextMenuConfigurationForRowAt indexPath: IndexPath, + point: CGPoint + ) -> UIContextMenuConfiguration? { + if output.editState.value == true { + return nil + } else { + return UIContextMenuConfiguration(identifier: nil, previewProvider: { [didLongPressedSongSubject] in + didLongPressedSongSubject.onNext(indexPath.row) + return nil + }) + } + } } extension PlaylistViewController: PlaylistTableViewCellDelegate { @@ -132,8 +147,4 @@ extension PlaylistViewController: PlaylistTableViewCellDelegate { playPlatform: model.title.isContainShortsTagTitle ? .youtube : .automatic ).play() } - - func superButtonTapped(index: Int) { - tappedCellIndex.onNext(index) - } } diff --git a/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController.swift b/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController.swift index 2ac0bd8f7..e0f6159f1 100644 --- a/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController.swift +++ b/Projects/Features/PlaylistFeature/Sources/ViewControllers/PlaylistViewController.swift @@ -24,6 +24,7 @@ public final class PlaylistViewController: UIViewController, SongCartViewType { var isSelectedAllSongs = PublishSubject() var tappedAddPlaylist = PublishSubject() var tappedRemoveSongs = PublishSubject() + var didLongPressedSongSubject = PublishSubject() private(set) var containSongsFactory: any ContainSongsFactory private(set) var songDetailPresenter: any SongDetailPresentable @@ -48,7 +49,8 @@ public final class PlaylistViewController: UIViewController, SongCartViewType { selectAllSongsButtonDidTapEvent: isSelectedAllSongs.asObservable(), addPlaylistButtonDidTapEvent: tappedAddPlaylist.asObservable(), removeSongsButtonDidTapEvent: tappedRemoveSongs.asObservable(), - itemMovedEvent: playlistView.playlistTableView.rx.itemMoved.asObservable() + itemMovedEvent: playlistView.playlistTableView.rx.itemMoved.asObservable(), + didLongPressedSongEvent: didLongPressedSongSubject ) lazy var output = self.viewModel.transform(from: input) @@ -220,18 +222,27 @@ private extension PlaylistViewController { playlistView.playlistTableView.rx.itemSelected .withLatestFrom(output.playlists) { ($0, $1) } - .map { $0.1[$0.0.row] } - .bind(with: self, onNext: { [songDetailPresenter] owner, item in - let currentSongs = output.playlists.value - .map(\.id) - - owner.dismiss(animated: true) { - songDetailPresenter.present( - ids: Array(currentSongs), - selectedID: item.id - ) + .bind(with: self) { [songDetailPresenter, isEditingSubject = output.editState, tappedCellIndex] owner, item in + + let isEditing = isEditingSubject.value + let (indexPath, playlists) = item + + if isEditing { + tappedCellIndex.onNext(indexPath.row) + } else { + guard let selectedSong = playlists[safe: indexPath.row] else { return } + + let currentSongs = playlists + .map(\.id) + + owner.dismiss(animated: true) { + songDetailPresenter.present( + ids: Array(currentSongs), + selectedID: selectedSong.id + ) + } } - }) + } .disposed(by: disposeBag) } diff --git a/Projects/Features/PlaylistFeature/Sources/ViewModels/PlaylistViewModel.swift b/Projects/Features/PlaylistFeature/Sources/ViewModels/PlaylistViewModel.swift index f51be9027..ad3d4f962 100644 --- a/Projects/Features/PlaylistFeature/Sources/ViewModels/PlaylistViewModel.swift +++ b/Projects/Features/PlaylistFeature/Sources/ViewModels/PlaylistViewModel.swift @@ -21,6 +21,7 @@ final class PlaylistViewModel: ViewModelType { let addPlaylistButtonDidTapEvent: Observable let removeSongsButtonDidTapEvent: Observable let itemMovedEvent: Observable<(sourceIndex: IndexPath, destinationIndex: IndexPath)> + let didLongPressedSongEvent: Observable } struct Output { @@ -152,6 +153,23 @@ final class PlaylistViewModel: ViewModelType { playlists.insert(movedData, at: destIndex) output.playlists.accept(playlists) }.disposed(by: disposeBag) + + input.didLongPressedSongEvent + .compactMap { output.playlists.value[safe: $0] } + .bind(with: self) { owner, item in + owner.isEditing.toggle() + + var mutableSelectedSongIds = output.selectedSongIds.value + if mutableSelectedSongIds.contains(item.id) { + mutableSelectedSongIds.remove(item.id) + } else { + mutableSelectedSongIds.insert(item.id) + } + + output.editState.send(owner.isEditing) + output.selectedSongIds.accept(mutableSelectedSongIds) + } + .disposed(by: disposeBag) } private func bindTableView(output: Output) { diff --git a/Projects/Features/PlaylistFeature/Sources/Views/PlayListView.swift b/Projects/Features/PlaylistFeature/Sources/Views/PlayListView.swift index b49ad6757..5cda95c8c 100644 --- a/Projects/Features/PlaylistFeature/Sources/Views/PlayListView.swift +++ b/Projects/Features/PlaylistFeature/Sources/Views/PlayListView.swift @@ -68,6 +68,7 @@ public final class PlaylistView: UIView { $0.sectionHeaderTopPadding = 0 $0.backgroundColor = DesignSystemAsset.BlueGrayColor.blueGray100.color $0.showsVerticalScrollIndicator = true + $0.allowsSelectionDuringEditing = true } override init(frame: CGRect) { diff --git a/Projects/Features/PlaylistFeature/Sources/Views/PlaylistTableViewCell.swift b/Projects/Features/PlaylistFeature/Sources/Views/PlaylistTableViewCell.swift index 58c4db485..c45f85336 100644 --- a/Projects/Features/PlaylistFeature/Sources/Views/PlaylistTableViewCell.swift +++ b/Projects/Features/PlaylistFeature/Sources/Views/PlaylistTableViewCell.swift @@ -10,7 +10,6 @@ import UIKit import Utility internal protocol PlaylistTableViewCellDelegate: AnyObject { - func superButtonTapped(index: Int) func playButtonDidTap(model: PlaylistItemModel) } @@ -78,10 +77,6 @@ internal class PlaylistTableViewCell: UITableViewCell { fatalError("") } - override func prepareForReuse() { - super.prepareForReuse() - } - private func configureContents() { self.backgroundColor = .clear self.contentView.addSubview(self.thumbnailImageView) @@ -110,11 +105,6 @@ internal class PlaylistTableViewCell: UITableViewCell { $0.centerY.equalTo(contentView.snp.centerY) $0.right.equalTo(contentView.snp.right).offset(-20) } - - superButton.snp.makeConstraints { - $0.left.top.bottom.equalToSuperview() - $0.right.equalToSuperview() - } } } @@ -151,10 +141,6 @@ extension PlaylistTableViewCell { owner.delegate?.playButtonDidTap(model: song) } .disposed(by: disposeBag) - - superButton.addAction { [weak self] in - self?.delegate?.superButtonTapped(index: self?.model.index ?? 0) - } } private func updateButtonHidden(isEditing: Bool) { @@ -163,7 +149,6 @@ extension PlaylistTableViewCell { } else { playImageView.isHidden = false } - superButton.isHidden = !isEditing } private func updateConstraintPlayImageView(isEditing: Bool) {