Skip to content

Commit

Permalink
!Refactoring (#74)
Browse files Browse the repository at this point in the history
* !Refactoring

* * Fix lint
  • Loading branch information
iletai authored May 3, 2024
1 parent 5f7a94a commit b9ec22b
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 41 deletions.
95 changes: 57 additions & 38 deletions Sources/CalendarView/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,8 @@ extension CalendarView {
yearContentView()
case .week:
calendarWeekView()
case .single:
singleDayContentView()
}
}

Expand All @@ -130,17 +132,12 @@ extension CalendarView {
.maxWidthAble()
) {
ForEach(
yearData[month, default: []], id: \.self
yearData[month, default: []],
id: \.self
) { date in
if date.isInside(date: date, granularity: .month) {
if date.isToday, hightLightToDay {
dateView(date).hightLightToDayView()
} else {
dateView(date)
.onTapGesture {
onSelected(date)
}
}
if date.compare(.isSameMonth(month)) {
dateView(date)
.hightLightToDayView(date.isToday && hightLightToDay)
} else {
dateOutView(date)
.opacity(showDateOut ? 1.0 : 0.0)
Expand Down Expand Up @@ -174,12 +171,8 @@ extension CalendarView {
Section(header: weekDayAndMonthView) {
ForEach(weekData, id: \.self) { date in
if date.compare(.isSameWeek(self.date)) {
if date.isToday, hightLightToDay {
dateView(date)
.hightLightToDayView()
} else {
dateView(date)
}
dateView(date)
.hightLightToDayView(date.isToday && hightLightToDay)
} else {
dateOutView(date)
.opacity(showDateOut ? 1.0 : 0.0)
Expand All @@ -193,12 +186,8 @@ extension CalendarView {
Section(header: weekDayAndMonthView) {
ForEach(monthData, id: \.self) { date in
if date.compare(.isSameMonth(self.date)) {
if date.isToday, hightLightToDay {
dateView(date)
.hightLightToDayView()
} else {
dateView(date)
}
dateView(date)
.hightLightToDayView(date.isToday && hightLightToDay)
} else {
dateOutView(date)
.opacity(showDateOut ? 1.0 : 0.0)
Expand All @@ -217,26 +206,44 @@ extension CalendarView {
headerView(headerDates)
}
}

@ViewBuilder
private func singleDayContentView() -> some View {
VStack(spacing: 0) {
Text(date.dayName)
.font(.headline)
Text(date.toString(.date(.full)))
.font(.headline)
.maxWidthAble()
.maxHeightAble()
}
.maxWidthAble()
.maxHeightAble()
}
}

extension View {
@ViewBuilder
func hightLightToDayView(_ color: Color = .orange) -> some View {
background(color.clipShape(Circle()))
func hightLightToDayView(_ isToday: Bool, _ color: Color = .orange) -> some View {
if isToday {
background(color.clipShape(Circle()))
} else {
self
}
}
}

// MARK: - Data For Calendar
private extension CalendarView {
var yearData: YearData {
extension CalendarView {
fileprivate var yearData: YearData {
let dates = DateInRegion.enumerateDates(
from: date.startOfYear(calendar),
to: date.endOfYear(calendar),
increment: DateComponents(month: 1)
)
.map {
$0.date
}
.map {
$0.date
}
return dates.reduce(into: [:]) { month, date in
month[date] = generateDates(
date: date.startOfMonth(calendar).date,
Expand All @@ -245,35 +252,47 @@ private extension CalendarView {
}
}

var columnGridLayout: [GridItem] {
Array(
repeating: GridItem(.flexible(), spacing: spaceBetweenColumns),
count: CalendarDefine.kWeekDays
)
fileprivate var columnGridLayout: [GridItem] {
switch viewMode {
case .single:
return Array(
repeating: GridItem(.flexible()),
count: 1
)
default:
return Array(
repeating: GridItem(.flexible(), spacing: spaceBetweenColumns),
count: CalendarDefine.kWeekDays
)
}
}

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

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

var headerDates: [Date] {
fileprivate 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))
return Array(
yearData[date.dateAtStartOf(.year), default: []].prefix(CalendarDefine.kWeekDays)
)
case .single:
return [date]
}
}
}
14 changes: 11 additions & 3 deletions Sources/CalendarView/Components/CalendarViewMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public enum CalendarViewMode: CaseIterable {
case month
case week
case year
case single

var component: Calendar.Component {
switch self {
Expand All @@ -22,12 +23,15 @@ public enum CalendarViewMode: CaseIterable {
return .weekOfMonth
case .year:
return .year
case .single:
return .day
}
}

var dateComponent: DateComponents {
switch self {
case .month,
.single,
.week:
return DateComponents(day: 1)
case .year:
Expand All @@ -43,16 +47,18 @@ public enum CalendarViewMode: CaseIterable {
return false
case .year:
return true
case .single:
return false
}
}

var isDisableScroll: Bool { !enableScroll }

var enableScrollIndicator: ScrollIndicatorVisibility {
switch self {
case .month:
return .never
case .week:
case .month,
.single,
.week:
return .never
case .year:
return .visible
Expand All @@ -67,6 +73,8 @@ public enum CalendarViewMode: CaseIterable {
return .nextWeek
case .year:
return .nextYear
case .single:
return .nextWeekday(.sunday)
}
}
}

0 comments on commit b9ec22b

Please sign in to comment.