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/presentationlayer #137

Merged
merged 8 commits into from
Dec 12, 2022
Merged
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
9 changes: 9 additions & 0 deletions DailyQuest/DailyQuest/Domain/Entities/User.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,15 @@ struct User {
}

extension User {
func setAllow(allow: Bool) -> User {
return User(uuid: self.uuid,
nickName: self.nickName,
profileURL: self.profileURL,
backgroundImageURL: self.backgroundImageURL,
introduce: self.introduce,
allow: allow)
}

func setProfileImageURL(profileURL: String) -> User {
return User(uuid: self.uuid,
nickName: self.nickName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,18 @@ extension DefaultSettingsUseCase: SettingsUseCase {
.catchAndReturn(false)
.asObservable()
}

func updateAllow(allow: Bool) -> Single<Bool> {
userRepository.readUser()
.map { $0.setAllow(allow: allow) }
.flatMap(userRepository.updateUser(by:))
.map { _ in true }
.catchAndReturn(false)
}

func fetchAllow() -> Single<Bool?> {
userRepository.readUser()
.map { $0.allow }
.catchAndReturn(nil)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,7 @@ import RxSwift
protocol SettingsUseCase {
func isLoggedIn() -> Observable<Bool>
func signOut() -> Observable<Bool>

func updateAllow(allow: Bool) -> Single<Bool>
func fetchAllow() -> Single<Bool?>
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ final class DefaultBrowseCoordinator: BrowseCoordinator {
func start() {
let browseViewController = browseSceneDIContainer.makeBrowseViewController()
navigationController.pushViewController(browseViewController, animated: false)
navigationController.isNavigationBarHidden = true

browseViewController
.coordinatorPublisher
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@ final class BrowseViewController: UITableViewController {
private var viewModel: BrowseViewModel!
private var disposableBag = DisposeBag()

lazy var activityIndicator: UIActivityIndicatorView = {
// Create an indicator.
let activityIndicator = UIActivityIndicatorView()
activityIndicator.color = .maxDarkYellow

let transfrom = CGAffineTransform.init(scaleX: 2, y: 2)
activityIndicator.transform = transfrom

activityIndicator.startAnimating()
return activityIndicator
}()

// MARK: - Life Cycle
static func create(with viewModel: BrowseViewModel) -> BrowseViewController {
let view = BrowseViewController()
Expand All @@ -28,10 +40,18 @@ final class BrowseViewController: UITableViewController {
super.viewDidLoad()

configure()

configureIndicatorBar()
bind()
}

private func configureIndicatorBar() {
self.view.addSubview(activityIndicator)
activityIndicator.snp.makeConstraints { make in
make.width.height.equalTo(50)
make.centerX.centerY.equalToSuperview()
}
}

/**
table view์˜ ๊ธฐ๋ณธ ์ •๋ณด๋ฅผ ์„ค์ •ํ•ฉ๋‹ˆ๋‹ค.
*/
Expand All @@ -50,6 +70,9 @@ final class BrowseViewController: UITableViewController {

output
.data
.do{ [weak self] _ in
self?.activityIndicator.stopAnimating()
}
.drive(tableView.rx.items(cellIdentifier: BrowseCell.reuseIdentifier, cellType: BrowseCell.self)) { row, item, cell in
cell.setup(with: item)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@
//

import UIKit
import RxSwift
import RxCocoa

final class ToggleCell: UITableViewCell {
static let reuseIdentifier = "ToggleCell"
private var viewModel: ToggleItemViewModel!

var toggleItemDidClicked = PublishSubject<Bool>()
private var disposableBag = DisposeBag()

private let padding = 20

Expand Down Expand Up @@ -45,9 +51,9 @@ final class ToggleCell: UITableViewCell {
}

private func configureUI() {
addSubview(icon)
addSubview(title)
addSubview(toggle)
contentView.addSubview(icon)
contentView.addSubview(title)
contentView.addSubview(toggle)

icon.snp.makeConstraints { make in
make.leading.top.bottom.equalToSuperview().inset(padding)
Expand All @@ -67,5 +73,40 @@ final class ToggleCell: UITableViewCell {
func setup(with viewModel: ToggleItemViewModel) {
icon.image = UIImage(systemName: viewModel.imageName)
title.text = viewModel.title
self.viewModel = viewModel
bind()
}

func bind() {
toggle.rx.tapGesture()
.when(.ended)
.do(onNext: { _ in
print(self.toggle.isOn)
})
.bind(onNext: {_ in
self.toggleItemDidClicked.onNext(!self.toggle.isOn)
})
.disposed(by: disposableBag)

let output = viewModel.transform(input: ToggleItemViewModel.Input(
toggleItemDidClicked: toggleItemDidClicked
))

output.toggleItemResult
.subscribe(onNext: { isOn in
guard let isOn = isOn else {
self.toggle.isOn = false
self.toggle.isEnabled = false
return
}

if !self.toggle.isEnabled {
self.toggle.isEnabled = true
}
DispatchQueue.main.async {
self.toggle.isOn = isOn
}
})
.disposed(by: disposableBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,34 @@
//

import Foundation
import RxSwift

struct ToggleItemViewModel {
let title: String
let imageName: String
let settingsUseCase: SettingsUseCase!

struct Input {
let toggleItemDidClicked: Observable<Bool>
}

struct Output {
let toggleItemResult: Observable<Bool?>
}

func transform(input: Input) -> Output {
let fetchAllow = settingsUseCase.isLoggedIn()
.flatMap { _ in settingsUseCase.fetchAllow() }
.asObservable()

let changeAllow = input.toggleItemDidClicked
.flatMap { isOn in
settingsUseCase.updateAllow(allow: isOn).asObservable()
.map { result in result ? isOn : nil }
}

let toggleItemResult = Observable.merge(fetchAllow, changeAllow)

return Output(toggleItemResult: toggleItemResult)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,17 @@ final class LoginViewController: UIViewController {
return UIButton(configuration: config)
}()

lazy var activityIndicator: UIActivityIndicatorView = {
// Create an indicator.
let activityIndicator = UIActivityIndicatorView()
activityIndicator.color = .maxDarkYellow

let transfrom = CGAffineTransform.init(scaleX: 2, y: 2)
activityIndicator.transform = transfrom

return activityIndicator
}()

// MARK: Life Cycle
static func create(with viewModel: LoginViewModel) -> LoginViewController {
let vc = LoginViewController()
Expand All @@ -68,6 +79,10 @@ final class LoginViewController: UIViewController {
return vc
}

override func viewWillAppear(_ animated: Bool) {
navigationController?.isNavigationBarHidden = false
}

override func viewDidLoad() {
super.viewDidLoad()

Expand All @@ -85,11 +100,17 @@ final class LoginViewController: UIViewController {
container.addArrangedSubview(signUpButton)

view.addSubview(container)
view.addSubview(activityIndicator)

container.snp.makeConstraints { make in
make.center.equalToSuperview()
make.width.equalToSuperview().multipliedBy(0.8)
}

activityIndicator.snp.makeConstraints { make in
make.width.height.equalTo(50)
make.centerX.centerY.equalToSuperview()
}
}

private func setup(with authViewModel: LoginViewModel) {
Expand All @@ -104,10 +125,16 @@ extension LoginViewController {
self.itemDidClick.onNext(.showSignUpFlow)
}).disposed(by: disposableBag)

let submitButtonDidTapEvent = submitButton.rx.tap
.asObservable()
.do(onNext: { [weak self] _ in
self?.activityIndicator.startAnimating()
})

let input = LoginViewModel.Input(
emailFieldDidEditEvent: emailField.rx.text.orEmpty.asObservable(),
passwordFieldDidEditEvent: passwordField.rx.text.orEmpty.asObservable(),
submitButtonDidTapEvent: submitButton.rx.tap.asObservable()
submitButtonDidTapEvent: submitButtonDidTapEvent
)

let output = viewModel.transform(input: input, disposeBag: disposableBag)
Expand All @@ -126,6 +153,7 @@ extension LoginViewController {

extension LoginViewController: Alertable {
private func analyse(result: Bool) {
activityIndicator.stopAnimating()
if result {
itemDidClick.onNext(.back)
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ final class SettingsViewController: UITableViewController {
private var disposableBag = DisposeBag()

var itemDidClick = PublishSubject<Event>()
var toggleButtonDidClick = PublishSubject<Event>()

// MARK: - Life Cycle
static func create(with viewModel: SettingsViewModel) -> SettingsViewController {
Expand All @@ -26,6 +27,7 @@ final class SettingsViewController: UITableViewController {

override func viewDidLoad() {
super.viewDidLoad()
navigationController?.isNavigationBarHidden = true
tableView.separatorStyle = .singleLine

register()
Expand Down
Loading