Skip to content

Commit

Permalink
feat/#241 준비/이동/도착 시간 업데이트 API 연결
Browse files Browse the repository at this point in the history
  • Loading branch information
mmaybei committed Jul 18, 2024
1 parent 885a458 commit 81f9369
Show file tree
Hide file tree
Showing 4 changed files with 130 additions and 35 deletions.
34 changes: 34 additions & 0 deletions KkuMulKum/Source/Home/ServiceType/HomeServiceType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,31 @@ protocol HomeServiceType {
func fetchLoginUser() async throws -> ResponseBodyDTO<LoginUserModel>?
func fetchNearestPromise() async throws -> ResponseBodyDTO<NearestPromiseModel>?
func fetchUpcomingPromise() async throws -> ResponseBodyDTO<UpcomingPromiseListModel>?

func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>?
func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>?
func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>?
}

extension HomeService: HomeServiceType {
func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return try await request(
with: .updatePreparationStatus(promiseID: promiseID)
)
}

func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return try await request(
with: .updateDepartureStatus(promiseID: promiseID)
)
}

func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return try await request(
with: .updateArrivalStatus(promiseID: promiseID)
)
}

func fetchLoginUser() async throws -> ResponseBodyDTO<LoginUserModel>? {
return try await request(with: .fetchLoginUser)
}
Expand All @@ -30,6 +52,18 @@ extension HomeService: HomeServiceType {
}

final class MockHomeService: HomeServiceType {
func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return nil
}

func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return nil
}

func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO<EmptyModel>? {
return nil
}

func fetchLoginUser() async throws -> ResponseBodyDTO<LoginUserModel>? {
let mockData = ResponseBodyDTO<LoginUserModel>(
success: true,
Expand Down
73 changes: 42 additions & 31 deletions KkuMulKum/Source/Home/ViewController/HomeViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ class HomeViewController: BaseViewController {
view.backgroundColor = .maincolor
register()

updateUI()

updateUserInfo()
updateNearestPromise()
updateUpcomingPromise()

viewModel.requestLoginUser()
viewModel.requestNearestPromise()
viewModel.requestUpcomingPromise()

bindViewModel()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -188,17 +188,43 @@ private extension HomeViewController {
)
}

func updateUI() {
viewModel.currentState.bind { [weak self] state in
switch state {
case .prepare:
self?.setPrepareUI()
case .move:
self?.setMoveUI()
case .arrive:
self?.setArriveUI()
case .none:
break
func bindViewModel() {
viewModel.isPreapreSucceedToSave.bind { [weak self] _ in
if self?.viewModel.isPreapreSucceedToSave.value == true {
DispatchQueue.main.async {
self?.setPrepareUI()
self?.rootView.todayPromiseView.prepareTimeLabel.setText(
self?.viewModel.homePrepareTime ?? "",
style: .caption02,
color: .gray8
)
}
}
}

viewModel.isMoveSucceedToSave.bind { [weak self] _ in
if self?.viewModel.isMoveSucceedToSave.value == true {
DispatchQueue.main.async {
self?.setMoveUI()
self?.rootView.todayPromiseView.moveTimeLabel.setText(
self?.viewModel.homeMoveTime ?? "",
style: .caption02,
color: .gray8
)
}
}
}

viewModel.isArriveSucceedToSave.bind { [weak self] _ in
if self?.viewModel.isArriveSucceedToSave.value == true {
DispatchQueue.main.async {
self?.setArriveUI()
self?.rootView.todayPromiseView.arriveTimeLabel.setText(
self?.viewModel.homeArriveTime ?? "",
style: .caption02,
color: .gray8
)
}
}
}
}
Expand Down Expand Up @@ -402,31 +428,16 @@ private extension HomeViewController {

@objc
func prepareButtonDidTap(_ sender: UIButton) {
viewModel.updateState(newState: .prepare)
rootView.todayPromiseView.prepareTimeLabel.setText(
viewModel.homePrepareTime,
style: .caption02,
color: .gray8
)
viewModel.updatePrepareStatus()
}

@objc
func moveButtonDidTap(_ sender: UIButton) {
viewModel.updateState(newState: .move)
rootView.todayPromiseView.moveTimeLabel.setText(
viewModel.homeMoveTime,
style: .caption02,
color: .gray8
)
viewModel.updateMoveStatus()
}

@objc
func arriveButtonDidTap(_ sender: UIButton) {
viewModel.updateState(newState: .arrive)
rootView.todayPromiseView.arriveTimeLabel.setText(
viewModel.homeArriveTime,
style: .caption02,
color: .gray8
)
viewModel.updateArriveStatus()
}
}
57 changes: 54 additions & 3 deletions KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ enum ReadyState {
}

final class HomeViewModel {
var currentState = ObservablePattern<ReadyState>(.none)
var promiseID = ObservablePattern<Int>?(nil)

var loginUser = ObservablePattern<ResponseBodyDTO<LoginUserModel>?>(nil)
var nearestPromise = ObservablePattern<ResponseBodyDTO<NearestPromiseModel>?>(nil)
Expand All @@ -26,6 +26,10 @@ final class HomeViewModel {
var levelName = ObservablePattern<String>("")
var levelCaption = ObservablePattern<String>("")

var isPreapreSucceedToSave = ObservablePattern<Bool>(false)
var isMoveSucceedToSave = ObservablePattern<Bool>(false)
var isArriveSucceedToSave = ObservablePattern<Bool>(false)

private let service: HomeServiceType

init(service: HomeServiceType) {
Expand All @@ -42,8 +46,7 @@ final class HomeViewModel {
$0.pmSymbol = "PM"
}

func updateState(newState: ReadyState) {
currentState.value = newState
private func updateState(newState: ReadyState) {
let currentTimeString = dateFormatter.string(from: Date())
switch newState {
case .prepare:
Expand Down Expand Up @@ -84,6 +87,54 @@ final class HomeViewModel {
}
}

func updatePrepareStatus() {
Task {
do {
guard let responseBody = try await service.updatePreparationStatus(with: nearestPromise.value?.data?.promiseID ?? 1)
else {
isPreapreSucceedToSave.value = false
return
}
isPreapreSucceedToSave.value = responseBody.success
updateState(newState: .prepare)
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
}
}

func updateMoveStatus() {
Task {
do {
guard let responseBody = try await service.updateDepartureStatus(with: nearestPromise.value?.data?.promiseID ?? 1)
else {
isMoveSucceedToSave.value = false
return
}
isMoveSucceedToSave.value = responseBody.success
updateState(newState: .move)
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
}
}

func updateArriveStatus() {
Task {
do {
guard let responseBody = try await service.updateArrivalStatus(with: nearestPromise.value?.data?.promiseID ?? 1)
else {
isArriveSucceedToSave.value = false
return
}
isArriveSucceedToSave.value = responseBody.success
updateState(newState: .arrive)
} catch {
print(">>> \(error.localizedDescription) : \(#function)")
}
}
}

func requestLoginUser() {
Task {
do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ final class SetReadyInfoViewModel {
let moveMinute = ObservablePattern<String>("")
let isSucceedToSave = ObservablePattern<Bool>(false)

// TODO: 준비 및 이동 시간 분 단위로 계산
var readyTime: Int = 0
var moveTime: Int = 0

Expand Down

0 comments on commit 81f9369

Please sign in to comment.