Skip to content

Commit

Permalink
refactor/#388 약속 정보 ViewController 재구현
Browse files Browse the repository at this point in the history
  • Loading branch information
youz2me committed Sep 15, 2024
1 parent d7e8821 commit b566408
Show file tree
Hide file tree
Showing 3 changed files with 134 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class PromiseInfoView: BaseView {
$0.layer.cornerRadius = Screen.height(18)
}

private let locationInfoLabel: UILabel = UILabel().then {
let locationInfoLabel: UILabel = UILabel().then {
$0.setText("위치", style: .body05, color: .maincolor)
}

Expand All @@ -77,7 +77,7 @@ class PromiseInfoView: BaseView {
$0.layer.cornerRadius = Screen.height(8)
}

private let timeInfoLabel: UILabel = UILabel().then {
let timeInfoLabel: UILabel = UILabel().then {
$0.setText("약속시간", style: .body05, color: .maincolor)
}

Expand All @@ -86,7 +86,7 @@ class PromiseInfoView: BaseView {
$0.layer.cornerRadius = Screen.height(8)
}

private let readyLevelInfoLabel: UILabel = UILabel().then {
let readyLevelInfoLabel: UILabel = UILabel().then {
$0.setText("꾸레벨", style: .body05, color: .maincolor)
}

Expand All @@ -95,7 +95,7 @@ class PromiseInfoView: BaseView {
$0.layer.cornerRadius = Screen.height(8)
}

private let penaltyLevelInfoLabel: UILabel = UILabel().then {
let penaltyLevelInfoLabel: UILabel = UILabel().then {
$0.setText("벌칙", style: .body05, color: .maincolor)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,9 @@ class PromiseInfoViewController: BaseViewController {

override func viewWillAppear(_ animated: Bool) {
super.viewWillAppear(animated)

viewModel.fetchPromiseInfo()
viewModel.fetchPromiseParticipantList()
}


Expand Down Expand Up @@ -70,12 +73,77 @@ class PromiseInfoViewController: BaseViewController {

extension PromiseInfoViewController {
func setupBinding() {
viewModel.promiseInfo.bindOnMain(with: self) { owner, info in
guard let info else { return }

owner.rootView.editButton.isHidden = !(info.isParticipant)

owner.rootView.promiseNameLabel.setText(info.promiseName, style: .body01)
owner.rootView.locationContentLabel.setText(info.placeName, style: .body04)
owner.rootView.readyLevelContentLabel.setText(info.dressUpLevel, style: .body04)
owner.rootView.penaltyLevelContentLabel.setText(info.penalty, style: .body04)
owner.rootView.timeContentLabel.setText(info.time, style: .body04)
}

viewModel.dDay.bindOnMain(with: self) { owner, dDay in
guard let dDay else { return }

switch dDay {
case 1...:
owner.rootView.dDayLabel.setText("D+\(-dDay)", style: .body05, color: .gray4)
owner.rootView.promiseImageView.image = .imgPromiseGray
owner.rootView.promiseNameLabel.textColor = .gray4
owner.rootView.locationInfoLabel.textColor = .gray4
owner.rootView.timeInfoLabel.textColor = .gray4
owner.rootView.readyLevelInfoLabel.textColor = .gray4
owner.rootView.penaltyLevelInfoLabel.textColor = .gray4
case 0:
owner.rootView.dDayLabel.setText("D-DAY", style: .body05, color: .mainorange)
case ..<0:
owner.rootView.dDayLabel.setText("D-\(dDay)", style: .body05, color: .gray5)
default:
break
}
}

viewModel.participants.bindOnMain(with: self) { owner, _ in
owner.rootView.participantCollectionView.reloadData()
}
}

@objc
func editButtonDidTap() {
guard var dressUpLevel = viewModel.promiseInfo.value?.dressUpLevel else { return }

let levels = ["LV1", "LV2", "LV3", "LV4", "FREE"]

if dressUpLevel.contains("마음대로 입고 오기") {
dressUpLevel = "FREE"
} else {
if let matched = levels.first(where: { level in
dressUpLevel.replacingOccurrences(of: " ", with: "").contains(level)
}) {
dressUpLevel = matched
}
}

let viewController = EditPromiseViewController(
viewModel: EditPromiseViewModel(
promiseID: viewModel.promiseID,
promiseName: viewModel.promiseInfo.value?.promiseName,
placeName: viewModel.promiseInfo.value?.placeName,
xCoordinate: viewModel.promiseInfo.value?.x,
yCoordinate: viewModel.promiseInfo.value?.y,
address: viewModel.promiseInfo.value?.address,
roadAddress: viewModel.promiseInfo.value?.roadAddress,
time: viewModel.promiseInfo.value?.time,
dressUpLevel: dressUpLevel,
penalty: viewModel.promiseInfo.value?.penalty,
service: PromiseService()
)
)

navigationController?.pushViewController(viewController, animated: true)
}
}

Expand All @@ -87,7 +155,7 @@ extension PromiseInfoViewController: UICollectionViewDataSource {
_ collectionView: UICollectionView,
numberOfItemsInSection section: Int
) -> Int {
return 0
return viewModel.participants.value.count
}

func collectionView(
Expand All @@ -101,6 +169,14 @@ extension PromiseInfoViewController: UICollectionViewDataSource {
return UICollectionViewCell()
}

let info = viewModel.participants.value[indexPath.row]

cell.userNameLabel.setText(info.name, style: .caption02, color: .gray6)
cell.profileImageView.kf.setImage(
with: URL(string: info.profileImageURL ?? ""),
placeholder: UIImage.imgProfile
)

return cell
}
}
Expand Down
71 changes: 53 additions & 18 deletions KkuMulKum/Source/Promise/ViewModel/PromiseViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class PromiseViewModel {

let promiseID: Int
let promiseInfo = ObservablePattern<PromiseInfoModel?>(nil)
let participants = ObservablePattern<[Participant]>([])
let dDay = ObservablePattern<Int?>(nil)

private let service: PromiseServiceProtocol

Expand All @@ -29,6 +31,24 @@ class PromiseViewModel {

// MARK: - Extension

private extension PromiseViewModel {
func calculateDday() {
let calendar = Calendar.current
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")

guard let dateWithTime = dateFormatter.date(from: promiseInfo.value?.time ?? "") else { return }

let dateOnly = calendar.startOfDay(for: dateWithTime)
let today = calendar.startOfDay(for: Date())
let components = calendar.dateComponents([.day], from: today, to: dateOnly)

dDay.value = components.day
}
}

extension PromiseViewModel {
/// 약속 상세 정보 조회 API 구현 함수
func fetchPromiseInfo() {
Expand All @@ -40,30 +60,37 @@ extension PromiseViewModel {
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")

return
}

promiseInfo.value = result?.data

calculateDday()
} catch {
print(">>>>> \(error.localizedDescription) : \(#function)")
}


}
}

/// 약속 참여자 목록 API 구현 함수
func fetchPromiseParticipantList() {
Task {
do {
let responseBody = try await service.fetchPromiseParticipantList(with: promiseID)
let result = try await service.fetchPromiseParticipantList(with: promiseID)

guard let success = responseBody?.success,
success == true
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}

guard let data = result?.data else {
print(">>>>> \("데이터 없음") : \(#function)")
return
}

participants.value = data.participants
} catch {
print(">>>>> \(error.localizedDescription) : \(#function)")
}
Expand All @@ -74,11 +101,12 @@ extension PromiseViewModel {
func fetchMyReadyStatus() {
Task {
do {
let responseBody = try await service.fetchMyReadyStatus(with: promiseID)
let result = try await service.fetchMyReadyStatus(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
} catch {
Expand All @@ -91,11 +119,12 @@ extension PromiseViewModel {
func updatePreparationStatus() {
Task {
do {
let responseBody = try await service.updatePreparationStatus(with: promiseID)
let result = try await service.updatePreparationStatus(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
}
Expand All @@ -109,11 +138,12 @@ extension PromiseViewModel {
func updateDepartureStatus() {
Task {
do {
let responseBody = try await service.updateDepartureStatus(with: promiseID)
let result = try await service.updateDepartureStatus(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
}
Expand All @@ -127,11 +157,12 @@ extension PromiseViewModel {
func updateArrivalStatus() {
Task {
do {
let responseBody = try await service.updateArrivalStatus(with: promiseID)
let result = try await service.updateArrivalStatus(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
}
Expand All @@ -145,11 +176,12 @@ extension PromiseViewModel {
func fetchTardyInfo() {
Task {
do {
let responseBody = try await service.fetchTardyInfo(with: promiseID)
let result = try await service.fetchTardyInfo(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
} catch {
Expand All @@ -162,11 +194,12 @@ extension PromiseViewModel {
func updatePromiseCompletion() {
Task {
do {
let responseBody = try await service.updatePromiseCompletion(with: promiseID)
let result = try await service.updatePromiseCompletion(with: promiseID)

guard let success = responseBody?.success,
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
} catch {
Expand All @@ -184,6 +217,7 @@ extension PromiseViewModel {
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
} catch {
Expand All @@ -201,6 +235,7 @@ extension PromiseViewModel {
guard let success = result?.success,
success == true
else {
print(">>>>> \(String(describing: result)) : \(#function)")
return
}
} catch {
Expand Down

0 comments on commit b566408

Please sign in to comment.