summaryrefslogtreecommitdiff
path: root/Sora/Views/Generic
diff options
context:
space:
mode:
Diffstat (limited to 'Sora/Views/Generic')
-rw-r--r--Sora/Views/Generic/CollectionAlertsModifier.swift59
-rw-r--r--Sora/Views/Generic/GenericListView.swift44
2 files changed, 70 insertions, 33 deletions
diff --git a/Sora/Views/Generic/CollectionAlertsModifier.swift b/Sora/Views/Generic/CollectionAlertsModifier.swift
new file mode 100644
index 0000000..6c39da8
--- /dev/null
+++ b/Sora/Views/Generic/CollectionAlertsModifier.swift
@@ -0,0 +1,59 @@
+import SwiftUI
+
+struct CollectionAlertsModifier: ViewModifier {
+ @Binding var isNewCollectionAlertPresented: Bool
+ @Binding var newCollectionName: String
+ @Binding var isCollectionErrorAlertPresented: Bool
+ let onCreate: (String) -> Void
+
+ func body(content: Content) -> some View {
+ content
+ .alert(
+ "New Collection",
+ isPresented: $isNewCollectionAlertPresented
+ ) {
+ TextField("Collection Name", text: $newCollectionName)
+
+ Button("Cancel") {
+ newCollectionName = ""
+ isNewCollectionAlertPresented = false
+ }
+
+ Button("Create") {
+ if newCollectionName.isEmpty {
+ isCollectionErrorAlertPresented = true
+ } else {
+ onCreate(newCollectionName)
+ newCollectionName = ""
+ isNewCollectionAlertPresented = false
+ }
+ }
+ }
+ .alert(
+ "Error",
+ isPresented: $isCollectionErrorAlertPresented
+ ) {
+ Button("OK", role: .cancel) { () }
+ } message: {
+ Text("Collection name cannot be empty.")
+ }
+ }
+}
+
+extension View {
+ func collectionAlerts(
+ isNewCollectionAlertPresented: Binding<Bool>,
+ newCollectionName: Binding<String>,
+ isCollectionErrorAlertPresented: Binding<Bool>,
+ onCreate: @escaping (String) -> Void
+ ) -> some View {
+ modifier(
+ CollectionAlertsModifier(
+ isNewCollectionAlertPresented: isNewCollectionAlertPresented,
+ newCollectionName: newCollectionName,
+ isCollectionErrorAlertPresented: isCollectionErrorAlertPresented,
+ onCreate: onCreate
+ )
+ )
+ }
+}
diff --git a/Sora/Views/Generic/GenericListView.swift b/Sora/Views/Generic/GenericListView.swift
index 0945b82..53ad364 100644
--- a/Sora/Views/Generic/GenericListView.swift
+++ b/Sora/Views/Generic/GenericListView.swift
@@ -298,42 +298,20 @@ struct GenericListView<T: Identifiable & Hashable & GenericItem>: View {
Button("Cancel", role: .cancel) { () }
}
- .alert(
- "New Collection",
- isPresented: $isNewCollectionAlertPresented
- ) {
- TextField("Collection Name", text: $newCollectionName)
+ .collectionAlerts(
+ isNewCollectionAlertPresented: $isNewCollectionAlertPresented,
+ newCollectionName: $newCollectionName,
+ isCollectionErrorAlertPresented: $isCollectionErrorAlertPresented
+ ) { newCollectionName in
+ let newFolder = SettingsFolder(name: newCollectionName)
- Button("Cancel") {
- newCollectionName = ""
- isNewCollectionAlertPresented = false
- }
+ settings.folders.append(newFolder)
- Button("Create") {
- if newCollectionName.isEmpty {
- isCollectionErrorAlertPresented = true
- } else {
- let newFolder = SettingsFolder(name: newCollectionName)
-
- settings.folders.append(newFolder)
-
- if let id = itemPendingCollectionAssignment {
- settings.updateBookmarkFolder(withID: id, folder: newFolder.id)
- }
-
- itemPendingCollectionAssignment = nil
- newCollectionName = ""
- isNewCollectionAlertPresented = false
- }
+ if let id = itemPendingCollectionAssignment {
+ settings.updateBookmarkFolder(withID: id, folder: newFolder.id)
}
- }
- .alert(
- "Error",
- isPresented: $isCollectionErrorAlertPresented,
- ) {
- Button("OK", role: .cancel) { () }
- } message: {
- Text("Collection name cannot be empty.")
+
+ itemPendingCollectionAssignment = nil
}
}