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