Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor/#138 ReactorKit 적용 - 북마크 #141

Open
wants to merge 14 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
1 change: 1 addition & 0 deletions Reet-Place/Podfile
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ target 'Reet-Place' do
pod 'RxDataSources', '~> 5.0'

# Others
pod 'ReactorKit'
pod 'Alamofire'
pod 'SnapKit'
pod 'Then'
Expand Down
11 changes: 10 additions & 1 deletion Reet-Place/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ PODS:
- NMapsGeometry (1.0.1)
- NMapsMap (3.16.2):
- NMapsGeometry
- ReactorKit (3.2.0):
- RxSwift (~> 6.0)
- WeakMapTable (~> 1.1)
- RxCocoa (6.5.0):
- RxRelay (= 6.5.0)
- RxSwift (= 6.5.0)
Expand All @@ -31,6 +34,7 @@ PODS:
- RxSwift (6.5.0)
- SnapKit (5.6.0)
- Then (3.0.0)
- WeakMapTable (1.2.0)

DEPENDENCIES:
- Alamofire
Expand All @@ -39,6 +43,7 @@ DEPENDENCIES:
- KakaoSDKUser (<= 2.16)
- Kingfisher (~> 7.0)
- NMapsMap
- ReactorKit
- RxCocoa (= 6.5.0)
- RxDataSources (~> 5.0)
- RxGesture
Expand All @@ -56,13 +61,15 @@ SPEC REPOS:
- Kingfisher
- NMapsGeometry
- NMapsMap
- ReactorKit
- RxCocoa
- RxDataSources
- RxGesture
- RxRelay
- RxSwift
- SnapKit
- Then
- WeakMapTable

SPEC CHECKSUMS:
Alamofire: 4e95d97098eacb88856099c4fc79b526a299e48c
Expand All @@ -73,14 +80,16 @@ SPEC CHECKSUMS:
Kingfisher: 6c5449c6450c5239166510ba04afe374a98afc4f
NMapsGeometry: 53c573ead66466681cf123f99f698dc8071a4b83
NMapsMap: aaa64717249b06ae82c3a3addb3a01f0e33100ab
ReactorKit: e8b11d6b9c415405f381669b095c154a05b59eca
RxCocoa: 94f817b71c07517321eb4f9ad299112ca8af743b
RxDataSources: aa47cc1ed6c500fa0dfecac5c979b723542d79cf
RxGesture: f3efb47ed2d26a8082f7b660d4a59970e275a7f8
RxRelay: 1de1523e604c72b6c68feadedd1af3b1b4d0ecbd
RxSwift: 5710a9e6b17f3c3d6e40d6e559b9fa1e813b2ef8
SnapKit: e01d52ebb8ddbc333eefe2132acf85c8227d9c25
Then: 844265ae87834bbe1147d91d5d41a404da2ec27d
WeakMapTable: 05c694ce8439a7a9ebabb56187287a63c57673d6

PODFILE CHECKSUM: 6eace9c5f53459f66ad27511b12d74fe7bca04d0
PODFILE CHECKSUM: 65003312d4883474af61cbcbaac9fb791372e229

COCOAPODS: 1.11.3
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class BookmarkTypeCVC: BaseCollectionViewCell {

// MARK: - Function

func configureData(typeInfo: TypeInfo) {
func configureData(typeInfo: BookmarkTypeInfo) {
switch typeInfo.type {
case "WANT":
titleLabel.text = "BookmarkWishlist".localized
Expand Down

This file was deleted.

20 changes: 20 additions & 0 deletions Reet-Place/Reet-Place/Screen/BookMark/Model/BookmarkTypeInfo.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// BookmarkTypeInfo.swift
// Reet-Place
//
// Created by 김태현 on 2023/04/16.
//

import Foundation

struct BookmarkTypeInfo {
let type: String
let cnt: Int
let thumbnailUrlString: String
}

extension BookmarkTypeInfo {
static let empty: Self = .init(type: .empty,
cnt: 0,
thumbnailUrlString: .empty)
}
148 changes: 63 additions & 85 deletions Reet-Place/Reet-Place/Screen/BookMark/ViewController/BookmarkVC.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,14 @@ import UIKit

import RxSwift
import RxCocoa
import ReactorKit

import Then
import SnapKit

import Kingfisher

final class BookmarkVC: BaseNavigationViewController {
final class BookmarkVC: BaseNavigationViewController, View {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

View 채택 같은 부분은 SwiftUI인가요? 어느 부분에서 코드 구현이 되어있는지 잘 몰라서ㅜㅜ 질문 드리고자합니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ReactorKit의 View 입니다!
Reactor와 연결해주기 위해 View를 채택해주어야 합니다.
링크 참고해주세요!


// MARK: - UI components

Expand Down Expand Up @@ -51,22 +52,22 @@ final class BookmarkVC: BaseNavigationViewController {
}

private let viewModel = BookmarkVM()
var disposeBag = DisposeBag()


// MARK: - Life Cycle

override func viewDidLoad() {
super.viewDidLoad()

reactor = viewModel
}

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

removeCache()

// TODO: - 서버 에러 해결 후 변경
viewModel.getBookmarkMock()
viewModel.action.onNext(.entering)
}

override func configureView() {
Expand All @@ -81,22 +82,65 @@ final class BookmarkVC: BaseNavigationViewController {
configureLayout()
}

override func bindRx() {
super.bindRx()

}

override func bindInput() {
super.bindInput()

bindBtn()
}

override func bindOutput() {
super.bindOutput()
// MARK: - Bind Reactor

func bind(reactor: BookmarkVM) {
reactor.state.map { $0.isAuthenticated }
.distinctUntilChanged()
.withUnretained(self)
.observe(on: MainScheduler.asyncInstance)
.bind(onNext: { owner, isAuthenticated in
owner.allBookmarkBtn.isHidden = !isAuthenticated
owner.bookmarkTypeCV.isHidden = !isAuthenticated
owner.emptyBookmarkView.isHidden = !isAuthenticated
owner.induceBookmarkView.isHidden = !isAuthenticated

owner.requestLoginView.isHidden = isAuthenticated
})
.disposed(by: bag)

reactor.state.map { $0.bookmarkTotalCount }
.distinctUntilChanged()
.withUnretained(self)
.subscribe(onNext: { owner, count in
owner.allBookmarkBtn.configureButton(for: count > 0 ? .active : .disabled,
count: count)
})
.disposed(by: disposeBag)

reactor.state.map { $0.bookmarkWishInfo }
.withUnretained(self)
.observe(on: MainScheduler.asyncInstance)
.subscribe(onNext: { owner, data in
owner.bookmarkTypeCV.reloadData()
})
.disposed(by: disposeBag)

reactor.state.map { $0.bookmarkHistoryInfo }
.withUnretained(self)
.observe(on: MainScheduler.asyncInstance)
.subscribe(onNext: { owner, data in
owner.bookmarkTypeCV.reloadData()
})
.disposed(by: disposeBag)

bindBookmark()
bindType()
reactor.state.map { $0.bookmarkTotalCount }
.map { $0 == 0 }
.distinctUntilChanged()
.withUnretained(self)
.observe(on: MainScheduler.asyncInstance)
.subscribe(onNext: { owner, isEmpty in
owner.bookmarkTypeCV.isHidden = isEmpty
owner.induceBookmarkView.isHidden = isEmpty
owner.emptyBookmarkView.isHidden = !isEmpty
})
.disposed(by: disposeBag)
}


Expand Down Expand Up @@ -192,6 +236,7 @@ extension BookmarkVC {
// MARK: - Input

extension BookmarkVC {

private func bindBtn() {
// 북마크 모두 보기
allBookmarkBtn.rx.tap
Expand All @@ -218,74 +263,6 @@ extension BookmarkVC {
})
.disposed(by: bag)
}
}


// MARK: - Output

extension BookmarkVC {

private func bindBookmark() {
// 북마크 All 개수
viewModel.output.BookmarkAllCnt
.withUnretained(self)
.subscribe(onNext: { owner, _ in
let allCnt = owner.viewModel.output.BookmarkAllCnt.value

owner.allBookmarkBtn.configureButton(for: allCnt > 0 ? .active : .disabled,
count: allCnt)
})
.disposed(by: bag)

// 북마크 - 가고싶어요 개수, 이미지
viewModel.output.BookmarkWishlistInfo
.withUnretained(self)
.subscribe(onNext: { owner, data in
DispatchQueue.main.async {
owner.bookmarkTypeCV.reloadData()
}
})
.disposed(by: bag)

// 북마크 - 다녀왔어요 개수, 이미지
viewModel.output.BookmarkHistoryInfo
.withUnretained(self)
.subscribe(onNext: { owner, data in
DispatchQueue.main.async {
owner.bookmarkTypeCV.reloadData()
}
})
.disposed(by: bag)
}

private func bindType() {
// 로그인 여부 체크
viewModel.output.isAuthenticated
.withUnretained(self)
.bind(onNext: { owner, isAuthenticated in
DispatchQueue.main.async {
owner.allBookmarkBtn.isHidden = !isAuthenticated
owner.bookmarkTypeCV.isHidden = !isAuthenticated
owner.emptyBookmarkView.isHidden = !isAuthenticated
owner.induceBookmarkView.isHidden = !isAuthenticated

owner.requestLoginView.isHidden = isAuthenticated
}
})
.disposed(by: bag)

// 북마크 개수가 0개인지 확인
viewModel.output.isEmptyBookmark
.withUnretained(self)
.bind(onNext: { owner, isEmptyBookmark in
DispatchQueue.main.async {
owner.bookmarkTypeCV.isHidden = isEmptyBookmark
owner.induceBookmarkView.isHidden = isEmptyBookmark
owner.emptyBookmarkView.isHidden = !isEmptyBookmark
}
})
.disposed(by: bag)
}

}

Expand All @@ -302,9 +279,9 @@ extension BookmarkVC: UICollectionViewDelegate {
var bookmarkSearchType: BookmarkSearchType = .want

if indexPath.row == 0 {
guard viewModel.output.BookmarkWishlistInfo.value.cnt != 0 else { return }
guard viewModel.currentState.bookmarkWishInfo.cnt != 0 else { return }
} else {
guard viewModel.output.BookmarkHistoryInfo.value.cnt != 0 else { return }
guard viewModel.currentState.bookmarkHistoryInfo.cnt != 0 else { return }
bookmarkSearchType = .done
}

Expand All @@ -320,12 +297,13 @@ extension BookmarkVC: UICollectionViewDataSource {

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
guard let cell = collectionView.dequeueReusableCell(withReuseIdentifier: BookmarkTypeCVC.className, for: indexPath) as? BookmarkTypeCVC else { return UICollectionViewCell() }

if indexPath.row == 0 {
cell.configureData(typeInfo: viewModel.output.BookmarkWishlistInfo.value)
cell.configureData(typeInfo: viewModel.currentState.bookmarkWishInfo)
}

if indexPath.row == 1 {
cell.configureData(typeInfo: viewModel.output.BookmarkHistoryInfo.value)
cell.configureData(typeInfo: viewModel.currentState.bookmarkHistoryInfo)
}

return cell
Expand Down
Loading