From 5e05d2f0ed5268ac6799aae73f6f6c2a4838d2a7 Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Thu, 16 Jan 2025 00:12:05 +0100 Subject: [PATCH] Replace default views with basic layout --- password/ContentView.swift | 59 -------------------------- password/Item.swift | 18 -------- password/passwordApp.swift | 17 +------- password/view/list/ListView.swift | 28 ++++++++++++ password/view/list/ListViewModel.swift | 13 ++++++ 5 files changed, 42 insertions(+), 93 deletions(-) delete mode 100644 password/ContentView.swift delete mode 100644 password/Item.swift create mode 100644 password/view/list/ListView.swift create mode 100644 password/view/list/ListViewModel.swift diff --git a/password/ContentView.swift b/password/ContentView.swift deleted file mode 100644 index 06771dd..0000000 --- a/password/ContentView.swift +++ /dev/null @@ -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) -} diff --git a/password/Item.swift b/password/Item.swift deleted file mode 100644 index 771ad2b..0000000 --- a/password/Item.swift +++ /dev/null @@ -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 - } -} diff --git a/password/passwordApp.swift b/password/passwordApp.swift index a1ee60a..9ae8896 100644 --- a/password/passwordApp.swift +++ b/password/passwordApp.swift @@ -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) } } diff --git a/password/view/list/ListView.swift b/password/view/list/ListView.swift new file mode 100644 index 0000000..4b03436 --- /dev/null +++ b/password/view/list/ListView.swift @@ -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()) +} diff --git a/password/view/list/ListViewModel.swift b/password/view/list/ListViewModel.swift new file mode 100644 index 0000000..648ba9c --- /dev/null +++ b/password/view/list/ListViewModel.swift @@ -0,0 +1,13 @@ +// +// ListViewModel.swift +// password +// +// Created by Markus Thielker on 16.01.25. +// + +import Foundation + +class ListViewModel: ObservableObject { + + @Published var passwords: [Password] = [] +}