2023-05-21 11:43:45 +00:00
|
|
|
//
|
|
|
|
// ContentView.swift
|
|
|
|
// Leganto
|
|
|
|
//
|
|
|
|
// Created by Louis Hollingworth on 2023-05-21.
|
|
|
|
//
|
|
|
|
|
|
|
|
import SwiftUI
|
|
|
|
import CoreData
|
|
|
|
|
|
|
|
struct ContentView: View {
|
2023-05-21 12:52:47 +00:00
|
|
|
@Environment(\.managedObjectContext) var moc
|
|
|
|
@FetchRequest(sortDescriptors: [], animation: .default) var feeds: FetchedResults<Feed>
|
2023-05-22 18:30:02 +00:00
|
|
|
|
|
|
|
@State private var showAddScreen = false
|
2023-05-21 11:43:45 +00:00
|
|
|
|
|
|
|
var body: some View {
|
2023-05-22 18:30:02 +00:00
|
|
|
NavigationView {
|
|
|
|
List {
|
|
|
|
ForEach(feeds) { item in
|
2023-05-23 17:37:04 +00:00
|
|
|
NavigationLink {
|
|
|
|
FeedView(source: item)
|
|
|
|
} label: {
|
|
|
|
HStack {
|
|
|
|
if Genre(rawValue: item.genre) == .news {
|
|
|
|
Image(systemName: "newspaper")
|
|
|
|
} else if Genre(rawValue: item.genre) == .technology {
|
|
|
|
Image(systemName: "laptopcomputer")
|
|
|
|
} else {
|
|
|
|
Image(systemName: "doc")
|
|
|
|
}
|
|
|
|
|
|
|
|
Text(item.name!)
|
|
|
|
}
|
|
|
|
}
|
2023-05-22 18:30:02 +00:00
|
|
|
}
|
|
|
|
.onDelete(perform: deleteItems)
|
|
|
|
}
|
|
|
|
.navigationTitle("feeds")
|
|
|
|
.toolbar {
|
|
|
|
ToolbarItem {
|
|
|
|
Button(action: {
|
|
|
|
showAddScreen.toggle()
|
|
|
|
}) {
|
|
|
|
Label("add", systemImage: "plus")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
.sheet(isPresented: $showAddScreen) {
|
|
|
|
AddFeed()
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 11:43:45 +00:00
|
|
|
}
|
2023-05-21 12:52:47 +00:00
|
|
|
|
2023-05-22 18:30:02 +00:00
|
|
|
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)")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-05-21 11:43:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
struct ContentView_Previews: PreviewProvider {
|
|
|
|
static var previews: some View {
|
2023-05-21 12:52:47 +00:00
|
|
|
ContentView()
|
2023-05-23 17:37:04 +00:00
|
|
|
.environment(\.managedObjectContext, DataController.preview.container.viewContext)
|
2023-05-21 11:43:45 +00:00
|
|
|
}
|
|
|
|
}
|