This commit is contained in:
Vladimir Dubovik
2024-12-11 13:33:35 +03:00
parent 57e241292f
commit 1de531abc8
13 changed files with 325 additions and 132 deletions

View File

@ -0,0 +1,25 @@
//
// LoadingView.swift
// Schedule ICTIS
//
// Created by G412 on 11.12.2024.
//
import SwiftUI
struct LoadingView: View {
@Binding var isLoading: Bool
var body: some View {
ZStack {
Color("background")
.ignoresSafeArea()
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .secondary))
.scaleEffect(1.5)
}
}
}
#Preview {
LoadingView(isLoading: .constant(true))
}

View File

@ -9,10 +9,6 @@ import SwiftUI
struct MainView: View {
@State private var searchText: String = ""
@State private var currentDate: Date = Date()
@State private var weekSlider: [[Date.WeekDay]] = []
@State private var currentWeekIndex: Int = 1
@State private var createWeek: Bool = false
@State private var isShowingMonthSlider: Bool = false
@State private var isFirstAppearence = true
@ObservedObject var vm: ViewModel
@ -20,7 +16,10 @@ struct MainView: View {
var body: some View {
VStack {
SearchBarView(text: $searchText, vm: vm)
if (vm.isFirstStartOffApp) {
if (vm.isFirstStartOffApp && vm.isLoading) {
LoadingView(isLoading: $vm.isLoading)
}
else if (vm.isFirstStartOffApp) {
FirstLaunchScheduleView()
}
else {
@ -34,23 +33,6 @@ struct MainView: View {
Text(error.failureReason)
}
.background(Color("background"))
.onAppear(perform: {
currentDate = vm.selectedDay
vm.updateSelectedDayIndex(currentDate)
if weekSlider.isEmpty {
let currentWeek = Date().fetchWeek(vm.selectedDay)
if let firstDate = currentWeek.first?.date {
weekSlider.append(firstDate.createPrevioustWeek())
}
weekSlider.append(currentWeek)
if let lastDate = currentWeek.last?.date {
weekSlider.append(lastDate.createNextWeek())
}
}
})
}
@ViewBuilder
@ -58,14 +40,14 @@ struct MainView: View {
VStack (alignment: .leading, spacing: 6) {
HStack {
VStack (alignment: .leading, spacing: 0) {
Text(currentDate.format("EEEE"))
Text(vm.selectedDay.format("EEEE"))
.font(.system(size: 40, weight: .semibold))
.foregroundStyle(.black)
HStack (spacing: 5) {
Text(currentDate.format("dd"))
Text(vm.selectedDay.format("dd"))
.font(.system(size: 20, weight: .bold))
.foregroundStyle(Color("grayForDate"))
Text(currentDate.format("MMMM"))
Text(vm.selectedDay.format("MMMM"))
.font(.system(size: 20, weight: .bold))
.foregroundStyle(Color("grayForDate"))
Spacer()
@ -91,7 +73,7 @@ struct MainView: View {
Spacer()
}
if (!isShowingMonthSlider) {
WeekTabView(currentWeekIndex: $currentWeekIndex, weekSlider: $weekSlider, currentDate: $currentDate, vm: vm)
WeekTabView(vm: vm)
.transition(.opacity)
}
else {

View File

@ -10,75 +10,59 @@ import SwiftUI
struct ScheduleView: View {
@ObservedObject var vm: ViewModel
var body: some View {
ZStack (alignment: .top) {
ScrollView(.vertical, showsIndicators: false) {
VStack (spacing: 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: 10) {
VStack {
Text(convertTimeString(vm.classes[1][lessonIndex])[0])
.font(.system(size: 15, weight: .regular))
Text(convertTimeString(vm.classes[1][lessonIndex])[1])
.font(.system(size: 15, weight: .regular))
if vm.isLoading {
LoadingView(isLoading: $vm.isLoading)
}
else {
ZStack (alignment: .top) {
ScrollView(.vertical, showsIndicators: false) {
VStack (spacing: 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: 10) {
VStack {
Text(convertTimeString(vm.classes[1][lessonIndex])[0])
.font(.system(size: 15, weight: .regular))
Text(convertTimeString(vm.classes[1][lessonIndex])[1])
.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(getColorForClass(lesson))
Text(lesson)
.font(.system(size: 18, weight: .regular))
.padding(.top, 7)
.padding(.bottom, 7)
Spacer()
}
.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(onlineOrOffline(lesson) ? Color("greenForOffline") : Color("blueForOnline"))
Text(lesson)
.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)
}
}
}
}
.frame(width: UIScreen.main.bounds.width)
.padding(.bottom, 100)
.padding(.top, 30)
}
.frame(width: UIScreen.main.bounds.width)
.padding(.bottom, 100)
.padding(.top, 30)
VStack {
LinearGradient(gradient: Gradient(colors: [Color("background").opacity(0.9), Color("background").opacity(0.89)]), startPoint: .top, endPoint: .bottom)
}
.frame(width: UIScreen.main.bounds.width, height: 15)
}
VStack {
LinearGradient(gradient: Gradient(colors: [Color("background").opacity(0.9), Color("background").opacity(0.89)]), startPoint: .top, endPoint: .bottom)
// Rectangle()
// .frame(width: UIScreen.main.bounds.width, height: 25)
// .foregroundColor(Color("background").opacity(0.9))
}
.frame(width: UIScreen.main.bounds.width, height: 15)
}
}
func convertTimeString(_ input: String) -> [String] {
let parts = input.split(separator: "-")
if let firstPart = parts.first, let lastPart = parts.last {
return [String(firstPart), String(lastPart)]
} else {
return []
}
}
func onlineOrOffline(_ str: String) -> Bool {
if (MockData.onlineClasses.contains(str)) {
return false
}
else {
return true
}
}
}

View File

@ -1,14 +0,0 @@
//
// Tab.swift
// Schedule ICTIS
//
// Created by G412 on 13.11.2024.
//
import SwiftUI
enum TabBarModel: String, CaseIterable {
case schedule = "house"
case tasks = "books.vertical"
case settings = "gear"
}

View File

@ -11,7 +11,6 @@ struct MonthTabView: View {
@State private var currentMonthIndex: Int = 1
@State private var monthSlider: [[Date.MonthWeek]] = []
@State private var createMonth: Bool = false
@State private var currentDate: Date = .init()
@ObservedObject var vm: ViewModel
var body: some View {
VStack {
@ -39,32 +38,55 @@ struct MonthTabView: View {
.tabViewStyle(.page(indexDisplayMode: .never))
//.background(Color.green)
}
.onAppear(perform: {
vm.updateSelectedDayIndex()
if monthSlider.isEmpty {
let currentMonth = Date().fetchMonth(vm.selectedDay)
if let firstDate = currentMonth.first?.week[0].date {
monthSlider.append(firstDate.createPreviousMonth())
}
monthSlider.append(currentMonth)
if let lastDate = currentMonth.last?.week[6].date {
monthSlider.append(lastDate.createNextMonth())
}
}
})
.onChange(of: currentMonthIndex, initial: false) { oldValue, newValue in
if newValue == 0 || newValue == (monthSlider.count - 1) {
createMonth = true
}
}
.onAppear(perform: {
currentDate = vm.selectedDay
vm.updateSelectedDayIndex(currentDate)
if monthSlider.isEmpty {
let currentMonth = Date().fetchMonth(vm.selectedDay)
monthSlider.append(currentMonth)
}
})
}
@ViewBuilder
func MonthView(_ month: [Date.MonthWeek]) -> some View {
VStack {
VStack (spacing: 10) {
ForEach(month.indices, id: \.self) { index in
let week = month[index].week
WeekView(week)
}
}
.background {
GeometryReader {
let minX = $0.frame(in: .global).minX
Color.clear
.preference(key: OffsetKey.self, value: minX)
.onPreferenceChange(OffsetKey.self) { value in
if (abs(value.rounded()) - 20) < 5 && createMonth {
paginateMonth()
createMonth = false
}
}
}
}
}
@ViewBuilder
func WeekView(_ week: [Date.WeekDay]) -> some View {
HStack (spacing: 23) {
@ -72,18 +94,18 @@ struct MonthTabView: View {
VStack {
Text(day.date.format("dd"))
.font(.system(size: 15, weight: .bold))
.foregroundStyle(isDateInCurrentMonth(day.date) ? isSameDate(day.date, currentDate) ? Color.white : Color.black: isSameDate(day.date, currentDate) ? Color.white : Color("greyForDaysInMonthTabView"))
.foregroundStyle(isDateInCurrentMonth(day.date) ? isSameDate(day.date, vm.selectedDay) ? Color.white : Color.black: isSameDate(day.date, vm.selectedDay) ? Color.white : Color("greyForDaysInMonthTabView"))
}
.frame(width: 30, height: 30, alignment: .center)
.background( content: {
Group {
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
else {
Color("background")
}
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
}
@ -91,7 +113,7 @@ struct MonthTabView: View {
)
.overlay (
Group {
if day.date.isToday && !isSameDate(day.date, currentDate) {
if day.date.isToday && !isSameDate(day.date, vm.selectedDay) {
RoundedRectangle(cornerRadius: 100)
.stroke(Color("blueColor"), lineWidth: 2)
}
@ -99,12 +121,60 @@ struct MonthTabView: View {
)
.cornerRadius(15)
.onTapGesture {
currentDate = day.date
vm.updateSelectedDayIndex(currentDate)
if isSameWeek(day.date, vm.selectedDay) {
print("На одной неделе")
}
else {
var difBetweenWeeks = weeksBetween(startDate: vm.selectedDay, endDate: day.date)
if day.date < vm.selectedDay {
difBetweenWeeks = difBetweenWeeks * -1
}
print(difBetweenWeeks)
vm.fetchWeekSchedule("", difBetweenWeeks)
}
vm.selectedDay = day.date
vm.updateSelectedDayIndex()
}
}
}
}
func paginateMonth() {
let calendar = Calendar.current
if monthSlider.indices.contains(currentMonthIndex) {
if let firstDate = monthSlider[currentMonthIndex].first?.week[0].date,
currentMonthIndex == 0 {
// switch (vm.numOfGroup) {
// case "":
// vm.week -= 1
// default:
// vm.fetchWeekSchedule("new week", -1)
// }
monthSlider.insert(firstDate.createPreviousMonth(), at: 0)
monthSlider.removeLast()
currentMonthIndex = 1
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -5, to: vm.selectedDay) ?? Date.init()
vm.updateSelectedDayIndex()
vm.fetchWeekSchedule("", -5)
}
if let lastDate = monthSlider[currentMonthIndex].last?.week[6].date,
currentMonthIndex == (monthSlider.count - 1) {
// switch (vm.numOfGroup) {
// case "":
// vm.week += 1
// default:
// vm.fetchWeekSchedule("new week", 1)
// }
monthSlider.append(lastDate.createNextMonth())
monthSlider.removeFirst()
currentMonthIndex = monthSlider.count - 2
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 5, to: vm.selectedDay) ?? Date.init()
vm.updateSelectedDayIndex()
vm.fetchWeekSchedule("", 5)
}
}
}
}
#Preview {

View File

@ -8,9 +8,8 @@
import SwiftUI
struct WeekTabView: View {
@Binding var currentWeekIndex: Int
@Binding var weekSlider: [[Date.WeekDay]]
@Binding var currentDate: Date
@State private var currentWeekIndex: Int = 1
@State private var weekSlider: [[Date.WeekDay]] = []
@State private var createWeek: Bool = false
@ObservedObject var vm: ViewModel
var body: some View {
@ -27,6 +26,22 @@ struct WeekTabView: View {
.tabViewStyle(.page(indexDisplayMode: .never))
.frame(height: 90)
}
.onAppear(perform: {
vm.updateSelectedDayIndex()
if weekSlider.isEmpty {
let currentWeek = Date().fetchWeek(vm.selectedDay)
if let firstDate = currentWeek.first?.date {
weekSlider.append(firstDate.createPrevioustWeek())
}
weekSlider.append(currentWeek)
if let lastDate = currentWeek.last?.date {
weekSlider.append(lastDate.createNextWeek())
}
}
})
.onChange(of: currentWeekIndex, initial: false) { oldValue, newValue in
if newValue == 0 || newValue == (weekSlider.count - 1) {
createWeek = true
@ -41,24 +56,24 @@ struct WeekTabView: View {
VStack (spacing: 1) {
Text(day.date.format("E"))
.font(.system(size: 15, weight: .semibold))
.foregroundColor(day.date.format("E") == "Вс" ? Color(.red) : isSameDate(day.date, currentDate) ? Color("customGray1") : Color("customGray3"))
.foregroundColor(day.date.format("E") == "Вс" ? Color(.red) : isSameDate(day.date, vm.selectedDay) ? Color("customGray1") : Color("customGray3"))
.padding(.top, 13)
.foregroundColor(.gray)
Text(day.date.format("dd"))
.font(.system(size: 15, weight: .bold))
.foregroundStyle(isSameDate(day.date, currentDate) ? .white : .black)
.foregroundStyle(isSameDate(day.date, vm.selectedDay) ? .white : .black)
.padding(.bottom, 13)
}
.frame(width: 43, height: 55, alignment: .center)
.background( content: {
Group {
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
else {
Color(.white)
}
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
}
@ -66,7 +81,7 @@ struct WeekTabView: View {
)
.overlay (
Group {
if day.date.isToday && !isSameDate(day.date, currentDate) {
if day.date.isToday && !isSameDate(day.date, vm.selectedDay) {
RoundedRectangle(cornerRadius: 15)
.stroke(Color("blueColor"), lineWidth: 2)
}
@ -74,8 +89,8 @@ struct WeekTabView: View {
)
.cornerRadius(15)
.onTapGesture {
currentDate = day.date
vm.updateSelectedDayIndex(currentDate)
vm.selectedDay = day.date
vm.updateSelectedDayIndex()
}
}
}
@ -111,7 +126,7 @@ struct WeekTabView: View {
weekSlider.removeLast()
currentWeekIndex = 1
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -1, to: vm.selectedDay) ?? Date.init()
currentDate = vm.selectedDay
vm.updateSelectedDayIndex()
}
if let lastDate = weekSlider[currentWeekIndex].last?.date,
@ -126,8 +141,7 @@ struct WeekTabView: View {
weekSlider.removeFirst()
currentWeekIndex = weekSlider.count - 2
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 1, to: vm.selectedDay) ?? Date.init()
currentDate = vm.selectedDay
print(currentDate)
vm.updateSelectedDayIndex()
}
}
}