Lots of changes
This commit is contained in:
parent
e7510aef68
commit
955c7d9066
@ -20,20 +20,25 @@ struct MainView: View {
|
||||
var body: some View {
|
||||
VStack {
|
||||
SearchBarView(text: $searchText, vm: vm)
|
||||
CurrentDateView()
|
||||
if (vm.isFirstStartOffApp) {
|
||||
FirstLaunchScheduleView()
|
||||
}
|
||||
else {
|
||||
CurrentDateView()
|
||||
ScheduleView(vm: vm)
|
||||
}
|
||||
}
|
||||
.alert(isPresented: $vm.isShowingAlertForIncorrectGroup, error: vm.errorInNetwork) { error in
|
||||
|
||||
} message: { error in
|
||||
Text(error.failureReason)
|
||||
}
|
||||
.background(Color("background"))
|
||||
.onAppear(perform: {
|
||||
currentDate = vm.selectedDay
|
||||
vm.updateSelectedDayIndex(currentDate)
|
||||
if weekSlider.isEmpty {
|
||||
let currentWeek = Date().fetchWeek()
|
||||
let currentWeek = Date().fetchWeek(vm.selectedDay)
|
||||
|
||||
if let firstDate = currentWeek.first?.date {
|
||||
weekSlider.append(firstDate.createPrevioustWeek())
|
||||
@ -163,21 +168,37 @@ struct MainView: View {
|
||||
}
|
||||
|
||||
func paginateWeek() {
|
||||
let calendar = Calendar.current
|
||||
if weekSlider.indices.contains(currentWeekIndex) {
|
||||
if let firstDate = weekSlider[currentWeekIndex].first?.date,
|
||||
currentWeekIndex == 0 {
|
||||
vm.fetchWeekSchedule("new week", -1)
|
||||
switch (vm.numOfGroup) {
|
||||
case "":
|
||||
vm.week -= 1
|
||||
default:
|
||||
vm.fetchWeekSchedule("new week", -1)
|
||||
}
|
||||
weekSlider.insert(firstDate.createPrevioustWeek(), at: 0)
|
||||
weekSlider.removeLast()
|
||||
currentWeekIndex = 1
|
||||
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -1, to: vm.selectedDay) ?? Date.init()
|
||||
currentDate = vm.selectedDay
|
||||
}
|
||||
|
||||
if let lastDate = weekSlider[currentWeekIndex].last?.date,
|
||||
currentWeekIndex == (weekSlider.count - 1) {
|
||||
vm.fetchWeekSchedule("new week", 1)
|
||||
switch (vm.numOfGroup) {
|
||||
case "":
|
||||
vm.week += 1
|
||||
default:
|
||||
vm.fetchWeekSchedule("new week", 1)
|
||||
}
|
||||
weekSlider.append(lastDate.createNextWeek())
|
||||
weekSlider.removeFirst()
|
||||
currentWeekIndex = weekSlider.count - 2
|
||||
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 1, to: vm.selectedDay) ?? Date.init()
|
||||
currentDate = vm.selectedDay
|
||||
print(currentDate)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -43,16 +43,17 @@ struct ScheduleView: View {
|
||||
.frame(maxWidth: UIScreen.main.bounds.width - 40, maxHeight: 230)
|
||||
.background(Color.white)
|
||||
.cornerRadius(20)
|
||||
.shadow(color: .black.opacity(0.25), radius: 4, x: 4, y: 4)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
.frame(width: UIScreen.main.bounds.width)
|
||||
.padding(.bottom, 100)
|
||||
.padding(.top, 10)
|
||||
}
|
||||
}
|
||||
//ктбо2-6
|
||||
|
||||
func convertTimeString(_ input: String) -> [String] {
|
||||
let parts = input.split(separator: "-")
|
||||
|
@ -22,14 +22,14 @@ struct SearchBarView: View {
|
||||
TextField("Поиск группы", text: $text)
|
||||
.disableAutocorrection(true)
|
||||
.onTapGesture {
|
||||
isEditing = true
|
||||
self.isEditing = true
|
||||
}
|
||||
.onSubmit {
|
||||
isEditing = false
|
||||
self.isEditing = false
|
||||
if (!text.isEmpty) {
|
||||
vm.isFirstStartOffApp = false
|
||||
vm.fetchWeekSchedule(text)
|
||||
}
|
||||
self.text = ""
|
||||
}
|
||||
.submitLabel(.search)
|
||||
if isEditing {
|
||||
@ -53,19 +53,20 @@ struct SearchBarView: View {
|
||||
RoundedRectangle(cornerRadius: 10)
|
||||
.fill(.white)
|
||||
)
|
||||
Button {
|
||||
|
||||
} label: {
|
||||
ZStack {
|
||||
Rectangle()
|
||||
.frame(width: 40, height: 40)
|
||||
.foregroundStyle(Color("blueColor"))
|
||||
.cornerRadius(15)
|
||||
Image(systemName: "plus")
|
||||
.resizable()
|
||||
.foregroundStyle(.white)
|
||||
.scaledToFit()
|
||||
.frame(width: 16)
|
||||
if (!vm.isFirstStartOffApp && !vm.isShowingAlertForIncorrectGroup) {
|
||||
Button {
|
||||
} label: {
|
||||
ZStack {
|
||||
Rectangle()
|
||||
.frame(width: 40, height: 40)
|
||||
.foregroundStyle(Color("blueColor"))
|
||||
.cornerRadius(15)
|
||||
Image(systemName: "plus")
|
||||
.resizable()
|
||||
.foregroundStyle(.white)
|
||||
.scaledToFit()
|
||||
.frame(width: 16)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -7,8 +7,35 @@
|
||||
|
||||
import Foundation
|
||||
|
||||
enum NetworkError: String, Error {
|
||||
case invalidUrl = "Invalid URL"
|
||||
case invalidResponse = "Invalid response form the server"
|
||||
case invalidData = "Data received from the server is invalid"
|
||||
enum NetworkError: String, Error, LocalizedError {
|
||||
case invalidUrl
|
||||
case invalidResponse
|
||||
case invalidData
|
||||
case noNetwork
|
||||
|
||||
var errorDescription: String? {
|
||||
switch self {
|
||||
case .invalidUrl:
|
||||
"InvalidUrl"
|
||||
case .invalidResponse:
|
||||
"InvalidResponse"
|
||||
case .invalidData:
|
||||
"Проверьте номер группы"
|
||||
case .noNetwork:
|
||||
"No network connection"
|
||||
}
|
||||
}
|
||||
|
||||
var failureReason: String {
|
||||
switch self {
|
||||
case .invalidUrl:
|
||||
"Похоже не удалось составить ссылку для api"
|
||||
case .invalidResponse:
|
||||
"Для этой недели расписания еще нет"
|
||||
case .invalidData:
|
||||
"Похоже такой группы не существует"
|
||||
case .noNetwork:
|
||||
"Проверьте подключение к интернету и попробуйте заново"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ final class NetworkManager {
|
||||
}
|
||||
|
||||
func makeUrlForWeek(_ numOfWeek: Int, _ htmlNameOfGroup: String) -> String {
|
||||
return urlForWeek + htmlNameOfGroup + "&" + String(numOfWeek)
|
||||
return urlForWeek + htmlNameOfGroup + "&week=" + String(numOfWeek)
|
||||
}
|
||||
|
||||
func getSchedule(_ group: String) async throws -> Schedule {
|
||||
@ -42,8 +42,10 @@ final class NetworkManager {
|
||||
throw NetworkError.invalidData
|
||||
}
|
||||
}
|
||||
|
||||
func getScheduleForOtherWeek(_ numOfWeek: Int, _ htmlNameOfGroup: String) async throws -> Schedule {
|
||||
let newUrlForWeek = makeUrlForWeek(numOfWeek, htmlNameOfGroup)
|
||||
print(newUrlForWeek)
|
||||
guard let url = URL(string: newUrlForWeek) 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}
|
||||
|
@ -24,14 +24,17 @@ final class ViewModel: ObservableObject {
|
||||
@Published var week: Int = 0
|
||||
@Published var numOfGroup: String = ""
|
||||
@Published var isFirstStartOffApp = true
|
||||
@Published var isShowingAlertForIncorrectGroup: Bool = false
|
||||
@Published var errorInNetwork: NetworkError?
|
||||
|
||||
//MARK: Methods
|
||||
func fetchWeekSchedule(_ group: String = "new week", _ num: Int = 0) {
|
||||
func fetchWeekSchedule(_ group: String, _ num: Int = 0) {
|
||||
Task {
|
||||
do {
|
||||
let schedule: Schedule
|
||||
if (group == "new week") {
|
||||
schedule = try await NetworkManager.shared.getScheduleForOtherWeek(week + num, numOfGroup)
|
||||
var schedule: Schedule
|
||||
if (num != 0) {
|
||||
week += num
|
||||
schedule = try await NetworkManager.shared.getScheduleForOtherWeek(week, numOfGroup)
|
||||
}
|
||||
else{
|
||||
schedule = try await NetworkManager.shared.getSchedule(group)
|
||||
@ -39,12 +42,21 @@ final class ViewModel: ObservableObject {
|
||||
weekSchedule = schedule.table
|
||||
week = weekSchedule.week
|
||||
numOfGroup = weekSchedule.group
|
||||
print(week)
|
||||
print(numOfGroup)
|
||||
classes = weekSchedule.table
|
||||
self.isFirstStartOffApp = false
|
||||
self.isShowingAlertForIncorrectGroup = false
|
||||
}
|
||||
catch {
|
||||
if let error = error as? NetworkError {
|
||||
switch (error) {
|
||||
case .invalidResponse:
|
||||
print(4)
|
||||
case .invalidData:
|
||||
errorInNetwork = .invalidData
|
||||
self.isShowingAlertForIncorrectGroup = true
|
||||
default:
|
||||
print(2)
|
||||
}
|
||||
print(error)
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user