Replace default views with basic layout
This commit is contained in:
parent
e3deae28c6
commit
5e05d2f0ed
5 changed files with 42 additions and 93 deletions
|
@ -1,59 +0,0 @@
|
|||
//
|
||||
// ContentView.swift
|
||||
// password
|
||||
//
|
||||
// Created by Markus Thielker on 15.01.25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
struct ContentView: View {
|
||||
@Environment(\.modelContext) private var modelContext
|
||||
@Query private var items: [Item]
|
||||
|
||||
var body: some View {
|
||||
NavigationSplitView {
|
||||
List {
|
||||
ForEach(items) { item in
|
||||
NavigationLink {
|
||||
Text("Item at \(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))")
|
||||
} label: {
|
||||
Text(item.timestamp, format: Date.FormatStyle(date: .numeric, time: .standard))
|
||||
}
|
||||
}
|
||||
.onDelete(perform: deleteItems)
|
||||
}
|
||||
.navigationSplitViewColumnWidth(min: 180, ideal: 200)
|
||||
.toolbar {
|
||||
ToolbarItem {
|
||||
Button(action: addItem) {
|
||||
Label("Add Item", systemImage: "plus")
|
||||
}
|
||||
}
|
||||
}
|
||||
} detail: {
|
||||
Text("Select an item")
|
||||
}
|
||||
}
|
||||
|
||||
private func addItem() {
|
||||
withAnimation {
|
||||
let newItem = Item(timestamp: Date())
|
||||
modelContext.insert(newItem)
|
||||
}
|
||||
}
|
||||
|
||||
private func deleteItems(offsets: IndexSet) {
|
||||
withAnimation {
|
||||
for index in offsets {
|
||||
modelContext.delete(items[index])
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ContentView()
|
||||
.modelContainer(for: Item.self, inMemory: true)
|
||||
}
|
|
@ -1,18 +0,0 @@
|
|||
//
|
||||
// Item.swift
|
||||
// password
|
||||
//
|
||||
// Created by Markus Thielker on 15.01.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SwiftData
|
||||
|
||||
@Model
|
||||
final class Item {
|
||||
var timestamp: Date
|
||||
|
||||
init(timestamp: Date) {
|
||||
self.timestamp = timestamp
|
||||
}
|
||||
}
|
|
@ -6,27 +6,12 @@
|
|||
//
|
||||
|
||||
import SwiftUI
|
||||
import SwiftData
|
||||
|
||||
@main
|
||||
struct passwordApp: App {
|
||||
var sharedModelContainer: ModelContainer = {
|
||||
let schema = Schema([
|
||||
Item.self,
|
||||
])
|
||||
let modelConfiguration = ModelConfiguration(schema: schema, isStoredInMemoryOnly: false)
|
||||
|
||||
do {
|
||||
return try ModelContainer(for: schema, configurations: [modelConfiguration])
|
||||
} catch {
|
||||
fatalError("Could not create ModelContainer: \(error)")
|
||||
}
|
||||
}()
|
||||
|
||||
var body: some Scene {
|
||||
WindowGroup {
|
||||
ContentView()
|
||||
ListView(viewModel: ListViewModel())
|
||||
}
|
||||
.modelContainer(sharedModelContainer)
|
||||
}
|
||||
}
|
||||
|
|
28
password/view/list/ListView.swift
Normal file
28
password/view/list/ListView.swift
Normal file
|
@ -0,0 +1,28 @@
|
|||
//
|
||||
// ListView.swift
|
||||
// password
|
||||
//
|
||||
// Created by Markus Thielker on 16.01.25.
|
||||
//
|
||||
|
||||
import SwiftUI
|
||||
|
||||
struct ListView: View {
|
||||
|
||||
@ObservedObject var viewModel: ListViewModel
|
||||
|
||||
var body: some View {
|
||||
NavigationView {
|
||||
ForEach(viewModel.passwords) { password in
|
||||
NavigationLink(destination: Text(password.name)) {
|
||||
Text(password.name)
|
||||
}
|
||||
}
|
||||
}
|
||||
.navigationTitle("Password Trainer")
|
||||
}
|
||||
}
|
||||
|
||||
#Preview {
|
||||
ListView(viewModel: .init())
|
||||
}
|
13
password/view/list/ListViewModel.swift
Normal file
13
password/view/list/ListViewModel.swift
Normal file
|
@ -0,0 +1,13 @@
|
|||
//
|
||||
// ListViewModel.swift
|
||||
// password
|
||||
//
|
||||
// Created by Markus Thielker on 16.01.25.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
class ListViewModel: ObservableObject {
|
||||
|
||||
@Published var passwords: [Password] = []
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue