ViewModel was changed and searchbar is working now. You can text your group and find your schedule

This commit is contained in:
Vladimir Dubovik
2024-12-05 14:00:41 +03:00
parent ddceec8551
commit 0f8dcea0a6
9 changed files with 101 additions and 92 deletions

View File

@ -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)
}
}