Skip to content

Commit

Permalink
Merge pull request #348 from OMZigak/refactor/#345-my-promise
Browse files Browse the repository at this point in the history
[refactor] 내가 속한 약속 분기 처리 및 자잘한 오류 수정
  • Loading branch information
youz2me authored Aug 30, 2024
2 parents 92d3a35 + ef19462 commit 6389618
Show file tree
Hide file tree
Showing 8 changed files with 123 additions and 116 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ final class AddPromiseCompleteViewController: BaseViewController {
private let disposeBag = DisposeBag()



// MARK: - Initializer

init(promiseID: Int) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ private extension ChooseContentViewController {
button.isSelected = (button.identifier == penalty)
}
})

viewModel.isSuccess.bindOnMain(with: self) { owner, success in
let viewController = AddPromiseCompleteViewController(promiseID: self.viewModel.promiseID)

viewController.setupNavigationBarTitle(with: "약속 수정하기")
viewController.rootView.titleLabel.text = "약속이 수정되었어요!"

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

@objc
Expand All @@ -99,31 +108,6 @@ private extension ChooseContentViewController {

@objc
func confirmButtonDidTap() {
guard let participantList = viewModel.participantList?.value else { return }
let participants = participantList.filter { $0.isParticipant }.map { $0.memberID }

viewModel.putPromiseInfo(
request: EditPromiseRequestModel(
name: viewModel.promiseName?.value ?? "",
placeName: viewModel.placeName?.value ?? "",
address: viewModel.address ?? "",
roadAddress: viewModel.roadAddress ?? "",
time: viewModel.time ?? "",
dressUpLevel: viewModel.dressUpLevel?.value ?? "",
penalty: viewModel.penalty?.value ?? "",
x: viewModel.xCoordinate ?? 0,
y: viewModel.yCoordinate ?? 0,
participants: participants
)
) {
DispatchQueue.main.async {
let viewController = AddPromiseCompleteViewController(promiseID: self.viewModel.promiseID)

viewController.setupNavigationBarTitle(with: "약속 수정하기")
viewController.rootView.titleLabel.text = "약속이 수정되었어요!"

self.navigationController?.pushViewController(viewController, animated: true)
}
}
viewModel.putPromiseInfo()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class ChooseMemberViewController: BaseViewController {

setupBinding()

viewModel.fetchPromiseAvailableMember() {
self.setupSelectInitialCells()
}
viewModel.fetchPromiseAvailableMember()
}

override func viewWillAppear(_ animated: Bool) {
Expand Down Expand Up @@ -82,23 +80,6 @@ private extension ChooseMemberViewController {
})
}

// TODO: 셀 초기 선택 안되는 문제 해결
func setupSelectInitialCells() {
DispatchQueue.main.async {
guard let members = self.viewModel.participantList?.value else { return }

for index in 0 ..< members.count {
let indexPath = IndexPath(item: index, section: 0)

if members[index].isParticipant {
(self.rootView.memberListView.cellForItem(at: indexPath) as? SelectMemberCell)?.isSelected = true
}
}

self.rootView.memberListView.reloadData()
}
}

@objc
func confirmButtonDidTap() {
let viewController = ChooseContentViewController(viewModel: viewModel)
Expand Down Expand Up @@ -139,6 +120,13 @@ extension ChooseMemberViewController: UICollectionViewDataSource {
)
)

if let isParticipant = viewModel.participantList?.value[indexPath.row].isParticipant, isParticipant {
collectionView.selectItem(at: indexPath, animated: false, scrollPosition: [])
} else {
return cell
}


return cell
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ class EditPromiseViewModel {
let participantList: ObservablePattern<[AvailableMember]>?
let dressUpLevel: ObservablePattern<String>?
let penalty: ObservablePattern<String>?
let isSuccess = ObservablePattern<Bool>(false)

var xCoordinate: Double?
var yCoordinate: Double?
Expand Down Expand Up @@ -120,33 +121,49 @@ extension EditPromiseViewModel {
}

extension EditPromiseViewModel {
func putPromiseInfo(request: EditPromiseRequestModel, completion: @escaping () -> Void) {
func putPromiseInfo() {
let participants = participantList?.value.filter { $0.isParticipant }.map { $0.memberID } ?? []

let request = EditPromiseRequestModel(
name: promiseName?.value ?? "",
placeName: placeName?.value ?? "",
address: address ?? "",
roadAddress: roadAddress ?? "",
time: time ?? "",
dressUpLevel: dressUpLevel?.value ?? "",
penalty: penalty?.value ?? "",
x: xCoordinate ?? 0,
y: yCoordinate ?? 0,
participants: participants
)

Task {
do {
let result = try await service.putPromiseInfo(with: promiseID, request: request)

guard let success = result?.success,
success == true else {
success == true
else {
return
}

completion()
isSuccess.value = success
}
}
}

func fetchPromiseAvailableMember(completion: @escaping () -> Void) {
func fetchPromiseAvailableMember() {
Task {
do {
let result = try await service.fetchPromiseAvailableMember(with: promiseID)

guard let success = result?.success, success == true else {
guard let success = result?.success,
success == true
else {
return
}

participantList?.value = result?.data?.members ?? []

completion()
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class PromiseViewController: BaseViewController {
super.viewDidLoad()

setupNavigationBarBackButton()
setupPromiseEditButton()
setupPromiseEditButton(isHidden: false)
setupBindings()
}

Expand Down Expand Up @@ -154,18 +154,19 @@ private extension PromiseViewController {
owner.promiseInfoViewController.setupContent()
owner.promiseInfoViewController.setUpTimeContent()
owner.removePromiseViewContoller.promiseNameLabel.text = info?.promiseName ?? ""
owner.setupPromiseEditButton(isHidden: !(info?.isParticipant ?? false))
}
}

func setupPromiseEditButton() {
func setupPromiseEditButton(isHidden: Bool) {
let moreButton = UIBarButtonItem(
image: .imgMore.withRenderingMode(.alwaysOriginal),
style: .plain,
target: self,
action: #selector(self.moreButtonDidTap)
)
navigationItem.rightBarButtonItem = moreButton

navigationItem.rightBarButtonItem = isHidden ? nil : moreButton
}

@objc
Expand Down Expand Up @@ -193,9 +194,9 @@ private extension PromiseViewController {

@objc
func finishMeetingButtonDidTap() {
promiseTardyViewController.viewModel.updatePromiseCompletion()

navigationController?.popViewController(animated: true)
promiseTardyViewController.viewModel.updatePromiseCompletion {
self.navigationController?.popViewController(animated: true)
}
}

@objc
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class PromiseInfoViewController: BaseViewController {

viewModel.fetchPromiseParticipantList()
viewModel.fetchPromiseInfo()
viewModel.fetchTardyInfo()
}


Expand Down Expand Up @@ -86,22 +87,25 @@ extension PromiseInfoViewController {
}

func setupBinding() {
viewModel.participantsInfo.bind(with: self) { owner, participantsInfo in
DispatchQueue.main.async {
owner.rootView.participantNumberLabel.setText(
"약속 참여 인원 \(participantsInfo?.count ?? 0)",
style: .body05,
color: .maincolor
)

owner.rootView.participantNumberLabel.setHighlightText(
"\(participantsInfo?.count ?? 0)",
style: .body05,
color: .gray3
)

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

owner.rootView.editButton.isHidden = isPastDue
}

viewModel.participantsInfo.bindOnMain(with: self) { owner, participantsInfo in
owner.rootView.participantNumberLabel.setText(
"약속 참여 인원 \(participantsInfo?.count ?? 0)",
style: .body05,
color: .maincolor
)

owner.rootView.participantNumberLabel.setHighlightText(
"\(participantsInfo?.count ?? 0)",
style: .body05,
color: .gray3
)

owner.rootView.participantCollectionView.reloadData()
}
}

Expand Down Expand Up @@ -130,41 +134,41 @@ extension PromiseInfoViewController {
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss"
dateFormatter.locale = Locale(identifier: "ko_KR")
dateFormatter.timeZone = TimeZone(identifier: "Asia/Seoul")
guard let date = dateFormatter.date(from: viewModel.promiseInfo.value?.time ?? "") else {

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

dateFormatter.dateFormat = "M월 d일 a h:mm"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"

let time = dateFormatter.string(from: date)

let currentDate = Calendar.current

let components = currentDate.dateComponents([.day], from: Date(), to: date)

let calendar = Calendar.current

let dateOnly = calendar.startOfDay(for: dateWithTime)
let today = calendar.startOfDay(for: Date())

let components = calendar.dateComponents([.day], from: today, to: dateOnly)
guard let remainDay = components.day else {
return
}

rootView.timeContentLabel.setText(time, style: .body04)


if remainDay == 0 {
rootView.dDayLabel.setText("D-DAY" ,style: .body05, color: .mainorange)
rootView.dDayLabel.setText("D-DAY", style: .body05, color: .mainorange)
rootView.promiseImageView.image = .imgPromise
rootView.promiseNameLabel.textColor = .gray7
}
else if remainDay < 0 {
rootView.dDayLabel.setText("D+\(remainDay)" ,style: .body05, color: .gray4)
} else if remainDay < 0 {
rootView.dDayLabel.setText("D+\(-remainDay)", style: .body05, color: .gray4)
rootView.promiseImageView.image = .imgPromiseGray
rootView.promiseNameLabel.textColor = .gray4
}
else {
rootView.dDayLabel.setText("D-\(remainDay)" ,style: .body05, color: .gray5)
} else {
rootView.dDayLabel.setText("D-\(remainDay)", style: .body05, color: .gray5)
rootView.promiseImageView.image = .imgPromise
rootView.promiseNameLabel.textColor = .gray7
}

dateFormatter.dateFormat = "M월 d일 a h:mm"
dateFormatter.amSymbol = "AM"
dateFormatter.pmSymbol = "PM"
let time = dateFormatter.string(from: dateWithTime)

rootView.timeContentLabel.setText(time, style: .body04)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ class TardyViewController: BaseViewController {

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

viewModel.fetchPromiseParticipantList()
viewModel.fetchPromiseInfo()
viewModel.fetchTardyInfo()
}


Expand All @@ -57,24 +61,18 @@ class TardyViewController: BaseViewController {
private extension TardyViewController {
func setupBinding() {
/// 시간이 지나고 지각자가 없을 때 arriveView로 띄워짐
viewModel.isPastDue.bind(with: self) { owner, isPastDue in
DispatchQueue.main.async {
owner.tardyView.tardyCollectionView.isHidden = !isPastDue
owner.tardyView.tardyEmptyView.isHidden = isPastDue
owner.tardyView.finishMeetingButton.isEnabled = isPastDue
}
viewModel.isPastDue.bindOnMain(with: self) { owner, isPastDue in
owner.tardyView.tardyCollectionView.isHidden = !isPastDue
owner.tardyView.tardyEmptyView.isHidden = isPastDue
owner.tardyView.finishMeetingButton.isEnabled = isPastDue
}

viewModel.penalty.bind(with: self) {
owner,
penalty in
DispatchQueue.main.async {
owner.tardyView.tardyPenaltyView.contentLabel.setText(
penalty,
style: .body03,
color: .gray8
)
}
viewModel.penalty.bindOnMain(with: self) { owner, penalty in
owner.tardyView.tardyPenaltyView.contentLabel.setText(
penalty,
style: .body03,
color: .gray8
)
}

viewModel.hasTardy.bind(with: self) { owner, hasTardy in
Expand All @@ -88,6 +86,20 @@ private extension TardyViewController {
owner.tardyView.tardyCollectionView.reloadData()
}
}

viewModel.promiseInfo.bindOnMain(with: self) { owner, info in
owner.tardyView.finishMeetingButton.isEnabled = (info?.isParticipant ?? false)
}

viewModel.errorMessage.bindOnMain(with: self) { owner, error in
let toast = Toast()
toast.show(
message: error,
view: owner.view,
position: .bottom,
inset: 100
)
}
}
}

Expand Down
Loading

0 comments on commit 6389618

Please sign in to comment.