From 949292060bfe3a93f1842a6be627b08d9dea77d8 Mon Sep 17 00:00:00 2001 From: Markus Thielker Date: Thu, 16 Jan 2025 01:56:03 +0100 Subject: [PATCH] Improve password list view --- password/view/add/AddView.swift | 51 +++++++++++++++++++++++++++++++ password/view/list/ListView.swift | 29 ++++++++++++++++-- 2 files changed, 77 insertions(+), 3 deletions(-) create mode 100644 password/view/add/AddView.swift diff --git a/password/view/add/AddView.swift b/password/view/add/AddView.swift new file mode 100644 index 0000000..f871545 --- /dev/null +++ b/password/view/add/AddView.swift @@ -0,0 +1,51 @@ +// +// AddView.swift +// password +// +// Created by Markus Thielker on 16.01.25. +// + +import SwiftUI + +struct AddPasswordView: View { + + @ObservedObject var viewModel: ListViewModel + + @Environment(\.dismiss) var dismiss + + @State private var name: String = "" + @State private var value: String = "" + + var body: some View { + VStack { + VStack { + Text("Add Password") + .font(.headline) + Text("Create a new password to learn. It will be encrypted and stored securely.") + } + .padding(EdgeInsets(top: 0, leading: 0, bottom: 10, trailing: 0)) + Form { + TextField("Name", text: $name) + TextField("Value", text: $value) + Text("The password will not be visible again later. Make sure to save it somewhere else too!") + .font(.footnote) + HStack { + Button("Save") { + viewModel.savePassword(name: name, value: value) + name = "" + value = "" + dismiss() + } + Button("Cancel") { + dismiss() + } + } + .padding(EdgeInsets(top: 10, leading: 0, bottom: 0, trailing: 0)) + } + }.padding(20) + } +} + +#Preview { + AddPasswordView(viewModel: .init()) +} diff --git a/password/view/list/ListView.swift b/password/view/list/ListView.swift index 4b03436..3001a2c 100644 --- a/password/view/list/ListView.swift +++ b/password/view/list/ListView.swift @@ -10,16 +10,39 @@ import SwiftUI struct ListView: View { @ObservedObject var viewModel: ListViewModel + @State var isAddingPassword: Bool = false var body: some View { NavigationView { - ForEach(viewModel.passwords) { password in - NavigationLink(destination: Text(password.name)) { - Text(password.name) + List { + ForEach(viewModel.passwords) { password in + NavigationLink(destination: Text(password.name)) { + Text(password.name) + } + } + } + .scrollContentBackground(.hidden) + .toolbar { + Button(action: { isAddingPassword = true }) { + Image(systemName: "plus") + .frame(width: 20, height: 20) + } + Button(action: { + viewModel.loadAllPasswords() + }) { + Image(systemName: "arrow.trianglehead.clockwise") + .imageScale(.medium) + .frame(width: 20, height: 20) } } } .navigationTitle("Password Trainer") + .sheet(isPresented: $isAddingPassword) { + AddPasswordView(viewModel: viewModel) + } + .onAppear { + viewModel.loadAllPasswords() + } } }