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
Changes from 1 commit
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 @@ -10,12 +10,13 @@ import UIKit

import RxSwift
import RxCocoa
import ReactorKit

import SnapKit
import Then

final class BookmarkListVC: BaseNavigationViewController {

final class BookmarkListVC: BaseNavigationViewController, View {
// MARK: - UI components

override var alias: String {
Expand Down Expand Up @@ -47,6 +48,7 @@ final class BookmarkListVC: BaseNavigationViewController {

private let viewModel: BookmarkCardListVM
private let bookmarkType: BookmarkSearchType
var disposeBag: DisposeBag = .init()
Copy link
Collaborator

Choose a reason for hiding this comment

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

현재 이 disposeBag이 하는 역할이 있나요?? .init()으로 표현한 이유가 있을까요! (Reactor로 변화를 주다보니 기존에 사용하던 MVVM 패턴에서의 개념과 어느정도 조화를 이루는지 아직 감을 잘 못 잡고 있는 것 같습니다..ㅎ)

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와의 bind를 위해 disposeBag을 가져야 합니다!
.init()은 타입 어노테이션을 해줬기 때문에 편의상 사용했어용



// MARK: - Initialize
Expand All @@ -68,13 +70,13 @@ final class BookmarkListVC: BaseNavigationViewController {
override func viewDidLoad() {
super.viewDidLoad()

reactor = viewModel
}

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

viewModel.input.page.accept(0)
viewModel.getBookmarkList(type: bookmarkType)
viewModel.action.onNext(.entering(bookmarkType))
}

override func configureView() {
Expand All @@ -95,13 +97,18 @@ final class BookmarkListVC: BaseNavigationViewController {
bindButton()
}

override func bindOutput() {
super.bindOutput()

bindBookmarkAll()
}

// MARK: - Functions
// MARK: - Bind Reactor

func bind(reactor: BookmarkCardListVM) {
reactor.state.map { $0.bookmarkList }
.withUnretained(self)
.observe(on: MainScheduler.asyncInstance)
.subscribe(onNext: { owner, _ in
owner.tableView.reloadData()
})
.disposed(by: bag)
}

}

Expand Down Expand Up @@ -171,18 +178,6 @@ extension BookmarkListVC {
.disposed(by: bag)
}

private func bindBookmarkAll() {
viewModel.output.bookmarkList
.subscribe(onNext: { [weak self] _ in
guard let self = self else { return }

DispatchQueue.main.async {
self.tableView.reloadData()
}
})
.disposed(by: bag)
}

}


Expand All @@ -195,8 +190,8 @@ extension BookmarkListVC: UITableViewDelegate {

func scrollViewDidScroll(_ scrollView: UIScrollView) {
if tableView.contentOffset.y > tableView.contentSize.height - tableView.bounds.size.height {
if !viewModel.output.isPaging.value && !viewModel.output.isLastPage.value {
viewModel.getBookmarkList(type: bookmarkType)
if !viewModel.currentState.isPaging && !viewModel.currentState.isLastPage {
viewModel.action.onNext(.nextPage)
}
}
}
Expand All @@ -207,13 +202,13 @@ extension BookmarkListVC: UITableViewDelegate {

extension BookmarkListVC: UITableViewDataSource {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
viewModel.output.bookmarkList.value.count
viewModel.currentState.bookmarkList.count
}

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
guard let cell = tableView.dequeueReusableCell(withIdentifier: BookmarkCardTVC.className, for: indexPath) as? BookmarkCardTVC else { fatalError("No such Cell") }

let bookmarkInfo = viewModel.output.bookmarkList.value[indexPath.row]
let bookmarkInfo = viewModel.currentState.bookmarkList[indexPath.row]
cell.configureBookmarkCardTVC(with: bookmarkInfo,
bookmarkCardActionDelegate: self,
index: indexPath.row)
Expand All @@ -228,10 +223,9 @@ extension BookmarkListVC: UITableViewDataSource {
extension BookmarkListVC: BookmarkCardAction {

func infoToggle(index: Int) {
var card = viewModel.output.bookmarkList.value
card[index].infoHidden.toggle()
viewModel.output.bookmarkList.accept(card)
tableView.reloadData()
var cardList = viewModel.currentState.bookmarkList
cardList[index].infoHidden.toggle()
viewModel.action.onNext(.togglingInfo(index))
}

func showMenu(index: Int, location: CGRect, selectMenuType: SelectBoxStyle) {
Expand All @@ -243,13 +237,13 @@ extension BookmarkListVC: BookmarkCardAction {
}

if row == 1 {
let card = self.viewModel.output.bookmarkList.value[index]
let card = self.viewModel.currentState.bookmarkList[index]
UIPasteboard.general.string = card.placeDetailURL
self.showToast(message: "LinkCopied".localized)
}

if row == 2 {
self.viewModel.deleteBookmark(index: index)
self.viewModel.action.onNext(.deleteBookmark(index: index))
}
}
}
Expand All @@ -266,20 +260,20 @@ extension BookmarkListVC: BookmarkCardAction {

func showBottomSheet(index: Int) {
let bottomSheetVC = BookmarkBottomSheetVC(isBookmarking: true)
let cardInfo = viewModel.output.bookmarkList.value[index]
let cardInfo = viewModel.currentState.bookmarkList[index]
bottomSheetVC.configureSheetData(with: cardInfo)

bottomSheetVC.deletedBookmarkId
.withUnretained(self)
.subscribe { owner, id in
owner.viewModel.deleteBookmark(id: id)
owner.viewModel.action.onNext(.removeDeletedBookmark(id: id))
}
.disposed(by: bag)

bottomSheetVC.modifiedBookmarkInfo
.withUnretained(self)
.subscribe { owner, bookmarkInfo in
owner.viewModel.modifyBookmark(info: bookmarkInfo)
owner.viewModel.action.onNext(.modifyBookmark(bookmarkInfo))
}
.disposed(by: bag)

Expand Down