This commit is contained in:
Vladimir Dubovik
2024-12-11 13:33:35 +03:00
parent 57e241292f
commit 1de531abc8
13 changed files with 325 additions and 132 deletions

View File

@ -11,7 +11,6 @@ struct MonthTabView: View {
@State private var currentMonthIndex: Int = 1
@State private var monthSlider: [[Date.MonthWeek]] = []
@State private var createMonth: Bool = false
@State private var currentDate: Date = .init()
@ObservedObject var vm: ViewModel
var body: some View {
VStack {
@ -39,32 +38,55 @@ struct MonthTabView: View {
.tabViewStyle(.page(indexDisplayMode: .never))
//.background(Color.green)
}
.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())
}
}
})
.onChange(of: currentMonthIndex, initial: false) { oldValue, newValue in
if newValue == 0 || newValue == (monthSlider.count - 1) {
createMonth = true
}
}
.onAppear(perform: {
currentDate = vm.selectedDay
vm.updateSelectedDayIndex(currentDate)
if monthSlider.isEmpty {
let currentMonth = Date().fetchMonth(vm.selectedDay)
monthSlider.append(currentMonth)
}
})
}
@ViewBuilder
func MonthView(_ month: [Date.MonthWeek]) -> some View {
VStack {
VStack (spacing: 10) {
ForEach(month.indices, id: \.self) { index in
let week = month[index].week
WeekView(week)
}
}
.background {
GeometryReader {
let minX = $0.frame(in: .global).minX
Color.clear
.preference(key: OffsetKey.self, value: minX)
.onPreferenceChange(OffsetKey.self) { value in
if (abs(value.rounded()) - 20) < 5 && createMonth {
paginateMonth()
createMonth = false
}
}
}
}
}
@ViewBuilder
func WeekView(_ week: [Date.WeekDay]) -> some View {
HStack (spacing: 23) {
@ -72,18 +94,18 @@ struct MonthTabView: View {
VStack {
Text(day.date.format("dd"))
.font(.system(size: 15, weight: .bold))
.foregroundStyle(isDateInCurrentMonth(day.date) ? isSameDate(day.date, currentDate) ? Color.white : Color.black: isSameDate(day.date, currentDate) ? Color.white : Color("greyForDaysInMonthTabView"))
.foregroundStyle(isDateInCurrentMonth(day.date) ? isSameDate(day.date, vm.selectedDay) ? Color.white : Color.black: isSameDate(day.date, vm.selectedDay) ? Color.white : Color("greyForDaysInMonthTabView"))
}
.frame(width: 30, height: 30, alignment: .center)
.background( content: {
Group {
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
else {
Color("background")
}
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
}
@ -91,7 +113,7 @@ struct MonthTabView: View {
)
.overlay (
Group {
if day.date.isToday && !isSameDate(day.date, currentDate) {
if day.date.isToday && !isSameDate(day.date, vm.selectedDay) {
RoundedRectangle(cornerRadius: 100)
.stroke(Color("blueColor"), lineWidth: 2)
}
@ -99,12 +121,60 @@ struct MonthTabView: View {
)
.cornerRadius(15)
.onTapGesture {
currentDate = day.date
vm.updateSelectedDayIndex(currentDate)
if isSameWeek(day.date, vm.selectedDay) {
print("На одной неделе")
}
else {
var difBetweenWeeks = weeksBetween(startDate: vm.selectedDay, endDate: day.date)
if day.date < vm.selectedDay {
difBetweenWeeks = difBetweenWeeks * -1
}
print(difBetweenWeeks)
vm.fetchWeekSchedule("", difBetweenWeeks)
}
vm.selectedDay = day.date
vm.updateSelectedDayIndex()
}
}
}
}
func paginateMonth() {
let calendar = Calendar.current
if monthSlider.indices.contains(currentMonthIndex) {
if let firstDate = monthSlider[currentMonthIndex].first?.week[0].date,
currentMonthIndex == 0 {
// switch (vm.numOfGroup) {
// case "":
// vm.week -= 1
// default:
// vm.fetchWeekSchedule("new week", -1)
// }
monthSlider.insert(firstDate.createPreviousMonth(), at: 0)
monthSlider.removeLast()
currentMonthIndex = 1
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -5, to: vm.selectedDay) ?? Date.init()
vm.updateSelectedDayIndex()
vm.fetchWeekSchedule("", -5)
}
if let lastDate = monthSlider[currentMonthIndex].last?.week[6].date,
currentMonthIndex == (monthSlider.count - 1) {
// switch (vm.numOfGroup) {
// case "":
// vm.week += 1
// default:
// vm.fetchWeekSchedule("new week", 1)
// }
monthSlider.append(lastDate.createNextMonth())
monthSlider.removeFirst()
currentMonthIndex = monthSlider.count - 2
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 5, to: vm.selectedDay) ?? Date.init()
vm.updateSelectedDayIndex()
vm.fetchWeekSchedule("", 5)
}
}
}
}
#Preview {

View File

@ -8,9 +8,8 @@
import SwiftUI
struct WeekTabView: View {
@Binding var currentWeekIndex: Int
@Binding var weekSlider: [[Date.WeekDay]]
@Binding var currentDate: Date
@State private var currentWeekIndex: Int = 1
@State private var weekSlider: [[Date.WeekDay]] = []
@State private var createWeek: Bool = false
@ObservedObject var vm: ViewModel
var body: some View {
@ -27,6 +26,22 @@ struct WeekTabView: View {
.tabViewStyle(.page(indexDisplayMode: .never))
.frame(height: 90)
}
.onAppear(perform: {
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())
}
}
})
.onChange(of: currentWeekIndex, initial: false) { oldValue, newValue in
if newValue == 0 || newValue == (weekSlider.count - 1) {
createWeek = true
@ -41,24 +56,24 @@ struct WeekTabView: View {
VStack (spacing: 1) {
Text(day.date.format("E"))
.font(.system(size: 15, weight: .semibold))
.foregroundColor(day.date.format("E") == "Вс" ? Color(.red) : isSameDate(day.date, currentDate) ? Color("customGray1") : Color("customGray3"))
.foregroundColor(day.date.format("E") == "Вс" ? Color(.red) : isSameDate(day.date, vm.selectedDay) ? Color("customGray1") : Color("customGray3"))
.padding(.top, 13)
.foregroundColor(.gray)
Text(day.date.format("dd"))
.font(.system(size: 15, weight: .bold))
.foregroundStyle(isSameDate(day.date, currentDate) ? .white : .black)
.foregroundStyle(isSameDate(day.date, vm.selectedDay) ? .white : .black)
.padding(.bottom, 13)
}
.frame(width: 43, height: 55, alignment: .center)
.background( content: {
Group {
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
else {
Color(.white)
}
if isSameDate(day.date, currentDate) {
if isSameDate(day.date, vm.selectedDay) {
Color("blueColor")
}
}
@ -66,7 +81,7 @@ struct WeekTabView: View {
)
.overlay (
Group {
if day.date.isToday && !isSameDate(day.date, currentDate) {
if day.date.isToday && !isSameDate(day.date, vm.selectedDay) {
RoundedRectangle(cornerRadius: 15)
.stroke(Color("blueColor"), lineWidth: 2)
}
@ -74,8 +89,8 @@ struct WeekTabView: View {
)
.cornerRadius(15)
.onTapGesture {
currentDate = day.date
vm.updateSelectedDayIndex(currentDate)
vm.selectedDay = day.date
vm.updateSelectedDayIndex()
}
}
}
@ -111,7 +126,7 @@ struct WeekTabView: View {
weekSlider.removeLast()
currentWeekIndex = 1
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: -1, to: vm.selectedDay) ?? Date.init()
currentDate = vm.selectedDay
vm.updateSelectedDayIndex()
}
if let lastDate = weekSlider[currentWeekIndex].last?.date,
@ -126,8 +141,7 @@ struct WeekTabView: View {
weekSlider.removeFirst()
currentWeekIndex = weekSlider.count - 2
vm.selectedDay = calendar.date(byAdding: .weekOfYear, value: 1, to: vm.selectedDay) ?? Date.init()
currentDate = vm.selectedDay
print(currentDate)
vm.updateSelectedDayIndex()
}
}
}