Skip to content

Instantly share code, notes, and snippets.

@alpennec
Created January 26, 2024 20:56
Show Gist options
  • Save alpennec/a45f5ff94382dc922718906a60a35220 to your computer and use it in GitHub Desktop.
Save alpennec/a45f5ff94382dc922718906a60a35220 to your computer and use it in GitHub Desktop.
//
// Created by Axel Le Pennec on 26/01/2024.
// Copyright © 2024 Axel Le Pennec. All rights reserved.
//
import SwiftUI
@main
struct FB_SwiftUI_NavigationSplitView_Delete_SelectApp: App {
var body: some Scene {
WindowGroup {
ContentView()
}
}
}
enum SelectionString: Hashable {
case all
case item(String)
}
struct ContentView: View {
@State private var selectionString: SelectionString?
@State private var strings: [String] = []
var body: some View {
NavigationSplitView {
List(selection: $selectionString) {
Section {
NavigationLink(value: SelectionString.all) {
Text("All")
}
}
Section {
ForEach(strings.sorted(), id: \.self) { string in
NavigationLink(value: SelectionString.item(string)) {
Text(string)
}
}
}
}
.toolbar {
ToolbarItem {
Button("Add") {
strings.append(UUID().uuidString)
}
}
}
} detail: {
if let selectionString {
switch selectionString {
case .all:
Text("All")
case .item(let string):
Button(role: .destructive) {
if let index = strings.firstIndex(of: string) {
strings.remove(at: index)
self.selectionString = nil
}
} label: {
Text("Delete")
}
}
} else {
Text("Select an item")
}
}
}
}
#Preview {
ContentView()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment