From 0f8dcea0a60e37e5991e36d8216a9149cdc8eabb Mon Sep 17 00:00:00 2001 From: Vladimir Dubovik Date: Thu, 5 Dec 2024 14:00:41 +0300 Subject: [PATCH] ViewModel was changed and searchbar is working now. You can text your group and find your schedule --- Schedule ICTIS/ContentView.swift | 2 +- Schedule ICTIS/Helpers/Date+Extensions.swift | 3 +- .../MainView.swift} | 76 ++----------------- Schedule ICTIS/Main/ScheduleView.swift | 55 ++++++++++++++ Schedule ICTIS/{ => Main}/SearchBarView.swift | 12 ++- .../{ => Main}/TabBar/TabBarModel.swift | 0 .../{ => Main}/TabBar/TabBarView.swift | 0 Schedule ICTIS/Network/NetworkManager.swift | 14 +++- Schedule ICTIS/ViewModel/ViewModel.swift | 31 ++++---- 9 files changed, 101 insertions(+), 92 deletions(-) rename Schedule ICTIS/{ScheduleView.swift => Main/MainView.swift} (61%) create mode 100644 Schedule ICTIS/Main/ScheduleView.swift rename Schedule ICTIS/{ => Main}/SearchBarView.swift (87%) rename Schedule ICTIS/{ => Main}/TabBar/TabBarModel.swift (100%) rename Schedule ICTIS/{ => Main}/TabBar/TabBarView.swift (100%) diff --git a/Schedule ICTIS/ContentView.swift b/Schedule ICTIS/ContentView.swift index 6fa3ee8..eb11f8b 100644 --- a/Schedule ICTIS/ContentView.swift +++ b/Schedule ICTIS/ContentView.swift @@ -13,7 +13,7 @@ struct ContentView: View { ZStack { switch selectedTab { case .schedule: - ScheduleView() + MainView() case .tasks: Text("Tasks") case .settings: diff --git a/Schedule ICTIS/Helpers/Date+Extensions.swift b/Schedule ICTIS/Helpers/Date+Extensions.swift index 9d17cdb..e25fe44 100644 --- a/Schedule ICTIS/Helpers/Date+Extensions.swift +++ b/Schedule ICTIS/Helpers/Date+Extensions.swift @@ -37,6 +37,7 @@ extension Date { let startOfDate = calendar.startOfDay(for: date) var week: [WeekDay] = [] + // Создаем дату начала и конца недели let weekForDate = calendar.dateInterval(of: .weekOfMonth, for: startOfDate) //print("Start: \(weekForDate?.start)") //print("End: \(weekForDate?.end)") @@ -45,7 +46,7 @@ extension Date { return [] } - // Создаем дни для недели + // Создаем массив дней для недели (0..<7).forEach { index in if let weekDay = calendar.date(byAdding: .day, value: index, to: startOfWeek) { week.append(WeekDay(date: weekDay)) diff --git a/Schedule ICTIS/ScheduleView.swift b/Schedule ICTIS/Main/MainView.swift similarity index 61% rename from Schedule ICTIS/ScheduleView.swift rename to Schedule ICTIS/Main/MainView.swift index fc7ef28..8fdcca6 100644 --- a/Schedule ICTIS/ScheduleView.swift +++ b/Schedule ICTIS/Main/MainView.swift @@ -7,7 +7,7 @@ import SwiftUI -struct ScheduleView: View { +struct MainView: View { @State private var searchText: String = "" @State private var currentDate: Date = .init() @State private var weekSlider: [[Date.WeekDay]] = [] @@ -18,61 +18,9 @@ struct ScheduleView: View { var body: some View { VStack { - SearchBarView(text: $searchText) + SearchBarView(text: $searchText, vm: vm) HeaderView() - ScrollView(.vertical, showsIndicators: false) { - VStack (spacing: 12) { - ForEach(vm.weekSchedule, id: \.week) { element in - let selectedDayIndex = vm.selectedIndex - if selectedDayIndex < 8 { - let schedule = element.table[selectedDayIndex] - ForEach(schedule.indices.dropFirst(), id: \.self) { index in - let lesson = schedule[index] - let firstThreeCharacters = lesson.prefix(3) - let checkEnglish = lesson.prefix(6) - if !lesson.isEmpty && index != 0 { - GeometryReader { geometry in - HStack (spacing: 8) { - VStack (alignment: .center) { - Text(convertTimeString(element.table[1][index])[0]) - .font(.system(size: 15, weight: .light)) - Text(convertTimeString(element.table[1][index])[1]) - .font(.system(size: 15, weight: .light)) - } - .padding(.top, 4) - .padding(.bottom, 4) - .padding(.leading, 8) - Rectangle() - .frame(maxHeight: geometry.size.height - 10) - .frame(width: 3) - .cornerRadius(20) - .padding(.top, 4) - .padding(.bottom, 4) - .foregroundColor(checkEnglish == "пр.Ино" || firstThreeCharacters == "лек" ? Color.blue : Color.green) - Text(lesson) - .font(.system(size: 16, weight: .regular)) - .padding(.trailing, 8) - .frame(maxWidth: 280, alignment: .leading) // Выравнивание текста по левому краю - .padding(.top, 4) - .padding(.bottom, 4) - .multilineTextAlignment(.leading) - - } - .background(Color.white) - .cornerRadius(20) - .padding(.horizontal, 10) - .frame(maxWidth: 420, maxHeight: 130) - .shadow(color: Color.black.opacity(0.1), radius: 2, x: 4, y: 4) - } - .frame(height: 70) - } - } - } else { - Text("Сегодня нет пар") - } - } - } - } + ScheduleView(vm: vm) } .background(Color("background")) .onAppear(perform: { @@ -121,10 +69,9 @@ struct ScheduleView: View { } } .padding(.top, 8) - .padding(.leading, 20) + .padding(.leading, 5) Spacer() } - .frame(maxWidth: .infinity) TabView(selection: $currentWeekIndex) { ForEach(weekSlider.indices, id: \.self) { index in @@ -142,8 +89,8 @@ struct ScheduleView: View { if newValue == 0 || newValue == (weekSlider.count - 1) { createWeek = true } - vm.updateSelectedIndex(newValue) } + .padding(.horizontal) } @ViewBuilder @@ -170,7 +117,7 @@ struct ScheduleView: View { else { Color(.white) } - if day.date.isToday && isSameDate(day.date, currentDate) { + if isSameDate(day.date, currentDate) { Color("blueColor") } } @@ -224,17 +171,8 @@ struct ScheduleView: View { } } } - - 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 [] - } - } } #Preview { - ScheduleView() + MainView() } diff --git a/Schedule ICTIS/Main/ScheduleView.swift b/Schedule ICTIS/Main/ScheduleView.swift new file mode 100644 index 0000000..27d5149 --- /dev/null +++ b/Schedule ICTIS/Main/ScheduleView.swift @@ -0,0 +1,55 @@ +// +// ScheduleView.swift +// Schedule ICTIS +// +// Created by G412 on 05.12.2024. +// + +import SwiftUI + +struct ScheduleView: View { + @ObservedObject var vm: ViewModel + var body: some View { + ScrollView(.vertical, showsIndicators: false) { + VStack (spacing: 12) { + 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: 6) { + VStack { + Text(convertTimeString(vm.classes[1][lessonIndex])[0]) + .font(.system(size: 15, weight: .light)) + Text(convertTimeString(vm.classes[1][lessonIndex])[1]) + .font(.system(size: 15, weight: .light)) + } + Rectangle() + .frame(width: 2) + .frame(maxHeight: 100) + Text(lesson) + } + .background(Color.white) + .padding(.horizontal) + } + } + } + } + } + } + } + + 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 [] + } + } +} + +#Preview { + MainView() +} diff --git a/Schedule ICTIS/SearchBarView.swift b/Schedule ICTIS/Main/SearchBarView.swift similarity index 87% rename from Schedule ICTIS/SearchBarView.swift rename to Schedule ICTIS/Main/SearchBarView.swift index 8d67994..5c289ce 100644 --- a/Schedule ICTIS/SearchBarView.swift +++ b/Schedule ICTIS/Main/SearchBarView.swift @@ -10,6 +10,7 @@ import SwiftUI struct SearchBarView: View { @Binding var text: String @State private var isEditing = false + @ObservedObject var vm: ViewModel var body: some View { HStack (spacing: 11) { @@ -21,11 +22,15 @@ struct SearchBarView: View { TextField("Поиск группы", text: $text) .disableAutocorrection(true) .onTapGesture { - self.isEditing = true + isEditing = true } .onSubmit { - self.isEditing = false + isEditing = false + if (!text.isEmpty) { + vm.fetchWeekSchedule(text) + } } + .submitLabel(.search) if isEditing { Button { self.text = "" @@ -66,9 +71,10 @@ struct SearchBarView: View { .padding(.horizontal) .padding(.top, 5) .frame(height: 40) + .accentColor(.blue) } } #Preview { - ScheduleView() + MainView() } diff --git a/Schedule ICTIS/TabBar/TabBarModel.swift b/Schedule ICTIS/Main/TabBar/TabBarModel.swift similarity index 100% rename from Schedule ICTIS/TabBar/TabBarModel.swift rename to Schedule ICTIS/Main/TabBar/TabBarModel.swift diff --git a/Schedule ICTIS/TabBar/TabBarView.swift b/Schedule ICTIS/Main/TabBar/TabBarView.swift similarity index 100% rename from Schedule ICTIS/TabBar/TabBarView.swift rename to Schedule ICTIS/Main/TabBar/TabBarView.swift diff --git a/Schedule ICTIS/Network/NetworkManager.swift b/Schedule ICTIS/Network/NetworkManager.swift index 3906615..1609ab3 100644 --- a/Schedule ICTIS/Network/NetworkManager.swift +++ b/Schedule ICTIS/Network/NetworkManager.swift @@ -12,8 +12,11 @@ final class NetworkManager { //MARK: Properties static let shared = NetworkManager() private let decoder = JSONDecoder() - private let urlForGroup = "https://webictis.sfedu.ru/schedule-api/?query=ктбо2-6" + private let urlForGroup = "https://webictis.sfedu.ru/schedule-api/?query=" private let urlForWeek = "https://webictis.sfedu.ru/schedule-api/?group=51.html&week=15" + private var groupString: String = "" + private var numOfGroup: String = "" + private var numOfWeek: String = "" //MARK: Initializer private init() { @@ -21,8 +24,13 @@ final class NetworkManager { } //MARK: Methods - func getSchedule() async throws -> Schedule { - guard let url = URL(string: urlForGroup) else { throw NetworkError.invalidUrl} + func makeURL(_ group: String) -> String { + return urlForGroup + group + } + + func getSchedule(_ group: String) async throws -> Schedule { + let newUrlForGroup = makeURL(group) + guard let url = URL(string: newUrlForGroup) else {throw NetworkError.invalidUrl} let (data, response) = try await URLSession.shared.data(from: url) guard let response = response as? HTTPURLResponse, response.statusCode == 200 else {throw NetworkError.invalidResponse} diff --git a/Schedule ICTIS/ViewModel/ViewModel.swift b/Schedule ICTIS/ViewModel/ViewModel.swift index bda6c54..ce54557 100644 --- a/Schedule ICTIS/ViewModel/ViewModel.swift +++ b/Schedule ICTIS/ViewModel/ViewModel.swift @@ -10,20 +10,31 @@ import Foundation @MainActor final class ViewModel: ObservableObject { //MARK: Properties - @Published var weekSchedule: [Table] = [] + @Published var weekSchedule: Table = Table( + type: "", + name: "", + week: 0, + group: "", + table: [[]], + link: "" + ) @Published var selectedDay: Date = Date() @Published var selectedIndex: Int = 1 + @Published var classes: [[String]] = [] init() { - fetchWeekSchedule() + } + //MARK: Methods - func fetchWeekSchedule() { + func fetchWeekSchedule(_ group: String) { Task { do { - let schedule = try await NetworkManager.shared.getSchedule() - weekSchedule = [schedule.table] + let schedule = try await NetworkManager.shared.getSchedule(group) + weekSchedule = schedule.table + classes = weekSchedule.table + print(weekSchedule.week) } catch { if let error = error as? NetworkError { @@ -33,12 +44,6 @@ final class ViewModel: ObservableObject { } } - func getSelectedDaySchedule(for date: Date) -> [String]? { - guard let week = weekSchedule.first else { return nil } - let dayIndex = week.table.firstIndex { $0[0].contains(date.format("dd MMMM")) } - return dayIndex.flatMap { week.table[$0] } - } - func updateSelectedDayIndex(_ date: Date) { switch date.format("E") { case "Пн": @@ -59,8 +64,4 @@ final class ViewModel: ObservableObject { print(selectedIndex) } - func updateSelectedIndex(_ index: Int) { - selectedIndex = index - print(selectedIndex) - } }