diff --git a/KkuMulKum/Network/TargetType/HomeTargetType.swift b/KkuMulKum/Network/TargetType/HomeTargetType.swift index 509f8dfc..0a6a4fc5 100644 --- a/KkuMulKum/Network/TargetType/HomeTargetType.swift +++ b/KkuMulKum/Network/TargetType/HomeTargetType.swift @@ -16,6 +16,7 @@ enum HomeTargetType { case updatePreparationStatus(promiseID: Int) case updateDepartureStatus(promiseID: Int) case updateArrivalStatus(promiseID: Int) + case fetchMyReadyStatus(promiseID: Int) } extension HomeTargetType: TargetType { @@ -42,12 +43,14 @@ extension HomeTargetType: TargetType { return "/api/v1/promises/\(promiseID)/departure" case .updateArrivalStatus(let promiseID): return "/api/v1/promises/\(promiseID)/arrival" + case .fetchMyReadyStatus(let promiseID): + return "/api/v1/promises/\(promiseID)/status" } } var method: Moya.Method { switch self { - case .fetchLoginUser, .fetchNearestPromise, .fetchUpcomingPromise: + case .fetchLoginUser, .fetchNearestPromise, .fetchUpcomingPromise, .fetchMyReadyStatus: return .get case .updatePreparationStatus, .updateDepartureStatus, .updateArrivalStatus: return .patch diff --git a/KkuMulKum/Source/Home/ServiceType/HomeServiceType.swift b/KkuMulKum/Source/Home/ServiceType/HomeServiceType.swift index d7a4f77a..1b4360eb 100644 --- a/KkuMulKum/Source/Home/ServiceType/HomeServiceType.swift +++ b/KkuMulKum/Source/Home/ServiceType/HomeServiceType.swift @@ -13,9 +13,36 @@ protocol HomeServiceType { func fetchLoginUser() async throws -> ResponseBodyDTO? func fetchNearestPromise() async throws -> ResponseBodyDTO? func fetchUpcomingPromise() async throws -> ResponseBodyDTO? + func fetchMyReadyStatus(with promiseID: Int) async throws -> ResponseBodyDTO? + + func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO? + func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO? + func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO? } extension HomeService: HomeServiceType { + func fetchMyReadyStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return try await request(with: .fetchMyReadyStatus(promiseID: promiseID)) + } + + func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return try await request( + with: .updatePreparationStatus(promiseID: promiseID) + ) + } + + func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return try await request( + with: .updateDepartureStatus(promiseID: promiseID) + ) + } + + func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return try await request( + with: .updateArrivalStatus(promiseID: promiseID) + ) + } + func fetchLoginUser() async throws -> ResponseBodyDTO? { return try await request(with: .fetchLoginUser) } @@ -30,6 +57,22 @@ extension HomeService: HomeServiceType { } final class MockHomeService: HomeServiceType { + func fetchMyReadyStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return nil + } + + func updatePreparationStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return nil + } + + func updateDepartureStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return nil + } + + func updateArrivalStatus(with promiseID: Int) async throws -> ResponseBodyDTO? { + return nil + } + func fetchLoginUser() async throws -> ResponseBodyDTO? { let mockData = ResponseBodyDTO( success: true, diff --git a/KkuMulKum/Source/Home/ViewController/HomeViewController.swift b/KkuMulKum/Source/Home/ViewController/HomeViewController.swift index 7e707733..212500cd 100644 --- a/KkuMulKum/Source/Home/ViewController/HomeViewController.swift +++ b/KkuMulKum/Source/Home/ViewController/HomeViewController.swift @@ -53,15 +53,15 @@ class HomeViewController: BaseViewController { updateUserInfo() updateNearestPromise() updateUpcomingPromise() - - viewModel.requestLoginUser() - viewModel.requestNearestPromise() - viewModel.requestUpcomingPromise() } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) navigationController?.isNavigationBarHidden = true + + viewModel.requestLoginUser() + viewModel.requestNearestPromise() + viewModel.requestUpcomingPromise() } override func setupAction() { @@ -191,15 +191,17 @@ 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 + DispatchQueue.main.async { + switch state { + case .prepare: + self?.setPrepareUI() + case .move: + self?.setMoveUI() + case .arrive: + self?.setArriveUI() + case .none: + break + } } } } @@ -341,6 +343,10 @@ private extension HomeViewController { rootView.todayPromiseView.moveLabel.isHidden = false rootView.todayPromiseView.prepareLineView.isHidden = false + + rootView.todayPromiseView.prepareTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.preparationStartAt ?? "", style: .caption02, color: .gray8 + ) } func setMoveUI() { @@ -349,6 +355,7 @@ private extension HomeViewController { setProgressButton(rootView.todayPromiseView.moveButton) setEnableButton(rootView.todayPromiseView.arriveButton) + rootView.todayPromiseView.prepareButton.isEnabled = false rootView.todayPromiseView.moveButton.isEnabled = false rootView.todayPromiseView.arriveButton.isEnabled = true @@ -360,7 +367,16 @@ private extension HomeViewController { rootView.todayPromiseView.arriveLabel.isHidden = false rootView.todayPromiseView.prepareCheckView.isHidden = false + + rootView.todayPromiseView.prepareLineView.isHidden = false rootView.todayPromiseView.moveLineView.isHidden = false + + rootView.todayPromiseView.prepareTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.preparationStartAt ?? "", style: .caption02, color: .gray8 + ) + rootView.todayPromiseView.moveTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.departureAt ?? "", style: .caption02, color: .gray8 + ) } func setArriveUI() { @@ -368,6 +384,7 @@ private extension HomeViewController { setCompleteButton(rootView.todayPromiseView.moveButton) setCompleteButton(rootView.todayPromiseView.arriveButton) + rootView.todayPromiseView.prepareButton.isEnabled = false rootView.todayPromiseView.moveButton.isEnabled = false rootView.todayPromiseView.arriveButton.isEnabled = false @@ -379,9 +396,23 @@ private extension HomeViewController { rootView.todayPromiseView.moveLabel.isHidden = true rootView.todayPromiseView.arriveLabel.isHidden = true + rootView.todayPromiseView.prepareCheckView.isHidden = false rootView.todayPromiseView.moveCheckView.isHidden = false rootView.todayPromiseView.arriveCheckView.isHidden = false + + rootView.todayPromiseView.prepareLineView.isHidden = false + rootView.todayPromiseView.moveLineView.isHidden = false rootView.todayPromiseView.arriveLineView.isHidden = false + + rootView.todayPromiseView.prepareTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.preparationStartAt ?? "", style: .caption02, color: .gray8 + ) + rootView.todayPromiseView.moveTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.departureAt ?? "", style: .caption02, color: .gray8 + ) + rootView.todayPromiseView.arriveTimeLabel.setText( + self.viewModel.myReadyStatus.value?.data?.arrivalAt ?? "", style: .caption02, color: .gray8 + ) } @@ -404,31 +435,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() } } diff --git a/KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift b/KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift index aa791aba..23cad8e1 100644 --- a/KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift +++ b/KkuMulKum/Source/Home/ViewModel/HomeViewModel.swift @@ -22,6 +22,7 @@ final class HomeViewModel { var loginUser = ObservablePattern?>(nil) var nearestPromise = ObservablePattern?>(nil) var upcomingPromiseList = ObservablePattern?>(nil) + var myReadyStatus = ObservablePattern?>(nil) var levelName = ObservablePattern("") var levelCaption = ObservablePattern("") @@ -32,33 +33,8 @@ final class HomeViewModel { self.service = service } - var homePrepareTime: String = "" - var homeMoveTime: String = "" - var homeArriveTime: String = "" - - private let dateFormatter = DateFormatter().then { - $0.dateFormat = "a hh:mm" - $0.amSymbol = "AM" - $0.pmSymbol = "PM" - } - - func updateState(newState: ReadyState) { - currentState.value = newState - let currentTimeString = dateFormatter.string(from: Date()) - switch newState { - case .prepare: - homePrepareTime = currentTimeString - case .move: - homeMoveTime = currentTimeString - case .arrive: - homeArriveTime = currentTimeString - case .none: - break - } - } - ///서버에서 보내주는 level Int 값에 따른 levelName - func getLevelName(level: Int) -> String { + private func getLevelName(level: Int) -> String { switch level { case 1: return "빼꼼 꾸물이" case 2: return "밍기적 꾸물이" @@ -69,7 +45,7 @@ final class HomeViewModel { } ///서버에서 보내주는 level Int 값에 따른 levelCaption - func getLevelCaption(level: Int) -> String { + private func getLevelCaption(level: Int) -> String { switch level { case 1: return "꾸물꿈에 오신 것을 환영해요!\n정시 도착으로 캐릭터를 성장시켜 보세요." @@ -84,6 +60,82 @@ final class HomeViewModel { } } + private func judgeReadyStatus() { + let data = myReadyStatus.value?.data + if data?.preparationStartAt == nil && data?.departureAt == nil && data?.arrivalAt == nil { + currentState.value = .none + } else if data?.preparationStartAt != nil && data?.departureAt == nil && data?.arrivalAt == nil { + currentState.value = .prepare + } else if data?.departureAt != nil && data?.arrivalAt == nil { + currentState.value = .move + } else if data?.arrivalAt != nil { + currentState.value = .arrive + } + } + + func requestMyReadyStatus() { + Task { + do { + print(currentState.value) + myReadyStatus.value = try await service.fetchMyReadyStatus( + with: nearestPromise.value?.data?.promiseID ?? 1 + ) + judgeReadyStatus() + print(currentState.value) + } catch { + print(">>> \(error.localizedDescription) : \(#function)") + } + } + } + + func updatePrepareStatus() { + Task { + do { + guard let responseBody = try await service.updatePreparationStatus( + with: nearestPromise.value?.data?.promiseID ?? 1 + ) + else { + return + } + currentState.value = .prepare + } catch { + print(">>> \(error.localizedDescription) : \(#function)") + } + } + } + + func updateMoveStatus() { + Task { + do { + guard let responseBody = try await service.updateDepartureStatus( + with: nearestPromise.value?.data?.promiseID ?? 1 + ) + else { + return + } + currentState.value = .move + } catch { + print(">>> \(error.localizedDescription) : \(#function)") + } + } + } + + func updateArriveStatus() { + Task { + do { + guard let responseBody = try await service.updateArrivalStatus( + with: nearestPromise.value?.data?.promiseID ?? 1 + ) + else { + return + } + currentState.value = .arrive + } catch { + print(">>> \(error.localizedDescription) : \(#function)") + } + } + } + func requestLoginUser() { Task { do { @@ -100,6 +152,7 @@ final class HomeViewModel { Task { do { nearestPromise.value = try await service.fetchNearestPromise() + requestMyReadyStatus() } catch { print(">>> \(error.localizedDescription) : \(#function)") }