Skip to content

Commit

Permalink
Merge pull request #98 from Nexters/feature/#97
Browse files Browse the repository at this point in the history
  • Loading branch information
haeseoklee authored Nov 29, 2024
2 parents 5b1f3cd + 6106636 commit 01ba3b9
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 25 deletions.
36 changes: 29 additions & 7 deletions Projects/Feature/Home/Interface/Sources/Home/HomeFeature.swift
Original file line number Diff line number Diff line change
Expand Up @@ -278,12 +278,26 @@ public struct HomeFeature {
switch action {
case .dismiss:
return .none
case .presented(.imageUpload(.delegate(.didFinishImageUpload))):

case .presented(.imageUpload(.delegate(.didFinishImageUpload(.success)))):
guard let myPiece = state.competition?.myPiece else { return .none }
state.movingPiece = myPiece
state.competition?.board.remove(piece: myPiece)
return .none

case .presented(.imageUpload(.delegate(.didFinishImageUpload(.failure)))):
guard let mission = state.mission else { return .none }
state.ctaButtonState = makeCTAButtonState(isMeCertificated: state.competition?.isMeVerified == true, mission: mission)
return .none

case .presented(.imageUpload(.delegate(.didStartImageUpload))):
state.ctaButtonState = CTAButtonState(
info: state.ctaButtonState.info,
title: "",
status: .loading
)
return .none

case .presented(.verificationResult(.delegate(.didTapCloseButton))):
return .send(.loadData(missionId: state.missionId ?? 0))

Expand Down Expand Up @@ -369,28 +383,36 @@ private extension HomeFeature {
public extension HomeFeature {

struct CTAButtonState {
static let `default`: Self = .init(isEnabled: false, info: "", title: "")
let isEnabled: Bool
enum Status {
case enabled
case disabled
case loading
var isEnabled: Bool { self == .enabled }
var isDisabled: Bool { self == .disabled }
var isLoading: Bool { self == .loading }
}
static let `default`: Self = .init(info: "", title: "", status: .enabled)
let info: String
let title: String
let status: Status
}

func makeCTAButtonState(isMeCertificated: Bool, mission: Mission) -> CTAButtonState {
let info = "\(mission.sortedVerificationWeekDays.map { $0.toKorean }.joined(separator: " ")) | \(mission.missionTimeDescription)"
switch isMeCertificated {
case true:
return .init(
isEnabled: false,
info: info,
title: "오늘 미션 인증 완료!"
title: "오늘 미션 인증 완료!",
status: .disabled
)
case false:
return .init(
isEnabled: mission.checkIsMissionTime,
info: info,
title: mission.checkIsMissionDay
? (mission.checkIsMissionTime ? "오늘 미션 인증하기" : "오늘 미션 인증 시간 마감")
: "오늘은 미션일이 아니에요"
: "오늘은 미션일이 아니에요",
status: mission.checkIsMissionTime ? .enabled : .disabled
)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,32 @@ struct HomeBottomView: View {
.padding(.top, 16)
.padding(.bottom, 6)

PhotoPickerView(selectedImages: $store.selectedImages.sending(\.didSelectImages), maxSelectedCount: 1) {
PhotoPickerView(
selectedImages: $store.selectedImages.sending(\.didSelectImages),
maxSelectedCount: 1
) {
Text(store.ctaButtonState.title)
.font(.pretendard(kind: .body_lg, type: .bold))
.foregroundColor(SharedDesignSystemAsset.Colors.white.swiftUIColor)
.frame(height: 60)
.frame(maxWidth: .infinity)
.background(
store.ctaButtonState.isEnabled
store.ctaButtonState.status.isEnabled
? SharedDesignSystemAsset.Colors.orange.swiftUIColor
: SharedDesignSystemAsset.Colors.disabled.swiftUIColor
)
.cornerRadius(30)
.overlay {
VStack(alignment: .center, spacing: 0) {
ProgressView()
.progressViewStyle(CircularProgressViewStyle())
.isHidden(!store.ctaButtonState.status.isLoading, remove: true)
}
}
}
.padding(.horizontal, HomeView.Constant.horizontalPadding)
.padding(.bottom, 36)
.disabled(!store.ctaButtonState.isEnabled)
.disabled(store.ctaButtonState.status.isDisabled)
}
.background(.ultraThinMaterial)
.cornerRadius(20, corners: [.topLeft, .topRight])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public struct ImageUploadFeature {
public let updatedDate: Date
public let selectedImage: UIImage
public var isLoading: Bool = false
public var isButtonDisabled: Bool = false

public var formatedDate: String {
DateFormatter.yearMonthDayFormatter.string(from: updatedDate)
Expand All @@ -46,41 +47,47 @@ public struct ImageUploadFeature {
}

public enum Delegate {
case didFinishImageUpload
case didStartImageUpload
case didFinishImageUpload(Result<Void, Error>)
}

public var body: some ReducerOf<Self> {
Reduce { state, action in
switch action {
case .didTapUploadButton:
state.isLoading = true
return .run { [
missionId = state.missionId,
selectedImage = state.selectedImage
] send in
await send(.didFinishImageUpload(
Result {
if let data = selectedImage.jpegData(compressionQuality: 0.5) {
return try await verificationService.postVerificationsMe(missionId, data)
state.isButtonDisabled = true
return .concatenate(
.send(.delegate(.didStartImageUpload)),
.run { [
missionId = state.missionId,
selectedImage = state.selectedImage
] send in
await send(.didFinishImageUpload(
Result {
if let data = selectedImage.jpegData(compressionQuality: 0.5) {
return try await verificationService.postVerificationsMe(missionId, data)
}
}
}
))
}
))
}
)
case .didTapCloseButton:
return .run { _ in
await self.dismiss()
}
case .didFinishImageUpload(.success):
state.isLoading = false
return .concatenate(
.send(.delegate(.didFinishImageUpload)),
.send(.delegate(.didFinishImageUpload(.success(())))),
.run { _ in
await self.dismiss()
}
)
case .didFinishImageUpload(.failure):
state.isLoading = false
return .none
state.isButtonDisabled = false
return .send(.delegate(.didFinishImageUpload(.failure(NSError()))))
case .delegate:
return .none
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,11 +77,16 @@ public struct ImageUploadView: View {
.foregroundColor(SharedDesignSystemAsset.Colors.white.swiftUIColor)
.frame(height: 60)
.frame(maxWidth: .infinity)
.background(SharedDesignSystemAsset.Colors.orange.swiftUIColor)
.background(
store.state.isButtonDisabled
? SharedDesignSystemAsset.Colors.disabled.swiftUIColor
: SharedDesignSystemAsset.Colors.orange.swiftUIColor
)
.cornerRadius(30)
}
.padding(.horizontal, 24)
.padding(.bottom, safeAreaInsets.bottom)
.disabled(store.state.isButtonDisabled)
}
.background(SharedDesignSystemAsset.Colors.white.swiftUIColor)
}
Expand Down

0 comments on commit 01ba3b9

Please sign in to comment.