// // AddFeed.swift // Leganto // // Created by Louis Hollingworth on 2023-05-21. // import SwiftUI import CoreData enum Genre: Int16, CaseIterable, Identifiable { case uncategorised = 0 case technology = 1 case news = 2 var id: Self { self } } struct AddFeed: View { var body: some View { #if os(iOS) NavigationView { AddFeedContent() } #else AddFeedContent() .padding(.all) #endif } } struct AddFeedContent: View { @Environment(\.managedObjectContext) var moc @Environment(\.dismiss) var dismiss @State private var name = "" @State private var desc = "" @State private var genre: Genre = .uncategorised @State private var url = "" var body: some View { Form { Section { TextField("name", text: $name) TextField("desc", text: $desc) TextField("url", text: $url) Picker("genre", selection: $genre) { ForEach(Genre.allCases) { option in Text(LocalizedStringKey(String(describing: option))) } } } } .toolbar { ToolbarItem { Button { dismiss() } label: { Text("cancel") } } ToolbarItem { Button(action: addItem) { Text("save") } } } } func addItem() { let newFeed = Feed(context: moc) newFeed.id = UUID() newFeed.name = name newFeed.desc = desc newFeed.genre = genre.rawValue newFeed.url = url newFeed.dateAdded = Date() newFeed.dateUpdated = Date() try? moc.save() dismiss() } } struct AddFeed_Previews: PreviewProvider { static var previews: some View { AddFeed() } }