From 1074f4a0c73b5a910f80301b0b56c69e774ec4e9 Mon Sep 17 00:00:00 2001 From: NiraliSonani Date: Fri, 3 Jan 2025 18:36:09 +0530 Subject: [PATCH] Removed state --- .../Home/Account/Feedback/FeedbackView.swift | 52 +++++++++---------- .../Account/Feedback/FeedbackViewModel.swift | 29 ++++------- 2 files changed, 34 insertions(+), 47 deletions(-) diff --git a/Splito/UI/Home/Account/Feedback/FeedbackView.swift b/Splito/UI/Home/Account/Feedback/FeedbackView.swift index 24ee0946..d109c415 100644 --- a/Splito/UI/Home/Account/Feedback/FeedbackView.swift +++ b/Splito/UI/Home/Account/Feedback/FeedbackView.swift @@ -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) diff --git a/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift b/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift index ff9c5c82..e6e41293 100644 --- a/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift +++ b/Splito/UI/Home/Account/Feedback/FeedbackViewModel.swift @@ -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 = Set() @Published var failedAttachments: [Attachment] = [] @@ -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) } } } @@ -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." @@ -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 @@ -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 } @@ -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