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

+ Update LayoutCalendar #77

Merged
merged 4 commits into from
Jun 17, 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
23 changes: 19 additions & 4 deletions CalendarExampleView/CalendarExampleView/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,29 @@ struct ContentView: View {
case .year(let yearDisplayMode):
switch yearDisplayMode {
case .compact:
return .system(size: 10, weight: .regular)
return .system(size: 8, weight: .regular)
case .full:
return .footnote.weight(.semibold)
}
}
}

var heightCellCalendar: CGFloat {
switch viewMode {
case .month,
.week,
.single:
return 30.0
case .year(let yearDisplayMode):
switch yearDisplayMode {
case .compact:
return 20.0
case .full:
return 30.0
}
}
}

var body: some View {
VStack {
ScrollView {
Expand All @@ -51,7 +67,7 @@ struct ContentView: View {
)
}
.frameInfinity()
.frame(height: 30)
.frame(height: heightCellCalendar)
.background(listSelectedDate.contains(date) ? .cyan : .clear)
}, headerView: { date in
HStack {
Expand All @@ -73,15 +89,14 @@ struct ContentView: View {
)
}
.frameInfinity()
.frame(height: 30)
.frame(height: heightCellCalendar)
.background(listSelectedDate.contains(date) ? .cyan : .clear)
}
)
.enableHeader(isShowHeader)
.enableDateOut(isShowDateOut)
.firstWeekDay(firstWeekDate)
.calendarLocate(locale: Locales.vietnamese.toLocale())
.enablePinedView([.sectionHeaders, .sectionFooters])
.setViewMode(viewMode)
.rowsSpacing(8)
.columnSpacing(8)
Expand Down
40 changes: 20 additions & 20 deletions Sources/CalendarView/CalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,7 @@ public struct CalendarView<
.highPriorityGesture(swipeGesture)
.marginDefault()
.background(backgroundCalendar)
.animation(calendarOptions.swapAnimation, value: calendarOptions.viewMode)
}
}

Expand All @@ -186,17 +187,22 @@ extension CalendarView {
switch calendarOptions.viewMode {
case .month:
monthContentView()
.transition(calendarOptions.swapRight)
case .year(let displayMode):
switch displayMode {
case .compact:
yearContentCompactView()
.transition(calendarOptions.swapRight)
case .full:
yearContentView()
.transition(calendarOptions.swapLeft)
}
case .week:
calendarWeekView()
.transition(calendarOptions.swapLeft)
case .single:
singleDayContentView()
.transition(calendarOptions.swapLeft)
}
}

Expand All @@ -212,14 +218,7 @@ extension CalendarView {
Section(
header:
LazyVStack(alignment: .leading) {
HStack {
Spacer()
Text(month.monthName(.defaultStandalone) + " \(month.year)")
.font(.footnote)
.fontWeight(.bold)
Spacer()
}
.allowVisibleWith(calendarOptions.isShowHeader)
monthTitle(for: month)
Divider()
.allowVisibleWith(calendarOptions.isShowDivider)
.padding(.bottom, 4)
Expand Down Expand Up @@ -247,20 +246,17 @@ extension CalendarView {
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)
monthTitle(for: month)
Divider()
.allowVisibleWith(calendarOptions.isShowDivider)
.padding(.bottom, 2)
LazyVGrid(columns: Array(
LazyVGrid(
columns: Array(
repeating: GridItem(.flexible(), spacing: 0, alignment: .top),
count: CalendarDefine.kWeekDays
), alignment: .leading, spacing: .zero
),
alignment: .leading,
spacing: .zero
) {
ForEach(
yearData[month, default: []],
Expand Down Expand Up @@ -290,9 +286,13 @@ extension CalendarView {
fileprivate func monthTitle(for month: Date) -> some View {
HStack {
Spacer()
Text(month.monthName(.defaultStandalone) + " \(month.year)")
.font(.footnote)
.fontWeight(.bold)
Text(
month.toFormat(
"MMM",
locale: calendarOptions.calendar.locale
)
.uppercased()
).font(fontTitle)
Spacer()
}
.allowVisibleWith(calendarOptions.isShowHeader)
Expand Down
20 changes: 18 additions & 2 deletions Sources/CalendarView/Common/CalendarViewOption.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
//

import Foundation
import SwiftDate
import SwiftUI

/// A struct that represents the options for a calendar view.
public struct CalendarViewOption {
Expand All @@ -18,6 +20,17 @@ public struct CalendarViewOption {
public var spacingBetweenDay: CGFloat
public var viewMode: CalendarViewMode
public var spaceBetweenColumns: CGFloat
public var compactMonthCount = 3
public var locateForCalendar: Locale
public let swapAnimation: Animation = .easeInOut(duration: 0.3)
public let swapRight = AnyTransition.asymmetric(
insertion: .scale(scale: 1.5, anchor: .center),
removal: .opacity
)
public let swapLeft = AnyTransition.asymmetric(
insertion: .scale(scale: 1.5, anchor: .center),
removal: .opacity
)

/// Initializes a new instance of `CalendarViewOption` with default values.
init() {
Expand All @@ -30,12 +43,13 @@ public struct CalendarViewOption {
self.spacingBetweenDay = 8.0
self.viewMode = .year(.full)
self.spaceBetweenColumns = 8.0
self.locateForCalendar = Locale.vietnam.toLocale()
}
}

public extension CalendarViewOption {
extension CalendarViewOption {
/// The default `CalendarViewOption` instance.
static var defaultOption: CalendarViewOption {
public static var defaultOption: CalendarViewOption {
var options = CalendarViewOption()
options.backgroundStatus = .hidden
options.calendar = .gregorian
Expand All @@ -46,6 +60,8 @@ public extension CalendarViewOption {
options.spacingBetweenDay = 8.0
options.viewMode = .year(.full)
options.isShowDivider = true
options.compactMonthCount = 3
options.locateForCalendar = Locale.vietnam.toLocale()
return options
}
}
20 changes: 16 additions & 4 deletions Sources/CalendarView/Components/CalendarView+MakeData.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,18 @@ extension CalendarView {
switch mode {
case .compact:
return Array(
repeating: GridItem(.flexible(), alignment: .top),
count: 2
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns, alignment: .top),
count: calendarOptions.compactMonthCount
)
case .full:
return Array(
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns),
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns, alignment: .top),
count: CalendarDefine.kWeekDays
)
}
default:
return Array(
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns),
repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns, alignment: .top),
count: CalendarDefine.kWeekDays
)
}
Expand Down Expand Up @@ -122,4 +122,16 @@ extension CalendarView {
return [date]
}
}

var fontTitle: Font {
switch calendarOptions.viewMode {
case .month,
.single,
.year(.full):
return .footnote.bold()
case .year(.compact):
return .system(size: 10, weight: .semibold)
default: return .footnote.bold()
}
}
}
8 changes: 7 additions & 1 deletion Sources/CalendarView/Components/CalendarViewMode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,13 @@ import SwiftDate
/// Represents the different modes of the calendar view.
public enum CalendarViewMode: CaseIterable, Hashable {
public static var allCases: [CalendarViewMode] {
[CalendarViewMode.year(.full), CalendarViewMode.year(.compact), CalendarViewMode.month, CalendarViewMode.single]
[
CalendarViewMode.year(.full),
CalendarViewMode.year(.compact),
CalendarViewMode.month,
CalendarViewMode.week,
CalendarViewMode.single
]
}

case month
Expand Down
2 changes: 1 addition & 1 deletion SwiftUICalendarView.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ Pod::Spec.new do |s|
SwiftUICalendarView is a free and open-source library in SwiftUI to make calendar.
DESC

s.version = '1.4.10'
s.version = '1.4.11'
s.platform = :ios
s.ios.deployment_target = '16.0' # Updated deployment target to a valid iOS version
s.swift_version = '5.9'
Expand Down
Loading