Skip to content

Commit

Permalink
Feature/refactoring (#76)
Browse files Browse the repository at this point in the history
* !Refactoring

* !Refactoring

* + add compact mode year view

* * Fix lint
  • Loading branch information
iletai authored Jun 16, 2024
1 parent eca1429 commit 0aec2cf
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,8 @@
DEVELOPMENT_TEAM = AP58YLHQ2S;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_CFBundleDisplayName = CalendarExample;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.lifestyle";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down Expand Up @@ -316,6 +318,8 @@
DEVELOPMENT_TEAM = AP58YLHQ2S;
ENABLE_PREVIEWS = YES;
GENERATE_INFOPLIST_FILE = YES;
INFOPLIST_KEY_CFBundleDisplayName = CalendarExample;
INFOPLIST_KEY_LSApplicationCategoryType = "public.app-category.lifestyle";
INFOPLIST_KEY_UIApplicationSceneManifest_Generation = YES;
INFOPLIST_KEY_UIApplicationSupportsIndirectInputEvents = YES;
INFOPLIST_KEY_UILaunchScreen_Generation = YES;
Expand Down
28 changes: 21 additions & 7 deletions CalendarExampleView/CalendarExampleView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,28 @@ struct ContentView: View {
@State var isShowDateOut = false
@State var firstWeekDate = CalendarWeekday.monday
@State var isShowDivider = false
@State var viewMode = CalendarViewMode.year
@State var viewMode = CalendarViewMode.year(.full)
@State private var selectedDate = Date()
@State private var colorDay = Color.white
@State var listSelectedDate = [Date]()
@State var isHightLightToDay = true

var fontDate: Font {
switch viewMode {
case .month,
.week,
.single:
return .footnote
case .year(let yearDisplayMode):
switch yearDisplayMode {
case .compact:
return .system(size: 10, weight: .regular)
case .full:
return .footnote.weight(.semibold)
}
}
}

var body: some View {
VStack {
ScrollView {
Expand All @@ -28,8 +45,7 @@ struct ContentView: View {
, dateView: { date in
VStack {
Text(date.dayName)
.font(.footnote)
.fontWeight(.semibold)
.font(fontDate)
.foregroundColor(
Calendar.current.isDateInWeekend(date) ? .red : .black
)
Expand All @@ -41,8 +57,7 @@ struct ContentView: View {
HStack {
ForEach(date, id: \.self) {
Text($0.weekDayShortName.uppercased())
.font(.footnote)
.fontWeight(.bold)
.font(fontDate)
.foregroundColor(
Calendar.current.isDateInWeekend($0) ? .red : .black
)
Expand All @@ -52,8 +67,7 @@ struct ContentView: View {
}, dateOutView: { date in
VStack {
Text(date.dayName)
.font(.footnote)
.fontWeight(.semibold)
.font(fontDate)
.foregroundColor(
Calendar.current.isDateInWeekend(date) ? .red.opacity(0.4) : .gray
)
Expand Down
45 changes: 43 additions & 2 deletions Sources/CalendarView/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,13 @@ extension CalendarView {
switch calendarOptions.viewMode {
case .month:
monthContentView()
case .year:
yearContentView()
case .year(let displayMode):
switch displayMode {
case .compact:
yearContentCompactView()
case .full:
yearContentView()
}
case .week:
calendarWeekView()
case .single:
Expand Down Expand Up @@ -238,6 +243,42 @@ extension CalendarView {
}
}

@ViewBuilder
fileprivate func yearContentCompactView() -> some View {
ForEach(yearData.keys.sorted(), id: \.self) { month in
LazyVStack(alignment: .leading, spacing: .zero) {
HStack {
Text(month.monthName(.short))
.font(.system(size: 10))
.fontWeight(.regular)
Spacer()
}
.allowVisibleWith(calendarOptions.isShowHeader)
Divider()
.allowVisibleWith(calendarOptions.isShowDivider)
.padding(.bottom, 2)
LazyVGrid(columns: Array(
repeating: GridItem(.flexible(), spacing: 0, alignment: .top),
count: CalendarDefine.kWeekDays
), alignment: .leading, spacing: .zero
) {
ForEach(
yearData[month, default: []],
id: \.self
) { date in
if date.compare(.isSameMonth(month)) {
dateView(date)
.hightLightToDayView(date.isToday && calendarOptions.isShowHightLightToDay)
} else {
dateOutView(date)
.allowVisibleWith(calendarOptions.isShowDateOut)
}
}
}
}
}
}

/**
Returns the month title view for a given month.

Expand Down
4 changes: 2 additions & 2 deletions Sources/CalendarView/Common/CalendarViewOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public struct CalendarViewOption {
self.calendar = .gregorian
self.backgroundStatus = .hidden
self.spacingBetweenDay = 8.0
self.viewMode = .year
self.viewMode = .year(.full)
self.spaceBetweenColumns = 8.0
}
}
Expand All @@ -44,7 +44,7 @@ public extension CalendarViewOption {
options.isShowHeader = true
options.spaceBetweenColumns = 8.0
options.spacingBetweenDay = 8.0
options.viewMode = .year
options.viewMode = .year(.full)
options.isShowDivider = true
return options
}
Expand Down
13 changes: 13 additions & 0 deletions Sources/CalendarView/Components/CalendarView+MakeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,19 @@ extension CalendarView {
repeating: GridItem(.flexible()),
count: 1
)
case .year(let mode):
switch mode {
case .compact:
return Array(
repeating: GridItem(.flexible(), alignment: .top),
count: 2
)
case .full:
return Array(
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns),
count: CalendarDefine.kWeekDays
)
}
default:
return Array(
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns),
Expand Down
15 changes: 13 additions & 2 deletions Sources/CalendarView/Components/CalendarViewMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,14 @@ import SwiftUI
import SwiftDate

/// Represents the different modes of the calendar view.
public enum CalendarViewMode: CaseIterable {
public enum CalendarViewMode: CaseIterable, Hashable {
public static var allCases: [CalendarViewMode] {
[CalendarViewMode.year(.full), CalendarViewMode.year(.compact), CalendarViewMode.month, CalendarViewMode.single]
}

case month
case week
case year
case year(YearDisplayMode)
case single

/// The corresponding `Calendar.Component` for each mode.
Expand Down Expand Up @@ -77,3 +81,10 @@ public enum CalendarViewMode: CaseIterable {
}
}
}

extension CalendarViewMode: Equatable {}

public enum YearDisplayMode {
case compact
case full
}

0 comments on commit 0aec2cf

Please sign in to comment.