Skip to content

Commit

Permalink
feat: 러닝 목표 설정 페이지 완성
Browse files Browse the repository at this point in the history
  • Loading branch information
guesswb committed Jul 5, 2023
1 parent 2e9d722 commit 82804cf
Show file tree
Hide file tree
Showing 10 changed files with 159 additions and 166 deletions.
3 changes: 3 additions & 0 deletions RunningCrew/Sources/Presenter/View/Base/BaseView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@
//

import UIKit
import RxSwift

class BaseView: UIView {

var disposeBag = DisposeBag()

override init(frame: CGRect) {
super.init(frame: frame)
setupUI()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ extension RecordRunningCoordinator {

extension RecordRunningCoordinator: RecordRunningViewControllerDelegate {
func showIndividualView() {
let runningStartVC: RunningStartViewController = RunningStartViewController(viewModel: RunningStartViewModel(viewTitle: "개인러닝"))
let runningStartVC: RunningStartViewController = RunningStartViewController(viewModel: RunningStartViewModel())
runningStartVC.delegate = self
self.navigationController.pushViewController(runningStartVC, animated: true)
}
Expand All @@ -54,8 +54,8 @@ extension RecordRunningCoordinator: RecordRunningViewControllerDelegate {
}

extension RecordRunningCoordinator: RunningStartViewControllerDelegate {
func showGoalSettingView(goalType: GoalType, viewModel: RunningStartViewModel) {
let goalSettingVC = GoalSettingViewController(goalType: goalType, viewModel: viewModel)
func showGoalSettingView(viewModel: RunningStartViewModel) {
let goalSettingVC = GoalSettingViewController(viewModel: viewModel)
goalSettingVC.delegate = self
self.navigationController.pushViewController(goalSettingVC, animated: false)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,10 @@ protocol GoalSettingViewDelegate: AnyObject {
class GoalSettingViewController: BaseViewController {

weak var delegate: GoalSettingViewDelegate?

let goalType: GoalType

let viewModel: RunningStartViewModel

init(goalType: GoalType, viewModel: RunningStartViewModel) {
self.goalType = goalType
init(viewModel: RunningStartViewModel) {
self.viewModel = viewModel
super.init(nibName: nil, bundle: nil)
}
Expand All @@ -39,13 +37,13 @@ class GoalSettingViewController: BaseViewController {
}

lazy var goalLabelBindingTextField: GoalTextField = {
let textField = GoalTextField(goalType: goalType)
let textField = GoalTextField(goalType: viewModel.goalType)

return textField
}()

lazy var goalLabel: GoalSettingStackView = {
let goalLabel = GoalSettingStackView(goalType: goalType)
let goalLabel = GoalSettingStackView(goalType: viewModel.goalType)

return goalLabel
}()
Expand Down Expand Up @@ -79,77 +77,53 @@ class GoalSettingViewController: BaseViewController {
}

override func bind() {
let input = RunningStartViewModel.Input(
nextButtonDidTap: goalLabel.nextButton.rx.tap.asObservable(),
beforeButtonDidTap: goalLabel.beforeButton.rx.tap.asObservable(),
navigationRightButtonDidTap: navigationItem.rightBarButtonItem?.rx.tap
.map { self.goalLabel.goalSettingLabelStackView.destinationLabel.text ?? "" })

let output = viewModel.transform(input: input)

output.goalText
.drive(goalLabel.goalSettingLabelStackView.destinationLabel.rx.text)
.disposed(by: disposeBag)

goalLabelBindingTextField.rx.text.orEmpty
.map { [weak self] input -> String? in
guard let self = self else { return "" }

switch self.goalType {
switch self.viewModel.goalType.value {
case .distance:
if input.isEmpty {
return "0"
return "0.00"
} else if input.count <= 2 {
return input + ".00"
} else {
goalLabelBindingTextField.text = String(input.prefix(2))
return (goalLabelBindingTextField.text ?? "0") + ".00"
}
if input == "0" {
self.goalLabelBindingTextField.text?.removeLast()
return "0"
}
return input
case .time:
if input.isEmpty {
return "00:00"
} else if input.count <= 4 {
let string = String(format: "%.4d", Int(input) ?? 0)
return String(string.prefix(2)) + ":" + String(string.suffix(2))
} else {
goalLabelBindingTextField.text = String(input.prefix(4))
return (goalLabelBindingTextField.text ?? "00").prefix(2) + ":" + (goalLabelBindingTextField.text ?? "00").suffix(2)
}
let len = input.count
let paddedStr = String(repeating: "0", count: 4 - len) + input
let index1 = paddedStr.index(paddedStr.startIndex, offsetBy: 2)
let index2 = paddedStr.index(paddedStr.startIndex, offsetBy: 4)
let result = "\(paddedStr[..<index1]):\(paddedStr[index1..<index2])"

return result
}
}
.bind(to: self.goalLabel.goalSettingLabelStackView.destinationLabel.rx.text)
.disposed(by: disposeBag)


goalLabelBindingTextField.rx.text.orEmpty
.asDriver()
.map({ input in
if input.contains(".") || input.contains(":") {
return input.count <= 5
} else {
return input.count <= 4
}
})
.drive { [weak self] in
if !$0 {
self?.goalLabelBindingTextField.text = String(self?.goalLabelBindingTextField.text?.dropLast() ?? "")
}
}.disposed(by: disposeBag)

navigationItem.leftBarButtonItem?.rx.tap
.bind { self.delegate?.tapCancleButton() }
.disposed(by: disposeBag)

navigationItem.rightBarButtonItem?.rx.tap
.bind { [weak self] _ in
guard let self = self else { return }

switch self.goalType {
case .distance:
viewModel.goalDistance.accept(Float(goalLabelBindingTextField.text ?? "0") ?? 0)
case .time:
let time = (goalLabelBindingTextField.text ?? "00:00").split(separator: ":").map {Int($0)}
viewModel.goalHour.accept(time[0] ?? 0)
viewModel.goalMinute.accept(time[1] ?? 0)
}

self.delegate?.tapSettingButton()
}
.bind { self.delegate?.tapSettingButton() }
.disposed(by: disposeBag)
}
}






Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
import UIKit
import RxSwift

class RecordViewController: UIViewController {
class RecordViewController: BaseViewController {

@IBOutlet weak var runningMeasuringView: UIView!
@IBOutlet weak var readyDiscussionLabel: UILabel!
Expand All @@ -23,7 +23,6 @@ class RecordViewController: UIViewController {
private var timer: Timer?
private var readyTimerNum = 5
var viewModel: RecordViewModel?
let disposeBag = DisposeBag()

override func viewDidLoad() {
super.viewDidLoad()
Expand All @@ -45,6 +44,15 @@ class RecordViewController: UIViewController {
fatalError("init(coder:) has not been implemented")
}

//MARK: - deinit
deinit {
timer?.invalidate()
timer = nil
viewModel?.deinitViewModel()
print("deinit record viewcontroller")
}


//MARK: - Ready Time Method
private func startReadyTimer() {
self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(readyTimerCallBack), userInfo: nil, repeats: true)
Expand Down Expand Up @@ -78,14 +86,6 @@ class RecordViewController: UIViewController {
return .lightContent
}

//MARK: - bind
private func bind() {
viewModel?.timerText.asDriver()
.drive(runningTimerLabel.rx
.text)
.disposed(by: disposeBag)
}

//MARK: - Action Method

@IBAction func tapPauseOrPlayButton(_ sender: Any) {
Expand Down Expand Up @@ -144,14 +144,12 @@ class RecordViewController: UIViewController {
completeButtonRingLayer.strokeEnd = 0
}

//MARK: - CompleteButton Action Method


//MARK: - deinit
deinit {
timer?.invalidate()
timer = nil
viewModel?.deinitViewModel()
print("deinit record viewcontroller")

//MARK: - bind
private func bind() {
viewModel?.timerText.asDriver()
.drive(runningTimerLabel.rx
.text)
.disposed(by: disposeBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import RxSwift
import RxGesture

protocol RunningStartViewControllerDelegate: AnyObject {
func showGoalSettingView(goalType: GoalType, viewModel: RunningStartViewModel)
func showGoalSettingView(viewModel: RunningStartViewModel)
func showRecordView()
}

Expand Down Expand Up @@ -63,22 +63,13 @@ class RunningStartViewController: BaseViewController {
return stackView
}()

lazy var distanceSettingStackView: GoalSettingStackView = {
let distanceSettingStackView = GoalSettingStackView(goalType: .distance)
distanceSettingStackView.translatesAutoresizingMaskIntoConstraints = false
distanceSettingStackView.goalSettingLabelStackView.isUserInteractionEnabled = true
distanceSettingStackView.beforeButton.isHidden = true
lazy var goalSettingStackView: GoalSettingStackView = {
let goalSettingStackView = GoalSettingStackView(goalType: viewModel.goalType)
goalSettingStackView.translatesAutoresizingMaskIntoConstraints = false
goalSettingStackView.goalSettingLabelStackView.isUserInteractionEnabled = true
goalSettingStackView.beforeButton.isHidden = true

return distanceSettingStackView
}()

lazy var timeSettingStackView: GoalSettingStackView = {
let timeSettingStackView = GoalSettingStackView(goalType: .time)
timeSettingStackView.translatesAutoresizingMaskIntoConstraints = false
timeSettingStackView.goalSettingLabelStackView.isUserInteractionEnabled = true
timeSettingStackView.nextButton.isHidden = true

return timeSettingStackView
return goalSettingStackView
}()

lazy var startButton: UIButton = {
Expand All @@ -95,17 +86,14 @@ class RunningStartViewController: BaseViewController {
print("deinit runningStart view")
}


override func setView() {
timeSettingStackView.isHidden = true
self.view.backgroundColor = .systemBackground
}

override func setAddView() {
view.addSubview(mapView)
view.addSubview(startButtonStackView)
startButtonStackView.addArrangedSubview(distanceSettingStackView)
startButtonStackView.addArrangedSubview(timeSettingStackView)
startButtonStackView.addArrangedSubview(goalSettingStackView)
startButtonStackView.addArrangedSubview(startButton)
}

Expand All @@ -128,44 +116,24 @@ class RunningStartViewController: BaseViewController {
//MARK: - Method

override func bind() {
let input = RunningStartViewModel.Input(
nextButtonDidTap: goalSettingStackView.nextButton.rx.tap.asObservable(),
beforeButtonDidTap: goalSettingStackView.beforeButton.rx.tap.asObservable(),
navigationRightButtonDidTap: nil)

distanceSettingStackView.goalSettingLabelStackView.destinationLabel.rx.tapGesture()
.when(.recognized)
.bind { _ in self.delegate?.showGoalSettingView(goalType: .distance, viewModel: self.viewModel) }
.disposed(by: disposeBag)

timeSettingStackView.goalSettingLabelStackView.destinationLabel.rx.tapGesture()
.when(.recognized)
.bind { _ in self.delegate?.showGoalSettingView(goalType: .time, viewModel: self.viewModel) }
.disposed(by: disposeBag)
let output = viewModel.transform(input: input)

distanceSettingStackView.nextButton.rx.tap
.bind { _ in
self.distanceSettingStackView.isHidden = true
self.timeSettingStackView.isHidden = false
}
output.goalText
.drive(goalSettingStackView.goalSettingLabelStackView.destinationLabel.rx.text)
.disposed(by: disposeBag)

timeSettingStackView.beforeButton.rx.tap
.bind { _ in
self.distanceSettingStackView.isHidden = false
self.timeSettingStackView.isHidden = true
}
goalSettingStackView.goalSettingLabelStackView.destinationLabel.rx.tapGesture()
.when(.recognized)
.bind { _ in self.delegate?.showGoalSettingView(viewModel: self.viewModel) }
.disposed(by: disposeBag)

startButton.rx.tap
.bind { _ in
self.delegate?.showRecordView()
}
.disposed(by: disposeBag)

viewModel.goalDistance.asDriver()
.map({String(format: "%.2f", $0)})
.drive(distanceSettingStackView.goalSettingLabelStackView.destinationLabel.rx.text)
.disposed(by: disposeBag)

viewModel.goalTimeRelay.asDriver()
.drive(timeSettingStackView.goalSettingLabelStackView.destinationLabel.rx.text)
.bind { self.delegate?.showRecordView() }
.disposed(by: disposeBag)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GoalSettingLabelStackView: UIStackView {
lazy var underLineView: UIView = {
let view = UIView()
view.translatesAutoresizingMaskIntoConstraints = false
view.backgroundColor = .black
view.backgroundColor = .darkModeBasicColor

return view
}()
Expand Down
Loading

0 comments on commit 82804cf

Please sign in to comment.