201 lines
12 KiB
Swift
201 lines
12 KiB
Swift
//
|
||
// ScheduleView.swift
|
||
// Schedule ICTIS
|
||
//
|
||
// Created by Mironov Egor on 05.12.2024.
|
||
// ктбо2-6
|
||
|
||
import SwiftUI
|
||
|
||
struct ScheduleView: View {
|
||
@ObservedObject var vm: ScheduleViewModel
|
||
@FetchRequest(fetchRequest: ClassModel.all()) private var classes // Делаем запрос в CoreData и получаем список сохраненных пар
|
||
@State private var selectedClass: ClassModel? = nil
|
||
@State private var lastOffset: CGFloat = 0
|
||
@State private var scrollTimer: Timer? = nil
|
||
@State private var isShowingMyPairs = false
|
||
@Binding var isScrolling: Bool
|
||
var provider = ClassProvider.shared
|
||
var hasLessons: Bool {
|
||
return vm.classes.indices.contains(vm.selectedIndex) &&
|
||
vm.classes[vm.selectedIndex].dropFirst().contains { !$0.isEmpty }
|
||
}
|
||
var hasVPK: Bool {
|
||
return vm.vpks.indices.contains(vm.selectedIndex) && vm.vpks[vm.selectedIndex].dropFirst().contains { !$0.isEmpty }
|
||
}
|
||
var body: some View {
|
||
if vm.isLoading {
|
||
LoadingScheduleView()
|
||
}
|
||
else {
|
||
if vm.errorInNetwork != .invalidResponse {
|
||
ZStack (alignment: .top) {
|
||
ScrollView(.vertical, showsIndicators: false) {
|
||
VStack (spacing: 30) {
|
||
VStack (alignment: .leading, spacing: 20 ) {
|
||
if hasLessons {
|
||
Text("Учебное расписание")
|
||
.font(.custom("Montserrat-Bold", fixedSize: 20))
|
||
}
|
||
ForEach(vm.classes.indices, id: \.self) { index in
|
||
if index != 0 && index != 1 && index == vm.selectedIndex {
|
||
let daySchedule = vm.classes[index] // Это массив строк для дня
|
||
ForEach(daySchedule.indices.dropFirst(), id: \.self) { lessonIndex in
|
||
let lesson = daySchedule[lessonIndex] // Это строка с расписанием одной пары
|
||
if !lesson.isEmpty {
|
||
HStack(spacing: 15) {
|
||
VStack {
|
||
Text(convertTimeString(vm.classes[1][lessonIndex])[0])
|
||
.font(.custom("Montserrat-Regular", fixedSize: 15))
|
||
.padding(.bottom, 1)
|
||
Text(convertTimeString(vm.classes[1][lessonIndex])[1])
|
||
.font(.custom("Montserrat-Regular", fixedSize: 15))
|
||
.padding(.top, 1)
|
||
}
|
||
.frame(width: 48)
|
||
.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(getColorForClass(lesson))
|
||
Text(lesson)
|
||
.font(.custom("Montserrat-Medium", fixedSize: 16))
|
||
.lineSpacing(3)
|
||
.padding(.top, 9)
|
||
.padding(.bottom, 9)
|
||
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)
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if classes.contains(where: { daysAreEqual($0.day, vm.selectedDay) }) {
|
||
VStack(alignment: .leading, spacing: 20) {
|
||
Text("Мои пары")
|
||
.font(.custom("Montserrat-Bold", fixedSize: 20))
|
||
ForEach(classes) { _class in
|
||
if daysAreEqual(_class.day, vm.selectedDay) {
|
||
CreatedClassView(_class: _class)
|
||
.onTapGesture {
|
||
selectedClass = _class
|
||
}
|
||
}
|
||
}
|
||
}
|
||
}
|
||
if UserDefaults.standard.string(forKey: "vpk") != nil {
|
||
VStack (alignment: .leading, spacing: 20 ) {
|
||
if hasVPK {
|
||
Text("ВПК")
|
||
.font(.custom("Montserrat-Bold", fixedSize: 20))
|
||
}
|
||
ForEach(vm.vpks.indices, id: \.self) { index in
|
||
if index != 0 && index != 1 && index == vm.selectedIndex {
|
||
let dayVPK = vm.vpks[index] // Это массив строк для дня
|
||
ForEach(dayVPK.indices.dropFirst(), id: \.self) { lessonIndex in
|
||
let lesson = dayVPK[lessonIndex] // Это строка с расписанием одной пары
|
||
if !lesson.isEmpty {
|
||
HStack(spacing: 15) {
|
||
VStack {
|
||
Text(convertTimeString(vm.vpks[1][lessonIndex])[0])
|
||
.font(.custom("Montserrat-Regular", fixedSize: 15))
|
||
.padding(.bottom, 1)
|
||
Text(convertTimeString(vm.vpks[1][lessonIndex])[1])
|
||
.font(.custom("Montserrat-Regular", fixedSize: 15))
|
||
.padding(.top, 1)
|
||
}
|
||
.frame(width: 48)
|
||
.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(getColorForClass(lesson))
|
||
Text(lesson)
|
||
.font(.custom("Montserrat-Medium", fixedSize: 16))
|
||
.lineSpacing(3)
|
||
.padding(.top, 9)
|
||
.padding(.bottom, 9)
|
||
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(width: UIScreen.main.bounds.width)
|
||
.padding(.bottom, 100)
|
||
.padding(.top, 10)
|
||
.background(GeometryReader { geometry in
|
||
Color.clear.preference(key: ViewOffsetKey.self, value: geometry.frame(in: .global).minY)
|
||
})
|
||
}
|
||
.onPreferenceChange(ViewOffsetKey.self) { offset in
|
||
if offset != lastOffset {
|
||
// Скролл происходит
|
||
isScrolling = true
|
||
|
||
// Останавливаем предыдущий таймер
|
||
scrollTimer?.invalidate()
|
||
// Запускаем новый таймер
|
||
scrollTimer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: false) { _ in
|
||
// Скролл остановился
|
||
isScrolling = false
|
||
}
|
||
}
|
||
lastOffset = offset
|
||
}
|
||
.onDisappear {
|
||
scrollTimer?.invalidate()
|
||
}
|
||
VStack {
|
||
LinearGradient(gradient: Gradient(colors: [Color("background").opacity(0.95), Color.white.opacity(0.1)]), startPoint: .top, endPoint: .bottom)
|
||
}
|
||
.frame(width: UIScreen.main.bounds.width, height: 15)
|
||
}
|
||
//Sheet будет открываться, когда selectedClass будет становиться не nil
|
||
.sheet(item: $selectedClass,
|
||
onDismiss: {
|
||
selectedClass = nil
|
||
},
|
||
content: { _class in
|
||
CreateEditClassView(vm: .init(provider: provider, _class: _class), day: vm.selectedDay)
|
||
})
|
||
}
|
||
else {
|
||
NoScheduleView()
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
struct ViewOffsetKey: PreferenceKey {
|
||
typealias Value = CGFloat
|
||
static var defaultValue = CGFloat.zero
|
||
static func reduce(value: inout Value, nextValue: () -> Value) {
|
||
value += nextValue()
|
||
}
|
||
}
|
||
|
||
#Preview {
|
||
ContentView()
|
||
}
|