diff --git a/Leganto/AddFeed.swift b/Leganto/AddFeed.swift index 6659c88..b3c5384 100644 --- a/Leganto/AddFeed.swift +++ b/Leganto/AddFeed.swift @@ -6,20 +6,63 @@ // import SwiftUI +import CoreData -enum Genre: Int16 { +enum Genre: Int16, CaseIterable, Identifiable { case uncategorised = 0 case technology = 1 case news = 2 + + var id: Self { self } } struct AddFeed: View { + @Environment(\.managedObjectContext) var moc + @Environment(\.dismiss) var dismiss + @State private var name = "" @State private var desc = "" - @State private var genre = Genre.uncategorised.rawValue + @State private var genre: Genre = .uncategorised @State private var url = "" + var body: some View { - Text(/*@START_MENU_TOKEN@*/"Hello, World!"/*@END_MENU_TOKEN@*/) + NavigationView { + 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(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() } } diff --git a/Leganto/ContentView.swift b/Leganto/ContentView.swift index 2f26ca5..00f196e 100644 --- a/Leganto/ContentView.swift +++ b/Leganto/ContentView.swift @@ -10,16 +10,46 @@ import CoreData struct ContentView: View { @Environment(\.managedObjectContext) var moc - @FetchRequest(sortDescriptors: [], animation: .default) var feeds: FetchedResults + + @State private var showAddScreen = false var body: some View { - Text("Hello World") + NavigationView { + List { + ForEach(feeds) { item in + Text(item.name!) + } + .onDelete(perform: deleteItems) + } + .navigationTitle("feeds") + .toolbar { + ToolbarItem { + Button(action: { + showAddScreen.toggle() + }) { + Label("add", systemImage: "plus") + } + } + } + .sheet(isPresented: $showAddScreen) { + AddFeed() + } + } } - func addItem() {} - - func deleteItems() {} + func deleteItems(offsets: IndexSet) { + withAnimation { + offsets.map { feeds[$0] }.forEach(moc.delete) + + do { + try moc.save() + } catch { + let err = error as NSError + fatalError("Unresolved error \(err), \(err.userInfo)") + } + } + } } diff --git a/Leganto/en-GB.lproj/Localizable.strings b/Leganto/en-GB.lproj/Localizable.strings index ced3eab..eb8bf1b 100644 --- a/Leganto/en-GB.lproj/Localizable.strings +++ b/Leganto/en-GB.lproj/Localizable.strings @@ -6,4 +6,16 @@ */ +// General "feeds" = "Feeds"; +"save" = "Save"; +"name" = "Name"; +"desc" = "Description"; +"url" = "URL"; +"genre" = "Genre"; +"fadd" = "Add Feed"; + +// Genre categories +"uncategorised" = "Uncategorised"; +"technology" = "Technology"; +"news" = "News";