Skip to content

Commit

Permalink
#112 refactor: PublishSubject를 이용하지 않고 RxCocoa를 이용하도록 변경
Browse files Browse the repository at this point in the history
  • Loading branch information
LEEYOONJONG committed Nov 30, 2022
1 parent 438bba5 commit 4580595
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 80 deletions.
11 changes: 1 addition & 10 deletions Segno/Segno/Presentation/View/NicknameCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@ final class NicknameCell: UITableViewCell {
}

private let disposeBag = DisposeBag()
var nicknameChangeButtonTapped = PublishSubject<String>()


private lazy var nicknameView: UIView = {
let view = UIView()
return view
Expand Down Expand Up @@ -55,7 +54,6 @@ final class NicknameCell: UITableViewCell {
button.backgroundColor = .appColor(.color4)
button.setTitleColor(.appColor(.white), for: .normal)
button.layer.cornerRadius = Metric.cornerRadius
button.addTarget(self, action: #selector(okButtonTapped), for: .touchUpInside)
return button
}()

Expand Down Expand Up @@ -96,11 +94,4 @@ final class NicknameCell: UITableViewCell {
$0.height.equalTo(Metric.textHeight)
}
}

@objc private func okButtonTapped() {
debugPrint("\(nicknameTextField.text!) - 확인 버튼을 누름")
guard let newNickname = nicknameTextField.text else { return }
nicknameChangeButtonTapped
.onNext(newNickname)
}
}
25 changes: 1 addition & 24 deletions Segno/Segno/Presentation/View/SettingsActionSheetCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,13 @@ import UIKit
import RxSwift
import SnapKit

enum SettingsActionSheetMode {
case darkmode
}

final class SettingsActionSheetCell: UITableViewCell {
private enum Metric {
static let labelFontSize: CGFloat = 20
static let edgeSpacing: CGFloat = 20
}

var settingsActionSheetTapped = PublishSubject<(SettingsActionSheetMode, Any?)>()

lazy var titleLabel: UILabel = {
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = .appFont(.shiningStar, size: Metric.labelFontSize)
return label
Expand Down Expand Up @@ -51,21 +45,4 @@ final class SettingsActionSheetCell: UITableViewCell {
func configure(title: String) {
titleLabel.text = title
}

func tapped(mode: SettingsActionSheetMode, _ completionHandler: @escaping (UIAlertController) -> Void) {
switch mode {
case .darkmode:
let actionSheet = UIAlertController(title: "다크 모드 설정", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "시스템 설정", style: .default, handler: { _ in
self.settingsActionSheetTapped.onNext((.darkmode, 0))
}))
actionSheet.addAction(UIAlertAction(title: "항상 밝게", style: .default, handler: { _ in
self.settingsActionSheetTapped.onNext((.darkmode, 1))
}))
actionSheet.addAction(UIAlertAction(title: "항상 어둡게", style: .default, handler: { _ in
self.settingsActionSheetTapped.onNext((.darkmode, 2))
}))
completionHandler(actionSheet)
}
}
}
17 changes: 1 addition & 16 deletions Segno/Segno/Presentation/View/SettingsSwitchCell.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,14 @@ final class SettingsSwitchCell: UITableViewCell {
static let labelFontSize: CGFloat = 20
}

var settingsSwitchTapped = PublishSubject<(CellActions, Any?)>()

lazy var titleLabel: UILabel = {
private lazy var titleLabel: UILabel = {
let label = UILabel()
label.font = .appFont(.shiningStar, size: Metric.labelFontSize)
return label
}()

lazy var switchButton: UISwitch = {
let switchButton = UISwitch()
switchButton.addTarget(self, action: #selector(switchButtonTapped), for: .touchUpInside)
return switchButton
}()

Expand Down Expand Up @@ -59,16 +56,4 @@ final class SettingsSwitchCell: UITableViewCell {
switchButton.isOn = isOn
switchButton.tag = action.toRow
}

@objc private func switchButtonTapped() {
guard let action = CellActions(rawValue: switchButton.tag) else { return }
let switchStatus = switchButton.isOn
switch action {
case .autoplay:
settingsSwitchTapped
.onNext((action, switchStatus))
default:
break
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ final class SettingsViewController: UIViewController {
setupView()
setupLayout()
bindTableView()
bind()
}

private func setupView() {
Expand All @@ -69,27 +68,31 @@ final class SettingsViewController: UIViewController {
guard let cell = tableView.dequeueReusableCell(withIdentifier: "NicknameCell") as? NicknameCell,
let self = self else { return UITableViewCell() }

cell.nicknameChangeButtonTapped
.flatMap { nickname in
self.viewModel.changeNickname(to: nickname)
cell.okButton.rx.tap
.flatMap { a in
guard let newNickname = cell.nicknameTextField.text else {
return Observable<Bool>.empty()
}
debugPrint("입력된 아이디 : ", newNickname)
return self.viewModel.changeNickname(to: newNickname)
}
.subscribe(onNext: { result in
// TODO: result 결과에 따른 Alert 작성
debugPrint("viewModel 메서드 실행 결과 : ", result)
})
.disposed(by: self.disposeBag)

return cell
case .settingsSwitch(let title, let isOn):
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsSwitchCell") as? SettingsSwitchCell,
let action = CellActions(rawValue: row),
let self = self else { return UITableViewCell() }

cell.settingsSwitchTapped
.subscribe(onNext: { (cellActions, value) in
switch cellActions {
cell.switchButton.rx.controlEvent(.touchUpInside)
.subscribe(onNext: {
let isOn = cell.switchButton.isOn
switch action {
case .autoplay:
guard let value = value as? Bool else { return }
return self.viewModel.changeAutoPlayMode(to: value)
self.viewModel.changeAutoPlayMode(to: isOn)
default:
break
}
Expand All @@ -99,18 +102,9 @@ final class SettingsViewController: UIViewController {
cell.configure(title: title, isOn: isOn, action: action)
return cell
case .settingsActionSheet(let title):
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsActionSheetCell") as? SettingsActionSheetCell,
let self = self else { return UITableViewCell() }

cell.settingsActionSheetTapped
.subscribe(onNext: { (actionSheetMode, value) in
switch actionSheetMode {
case .darkmode:
guard let status = value as? Int else { return }
self.viewModel.changeDarkMode(to: status)
}
})
.disposed(by: self.disposeBag)
guard let cell = tableView.dequeueReusableCell(withIdentifier: "SettingsActionSheetCell") as? SettingsActionSheetCell else {
return UITableViewCell()
}

cell.configure(title: title)
return cell
Expand All @@ -124,20 +118,23 @@ final class SettingsViewController: UIViewController {
guard let action = CellActions(rawValue: indexPath.row) else { return }
switch action {
case .darkmode: // 다크 모드 설정
guard let cell = self?.tableView.cellForRow(at: indexPath) as? SettingsActionSheetCell else { return }
cell.tapped(mode: .darkmode) { alertController in
self?.present(alertController, animated: true)
}
let actionSheet = UIAlertController(title: "다크 모드 설정", message: nil, preferredStyle: .actionSheet)
actionSheet.addAction(UIAlertAction(title: "시스템 설정", style: .default, handler: { _ in
self?.viewModel.changeDarkMode(to: 0)
}))
actionSheet.addAction(UIAlertAction(title: "항상 밝게", style: .default, handler: { _ in
self?.viewModel.changeDarkMode(to: 1)
}))
actionSheet.addAction(UIAlertAction(title: "항상 어둡게", style: .default, handler: { _ in
self?.viewModel.changeDarkMode(to: 2)
}))
self?.present(actionSheet, animated: true)
default:
break
}
})
.disposed(by: disposeBag)
}

private func bind() {

}
}

#if canImport(SwiftUI) && DEBUG
Expand Down

0 comments on commit 4580595

Please sign in to comment.