diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/DTOs/CollectionListResponseDTO.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/DTOs/CollectionListResponseDTO.swift index b3494c56..bcddea3b 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/DTOs/CollectionListResponseDTO.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/DTOs/CollectionListResponseDTO.swift @@ -5,13 +5,17 @@ struct CollectionListResponseDTO: Decodable { let name: String let createdAt: [Int] let recentBookmarks: [BookmarkDTO] + let includedCount: Int? + let allIncluded: Bool? func toDomain() -> CollectionResponse { return CollectionResponse( collectionId: collectionId, name: name, createdAt: createdAt, - recentBookmarks: recentBookmarks.toDomain() + recentBookmarks: recentBookmarks.toDomain(), + includedCount: includedCount, + allIncluded: allIncluded ) } } diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/Repositories/CollectionRepositoryImpl.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/Repositories/CollectionRepositoryImpl.swift index 608ed7c8..b0b051c4 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/Repositories/CollectionRepositoryImpl.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Data/Repositories/CollectionRepositoryImpl.swift @@ -11,8 +11,10 @@ public final class CollectionRepositoryImpl: CollectionRepository { self.interceptor = TokenInterceptor() } - public func fetchCollectionList(sort: String?) -> Observable<[CollectionResponse]> { - let endpoint = CollectionEndPoint.fetchCollectionList(query: FetchListQuery(sort: sort)) + public func fetchCollectionList(sort: String?, bookmarkIds: [Int]?) -> Observable<[CollectionResponse]> { + let endpoint = CollectionEndPoint.fetchCollectionList( + query: FetchListQuery(sort: sort, bookmarkIds: Self.makeBookmarkIdsParameter(bookmarkIds)) + ) return provider.requestData(endPoint: endpoint, interceptor: interceptor) .map { $0.map { $0.toDomain() } } } @@ -52,6 +54,13 @@ public final class CollectionRepositoryImpl: CollectionRepository { private extension CollectionRepositoryImpl { struct FetchListQuery: Encodable { let sort: String? + let bookmarkIds: String? + } + + /// 쿼리 인코더가 배열을 다루지 못해 콤마로 이어 붙인다. (`bookmarkIds=1,2,3`) + static func makeBookmarkIdsParameter(_ bookmarkIds: [Int]?) -> String? { + guard let bookmarkIds, !bookmarkIds.isEmpty else { return nil } + return bookmarkIds.map(String.init).joined(separator: ",") } struct CreateBody: Encodable { diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalReactor.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalReactor.swift index 7e3d6ccb..a5b53ff3 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalReactor.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalReactor.swift @@ -9,6 +9,8 @@ final class BookmarkModalReactor: Reactor { case dismissWithData case addCollection case collectionError + /// 선택한 북마크가 이미 모두 포함된 컬렉션을 탭한 경우 + case alreadyIncluded } enum Action { @@ -45,7 +47,7 @@ final class BookmarkModalReactor: Reactor { func mutate(action: Action) -> Observable { switch action { case .viewWillAppear, .completeAdding: - return collectionRepository.fetchCollectionList(sort: nil) + return collectionRepository.fetchCollectionList(sort: nil, bookmarkIds: currentState.bookmarkIds) .map { .setCollection($0) } case .addButtonTapped: @@ -63,10 +65,16 @@ final class BookmarkModalReactor: Reactor { return .just(.navigateTo(.addCollection)) case .selectItem(let id): + guard let collection = currentState.collections.first(where: { $0.collectionId == id }) else { + return .empty() + } + guard collection.allIncluded != true else { + return .just(.navigateTo(.alreadyIncluded)) + } var newItems = currentState.selectedItems if let index = newItems.firstIndex(where: { $0.collectionId == id }) { newItems.remove(at: index) - } else if let collection = currentState.collections.first(where: { $0.collectionId == id }) { + } else { newItems.append(collection) } return .just(.checkCollection(newItems)) diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalViewController.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalViewController.swift index 879688be..d49fdc21 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalViewController.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/BookmarkModal/BookmarkModalViewController.swift @@ -119,6 +119,12 @@ extension BookmarkModalViewController { owner.present(vc, animated: true) case .collectionError: ToastFactory.createToast(message: "컬렉션 저장에 실패했어요. 다시 시도해주세요.") + case .alreadyIncluded: + GuideAlertFactory.show( + mainText: "선택한 북마크가 모두 추가되어\n있는 컬렉션입니다.", + ctaText: "확인", + ctaAction: {} + ) default: break } @@ -127,6 +133,21 @@ extension BookmarkModalViewController { } } +private extension BookmarkModalViewController { + /// 선택한 북마크가 컬렉션에 몇 개나 들어있는지 알려주는 보조 문구 + static func makeIncludedText(of collection: CollectionResponse) -> String? { + guard let includedCount = collection.includedCount, includedCount > 0 else { return nil } + return collection.allIncluded == true + ? "선택한 북마크 \(includedCount)개 모두 포함" + : "선택한 북마크 \(includedCount)개 포함" + } + + static func makeCheckState(of collection: CollectionResponse, isSelected: Bool) -> FolderCell.CheckState { + if collection.allIncluded == true { return .included } + return isSelected ? .selected : .unselected + } +} + extension BookmarkModalViewController: UICollectionViewDelegate, UICollectionViewDataSource { func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { (reactor?.currentState.collections.count ?? 0) + 1 @@ -141,8 +162,11 @@ extension BookmarkModalViewController: UICollectionViewDelegate, UICollectionVie } let collection = reactor.currentState.collections[indexPath.row - 1] let isSelected = reactor.currentState.selectedItems.contains(where: { $0.collectionId == collection.collectionId }) - cell.isChecked = isSelected - cell.inject(title: collection.name) + cell.inject( + title: collection.name, + subtitle: Self.makeIncludedText(of: collection), + state: Self.makeCheckState(of: collection, isSelected: isSelected) + ) return cell } diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/CollectionList/CollectionListReactor.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/CollectionList/CollectionListReactor.swift index 2c7db6d3..170f0bbf 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/CollectionList/CollectionListReactor.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/CollectionList/CollectionListReactor.swift @@ -43,19 +43,19 @@ final class CollectionListReactor: Reactor { func mutate(action: Action) -> Observable { switch action { case .viewWillAppear: - return collectionRepository.fetchCollectionList(sort: currentState.selectedSort?.sortParameter) + return collectionRepository.fetchCollectionList(sort: currentState.selectedSort?.sortParameter, bookmarkIds: nil) .map { .setListData($0) } case .itemTapped(let index): return .just(.navigateTo(.detail(currentState.collectionList[index]))) case .completeAdding: - return collectionRepository.fetchCollectionList(sort: currentState.selectedSort?.sortParameter) + return collectionRepository.fetchCollectionList(sort: currentState.selectedSort?.sortParameter, bookmarkIds: nil) .map { .setListData($0) } case .sortButtonTapped: return .just(.navigateTo(.sortFilter)) case .sortOptionSelected(let sort): return Observable.concat([ .just(.setSort(sort)), - collectionRepository.fetchCollectionList(sort: sort.sortParameter).map { .setListData($0) } + collectionRepository.fetchCollectionList(sort: sort.sortParameter, bookmarkIds: nil).map { .setListData($0) } ]) } } diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/Common/FolderCell.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/Common/FolderCell.swift index e159eca3..743a1994 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/Common/FolderCell.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeature/Presentation/Common/FolderCell.swift @@ -3,12 +3,25 @@ import SnapKit import UIKit final class FolderCell: UICollectionViewCell { + /// 컬렉션 선택 체크박스 상태 + enum CheckState { + /// 선택 가능한 기본 상태 + case unselected + /// 사용자가 선택한 상태 + case selected + /// 선택한 북마크가 이미 모두 포함되어 선택할 수 없는 상태 + case included + } + private enum Constant { static let iconInset: CGFloat = 8 static let radius: CGFloat = 8 static let margin: CGFloat = 16 static let iconSize: CGFloat = 24 static let buttonSize: CGFloat = 40 + static let checkBoxRadius: CGFloat = 6 + static let checkBoxBorderWidth: CGFloat = 1.5 + static let labelSpacing: CGFloat = 2 } private lazy var imageContainerView: UIView = { @@ -37,12 +50,26 @@ final class FolderCell: UICollectionViewCell { return label }() - let checkBoxButton: UIButton = { - let button = UIButton() - button.setImage(DesignSystemAsset.image(named: "checkSquareFill").withRenderingMode(.alwaysTemplate), for: .normal) - button.tintColor = .neutral300 - button.isUserInteractionEnabled = false - return button + private let subtitleLabel: UILabel = { + let label = UILabel() + label.numberOfLines = 1 + label.lineBreakMode = .byTruncatingTail + return label + }() + + private lazy var textStackView: UIStackView = { + let stack = UIStackView(arrangedSubviews: [titleLabel, subtitleLabel]) + stack.axis = .vertical + stack.alignment = .leading + stack.spacing = Constant.labelSpacing + return stack + }() + + let checkBoxView: UIImageView = { + let view = UIImageView() + view.contentMode = .scaleAspectFit + view.layer.cornerRadius = Constant.checkBoxRadius + return view }() private let divider: UIView = { @@ -51,29 +78,28 @@ final class FolderCell: UICollectionViewCell { return view }() - var isChecked: Bool = false { - didSet { - checkBoxButton.tintColor = isChecked ? .primary700 : .neutral300 - } - } + private var title: String? + private var subtitle: String? + + private(set) var state: CheckState = .unselected override init(frame: CGRect) { super.init(frame: frame) contentView.addSubview(imageContainerView) - contentView.addSubview(titleLabel) - contentView.addSubview(checkBoxButton) + contentView.addSubview(textStackView) + contentView.addSubview(checkBoxView) contentView.addSubview(divider) imageContainerView.snp.makeConstraints { make in make.leading.verticalEdges.equalToSuperview().inset(Constant.margin) make.size.equalTo(Constant.buttonSize) } - titleLabel.snp.makeConstraints { make in + textStackView.snp.makeConstraints { make in make.leading.equalTo(imageContainerView.snp.trailing).offset(Constant.margin) make.centerY.equalToSuperview() } - checkBoxButton.snp.makeConstraints { make in - make.leading.equalTo(titleLabel.snp.trailing).offset(Constant.margin) + checkBoxView.snp.makeConstraints { make in + make.leading.greaterThanOrEqualTo(textStackView.snp.trailing).offset(Constant.margin) make.centerY.equalToSuperview() make.trailing.equalToSuperview().inset(Constant.margin) make.size.equalTo(Constant.iconSize) @@ -82,12 +108,66 @@ final class FolderCell: UICollectionViewCell { make.height.equalTo(1) make.horizontalEdges.bottom.equalToSuperview() } + + applyState() } @available(*, unavailable) required init?(coder: NSCoder) { fatalError() } - func inject(title: String?) { - titleLabel.attributedText = .makeStyledString(font: .b_m_r, text: title, alignment: .left) + override func prepareForReuse() { + super.prepareForReuse() + inject(title: nil, subtitle: nil, state: .unselected) + } + + func inject(title: String?, subtitle: String?, state: CheckState) { + self.title = title + self.subtitle = subtitle + self.state = state + applyState() + } +} + +private extension FolderCell { + static let checkIcon = DesignSystemAsset.image(named: "checkSquareFill").withRenderingMode(.alwaysTemplate) + + func applyState() { + let isIncluded = state == .included + + titleLabel.attributedText = .makeStyledString( + font: .b_m_r, + text: title, + color: isIncluded ? .neutral500 : .textColor, + alignment: .left + ) + subtitleLabel.attributedText = .makeStyledString( + font: .cp_xs_r, + text: subtitle, + color: .neutral500, + alignment: .left + ) + subtitleLabel.isHidden = subtitle?.isEmpty ?? true + + switch state { + case .unselected: + // 이동 가능한 기본 상태. 체크 표시 없이 주황 테두리만 둔다. + checkBoxView.image = nil + checkBoxView.backgroundColor = .primary50 + checkBoxView.layer.borderWidth = Constant.checkBoxBorderWidth + checkBoxView.layer.borderColor = UIColor.primary700.cgColor + case .selected: + checkBoxView.image = Self.checkIcon + checkBoxView.tintColor = .primary700 + checkBoxView.backgroundColor = .clearMLS + checkBoxView.layer.borderWidth = 0 + case .included: + // 선택할 수 없으므로 체크 표시를 빼고 회색 비활성으로 둔다. + checkBoxView.image = nil + checkBoxView.backgroundColor = .neutral100 + checkBoxView.layer.borderWidth = Constant.checkBoxBorderWidth + checkBoxView.layer.borderColor = UIColor.neutral300.cgColor + } + + contentView.backgroundColor = isIncluded ? .neutral100 : .whiteMLS } } diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Entities/CollectionResponse.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Entities/CollectionResponse.swift index a69ddd5c..3c84f718 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Entities/CollectionResponse.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Entities/CollectionResponse.swift @@ -3,11 +3,24 @@ public struct CollectionResponse: Equatable { public var name: String public let createdAt: [Int] public var recentBookmarks: [BookmarkResponse] + /// 선택한 북마크 중 이 컬렉션에 포함된 개수. bookmarkIds를 함께 요청한 경우에만 내려온다. + public let includedCount: Int? + /// 선택한 북마크가 모두 포함되어 있는지 여부. bookmarkIds를 함께 요청한 경우에만 내려온다. + public let allIncluded: Bool? - public init(collectionId: Int, name: String, createdAt: [Int], recentBookmarks: [BookmarkResponse]) { + public init( + collectionId: Int, + name: String, + createdAt: [Int], + recentBookmarks: [BookmarkResponse], + includedCount: Int? = nil, + allIncluded: Bool? = nil + ) { self.collectionId = collectionId self.name = name self.createdAt = createdAt self.recentBookmarks = recentBookmarks + self.includedCount = includedCount + self.allIncluded = allIncluded } } diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Repositories/CollectionRepository.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Repositories/CollectionRepository.swift index 5bf337a3..75a3ec4e 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Repositories/CollectionRepository.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureInterface/Repositories/CollectionRepository.swift @@ -1,7 +1,7 @@ import RxSwift public protocol CollectionRepository { - func fetchCollectionList(sort: String?) -> Observable<[CollectionResponse]> + func fetchCollectionList(sort: String?, bookmarkIds: [Int]?) -> Observable<[CollectionResponse]> func createCollectionList(name: String) -> Completable func fetchCollection(id: Int) -> Observable<[BookmarkResponse]> func updateCollectionName(collectionId: Int, name: String) -> Completable diff --git a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureTesting/MockCollectionRepository.swift b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureTesting/MockCollectionRepository.swift index 65a76757..4ada2709 100644 --- a/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureTesting/MockCollectionRepository.swift +++ b/MLS/MLSBookmarkFeature/Sources/MLSBookmarkFeatureTesting/MockCollectionRepository.swift @@ -7,11 +7,12 @@ public final class MockCollectionRepository: CollectionRepository { public var fetchCollectionResult: Observable<[BookmarkResponse]> = .just([]) public var updateCollectionNameResult: Completable = .empty() public var deleteCollectionResult: Completable = .empty() + public var deleteBookmarksToCollectionResult: Completable = .empty() public var addCollectionAndBookmarkResult: Completable = .empty() public init() {} - public func fetchCollectionList(sort: String?) -> Observable<[CollectionResponse]> { + public func fetchCollectionList(sort: String?, bookmarkIds: [Int]?) -> Observable<[CollectionResponse]> { fetchCollectionListResult } @@ -31,6 +32,10 @@ public final class MockCollectionRepository: CollectionRepository { deleteCollectionResult } + public func deleteBookmarksToCollection(collectionIds: [Int], bookmarkIds: [Int]) -> Completable { + deleteBookmarksToCollectionResult + } + public func addCollectionAndBookmark(collectionIds: [Int], bookmarkIds: [Int]) -> Completable { addCollectionAndBookmarkResult } diff --git a/MLS/MLSBookmarkFeatureExample/MenuViewController.swift b/MLS/MLSBookmarkFeatureExample/MenuViewController.swift index dd7fcc80..00b275b5 100644 --- a/MLS/MLSBookmarkFeatureExample/MenuViewController.swift +++ b/MLS/MLSBookmarkFeatureExample/MenuViewController.swift @@ -85,6 +85,19 @@ final class MenuViewController: UITableViewController { } nav.present(vc, animated: true) }, + MenuItem( + title: "BookmarkModal – 컬렉션 옮기기", + subtitle: "포함 상태 확인용 (풀 시트로 바로 열림)" + ) { nav in + let factory = DIContainer.resolve(type: BookmarkModalFactory.self) + let vc = factory.make(bookmarkIds: [1, 2, 3]) + vc.modalPresentationStyle = .pageSheet + if let sheet = vc.sheetPresentationController { + sheet.detents = [.large()] + sheet.prefersGrabberVisible = true + } + nav.present(vc, animated: true) + }, MenuItem( title: "AddCollection", subtitle: "새 컬렉션 추가 모달" diff --git a/MLS/MLSBookmarkFeatureExample/SceneDelegate.swift b/MLS/MLSBookmarkFeatureExample/SceneDelegate.swift index aefd3329..c50414a8 100644 --- a/MLS/MLSBookmarkFeatureExample/SceneDelegate.swift +++ b/MLS/MLSBookmarkFeatureExample/SceneDelegate.swift @@ -2,6 +2,7 @@ import MLSBookmarkFeature import MLSBookmarkFeatureInterface import MLSBookmarkFeatureTesting import MLSCore +import RxSwift import UIKit class SceneDelegate: UIResponder, UIWindowSceneDelegate { @@ -25,6 +26,7 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { private func registerDependencies() { let mockBookmarkRepository = MockBookmarkRepository() let mockCollectionRepository = MockCollectionRepository() + mockCollectionRepository.fetchCollectionListResult = .just(Self.stubCollections) let mockAuthRepository = MockBookmarkAuthRepository() let mockUserDefaultsRepository = MockBookmarkUserDefaultsRepository() let mockParseUseCase = MockParseItemFilterResultUseCase() @@ -35,7 +37,10 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { collectionRepository: mockCollectionRepository ) let collectionSettingFactory = CollectionSettingFactoryImpl() - let collectionEditFactory = CollectionEditFactoryImpl(bookmarkModalFactory: bookmarkModalFactory) + let collectionEditFactory = CollectionEditFactoryImpl( + bookmarkModalFactory: bookmarkModalFactory, + collectionRepository: mockCollectionRepository + ) let collectionDetailFactory = CollectionDetailFactoryImpl( bookmarkModalFactory: bookmarkModalFactory, collectionSettingFactory: collectionSettingFactory, @@ -83,4 +88,14 @@ class SceneDelegate: UIResponder, UIWindowSceneDelegate { DIContainer.register(type: AddCollectionFactory.self) { addCollectionFactory } DIContainer.register(type: BookmarkOnBoardingFactory.self) { onBoardingFactory } } + + /// 데모용 컬렉션 목록. 포함 상태(미포함 / 일부 포함 / 모두 포함)를 모두 보여준다. + private static let stubCollections: [CollectionResponse] = [ + CollectionResponse(collectionId: 1, name: "여행 계획", createdAt: [2024, 1, 1], recentBookmarks: [], includedCount: 3, allIncluded: true), + CollectionResponse(collectionId: 2, name: "맛집 모음", createdAt: [2024, 1, 2], recentBookmarks: [], includedCount: 0, allIncluded: false), + CollectionResponse(collectionId: 3, name: "업무 자료", createdAt: [2024, 1, 3], recentBookmarks: [], includedCount: 2, allIncluded: false), + CollectionResponse(collectionId: 4, name: "디자인 레퍼런스", createdAt: [2024, 1, 4], recentBookmarks: [], includedCount: 1, allIncluded: false), + CollectionResponse(collectionId: 5, name: "읽을거리", createdAt: [2024, 1, 5], recentBookmarks: [], includedCount: 0, allIncluded: false), + CollectionResponse(collectionId: 6, name: "관심 상품", createdAt: [2024, 1, 6], recentBookmarks: [], includedCount: 0, allIncluded: false) + ] }