Skip to content

Commit 65a3250

Browse files
authored
Merge pull request #141 from boostcampwm-2022/refactor/browseView
Refactor/browse view
2 parents c0964a6 + 5e7b06e commit 65a3250

File tree

15 files changed

+232
-52
lines changed

15 files changed

+232
-52
lines changed

DailyQuest/DailyQuest/Domain/Entities/User.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,15 @@ struct User {
4444
}
4545

4646
extension User {
47+
func setAllow(allow: Bool) -> User {
48+
return User(uuid: self.uuid,
49+
nickName: self.nickName,
50+
profileURL: self.profileURL,
51+
backgroundImageURL: self.backgroundImageURL,
52+
introduce: self.introduce,
53+
allow: allow)
54+
}
55+
4756
func setProfileImageURL(profileURL: String) -> User {
4857
return User(uuid: self.uuid,
4958
nickName: self.nickName,

DailyQuest/DailyQuest/Domain/UseCases/Settings/DefaultSettingsUseCase.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,18 @@ extension DefaultSettingsUseCase: SettingsUseCase {
3737
.catchAndReturn(false)
3838
.asObservable()
3939
}
40+
41+
func updateAllow(allow: Bool) -> Single<Bool> {
42+
userRepository.readUser()
43+
.map { $0.setAllow(allow: allow) }
44+
.flatMap(userRepository.updateUser(by:))
45+
.map { _ in true }
46+
.catchAndReturn(false)
47+
}
48+
49+
func fetchAllow() -> Single<Bool?> {
50+
userRepository.readUser()
51+
.map { $0.allow }
52+
.catchAndReturn(nil)
53+
}
4054
}

DailyQuest/DailyQuest/Domain/UseCases/Settings/Protocols/SettingsUseCase.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,7 @@ import RxSwift
1212
protocol SettingsUseCase {
1313
func isLoggedIn() -> Observable<Bool>
1414
func signOut() -> Observable<Bool>
15+
16+
func updateAllow(allow: Bool) -> Single<Bool>
17+
func fetchAllow() -> Single<Bool?>
1518
}

DailyQuest/DailyQuest/Presentation/Browse/Flow/BrowseCoordinator.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ final class DefaultBrowseCoordinator: BrowseCoordinator {
3030
func start() {
3131
let browseViewController = browseSceneDIContainer.makeBrowseViewController()
3232
navigationController.pushViewController(browseViewController, animated: false)
33+
navigationController.isNavigationBarHidden = true
3334

3435
browseViewController
3536
.coordinatorPublisher

DailyQuest/DailyQuest/Presentation/Browse/View/UserInfoView.swift

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
//
77

88
import UIKit
9-
9+
import Kingfisher
1010
import SnapKit
1111

1212
final class UserInfoView: UIStackView {
@@ -59,5 +59,6 @@ final class UserInfoView: UIStackView {
5959

6060
func setup(with user: User) {
6161
welcomeLabel.text = user.nickName + "님의 퀘스트"
62+
userImage.setImage(with: user.profileURL)
6263
}
6364
}

DailyQuest/DailyQuest/Presentation/Browse/ViewController/BrowseViewController.swift

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,18 @@ final class BrowseViewController: UITableViewController {
1717
private var viewModel: BrowseViewModel!
1818
private var disposableBag = DisposeBag()
1919

20+
lazy var activityIndicator: UIActivityIndicatorView = {
21+
// Create an indicator.
22+
let activityIndicator = UIActivityIndicatorView()
23+
activityIndicator.color = .maxDarkYellow
24+
25+
let transfrom = CGAffineTransform.init(scaleX: 2, y: 2)
26+
activityIndicator.transform = transfrom
27+
28+
activityIndicator.startAnimating()
29+
return activityIndicator
30+
}()
31+
2032
// MARK: - Life Cycle
2133
static func create(with viewModel: BrowseViewModel) -> BrowseViewController {
2234
let view = BrowseViewController()
@@ -28,10 +40,18 @@ final class BrowseViewController: UITableViewController {
2840
super.viewDidLoad()
2941

3042
configure()
31-
43+
configureIndicatorBar()
3244
bind()
3345
}
3446

47+
private func configureIndicatorBar() {
48+
self.view.addSubview(activityIndicator)
49+
activityIndicator.snp.makeConstraints { make in
50+
make.width.height.equalTo(50)
51+
make.centerX.centerY.equalToSuperview()
52+
}
53+
}
54+
3555
/**
3656
table view의 기본 정보를 설정합니다.
3757
*/
@@ -50,6 +70,9 @@ final class BrowseViewController: UITableViewController {
5070

5171
output
5272
.data
73+
.do{ [weak self] _ in
74+
self?.activityIndicator.stopAnimating()
75+
}
5376
.drive(tableView.rx.items(cellIdentifier: BrowseCell.reuseIdentifier, cellType: BrowseCell.self)) { row, item, cell in
5477
cell.setup(with: item)
5578
}

DailyQuest/DailyQuest/Presentation/Common/View/QuestViewHeader.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ final class QuestViewHeader: UIStackView {
1818
// MARK: - Components
1919
private(set) lazy var titleLabel: UILabel = {
2020
let titleLabel = UILabel()
21-
titleLabel.text = "Today Quests"
21+
titleLabel.text = "오늘의 퀘스트"
2222
titleLabel.textColor = .maxViolet
2323
titleLabel.font = UIFont.boldSystemFont(ofSize: 32)
2424

DailyQuest/DailyQuest/Presentation/Home/ViewModel/HomeViewModel.swift

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,14 @@ final class HomeViewModel {
9494
.do(onNext: { [weak self] date in
9595
self?.currentDate = date
9696
})
97-
.flatMap(questUseCase.fetch(by:))
98-
.asDriver(onErrorJustReturn: [])
97+
.flatMap(questUseCase.fetch(by:))
98+
.asDriver(onErrorJustReturn: [])
9999

100-
let userNotification = NotificationCenter
101-
.default
102-
.rx
103-
.notification(.userUpdated)
104-
.map { _ in Date() }
100+
let userNotification = NotificationCenter
101+
.default
102+
.rx
103+
.notification(.userUpdated)
104+
.map { _ in Date() }
105105

106106
let userData = Observable
107107
.merge(
@@ -194,13 +194,10 @@ final class HomeViewModel {
194194
private extension HomeViewModel {
195195
func calculateRelative(_ date: Date) -> String {
196196
let today = Date()
197-
198-
if today.startOfDay > date.startOfDay {
199-
return "Previous Quests"
200-
} else if today.startOfDay < date.startOfDay {
201-
return "Upcomming Quests"
197+
if today.startOfDay == date.startOfDay {
198+
return "오늘의 퀘스트"
202199
} else {
203-
return "Today Quests"
200+
return "\(date.toFormatMonthDay)의 퀘스트 "
204201
}
205202
}
206203
}

DailyQuest/DailyQuest/Presentation/Settings/View/ToggleField/ToggleCell.swift

Lines changed: 44 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,15 @@
66
//
77

88
import UIKit
9+
import RxSwift
10+
import RxCocoa
911

1012
final class ToggleCell: UITableViewCell {
1113
static let reuseIdentifier = "ToggleCell"
14+
private var viewModel: ToggleItemViewModel!
15+
16+
var toggleItemDidClicked = PublishSubject<Bool>()
17+
private var disposableBag = DisposeBag()
1218

1319
private let padding = 20
1420

@@ -45,9 +51,9 @@ final class ToggleCell: UITableViewCell {
4551
}
4652

4753
private func configureUI() {
48-
addSubview(icon)
49-
addSubview(title)
50-
addSubview(toggle)
54+
contentView.addSubview(icon)
55+
contentView.addSubview(title)
56+
contentView.addSubview(toggle)
5157

5258
icon.snp.makeConstraints { make in
5359
make.leading.top.bottom.equalToSuperview().inset(padding)
@@ -67,5 +73,40 @@ final class ToggleCell: UITableViewCell {
6773
func setup(with viewModel: ToggleItemViewModel) {
6874
icon.image = UIImage(systemName: viewModel.imageName)
6975
title.text = viewModel.title
76+
self.viewModel = viewModel
77+
bind()
78+
}
79+
80+
func bind() {
81+
toggle.rx.tapGesture()
82+
.when(.ended)
83+
.do(onNext: { _ in
84+
print(self.toggle.isOn)
85+
})
86+
.bind(onNext: {_ in
87+
self.toggleItemDidClicked.onNext(!self.toggle.isOn)
88+
})
89+
.disposed(by: disposableBag)
90+
91+
let output = viewModel.transform(input: ToggleItemViewModel.Input(
92+
toggleItemDidClicked: toggleItemDidClicked
93+
))
94+
95+
output.toggleItemResult
96+
.subscribe(onNext: { isOn in
97+
guard let isOn = isOn else {
98+
self.toggle.isOn = false
99+
self.toggle.isEnabled = false
100+
return
101+
}
102+
103+
if !self.toggle.isEnabled {
104+
self.toggle.isEnabled = true
105+
}
106+
DispatchQueue.main.async {
107+
self.toggle.isOn = isOn
108+
}
109+
})
110+
.disposed(by: disposableBag)
70111
}
71112
}

DailyQuest/DailyQuest/Presentation/Settings/View/ToggleField/ToggleItemViewModel.swift

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,34 @@
66
//
77

88
import Foundation
9+
import RxSwift
910

1011
struct ToggleItemViewModel {
1112
let title: String
1213
let imageName: String
14+
let settingsUseCase: SettingsUseCase!
15+
16+
struct Input {
17+
let toggleItemDidClicked: Observable<Bool>
18+
}
19+
20+
struct Output {
21+
let toggleItemResult: Observable<Bool?>
22+
}
23+
24+
func transform(input: Input) -> Output {
25+
let fetchAllow = settingsUseCase.isLoggedIn()
26+
.flatMap { _ in settingsUseCase.fetchAllow() }
27+
.asObservable()
28+
29+
let changeAllow = input.toggleItemDidClicked
30+
.flatMap { isOn in
31+
settingsUseCase.updateAllow(allow: isOn).asObservable()
32+
.map { result in result ? isOn : nil }
33+
}
34+
35+
let toggleItemResult = Observable.merge(fetchAllow, changeAllow)
36+
37+
return Output(toggleItemResult: toggleItemResult)
38+
}
1339
}

0 commit comments

Comments
 (0)