Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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() } }
}
Expand Down Expand Up @@ -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 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ final class BookmarkModalReactor: Reactor {
case dismissWithData
case addCollection
case collectionError
/// 선택한 북마크가 이미 모두 포함된 컬렉션을 탭한 경우
case alreadyIncluded
}

enum Action {
Expand Down Expand Up @@ -45,7 +47,7 @@ final class BookmarkModalReactor: Reactor {
func mutate(action: Action) -> Observable<Mutation> {
switch action {
case .viewWillAppear, .completeAdding:
return collectionRepository.fetchCollectionList(sort: nil)
return collectionRepository.fetchCollectionList(sort: nil, bookmarkIds: currentState.bookmarkIds)
.map { .setCollection($0) }

case .addButtonTapped:
Expand All @@ -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))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
Expand All @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,19 +43,19 @@ final class CollectionListReactor: Reactor {
func mutate(action: Action) -> Observable<Mutation> {
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) }
])
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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 = {
Expand All @@ -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)
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand All @@ -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
}
Expand Down
Loading
Loading