From 709bc8fa56179fca45036ba566167648f2f129b0 Mon Sep 17 00:00:00 2001 From: iletai Date: Mon, 17 Jun 2024 11:31:04 +0700 Subject: [PATCH 1/4] + add feature update layout and animation --- .../CalendarExampleView/ContentView.swift | 21 +++++++++-- Sources/CalendarView/CalendarView.swift | 37 ++++++++++++++++--- .../Common/CalendarViewOption.swift | 20 +++++++++- .../Components/CalendarView+MakeData.swift | 8 ++-- .../Components/CalendarViewMode.swift | 8 +++- SwiftUICalendarView.podspec | 2 +- 6 files changed, 79 insertions(+), 17 deletions(-) diff --git a/CalendarExampleView/CalendarExampleView/ContentView.swift b/CalendarExampleView/CalendarExampleView/ContentView.swift index fca855d..478799a 100644 --- a/CalendarExampleView/CalendarExampleView/ContentView.swift +++ b/CalendarExampleView/CalendarExampleView/ContentView.swift @@ -29,13 +29,27 @@ 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 { @@ -51,7 +65,7 @@ struct ContentView: View { ) } .frameInfinity() - .frame(height: 30) + .frame(height: heightCellCalendar) .background(listSelectedDate.contains(date) ? .cyan : .clear) }, headerView: { date in HStack { @@ -73,7 +87,7 @@ struct ContentView: View { ) } .frameInfinity() - .frame(height: 30) + .frame(height: heightCellCalendar) .background(listSelectedDate.contains(date) ? .cyan : .clear) } ) @@ -81,7 +95,6 @@ struct ContentView: View { .enableDateOut(isShowDateOut) .firstWeekDay(firstWeekDate) .calendarLocate(locale: Locales.vietnamese.toLocale()) - .enablePinedView([.sectionHeaders, .sectionFooters]) .setViewMode(viewMode) .rowsSpacing(8) .columnSpacing(8) diff --git a/Sources/CalendarView/CalendarView.swift b/Sources/CalendarView/CalendarView.swift index 705d62c..0220328 100644 --- a/Sources/CalendarView/CalendarView.swift +++ b/Sources/CalendarView/CalendarView.swift @@ -160,6 +160,7 @@ public struct CalendarView< .highPriorityGesture(swipeGesture) .marginDefault() .background(backgroundCalendar) + .animation(calendarOptions.swapAnimation, value: calendarOptions.viewMode) } } @@ -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) } } @@ -214,7 +220,13 @@ extension CalendarView { LazyVStack(alignment: .leading) { HStack { Spacer() - Text(month.monthName(.defaultStandalone) + " \(month.year)") + Text( + month.toFormat( + "MMM", + locale: calendarOptions.calendar.locale + ) + .uppercased() + ) .font(.footnote) .fontWeight(.bold) Spacer() @@ -248,7 +260,13 @@ extension CalendarView { ForEach(yearData.keys.sorted(), id: \.self) { month in LazyVStack(alignment: .leading, spacing: .zero) { HStack { - Text(month.monthName(.short)) + Text( + month.toFormat( + "MMM", + locale: calendarOptions.calendar.locale + ) + .uppercased() + ) .font(.system(size: 10)) .fontWeight(.regular) Spacer() @@ -257,10 +275,13 @@ extension CalendarView { 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: []], @@ -290,7 +311,13 @@ extension CalendarView { fileprivate func monthTitle(for month: Date) -> some View { HStack { Spacer() - Text(month.monthName(.defaultStandalone) + " \(month.year)") + Text( + month.toFormat( + "MMM", + locale: calendarOptions.calendar.locale + ) + .uppercased() + ) .font(.footnote) .fontWeight(.bold) Spacer() diff --git a/Sources/CalendarView/Common/CalendarViewOption.swift b/Sources/CalendarView/Common/CalendarViewOption.swift index 20a6ad2..5f3892c 100644 --- a/Sources/CalendarView/Common/CalendarViewOption.swift +++ b/Sources/CalendarView/Common/CalendarViewOption.swift @@ -6,6 +6,8 @@ // import Foundation +import SwiftDate +import SwiftUI /// A struct that represents the options for a calendar view. public struct CalendarViewOption { @@ -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() { @@ -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 @@ -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 } } diff --git a/Sources/CalendarView/Components/CalendarView+MakeData.swift b/Sources/CalendarView/Components/CalendarView+MakeData.swift index 22cde9a..184639a 100644 --- a/Sources/CalendarView/Components/CalendarView+MakeData.swift +++ b/Sources/CalendarView/Components/CalendarView+MakeData.swift @@ -74,18 +74,18 @@ extension CalendarView { switch mode { case .compact: return Array( - repeating: GridItem(.flexible(), alignment: .top), - count: 2 + repeating: GridItem(.flexible(), spacing: 0, 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 ) } diff --git a/Sources/CalendarView/Components/CalendarViewMode.swift b/Sources/CalendarView/Components/CalendarViewMode.swift index c4a7937..d3978e9 100644 --- a/Sources/CalendarView/Components/CalendarViewMode.swift +++ b/Sources/CalendarView/Components/CalendarViewMode.swift @@ -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 diff --git a/SwiftUICalendarView.podspec b/SwiftUICalendarView.podspec index 4eb3f17..1af99c7 100644 --- a/SwiftUICalendarView.podspec +++ b/SwiftUICalendarView.podspec @@ -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' From 7cc75b7616a19e3c14d68f5b87b996a31ce465ef Mon Sep 17 00:00:00 2001 From: iletai Date: Mon, 17 Jun 2024 11:35:36 +0700 Subject: [PATCH 2/4] * Fix lint --- CalendarExampleView/CalendarExampleView/ContentView.swift | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/CalendarExampleView/CalendarExampleView/ContentView.swift b/CalendarExampleView/CalendarExampleView/ContentView.swift index 478799a..a92e84b 100644 --- a/CalendarExampleView/CalendarExampleView/ContentView.swift +++ b/CalendarExampleView/CalendarExampleView/ContentView.swift @@ -38,7 +38,9 @@ struct ContentView: View { var heightCellCalendar: CGFloat { switch viewMode { - case .month,.week, .single: + case .month, + .week, + .single: return 30.0 case .year(let yearDisplayMode): switch yearDisplayMode { From 68d614c4267435c0129a13cc21a41f5bce15c36e Mon Sep 17 00:00:00 2001 From: iletai Date: Mon, 17 Jun 2024 12:28:36 +0700 Subject: [PATCH 3/4] + add feature update layout and animation --- Sources/CalendarView/CalendarView.swift | 33 ++----------------- .../Components/CalendarView+MakeData.swift | 14 +++++++- 2 files changed, 16 insertions(+), 31 deletions(-) diff --git a/Sources/CalendarView/CalendarView.swift b/Sources/CalendarView/CalendarView.swift index 0220328..a33ff41 100644 --- a/Sources/CalendarView/CalendarView.swift +++ b/Sources/CalendarView/CalendarView.swift @@ -218,20 +218,7 @@ extension CalendarView { Section( header: LazyVStack(alignment: .leading) { - HStack { - Spacer() - Text( - month.toFormat( - "MMM", - locale: calendarOptions.calendar.locale - ) - .uppercased() - ) - .font(.footnote) - .fontWeight(.bold) - Spacer() - } - .allowVisibleWith(calendarOptions.isShowHeader) + monthTitle(for: month) Divider() .allowVisibleWith(calendarOptions.isShowDivider) .padding(.bottom, 4) @@ -259,19 +246,7 @@ extension CalendarView { fileprivate func yearContentCompactView() -> some View { ForEach(yearData.keys.sorted(), id: \.self) { month in LazyVStack(alignment: .leading, spacing: .zero) { - HStack { - Text( - month.toFormat( - "MMM", - locale: calendarOptions.calendar.locale - ) - .uppercased() - ) - .font(.system(size: 10)) - .fontWeight(.regular) - Spacer() - } - .allowVisibleWith(calendarOptions.isShowHeader) + monthTitle(for: month) Divider() .allowVisibleWith(calendarOptions.isShowDivider) .padding(.bottom, 2) @@ -317,9 +292,7 @@ extension CalendarView { locale: calendarOptions.calendar.locale ) .uppercased() - ) - .font(.footnote) - .fontWeight(.bold) + ).font(fontTitle) Spacer() } .allowVisibleWith(calendarOptions.isShowHeader) diff --git a/Sources/CalendarView/Components/CalendarView+MakeData.swift b/Sources/CalendarView/Components/CalendarView+MakeData.swift index 184639a..bebf357 100644 --- a/Sources/CalendarView/Components/CalendarView+MakeData.swift +++ b/Sources/CalendarView/Components/CalendarView+MakeData.swift @@ -74,7 +74,7 @@ extension CalendarView { switch mode { case .compact: return Array( - repeating: GridItem(.flexible(), spacing: 0, alignment: .top), + repeating: GridItem(.flexible(), spacing: calendarOptions.spaceBetweenColumns, alignment: .top), count: calendarOptions.compactMonthCount ) case .full: @@ -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() + } + } } From 387932c51a3f3ed2320f74c40760168e51f50fbf Mon Sep 17 00:00:00 2001 From: iletai Date: Mon, 17 Jun 2024 12:31:32 +0700 Subject: [PATCH 4/4] * Fix lint --- Sources/CalendarView/Components/CalendarView+MakeData.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CalendarView/Components/CalendarView+MakeData.swift b/Sources/CalendarView/Components/CalendarView+MakeData.swift index bebf357..3d42d2f 100644 --- a/Sources/CalendarView/Components/CalendarView+MakeData.swift +++ b/Sources/CalendarView/Components/CalendarView+MakeData.swift @@ -125,7 +125,7 @@ extension CalendarView { var fontTitle: Font { switch calendarOptions.viewMode { - case .month, + case .month, .single, .year(.full): return .footnote.bold()