Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize #73

Merged
merged 1 commit into from
Mar 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 19 additions & 11 deletions CalendarExampleView/CalendarExampleView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import SwiftDate
struct ContentView: View {
@State var isShowHeader = false
@State var isShowDateOut = false
@State var firstWeekDate = 1
@State var firstWeekDate = CalendarWeekday.monday
@State var isShowDivider = false
@State var viewMode = CalendarViewMode.year
@State private var selectedDate = Date()
Expand Down Expand Up @@ -63,7 +63,7 @@ struct ContentView: View {
)
.enableHeader(isShowHeader)
.enableDateOut(isShowDateOut)
.firstWeekDay(.sunday)
.firstWeekDay(firstWeekDate)
.calendarLocate(locale: Locales.vietnamese.toLocale())
.enablePinedView([.sectionHeaders, .sectionFooters])
.setViewMode(viewMode)
Expand Down Expand Up @@ -109,8 +109,8 @@ struct ContentView: View {
}

var listButtonDemo: some View {
ScrollView(.horizontal) {
HStack {
Grid(horizontalSpacing: 8.0, verticalSpacing: 8.0) {
GridRow {
Button {
isShowHeader.toggle()
} label: {
Expand All @@ -119,34 +119,36 @@ struct ContentView: View {
Button {
isShowDivider.toggle()
} label: {
Text("Dviider")
Text("Divider")
}

Button {
isShowDateOut.toggle()
} label: {
Text("DateOut")
}
}
GridRow {
Button {
firstWeekDate = Int.random(in: 1...6)
firstWeekDate = CalendarWeekday.allCases.randomElement()!
} label: {
Text("First Week Date")
Text("WeekDate")
}
Button {
let nextMonth = Calendar.gregorian.date(byAdding: .month, value: 1, to: selectedDate)
selectedDate = nextMonth!
} label: {
Text("Next month")
Text("Next")
}
Button {
isHightLightToDay.toggle()
} label: {
Text("isHightLightToDay")
Text("ToDay")
}

}
.buttonStyle(.bordered)
}
.buttonStyle(.bordered)
.maxWidthAble()
}

func onSelectedDate(_ date: Date) {
Expand All @@ -161,3 +163,9 @@ struct ContentView: View {
#Preview {
ContentView()
}

extension CalendarWeekday: CaseIterable {
static public var allCases: [CalendarWeekday] {
[.friday, .monday, .saturday, .thursday, .wednesday, .sunday, .tuesday]
}
}
105 changes: 58 additions & 47 deletions Sources/CalendarView/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -40,39 +40,6 @@ public struct CalendarView<
var pinedHeaderView = PinnedScrollableViews()
var onSelected: OnSelectedDate = { _ in }

private var yearData: YearData {
let dates = DateInRegion.enumerateDates(
from: date.startOfYear(calendar),
to: date.endOfYear(calendar),
increment: DateComponents(month: 1)
)
.map {
$0.date
}
return dates.reduce(into: [:]) { month, date in
month[date] = generateDates(
date: date.startOfMonth(calendar).date,
dateComponents: CalendarViewMode.month.dateComponent
)
}
}

private var monthData: MonthDateData {
generateDates(
date: date,
withComponent: .month,
dateComponents: DateComponents(day: 1)
)
}

private var weekData: WeekDataData {
generateDates(
date: date,
withComponent: .weekOfMonth,
dateComponents: DateComponents(day: 1)
)
}

@GestureState var isGestureFinished = true
var onDraggingEnded: OnEndDragAction?

Expand Down Expand Up @@ -110,16 +77,12 @@ public struct CalendarView<
public var body: some View {
ScrollView {
LazyVGrid(
columns: Array(
repeating: GridItem(
.flexible(),
spacing: spaceBetweenColumns
),
count: CalendarDefine.kWeekDays
),
columns: columnGridLayout,
spacing: spacingBetweenDay,
pinnedViews: pinedHeaderView
) { bodyContentView }
) {
bodyContentView
}
.marginDefault()
.background(backgroundCalendar)
.highPriorityGesture(swipeGesture)
Expand Down Expand Up @@ -162,11 +125,7 @@ extension CalendarView {
if showDivider {
Divider()
}
headerView(
Array(
yearData[month, default: []].prefix(CalendarDefine.kWeekDays)
)
)
headerView(headerDates)
}
.maxWidthAble()
) {
Expand Down Expand Up @@ -255,7 +214,7 @@ extension CalendarView {
if showDivider {
Divider()
}
headerView(Array(monthData.prefix(CalendarDefine.kWeekDays)))
headerView(headerDates)
}
}
}
Expand All @@ -266,3 +225,55 @@ extension View {
background(color.clipShape(Circle()))
}
}

// MARK: - Data For Calendar
private extension CalendarView {
var yearData: YearData {
let dates = DateInRegion.enumerateDates(
from: date.startOfYear(calendar),
to: date.endOfYear(calendar),
increment: DateComponents(month: 1)
)
.map {
$0.date
}
return dates.reduce(into: [:]) { month, date in
month[date] = generateDates(
date: date.startOfMonth(calendar).date,
dateComponents: CalendarViewMode.month.dateComponent
)
}
}

var columnGridLayout: [GridItem] {
Array(
repeating: GridItem(.flexible(), spacing: spaceBetweenColumns),
count: CalendarDefine.kWeekDays
)
}

var monthData: MonthDateData {
generateDates(
date: date,
withComponent: .month,
dateComponents: DateComponents(day: 1)
)
}

var weekData: WeekDataData {
generateDates(
date: date,
withComponent: .weekOfMonth,
dateComponents: DateComponents(day: 1)
)
}

var headerDates: [Date] {
switch viewMode {
case .month, .week:
return Array(monthData.prefix(CalendarDefine.kWeekDays))
case .year:
return Array(yearData[date.dateAtStartOf(.year), default: []].prefix(CalendarDefine.kWeekDays))
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ extension CalendarView: RootBuilder {
/// Sets the first day of the week in the calendar view.
/// - Parameter first: The index of the first day of the week (1 for Sunday, 2 for Monday, etc.).
/// - Returns: An instance of `Self` with the updated configuration.
public func firstWeekDay(_ first: CalendarView.Weekday) -> Self {
public func firstWeekDay(_ first: CalendarWeekday) -> Self {
mutating(\.calendar.firstWeekday, value: first.rawValue)
}

Expand Down
39 changes: 0 additions & 39 deletions Sources/CalendarView/Components/CalendarView+WeekDay.swift

This file was deleted.

37 changes: 37 additions & 0 deletions Sources/CalendarView/Components/CalendarWeekday.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
//
// CalendarView+WeekDay.swift
//
//
// Created by iletai on 27/03/2024.
//

import Foundation

public enum CalendarWeekday: Int, CustomStringConvertible {
case sunday = 1
case monday
case tuesday
case wednesday
case thursday
case friday
case saturday

public var description: String {
switch self {
case .sunday:
return "sunday"
case .monday:
return "monday"
case .tuesday:
return "tuesday"
case .wednesday:
return "wednesday"
case .thursday:
return "thursday"
case .friday:
return "friday"
case .saturday:
return "saturday"
}
}
}
16 changes: 0 additions & 16 deletions Sources/CalendarView/Utils/Angle+Extension.swift

This file was deleted.

Loading