2023-05-21 12:52:47 +00:00
|
|
|
//
|
|
|
|
// AddFeed.swift
|
|
|
|
// Leganto
|
|
|
|
//
|
|
|
|
// Created by Louis Hollingworth on 2023-05-21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
2023-05-22 18:30:02 +00:00
|
|
|
import CoreData
|
2023-05-21 12:52:47 +00:00
|
|
|
|
2023-05-22 18:30:02 +00:00
|
|
|
enum Genre: Int16, CaseIterable, Identifiable {
|
2023-05-21 12:52:47 +00:00
|
|
|
case uncategorised = 0
|
|
|
|
case technology = 1
|
|
|
|
case news = 2
|
2023-05-22 18:30:02 +00:00
|
|
|
|
|
|
|
var id: Self { self }
|
2023-05-21 12:52:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct AddFeed: View {
|
2023-05-23 17:37:04 +00:00
|
|
|
|
|
|
|
var body: some View {
|
|
|
|
#if os(iOS)
|
|
|
|
NavigationView {
|
|
|
|
AddFeedContent()
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
AddFeedContent()
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AddFeedContent: View {
|
2023-05-22 18:30:02 +00:00
|
|
|
@Environment(\.managedObjectContext) var moc
|
|
|
|
@Environment(\.dismiss) var dismiss
|
|
|
|
|
2023-05-21 12:52:47 +00:00
|
|
|
@State private var name = ""
|
|
|
|
@State private var desc = ""
|
2023-05-22 18:30:02 +00:00
|
|
|
@State private var genre: Genre = .uncategorised
|
2023-05-21 12:52:47 +00:00
|
|
|
@State private var url = ""
|
2023-05-22 18:30:02 +00:00
|
|
|
|
2023-05-21 12:52:47 +00:00
|
|
|
var body: some View {
|
2023-05-23 17:37:04 +00:00
|
|
|
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)))
|
2023-05-22 18:30:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 17:37:04 +00:00
|
|
|
}
|
2023-05-22 18:30:02 +00:00
|
|
|
.toolbar {
|
|
|
|
ToolbarItem {
|
|
|
|
Button(action: addItem) {
|
|
|
|
Text("save")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-23 17:37:04 +00:00
|
|
|
|
2023-05-22 18:30:02 +00:00
|
|
|
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()
|
2023-05-21 12:52:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
struct AddFeed_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
|
|
|
AddFeed()
|
|
|
|
}
|
|
|
|
}
|