DHBW-Service-App/DHBW-Service/Views/Tabs/LecturePlanItem.swift

114 lines
3.8 KiB
Swift
Raw Normal View History

//
// LecturePlanItem.swift
// DHBW-Service
//
// Created by Patrick Müller on 01.02.21.
//
import SwiftUI
import CoreData
struct LecturePlanItem: View {
@State var event: NSManagedObject
@State var isHidden = false
var body: some View {
VStack {
2021-02-07 19:39:56 +00:00
HStack {
2021-02-07 20:17:08 +00:00
Spacer()
VStack {
Text(event.value(forKey: "summary") as! String)
.font(.title3)
.frame(maxWidth: .infinity, alignment: .leading)
Text(event.value(forKey: "descr") as! String)
2021-02-07 19:39:56 +00:00
.bold()
2021-02-07 20:17:08 +00:00
.frame(maxWidth: .infinity, alignment: .leading)
Divider()
HStack {
VStack(alignment: .leading) {
Text("When")
Text("Where")
Text("Who")
}
VStack(alignment: .leading) {
Text(getDateAndTimeAsString(date: event.value(forKey: "startDate") as! Date)
+ " to "
+ getTimeAsString(date: event.value(forKey: "endDate") as! Date))
.bold()
Text(event.value(forKey: "location") as! String)
.bold()
Text(event.value(forKey: "location") as! String)
.bold()
}.frame(maxWidth: .infinity, alignment: .leading)
}
Divider()
HStack {
Button(action: {
event.setValue(!isHidden, forKey: "isHidden")
self.isHidden = !isHidden
PersistenceController.shared.save()
}){
if(self.isHidden){
Text("Show")
} else {
Text("Hide")
}
}
.padding()
.foregroundColor(.white)
.background(Color.blue)
.cornerRadius(15)
Spacer()
}
}
2021-02-07 20:17:08 +00:00
.padding()
.background(
RoundedRectangle(cornerRadius: 10)
.fill(Color.gray)
)
Spacer()
}
2021-02-07 20:17:08 +00:00
.frame(maxWidth: .infinity)
2021-02-07 19:39:56 +00:00
Spacer()
}
.onAppear{
self.isHidden = event.value(forKey: "isHidden") as! Bool
}
}
}
2021-02-07 20:17:08 +00:00
func getDateAndTimeAsString(date: Date) -> String {
2021-02-07 19:39:56 +00:00
let formatter = DateFormatter()
formatter.dateStyle = .short
2021-02-07 20:17:08 +00:00
formatter.timeStyle = .short
return formatter.string(from: date)
}
func getTimeAsString(date: Date) -> String {
let formatter = DateFormatter()
formatter.timeStyle = .short
2021-02-07 19:39:56 +00:00
return formatter.string(from: date)
}
struct LecturePlanItem_Previews: PreviewProvider {
static var previews: some View {
LecturePlanItem(event: getPreviewEvent())
.preferredColorScheme(.dark)
.previewDevice("iPhone 12")
.environmentObject(getFirstOpening())
.environment(\.managedObjectContext, PersistenceController.preview.container.viewContext)
}
static func getFirstOpening() -> LocalSettings {
let settings = LocalSettings();
settings.isFirstOpening = false;
return settings
}
static func getPreviewEvent() -> NSManagedObject {
return UtilityFunctions.getCoreDataObject(entity: "RaPlaEvent", sortDescriptors: [])[0]
}
}