-
Notifications
You must be signed in to change notification settings - Fork 4
Improve delete all flow #237
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
b3efd21
c23cce0
7cc01f8
faa042d
b4b73d5
07be303
ecad721
f8e7d3c
d85e28d
948435a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,50 @@ | ||
| import ConvosCore | ||
| import Foundation | ||
| import Observation | ||
|
|
||
| @MainActor | ||
| @Observable | ||
| final class AppSettingsViewModel { | ||
| // MARK: - State | ||
|
|
||
| private(set) var isDeleting: Bool = false | ||
| private(set) var deletionProgress: InboxDeletionProgress? | ||
| private(set) var deletionError: Error? | ||
|
|
||
| // MARK: - Dependencies | ||
|
|
||
| private let session: any SessionManagerProtocol | ||
|
|
||
| init(session: any SessionManagerProtocol) { | ||
| self.session = session | ||
| } | ||
|
|
||
| // MARK: - Actions | ||
|
|
||
| func deleteAllData(onComplete: @escaping () -> Void) { | ||
| guard !isDeleting else { return } | ||
| isDeleting = true | ||
| deletionError = nil | ||
| deletionProgress = nil | ||
|
|
||
| QuicknameSettingsViewModel.shared.delete() | ||
|
|
||
| Task { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The
|
||
| do { | ||
| for try await progress in session.deleteAllInboxesWithProgress() { | ||
| deletionProgress = progress | ||
| } | ||
| onComplete() | ||
macroscopeapp[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } catch { | ||
| deletionError = error | ||
| isDeleting = false | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| extension AppSettingsViewModel { | ||
| static var mock: AppSettingsViewModel { | ||
| AppSettingsViewModel(session: MockInboxesService()) | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,103 @@ | ||
| import ConvosCore | ||
| import SwiftUI | ||
|
|
||
| struct DeleteAllDataView: View { | ||
| @Environment(\.dismiss) var dismiss: DismissAction | ||
| @Bindable var viewModel: AppSettingsViewModel | ||
| let onComplete: () -> Void | ||
|
|
||
| var title: String { | ||
| "Delete everything?" | ||
| } | ||
|
|
||
| var subtitle: String { | ||
| "This will permanently delete all conversations on this device, as well as your Quickname." | ||
| } | ||
|
|
||
| var body: some View { | ||
| VStack(alignment: .leading, spacing: DesignConstants.Spacing.step4x) { | ||
| Text(title) | ||
| .font(.system(.largeTitle)) | ||
| .fontWeight(.bold) | ||
| Text(subtitle) | ||
| .font(.body) | ||
| .foregroundStyle(.colorTextSecondary) | ||
|
|
||
| if let error = viewModel.deletionError { | ||
| Text("\(error.localizedDescription). Try again.") | ||
| .font(.subheadline) | ||
| .foregroundStyle(.colorTextSecondary) | ||
| .padding(.vertical, DesignConstants.Spacing.stepX) | ||
| } | ||
|
|
||
| VStack(spacing: DesignConstants.Spacing.step4x) { | ||
| HoldToDeleteButton( | ||
| isDeleting: viewModel.isDeleting, | ||
| onDelete: { deleteAllData() } | ||
| ) | ||
| .hoverEffect(.lift) | ||
|
|
||
| Button { | ||
| dismiss() | ||
| } label: { | ||
| Text("Cancel") | ||
| } | ||
| .convosButtonStyle(.text) | ||
| .disabled(viewModel.isDeleting) | ||
| .hoverEffect(.lift) | ||
| } | ||
|
|
||
| .padding(.top, DesignConstants.Spacing.step4x) | ||
| } | ||
| .padding([.leading, .top, .trailing], DesignConstants.Spacing.step10x) | ||
| } | ||
|
|
||
| private func deleteAllData() { | ||
| viewModel.deleteAllData(onComplete: onComplete) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Hold To Delete Button | ||
|
|
||
| private struct HoldToDeleteButton: View { | ||
| let isDeleting: Bool | ||
| let onDelete: () -> Void | ||
|
|
||
| private var buttonConfig: HoldToConfirmStyleConfig { | ||
| var config = HoldToConfirmStyleConfig.default | ||
| config.duration = 3.0 | ||
| config.backgroundColor = .colorCaution | ||
| return config | ||
| } | ||
|
|
||
| var body: some View { | ||
| Button { | ||
| onDelete() | ||
| } label: { | ||
| textView | ||
| .foregroundStyle(.white) | ||
| .frame(maxWidth: .infinity) | ||
| } | ||
| .disabled(isDeleting) | ||
| .buttonStyle(HoldToConfirmPrimitiveStyle(config: buttonConfig)) | ||
| } | ||
|
|
||
| private var textView: some View { | ||
| ZStack { | ||
| Text("Hold to delete") | ||
| .opacity(isDeleting ? 0 : 1) | ||
|
|
||
| Text("Deleting...") | ||
| .opacity(isDeleting ? 1 : 0) | ||
| } | ||
| .animation(.easeInOut(duration: 0.2), value: isDeleting) | ||
| } | ||
| } | ||
|
|
||
| #Preview { | ||
| let viewModel = AppSettingsViewModel(session: ConvosClient.mock().session) | ||
| DeleteAllDataView( | ||
| viewModel: viewModel, | ||
| onComplete: {} | ||
| ) | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.