Skip to content

Instantly share code, notes, and snippets.

@jaywardell
Created March 13, 2025 02:09
Show Gist options
  • Select an option

  • Save jaywardell/11dc8406548f8e4e78901709bd0f0ed4 to your computer and use it in GitHub Desktop.

Select an option

Save jaywardell/11dc8406548f8e4e78901709bd0f0ed4 to your computer and use it in GitHub Desktop.
//
// ContentView.swift
// ReminderslikeUI
//
// Created by Joseph Wardell on 3/12/25.
//
import SwiftUI
import Observation
@Observable
final class ListData {
struct Item: Identifiable {
let id = UUID()
var title: String
let systemIconName: String
}
var items: [Item]
init(items: [Item]) {
self.items = items
}
}
struct ContentView: View {
let data: ListData
@State var editMode: EditMode = .inactive
@State private var lastButtonPressed = ""
@State private var lastInfoButtonPressed = ""
var body: some View {
List {
Section {
ForEach(data.items) { item in
HStack {
if editMode.isEditing == true {
Button(item.title, systemImage: item.systemIconName) {
lastButtonPressed = item.title
}
}
else {
NavigationLink {
Text(item.title)
} label: {
Label(item.title, systemImage: item.systemIconName)
}
}
Spacer()
if editMode.isEditing == true {
Button("Info", systemImage: "info.circle") {
lastInfoButtonPressed = item.title
}
.labelStyle(.iconOnly)
}
}
}
.onMove(perform: move)
.buttonStyle(.borderless)
}
footer: {
if !lastButtonPressed.isEmpty {
Text("last button pressed: \(lastButtonPressed)")
}
if !lastInfoButtonPressed.isEmpty {
Text("last info button pressed: \(lastInfoButtonPressed)")
}
}
}
.navigationBarItems(trailing: EditButton())
.environment(\.editMode, $editMode)
}
func move(from source: IndexSet, to destination: Int) {
print(#function)
data.items.move(fromOffsets: source, toOffset: destination)
}
}
#Preview {
@Previewable
@State var examples = ListData(items: [
.init(title: "Work", systemIconName: "hammer"),
.init(title: "Home", systemIconName: "house"),
.init(title: "Music", systemIconName: "music.quarternote.3"),
])
NavigationStack {
ContentView(data: examples)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment