Done with CoreData
This commit is contained in:
parent
e6b217aba4
commit
b4704bd4fc
@ -10,34 +10,32 @@ import SwiftUI
|
|||||||
struct CreatedClassView: View {
|
struct CreatedClassView: View {
|
||||||
let _class: ClassModel
|
let _class: ClassModel
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if datesAreEqual(_class.day, ClassModel.dateNow) {
|
HStack(spacing: 10) {
|
||||||
HStack(spacing: 10) {
|
VStack {
|
||||||
VStack {
|
Text(getTimeString(_class.starttime))
|
||||||
Text(getTimeString(_class.starttime))
|
.font(.system(size: 15, weight: .regular))
|
||||||
.font(.system(size: 15, weight: .regular))
|
Text(getTimeString(_class.endtime))
|
||||||
Text(getTimeString(_class.endtime))
|
.font(.system(size: 15, weight: .regular))
|
||||||
.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(.top, 7)
|
||||||
.padding(.bottom, 7)
|
.padding(.bottom, 7)
|
||||||
.padding(.leading, 10)
|
.foregroundColor(_class.important ? Color("redForImportant") : onlineOrNot(_class.online))
|
||||||
Rectangle()
|
Text(getSubjectName(_class.subject, _class.professor, _class.auditory))
|
||||||
.frame(width: 2)
|
.font(.system(size: 18, weight: .regular))
|
||||||
.frame(maxHeight: UIScreen.main.bounds.height - 18)
|
.padding(.top, 7)
|
||||||
.padding(.top, 7)
|
.padding(.bottom, 7)
|
||||||
.padding(.bottom, 7)
|
Spacer()
|
||||||
.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)
|
|
||||||
}
|
}
|
||||||
|
.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)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -10,7 +10,6 @@ import SwiftUI
|
|||||||
struct MainView: View {
|
struct MainView: View {
|
||||||
@State private var searchText: String = ""
|
@State private var searchText: String = ""
|
||||||
@State private var isShowingMonthSlider: Bool = false
|
@State private var isShowingMonthSlider: Bool = false
|
||||||
@State private var isFirstAppearence = true
|
|
||||||
@ObservedObject var vm: ScheduleViewModel
|
@ObservedObject var vm: ScheduleViewModel
|
||||||
|
|
||||||
var body: some View {
|
var body: some View {
|
||||||
|
@ -9,9 +9,9 @@ import SwiftUI
|
|||||||
|
|
||||||
struct ScheduleView: View {
|
struct ScheduleView: View {
|
||||||
@ObservedObject var vm: ScheduleViewModel
|
@ObservedObject var vm: ScheduleViewModel
|
||||||
@FetchRequest(fetchRequest: ClassModel.all()) private var classes
|
@FetchRequest(fetchRequest: ClassModel.all()) private var classes //Делаем запрос в CoreData и получаем список сохраненных пар
|
||||||
@State private var isShowingEditClassView = false
|
|
||||||
@State private var selectedClass: ClassModel? = nil
|
@State private var selectedClass: ClassModel? = nil
|
||||||
|
var provider = ClassProvider.shared
|
||||||
var body: some View {
|
var body: some View {
|
||||||
if vm.isLoading {
|
if vm.isLoading {
|
||||||
LoadingView(isLoading: $vm.isLoading)
|
LoadingView(isLoading: $vm.isLoading)
|
||||||
@ -58,11 +58,13 @@ struct ScheduleView: View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
ForEach(classes) { _class in
|
ForEach(classes) { _class in
|
||||||
CreatedClassView(_class: _class)
|
if daysAreEqual(_class.day, vm.selectedDay) {
|
||||||
.onTapGesture {
|
CreatedClassView(_class: _class)
|
||||||
isShowingEditClassView = true
|
.onTapGesture {
|
||||||
selectedClass = _class
|
selectedClass = _class
|
||||||
}
|
print(selectedClass)
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.frame(width: UIScreen.main.bounds.width)
|
.frame(width: UIScreen.main.bounds.width)
|
||||||
@ -74,17 +76,19 @@ struct ScheduleView: View {
|
|||||||
}
|
}
|
||||||
.frame(width: UIScreen.main.bounds.width, height: 15)
|
.frame(width: UIScreen.main.bounds.width, height: 15)
|
||||||
}
|
}
|
||||||
.sheet(isPresented: $isShowingEditClassView) {
|
//Sheet будет открываться, когда selectedClass будет становиться не nil
|
||||||
if let selectedClass = selectedClass {
|
.sheet(item: $selectedClass,
|
||||||
EditClassView(isShowingSheet: $isShowingEditClassView, _class: selectedClass)
|
onDismiss: {
|
||||||
}
|
selectedClass = nil
|
||||||
}
|
},
|
||||||
|
content: { _class in
|
||||||
|
CreateEditClassView(vm: .init(provider: provider, _class: _class))
|
||||||
|
})
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
NoScheduleView()
|
NoScheduleView()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -9,7 +9,7 @@ import SwiftUI
|
|||||||
|
|
||||||
struct SearchBarView: View {
|
struct SearchBarView: View {
|
||||||
@Binding var text: String
|
@Binding var text: String
|
||||||
@State private var isEditing = false
|
@FocusState private var isFocused: Bool
|
||||||
@State private var isShowingSheet: Bool = false
|
@State private var isShowingSheet: Bool = false
|
||||||
@ObservedObject var vm: ScheduleViewModel
|
@ObservedObject var vm: ScheduleViewModel
|
||||||
|
|
||||||
@ -24,11 +24,9 @@ struct SearchBarView: View {
|
|||||||
.padding(.trailing, 7)
|
.padding(.trailing, 7)
|
||||||
TextField("Поиск группы", text: $text)
|
TextField("Поиск группы", text: $text)
|
||||||
.disableAutocorrection(true)
|
.disableAutocorrection(true)
|
||||||
.onTapGesture {
|
.focused($isFocused)
|
||||||
self.isEditing = true
|
|
||||||
}
|
|
||||||
.onSubmit {
|
.onSubmit {
|
||||||
self.isEditing = false
|
self.isFocused = false
|
||||||
if (!text.isEmpty) {
|
if (!text.isEmpty) {
|
||||||
vm.fetchWeekSchedule(text)
|
vm.fetchWeekSchedule(text)
|
||||||
vm.group = text
|
vm.group = text
|
||||||
@ -36,21 +34,20 @@ struct SearchBarView: View {
|
|||||||
self.text = ""
|
self.text = ""
|
||||||
}
|
}
|
||||||
.submitLabel(.search)
|
.submitLabel(.search)
|
||||||
if isEditing {
|
if isFocused {
|
||||||
Button {
|
Button {
|
||||||
self.text = ""
|
self.text = ""
|
||||||
self.isEditing = false
|
self.isFocused = false
|
||||||
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
UIApplication.shared.sendAction(#selector(UIResponder.resignFirstResponder), to: nil, from: nil, for: nil)
|
||||||
} label: {
|
} label: {
|
||||||
Image(systemName: "xmark.circle.fill")
|
Image(systemName: "xmark.circle.fill")
|
||||||
.padding(.trailing, 20)
|
.padding(.trailing, 20)
|
||||||
.offset(x: 10)
|
.offset(x: 10)
|
||||||
.foregroundColor(.gray)
|
.foregroundColor(.gray)
|
||||||
.background(
|
.background(
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
.background(Color.red)
|
}
|
||||||
}
|
|
||||||
}
|
}
|
||||||
.frame(height: 40)
|
.frame(height: 40)
|
||||||
.background(
|
.background(
|
||||||
@ -80,7 +77,7 @@ struct SearchBarView: View {
|
|||||||
.frame(height: 40)
|
.frame(height: 40)
|
||||||
.accentColor(.blue)
|
.accentColor(.blue)
|
||||||
.sheet(isPresented: $isShowingSheet) {
|
.sheet(isPresented: $isShowingSheet) {
|
||||||
CreateClassView(isShowingSheet: $isShowingSheet, vm: .init(provider: provider))
|
CreateEditClassView(vm: .init(provider: provider))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,13 +7,13 @@
|
|||||||
|
|
||||||
import SwiftUI
|
import SwiftUI
|
||||||
|
|
||||||
struct CreateClassView: View {
|
struct CreateEditClassView: View {
|
||||||
@Binding var isShowingSheet: Bool
|
@Environment(\.dismiss) private var dismiss
|
||||||
@State private var isShowingDatePickerForDate: Bool = false
|
@State private var isShowingDatePickerForDate: Bool = false
|
||||||
@ObservedObject var vm: EditClassViewModel
|
@ObservedObject var vm: EditClassViewModel
|
||||||
@State private var isIncorrectDate1: Bool = false
|
@State private var isIncorrectDate1: Bool = false
|
||||||
@State private var isIncorrectDate2: Bool = false
|
@State private var isIncorrectDate2: Bool = false
|
||||||
|
var provider = ClassProvider.shared
|
||||||
var body: some View {
|
var body: some View {
|
||||||
NavigationView {
|
NavigationView {
|
||||||
ScrollView(.vertical, showsIndicators: false) {
|
ScrollView(.vertical, showsIndicators: false) {
|
||||||
@ -134,6 +134,31 @@ struct CreateClassView: View {
|
|||||||
.padding(.bottom, 10)
|
.padding(.bottom, 10)
|
||||||
|
|
||||||
CommentFieldView(textForComment: $vm._class.comment)
|
CommentFieldView(textForComment: $vm._class.comment)
|
||||||
|
.padding(.bottom, 20)
|
||||||
|
|
||||||
|
|
||||||
|
if !vm.isNew {
|
||||||
|
Button {
|
||||||
|
do {
|
||||||
|
try delete(vm._class)
|
||||||
|
} catch {
|
||||||
|
print(error)
|
||||||
|
}
|
||||||
|
dismiss()
|
||||||
|
} label: {
|
||||||
|
HStack {
|
||||||
|
Spacer()
|
||||||
|
Image(systemName: "trash")
|
||||||
|
Text("Удалить занятие")
|
||||||
|
.font(.system(size: 17, weight: .medium))
|
||||||
|
Spacer()
|
||||||
|
}
|
||||||
|
.frame(height: 40)
|
||||||
|
.background(Color.white)
|
||||||
|
.foregroundColor(Color.red)
|
||||||
|
.cornerRadius(10)
|
||||||
|
}
|
||||||
|
}
|
||||||
Spacer()
|
Spacer()
|
||||||
}
|
}
|
||||||
.padding(.horizontal)
|
.padding(.horizontal)
|
||||||
@ -142,56 +167,37 @@ struct CreateClassView: View {
|
|||||||
.toolbar {
|
.toolbar {
|
||||||
ToolbarItem(placement: .navigationBarLeading) {
|
ToolbarItem(placement: .navigationBarLeading) {
|
||||||
Button("Отменить") {
|
Button("Отменить") {
|
||||||
isShowingSheet = false
|
dismiss()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
ToolbarItem(placement: .navigationBarTrailing) {
|
||||||
Button("Сохранить") {
|
Button("Сохранить") {
|
||||||
do {
|
do {
|
||||||
try vm.save()
|
try vm.save()
|
||||||
|
dismiss()
|
||||||
} catch {
|
} catch {
|
||||||
print(error)
|
print(error)
|
||||||
}
|
}
|
||||||
isShowingSheet = false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
.navigationTitle("Новая пара")
|
.navigationTitle(vm.isNew ? "Новая пара" : "Изменить данные")
|
||||||
.background(Color("background"))
|
.background(Color("background"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func checkStartTimeLessThenEndTime(_ startTime: Date, _ endTime: Date) -> Bool {
|
func delete(_ _class: ClassModel) throws {
|
||||||
let calendar = Calendar.current
|
let context = provider.viewContext
|
||||||
|
let existingClass = try context.existingObject(with: _class.objectID)
|
||||||
let firstComponents = calendar.dateComponents([.hour, .minute], from: startTime)
|
context.delete(existingClass)
|
||||||
let secondComponents = calendar.dateComponents([.hour, .minute], from: endTime)
|
Task (priority: .background) {
|
||||||
|
try await context.perform {
|
||||||
guard let startHours = firstComponents.hour, let startMinutes = firstComponents.minute else {
|
try context.save()
|
||||||
return false
|
|
||||||
}
|
|
||||||
guard let endHours = secondComponents.hour, let endMinutes = secondComponents.minute else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
print("\(startHours) - \(endHours)")
|
|
||||||
print("\(startMinutes) - \(endMinutes)")
|
|
||||||
if Int(startHours) > Int(endHours) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
else if startHours == endHours {
|
|
||||||
if startMinutes < endMinutes {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
CreateClassView(isShowingSheet: .constant(true), vm: .init(provider: .shared))
|
CreateEditClassView(vm: .init(provider: .shared))
|
||||||
}
|
}
|
@ -1,103 +0,0 @@
|
|||||||
//
|
|
||||||
// SheetCreateClassView.swift
|
|
||||||
// Schedule ICTIS
|
|
||||||
//
|
|
||||||
// Created by Mironov Egor on 12.12.2024.
|
|
||||||
//
|
|
||||||
|
|
||||||
import SwiftUI
|
|
||||||
|
|
||||||
struct EditClassView: View {
|
|
||||||
@State private var isIncorrectDate1: Bool = false
|
|
||||||
@State private var isIncorrectDate2: Bool = false
|
|
||||||
@Binding var isShowingSheet: Bool
|
|
||||||
|
|
||||||
let _class: ClassModel
|
|
||||||
|
|
||||||
@State private var subject: String = ""
|
|
||||||
|
|
||||||
var body: some View {
|
|
||||||
NavigationStack {
|
|
||||||
ProfessorAuditoryClassFieldView(text: $subject, nameOfImage: "book", labelForField: "Предмет")
|
|
||||||
.padding(.bottom, 10)
|
|
||||||
List {
|
|
||||||
Section("General") {
|
|
||||||
LabeledContent {
|
|
||||||
Text(_class.subject)
|
|
||||||
} label: {
|
|
||||||
Text("Предмет")
|
|
||||||
}
|
|
||||||
|
|
||||||
LabeledContent {
|
|
||||||
Text(_class.auditory)
|
|
||||||
} label: {
|
|
||||||
Text("Аудитория")
|
|
||||||
}
|
|
||||||
|
|
||||||
LabeledContent {
|
|
||||||
Text(_class.day, style: .date)
|
|
||||||
} label: {
|
|
||||||
Text("Преподаватель")
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
Section("Notes") {
|
|
||||||
Text(_class.comment)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.toolbar {
|
|
||||||
ToolbarItem(placement: .navigationBarLeading) {
|
|
||||||
Button("Отменить") {
|
|
||||||
isShowingSheet = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
ToolbarItem(placement: .navigationBarTrailing) {
|
|
||||||
Button("Сохранить") {
|
|
||||||
isShowingSheet = false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
.onAppear {
|
|
||||||
subject = _class.subject
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkStartTimeLessThenEndTime(_ startTime: Date, _ endTime: Date) -> Bool {
|
|
||||||
let calendar = Calendar.current
|
|
||||||
|
|
||||||
let firstComponents = calendar.dateComponents([.hour, .minute], from: startTime)
|
|
||||||
let secondComponents = calendar.dateComponents([.hour, .minute], from: endTime)
|
|
||||||
|
|
||||||
guard let startHours = firstComponents.hour, let startMinutes = firstComponents.minute else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
guard let endHours = secondComponents.hour, let endMinutes = secondComponents.minute else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
|
|
||||||
print("\(startHours) - \(endHours)")
|
|
||||||
print("\(startMinutes) - \(endMinutes)")
|
|
||||||
if Int(startHours) > Int(endHours) {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
else if startHours == endHours {
|
|
||||||
if startMinutes < endMinutes {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#Preview {
|
|
||||||
NavigationStack {
|
|
||||||
EditClassView(isShowingSheet: .constant(true), _class: .preview())
|
|
||||||
}
|
|
||||||
}
|
|
@ -53,6 +53,7 @@ extension ClassModel {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Получаем все данные и сортируем их по дню
|
// Получаем все данные и сортируем их по дню
|
||||||
|
// Этот метод будет использоваться на View(ScheduleView), где отображаются пары
|
||||||
static func all() -> NSFetchRequest<ClassModel> {
|
static func all() -> NSFetchRequest<ClassModel> {
|
||||||
let request: NSFetchRequest<ClassModel> = classesFetchRequest
|
let request: NSFetchRequest<ClassModel> = classesFetchRequest
|
||||||
request.sortDescriptors = [
|
request.sortDescriptors = [
|
||||||
|
@ -72,7 +72,7 @@ extension View {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// MARK: ScheduleView
|
// MARK: ScheduleView
|
||||||
func datesAreEqual(_ date1: Date, _ date2: Date) -> Bool {
|
func daysAreEqual(_ date1: Date, _ date2: Date) -> Bool {
|
||||||
let calendar = Calendar.current
|
let calendar = Calendar.current
|
||||||
|
|
||||||
let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
|
let components1 = calendar.dateComponents([.year, .month, .day], from: date1)
|
||||||
@ -120,10 +120,34 @@ extension View {
|
|||||||
return formatter
|
return formatter
|
||||||
}
|
}
|
||||||
|
|
||||||
func checkUpFields(_ subject: String, _ auditory: String, _ professor: String, _ time1: Date, _ time3: Date) -> Bool {
|
func checkStartTimeLessThenEndTime(_ startTime: Date, _ endTime: Date) -> Bool {
|
||||||
if (subject != "" || auditory != "" || professor != "") {
|
let calendar = Calendar.current
|
||||||
|
|
||||||
|
let firstComponents = calendar.dateComponents([.hour, .minute], from: startTime)
|
||||||
|
let secondComponents = calendar.dateComponents([.hour, .minute], from: endTime)
|
||||||
|
|
||||||
|
guard let startHours = firstComponents.hour, let startMinutes = firstComponents.minute else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
guard let endHours = secondComponents.hour, let endMinutes = secondComponents.minute else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
print("\(startHours) - \(endHours)")
|
||||||
|
print("\(startMinutes) - \(endMinutes)")
|
||||||
|
if Int(startHours) > Int(endHours) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
else if startHours == endHours {
|
||||||
|
if startMinutes < endMinutes {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -11,11 +11,22 @@ import CoreData
|
|||||||
final class EditClassViewModel: ObservableObject {
|
final class EditClassViewModel: ObservableObject {
|
||||||
@Published var _class: ClassModel
|
@Published var _class: ClassModel
|
||||||
|
|
||||||
|
let isNew: Bool
|
||||||
|
|
||||||
private let context: NSManagedObjectContext
|
private let context: NSManagedObjectContext
|
||||||
|
|
||||||
init(provider: ClassProvider, _class: ClassModel? = nil) {
|
init(provider: ClassProvider, _class: ClassModel? = nil) {
|
||||||
self.context = provider.newContext
|
self.context = provider.newContext
|
||||||
self._class = ClassModel(context: self.context)
|
|
||||||
|
if let _class,
|
||||||
|
let existingClassCopy = try? context.existingObject(with: _class.objectID) as? ClassModel {
|
||||||
|
self._class = existingClassCopy
|
||||||
|
self.isNew = false
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
self._class = ClassModel(context: self.context)
|
||||||
|
self.isNew = true
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func save() throws {
|
func save() throws {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user