Fixed bug with new number of group on not current week
This commit is contained in:
parent
3eb5fb73eb
commit
4c3a46d40e
@ -28,7 +28,7 @@ struct SearchBarView: View {
|
|||||||
.onSubmit {
|
.onSubmit {
|
||||||
self.isFocused = false
|
self.isFocused = false
|
||||||
if (!text.isEmpty) {
|
if (!text.isEmpty) {
|
||||||
vm.fetchWeekSchedule(text)
|
vm.fetchWeekSchedule(group: text)
|
||||||
vm.group = text
|
vm.group = text
|
||||||
}
|
}
|
||||||
self.text = ""
|
self.text = ""
|
||||||
@ -54,7 +54,7 @@ struct SearchBarView: View {
|
|||||||
RoundedRectangle(cornerRadius: 10)
|
RoundedRectangle(cornerRadius: 10)
|
||||||
.fill(.white)
|
.fill(.white)
|
||||||
)
|
)
|
||||||
if (!vm.isFirstStartOffApp) {
|
if (!vm.isFirstStartOffApp && !isFocused) {
|
||||||
Button {
|
Button {
|
||||||
isShowingSheet = true
|
isShowingSheet = true
|
||||||
} label: {
|
} label: {
|
||||||
|
@ -185,16 +185,6 @@ struct CreateEditClassView: View {
|
|||||||
.background(Color("background"))
|
.background(Color("background"))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
func delete(_ _class: ClassModel) throws {
|
|
||||||
let context = provider.viewContext
|
|
||||||
let existingClass = try context.existingObject(with: _class.objectID)
|
|
||||||
context.delete(existingClass)
|
|
||||||
Task (priority: .background) {
|
|
||||||
try await context.perform {
|
|
||||||
try context.save()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#Preview {
|
#Preview {
|
||||||
|
@ -38,26 +38,22 @@ struct MonthTabView: View {
|
|||||||
.tabViewStyle(.page(indexDisplayMode: .never))
|
.tabViewStyle(.page(indexDisplayMode: .never))
|
||||||
}
|
}
|
||||||
.onAppear(perform: {
|
.onAppear(perform: {
|
||||||
vm.updateSelectedDayIndex()
|
updateMonthScreenViewForNewGroup()
|
||||||
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
|
.onChange(of: currentMonthIndex, initial: false) { oldValue, newValue in
|
||||||
if newValue == 0 || newValue == (monthSlider.count - 1) {
|
if newValue == 0 || newValue == (monthSlider.count - 1) {
|
||||||
createMonth = true
|
createMonth = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onChange(of: vm.isNewGroup, initial: false) { oldValue, newValue in
|
||||||
|
if newValue {
|
||||||
|
monthSlider.removeAll()
|
||||||
|
currentMonthIndex = 1
|
||||||
|
updateMonthScreenViewForNewGroup()
|
||||||
|
print(52)
|
||||||
|
vm.isNewGroup = false
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ViewBuilder
|
@ViewBuilder
|
||||||
@ -96,7 +92,7 @@ struct MonthTabView: View {
|
|||||||
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -5, to: vm.selectedDay) ?? Date.init()
|
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -5, to: vm.selectedDay) ?? Date.init()
|
||||||
vm.updateSelectedDayIndex()
|
vm.updateSelectedDayIndex()
|
||||||
vm.week -= 5
|
vm.week -= 5
|
||||||
vm.fetchWeekSchedule("")
|
vm.fetchWeekSchedule(isOtherWeek: true)
|
||||||
}
|
}
|
||||||
|
|
||||||
if let lastDate = monthSlider[currentMonthIndex].last?.week[6].date,
|
if let lastDate = monthSlider[currentMonthIndex].last?.week[6].date,
|
||||||
@ -107,7 +103,26 @@ struct MonthTabView: View {
|
|||||||
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 5, to: vm.selectedDay) ?? Date.init()
|
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 5, to: vm.selectedDay) ?? Date.init()
|
||||||
vm.updateSelectedDayIndex()
|
vm.updateSelectedDayIndex()
|
||||||
vm.week += 5
|
vm.week += 5
|
||||||
vm.fetchWeekSchedule("")
|
vm.fetchWeekSchedule(isOtherWeek: true)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension MonthTabView {
|
||||||
|
func updateMonthScreenViewForNewGroup() {
|
||||||
|
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())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -27,26 +27,41 @@ struct WeekTabView: View {
|
|||||||
.frame(height: 90)
|
.frame(height: 90)
|
||||||
}
|
}
|
||||||
.onAppear(perform: {
|
.onAppear(perform: {
|
||||||
vm.updateSelectedDayIndex()
|
updateWeekScreenViewForNewGroup()
|
||||||
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
|
.onChange(of: currentWeekIndex, initial: false) { oldValue, newValue in
|
||||||
if newValue == 0 || newValue == (weekSlider.count - 1) {
|
if newValue == 0 || newValue == (weekSlider.count - 1) {
|
||||||
createWeek = true
|
createWeek = true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
.onChange(of: vm.isNewGroup, initial: false) { oldValue, newValue in
|
||||||
|
if newValue {
|
||||||
|
weekSlider.removeAll()
|
||||||
|
currentWeekIndex = 1
|
||||||
|
updateWeekScreenViewForNewGroup()
|
||||||
|
print(52)
|
||||||
|
vm.isNewGroup = false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
extension WeekTabView {
|
||||||
|
func updateWeekScreenViewForNewGroup() {
|
||||||
|
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())
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -62,7 +62,7 @@ struct WeekViewForMonth: View {
|
|||||||
}
|
}
|
||||||
print(difBetweenWeeks)
|
print(difBetweenWeeks)
|
||||||
vm.week += difBetweenWeeks
|
vm.week += difBetweenWeeks
|
||||||
vm.fetchWeekSchedule("")
|
vm.fetchWeekSchedule(isOtherWeek: true)
|
||||||
}
|
}
|
||||||
vm.selectedDay = day.date
|
vm.selectedDay = day.date
|
||||||
vm.updateSelectedDayIndex()
|
vm.updateSelectedDayIndex()
|
||||||
|
@ -79,7 +79,7 @@ struct WeekViewForWeek: View {
|
|||||||
if let firstDate = weekSlider[currentWeekIndex].first?.date,
|
if let firstDate = weekSlider[currentWeekIndex].first?.date,
|
||||||
currentWeekIndex == 0 {
|
currentWeekIndex == 0 {
|
||||||
vm.week -= 1
|
vm.week -= 1
|
||||||
vm.fetchWeekSchedule("")
|
vm.fetchWeekSchedule(isOtherWeek: true)
|
||||||
weekSlider.insert(firstDate.createPrevioustWeek(), at: 0)
|
weekSlider.insert(firstDate.createPrevioustWeek(), at: 0)
|
||||||
weekSlider.removeLast()
|
weekSlider.removeLast()
|
||||||
currentWeekIndex = 1
|
currentWeekIndex = 1
|
||||||
@ -90,7 +90,7 @@ struct WeekViewForWeek: View {
|
|||||||
if let lastDate = weekSlider[currentWeekIndex].last?.date,
|
if let lastDate = weekSlider[currentWeekIndex].last?.date,
|
||||||
currentWeekIndex == (weekSlider.count - 1) {
|
currentWeekIndex == (weekSlider.count - 1) {
|
||||||
vm.week += 1
|
vm.week += 1
|
||||||
vm.fetchWeekSchedule("")
|
vm.fetchWeekSchedule(isOtherWeek: true)
|
||||||
weekSlider.append(lastDate.createNextWeek())
|
weekSlider.append(lastDate.createNextWeek())
|
||||||
weekSlider.removeFirst()
|
weekSlider.removeFirst()
|
||||||
currentWeekIndex = weekSlider.count - 2
|
currentWeekIndex = weekSlider.count - 2
|
||||||
|
@ -23,6 +23,7 @@ final class ClassModel: NSManagedObject, Identifiable {
|
|||||||
static var dateNow: Date = .now
|
static var dateNow: Date = .now
|
||||||
|
|
||||||
// Здесь мы выполняем дополнительную инициализацию, назначая значения по умолчанию
|
// Здесь мы выполняем дополнительную инициализацию, назначая значения по умолчанию
|
||||||
|
// Этот метод вызывается всякий раз, когда объект Core Data вставляется в контекст
|
||||||
override func awakeFromInsert() {
|
override func awakeFromInsert() {
|
||||||
super.awakeFromInsert()
|
super.awakeFromInsert()
|
||||||
|
|
||||||
|
@ -25,6 +25,7 @@ final class ClassProvider {
|
|||||||
|
|
||||||
var newContext: NSManagedObjectContext {
|
var newContext: NSManagedObjectContext {
|
||||||
//persistentContainer.newBackgroundContext()
|
//persistentContainer.newBackgroundContext()
|
||||||
|
//Можно использовать объявление newContext с помощью строки, которая написана выше, но вариант ниже потокобезопаснее
|
||||||
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
|
let context = NSManagedObjectContext(concurrencyType: .mainQueueConcurrencyType)
|
||||||
context.persistentStoreCoordinator = persistentContainer.persistentStoreCoordinator
|
context.persistentStoreCoordinator = persistentContainer.persistentStoreCoordinator
|
||||||
return context
|
return context
|
||||||
@ -37,7 +38,7 @@ final class ClassProvider {
|
|||||||
persistentContainer.persistentStoreDescriptions.first?.url = .init(filePath: "/dev/null")
|
persistentContainer.persistentStoreDescriptions.first?.url = .init(filePath: "/dev/null")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Выставляем флаг для автоматического сохранения изменений данных из Veiw в память
|
// Выставляем флаг для автоматического слияния данных из фонового контекста в основной
|
||||||
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
|
persistentContainer.viewContext.automaticallyMergesChangesFromParent = true
|
||||||
|
|
||||||
// Выполняем открытие файла с данными
|
// Выполняем открытие файла с данными
|
||||||
|
@ -151,3 +151,16 @@ extension View {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
extension CreateEditClassView {
|
||||||
|
func delete(_ _class: ClassModel) throws {
|
||||||
|
let context = provider.viewContext
|
||||||
|
let existingClass = try context.existingObject(with: _class.objectID)
|
||||||
|
context.delete(existingClass)
|
||||||
|
Task (priority: .background) {
|
||||||
|
try await context.perform {
|
||||||
|
try context.save()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -28,18 +28,25 @@ final class ScheduleViewModel: ObservableObject {
|
|||||||
@Published var errorInNetwork: NetworkError?
|
@Published var errorInNetwork: NetworkError?
|
||||||
@Published var isLoading: Bool = false
|
@Published var isLoading: Bool = false
|
||||||
@Published var group: String = ""
|
@Published var group: String = ""
|
||||||
|
@Published var isNewGroup: Bool = false
|
||||||
|
|
||||||
//MARK: Methods
|
//MARK: Methods
|
||||||
func fetchWeekSchedule(_ group: String) {
|
func fetchWeekSchedule(group: String = "default", isOtherWeek: Bool = false) {
|
||||||
isLoading = true
|
isLoading = true
|
||||||
Task {
|
Task {
|
||||||
do {
|
do {
|
||||||
var schedule: Schedule
|
var schedule: Schedule
|
||||||
if !self.numOfGroup.isEmpty {
|
// В этот if мы заходим только если пользователь перелистывает недели и нам известы номер группы(в html формате) и номер неделе, которая показывается пользователю
|
||||||
|
if (isOtherWeek || !isFirstStartOffApp) && (group == "default") {
|
||||||
schedule = try await NetworkManager.shared.getScheduleForOtherWeek(self.week, self.numOfGroup)
|
schedule = try await NetworkManager.shared.getScheduleForOtherWeek(self.week, self.numOfGroup)
|
||||||
}
|
}
|
||||||
else {
|
// В else мы заходим в том случае, если не знаем номера недели, которую нужно отобразить и номер группы(в html формате)
|
||||||
|
else {
|
||||||
schedule = try await NetworkManager.shared.getSchedule(group)
|
schedule = try await NetworkManager.shared.getSchedule(group)
|
||||||
|
if (!self.isFirstStartOffApp) {
|
||||||
|
self.isNewGroup = true
|
||||||
|
}
|
||||||
|
self.selectedDay = .init()
|
||||||
}
|
}
|
||||||
self.weekSchedule = schedule.table
|
self.weekSchedule = schedule.table
|
||||||
self.week = weekSchedule.week
|
self.week = weekSchedule.week
|
||||||
|
Loading…
x
Reference in New Issue
Block a user