Skip to content

Commit

Permalink
Removed state
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-nirali-s committed Jan 3, 2025
1 parent 8edb518 commit 1074f4a
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 47 deletions.
52 changes: 24 additions & 28 deletions Splito/UI/Home/Account/Feedback/FeedbackView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,35 +18,31 @@ struct FeedbackView: View {

var body: some View {
VStack(spacing: 0) {
if case .loading = viewModel.currentState {
LoaderView()
} else {
ScrollView(showsIndicators: false) {
VSpacer(24)

VStack(spacing: 24) {
FeedbackTitleView(titleText: $viewModel.title, focusField: $focusField,
isSelected: focusField == .title, isValidTitle: viewModel.isValidTitle,
shouldShowValidationMessage: viewModel.shouldShowValidationMessage)

FeedbackDescriptionView(titleText: $viewModel.description, focusField: $focusField,
isSelected: focusField == .description)

FeedbackAddAttachmentView(
attachedMedia: $viewModel.selectedAttachments, uploadingAttachments: $viewModel.uploadingAttachments,
failedAttachments: $viewModel.failedAttachments, selectedAttachments: $viewModel.selectedAttachments,
showMediaPickerOption: $viewModel.showMediaPickerOption, handleAttachmentTap: viewModel.handleAttachmentTap,
onRemoveAttachmentTap: viewModel.onRemoveAttachment, onRetryButtonTap: viewModel.onRetryAttachment(_:),
handleActionSelection: viewModel.handleActionSelection(_:), focusField: _focusField
)

PrimaryButton(
text: "Submit", isEnabled: viewModel.uploadingAttachments.isEmpty,
showLoader: viewModel.showLoader, onClick: viewModel.onSubmitBtnTap
)
}
.padding([.horizontal, .bottom], 16)
ScrollView(showsIndicators: false) {
VSpacer(24)

VStack(spacing: 24) {
FeedbackTitleView(titleText: $viewModel.title, focusField: $focusField,
isSelected: focusField == .title, isValidTitle: viewModel.isValidTitle,
shouldShowValidationMessage: viewModel.shouldShowValidationMessage)

FeedbackDescriptionView(titleText: $viewModel.description, focusField: $focusField,
isSelected: focusField == .description)

FeedbackAddAttachmentView(
attachedMedia: $viewModel.selectedAttachments, uploadingAttachments: $viewModel.uploadingAttachments,
failedAttachments: $viewModel.failedAttachments, selectedAttachments: $viewModel.selectedAttachments,
showMediaPickerOption: $viewModel.showMediaPickerOption, handleAttachmentTap: viewModel.handleAttachmentTap,
onRemoveAttachmentTap: viewModel.onRemoveAttachment, onRetryButtonTap: viewModel.onRetryAttachment(_:),
handleActionSelection: viewModel.handleActionSelection(_:), focusField: _focusField
)

PrimaryButton(
text: "Submit", isEnabled: viewModel.uploadingAttachments.isEmpty,
showLoader: viewModel.showLoader, onClick: viewModel.onSubmitBtnTap
)
}
.padding([.horizontal, .bottom], 16)
}
}
.frame(maxWidth: isIpad ? 600 : nil, alignment: .center)
Expand Down
29 changes: 10 additions & 19 deletions Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class FeedbackViewModel: BaseViewModel, ObservableObject {
@Inject private var feedbackRepository: FeedbackRepository

@Published var description: String = ""
@Published private(set) var currentState: ViewState = .initial
@Published private var uploadedAttachmentIDs: Set<String> = Set<String>()

@Published var failedAttachments: [Attachment] = []
Expand Down Expand Up @@ -145,14 +144,14 @@ extension FeedbackViewModel {
}

private func removeAttachmentFromArrays(attachmentId: String, removeAllSelected: Bool = false) {
withAnimation {
attachmentsUrls.removeAll { $0.id == attachmentId }
selectedAttachments.removeAll { $0.id == attachmentId }
uploadingAttachments.removeAll { $0.id == attachmentId }
withAnimation { [weak self] in
self?.attachmentsUrls.removeAll { $0.id == attachmentId }
self?.selectedAttachments.removeAll { $0.id == attachmentId }
self?.uploadingAttachments.removeAll { $0.id == attachmentId }

if removeAllSelected {
failedAttachments.removeAll { $0.id == attachmentId }
uploadedAttachmentIDs.remove(attachmentId)
self?.failedAttachments.removeAll { $0.id == attachmentId }
self?.uploadedAttachmentIDs.remove(attachmentId)
}
}
}
Expand All @@ -169,7 +168,7 @@ extension FeedbackViewModel {

private func validateAndUploadAttachment(data: Data, attachment: Attachment, maxSize: Int, type: StorageManager.AttachmentType) {
if data.count <= maxSize {
upload(data: data, attachment: attachment, type: type)
uploadAttachment(data: data, attachment: attachment, type: type)
} else {
selectedAttachments.removeAll { $0.id == attachment.id }
let message = type == .image ? "The image size exceeds the maximum allowed limit. Please select a smaller image."
Expand All @@ -178,7 +177,7 @@ extension FeedbackViewModel {
}
}

private func upload(data: Data, attachment: Attachment, type: StorageManager.AttachmentType) {
private func uploadAttachment(data: Data, attachment: Attachment, type: StorageManager.AttachmentType) {
uploadingAttachments.append(attachment)

Task { [weak self] in
Expand All @@ -190,7 +189,7 @@ extension FeedbackViewModel {
self?.attachmentsUrls.append((id: attachmentId, url: attachmentUrl))
self?.uploadingAttachments.removeAll { $0.id == attachmentId }
}
LogE("FeedbackViewModel: \(#function) Attachment uploaded successfully.")
LogD("FeedbackViewModel: \(#function) Attachment uploaded successfully.")
} catch {
self?.failedAttachments.append(attachment)
self?.uploadingAttachments.removeAll { $0.id == attachment.id }
Expand All @@ -209,14 +208,6 @@ extension FeedbackViewModel {
}
}

// MARK: - View's State
extension FeedbackViewModel {
enum ViewState {
case initial
case loading
}
}

extension FeedbackViewModel {
enum FocusedField {
case title, description
Expand Down

0 comments on commit 1074f4a

Please sign in to comment.