Done with CoreData working. Saving, updating and deleting are working correct now

This commit is contained in:
Vladimir Dubovik 2025-01-21 14:54:24 +03:00
parent b4704bd4fc
commit 3eb5fb73eb
4 changed files with 34 additions and 29 deletions

View File

@ -8,34 +8,38 @@
import SwiftUI import SwiftUI
struct CreatedClassView: View { struct CreatedClassView: View {
let _class: ClassModel @ObservedObject var _class: ClassModel
var provider = ClassProvider.shared
var body: some View { var body: some View {
HStack(spacing: 10) { let existingCopy = try? provider.viewContext.existingObject(with: _class.objectID)
VStack { if let check = existingCopy {
Text(getTimeString(_class.starttime)) HStack(spacing: 10) {
.font(.system(size: 15, weight: .regular)) VStack {
Text(getTimeString(_class.endtime)) Text(getTimeString(_class.starttime))
.font(.system(size: 15, weight: .regular)) .font(.system(size: 15, weight: .regular))
Text(getTimeString(_class.endtime))
.font(.system(size: 15, weight: .regular))
}
.padding(.top, 7)
.padding(.bottom, 7)
.padding(.leading, 10)
Rectangle()
.frame(width: 2)
.frame(maxHeight: UIScreen.main.bounds.height - 18)
.padding(.top, 7)
.padding(.bottom, 7)
.foregroundColor(_class.important ? Color("redForImportant") : onlineOrNot(_class.online))
Text(getSubjectName(_class.subject, _class.professor, _class.auditory))
.font(.system(size: 18, weight: .regular))
.padding(.top, 7)
.padding(.bottom, 7)
Spacer()
} }
.padding(.top, 7) .frame(maxWidth: UIScreen.main.bounds.width - 40, maxHeight: 230)
.padding(.bottom, 7) .background(Color.white)
.padding(.leading, 10) .cornerRadius(20)
Rectangle() .shadow(color: .black.opacity(0.25), radius: 4, x: 2, y: 2)
.frame(width: 2)
.frame(maxHeight: UIScreen.main.bounds.height - 18)
.padding(.top, 7)
.padding(.bottom, 7)
.foregroundColor(_class.important ? Color("redForImportant") : onlineOrNot(_class.online))
Text(getSubjectName(_class.subject, _class.professor, _class.auditory))
.font(.system(size: 18, weight: .regular))
.padding(.top, 7)
.padding(.bottom, 7)
Spacer()
} }
.frame(maxWidth: UIScreen.main.bounds.width - 40, maxHeight: 230)
.background(Color.white)
.cornerRadius(20)
.shadow(color: .black.opacity(0.25), radius: 4, x: 2, y: 2)
} }
} }

View File

@ -62,7 +62,6 @@ struct ScheduleView: View {
CreatedClassView(_class: _class) CreatedClassView(_class: _class)
.onTapGesture { .onTapGesture {
selectedClass = _class selectedClass = _class
print(selectedClass)
} }
} }
} }

View File

@ -141,10 +141,10 @@ struct CreateEditClassView: View {
Button { Button {
do { do {
try delete(vm._class) try delete(vm._class)
dismiss()
} catch { } catch {
print(error) print(error)
} }
dismiss()
} label: { } label: {
HStack { HStack {
Spacer() Spacer()
@ -194,7 +194,6 @@ struct CreateEditClassView: View {
try context.save() try context.save()
} }
} }
} }
} }

View File

@ -24,7 +24,10 @@ final class ClassProvider {
} }
var newContext: NSManagedObjectContext { var newContext: NSManagedObjectContext {
persistentContainer.newBackgroundContext() //persistentContainer.newBackgroundContext()
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
context.persistentStoreCoordinator = persistentContainer.persistentStoreCoordinator
return context
} }
private init() { private init() {