The view that displays my list:
import SwiftUI
import CoreData
struct DatabaseViewMain: View {
@ObservedObject var refactoringAccount = RefactoringAccount.refactoringAccountSingleton
var body: some View {
VStack {
//Creates the 'add account' button
HStack {
Spacer()
NavigationLink {
AddAccountContainer()
} label: {
SubHeaderViewIcon(subheaderIcon: "plus.square", subheaderIconColor: Color("EditButtonBlue"))
}
.padding()
}
//Creates the List view
List {
ForEach(refactoringAccount.savedAccounts) {account in
NavigationLink {
AccountDetailMain(accountBackEnd: account)
} label: {
DatabaseViewCell(
accountNameFromCell: account.accountCompanyName,
accountLastVisitedFromCell: account.accountCity,
accountTotalFromCell: account.accountLastDateVisited)
//Text(account.accountCompanyName)
}
.foregroundColor(.gray)
}
.onDelete(perform: refactoringAccount.deleteAccount)
.onAppear(perform: refactoringAccount.fetchAccount)
}
.listStyle(PlainListStyle())
}
.navigationBarHidden(true)
}
}
The view that my mentor created:
import SwiftUI
import Combine
import CoreData
struct AddAccountContainer: View {
@State var account: AccountBackEnd?
func newAccount() -> AccountBackEnd {
let context = RefactoringAccount.refactoringAccountSingleton.container.viewContext
let newAccount = AccountBackEnd(context: context)
return newAccount
}
var body: some View {
VStack() {
if let account = self.account {
AddAccountView(accountBackEnd: account)
}
}
.onAppear {
if account == nil { account = newAccount() }
}
}
}
The View that I’d like to use:
import SwiftUI
import CoreData
//View for user to add a new account with
struct AddAccountView: View {
@ObservedObject var accountBackEnd: AccountBackEnd
@Environment(\.presentationMode) var presentationMode
var body: some View {
ScrollView {
VStack {
Group {
MainAddHeaderView(mainTextField: "Account", iconField: "building")
Spacer()
.frame(height: 5)
HStack {
NavigationLink {
CancelButton()
} label: {
MainHeaderViewBottomLeft()
}
Spacer()
}
}
Button(action: {
do {
try accountBackEnd.managedObjectContext?.save()
} catch {
print("There was a problem saving: \(error)")
}
presentationMode.wrappedValue.dismiss()
print("It exited")
})
{
LargeSaveButtonBlue(saveButtonLabel: "Save Changes")
}
OneHundredPointSpacer()
//Exit
LargeDeleteButton(deleteButtonLabel: "Delete Changes")
OneHundredPointSpacer()
}
.navigationBarBackButtonHidden(true)
.navigationBarHidden(true)
}
}
}