Skip to content

Commit

Permalink
CAL-74: Extend customization options
Browse files Browse the repository at this point in the history
  • Loading branch information
FulcrumOne authored Dec 2, 2023
1 parent 2b4b66c commit b227c1e
Show file tree
Hide file tree
Showing 8 changed files with 81 additions and 13 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,13 @@
<picture>
<source media="(prefers-color-scheme: dark)" srcset="https://github.com/Mijick/Assets/blob/main/CalendarView/Logo/Dark.svg">
<source media="(prefers-color-scheme: light)" srcset="https://github.com/Mijick/Assets/blob/main/CalendarView/Logo/Light.svg">
<img alt="CalendarView Logo" width="72%">
<img alt="CalendarView Logo" width="200">
</picture>
</p>

<!-- Library Name -->
<h3 align="center">
Calendars made simple
Calendar View
</h3>

<!-- Library Description -->
Expand Down
44 changes: 44 additions & 0 deletions Sources/Internal/Extensions/Color++.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
//
// Color++.swift of CalendarView
//
// Created by Tomasz Kurylik
// - Twitter: https://twitter.com/tkurylik
// - Mail: tomasz.kurylik@mijick.com
// - GitHub: https://github.com/FulcrumOne
//
// Copyright ©2023 Mijick. Licensed under MIT License.


import SwiftUI

extension Color {
static let backgroundPrimary: Color = {
#if os(macOS)
Color(NSColor.windowBackgroundColor)
#else
Color(UIColor.systemBackground)
#endif
}()
static let backgroundSecondary: Color = {
#if os(macOS)
Color(NSColor.underPageBackgroundColor)
#else
Color(UIColor.secondarySystemBackground)
#endif
}()

static let onBackgroundPrimary: Color = {
#if os(macOS)
Color(NSColor.windowBackgroundColor)
#else
Color(UIColor.label)
#endif
}()
static let onBackgroundSecondary: Color = {
#if os(macOS)
Color(NSColor.windowBackgroundColor)
#else
Color(UIColor.secondaryLabel)
#endif
}()
}
7 changes: 5 additions & 2 deletions Sources/Internal/Views/MCalendarView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,20 @@ private extension MCalendarView {
}
func createScrollView() -> some View { ScrollViewReader { reader in
ScrollView(showsIndicators: false) {
LazyVStack(spacing: 24) {
LazyVStack(spacing: configData.monthsSpacing) {
ForEach(monthsData, id: \.month, content: createMonthItem)
}
.padding(.top, configData.monthsPadding.top)
.padding(.bottom, configData.monthsPadding.bottom)
.background(configData.monthsViewBackground)
}
.onAppear() { scrollToDate(reader, animatable: false) }
.onChange(of: configData.scrollDate) { _ in scrollToDate(reader, animatable: true) }
}}
}
private extension MCalendarView {
func createMonthItem(_ data: Data.MonthView) -> some View {
VStack(spacing: 12) {
VStack(spacing: configData.monthLabelDaysSpacing) {
createMonthLabel(data.month)
createMonthView(data)
}
Expand Down
4 changes: 2 additions & 2 deletions Sources/Internal/Views/MonthView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ struct MonthView: View {


var body: some View {
LazyVStack(spacing: 0) {
LazyVStack(spacing: config.daysSpacing.vertical) {
ForEach(data.items, id: \.last, content: createSingleRow)
}
.frame(maxWidth: .infinity)
Expand All @@ -29,7 +29,7 @@ struct MonthView: View {
}
private extension MonthView {
func createSingleRow(_ dates: [Date]) -> some View {
HStack(spacing: 0) {
HStack(spacing: config.daysSpacing.horizontal) {
ForEach(dates, id: \.self, content: createDayView)
}
}
Expand Down
24 changes: 23 additions & 1 deletion Sources/Public/Configurables/Public+CalendarConfig.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,28 @@ public extension CalendarConfig {
func locale(_ value: Locale) -> Self { MCalendar.locale = value; return self }
}

// MARK: - Views
// MARK: - Distances Between Objects
public extension CalendarConfig {
func monthsTopPadding(_ value: CGFloat) -> Self { changing(path: \.monthsPadding.top, to: value) }
func monthsBottomPadding(_ value: CGFloat) -> Self { changing(path: \.monthsPadding.bottom, to: value) }
func monthLabelToDaysDistance(_ value: CGFloat) -> Self { changing(path: \.monthLabelDaysSpacing, to: value) }
func monthsSpacing(_ value: CGFloat) -> Self { changing(path: \.monthsSpacing, to: value) }
func daysVerticalSpacing(_ value: CGFloat) -> Self { changing(path: \.daysSpacing.vertical, to: value) }
func daysHorizontalSpacing(_ value: CGFloat) -> Self { changing(path: \.daysSpacing.horizontal, to: value) }
}

// MARK: - Custom Views
public extension CalendarConfig {
func monthLabel(_ builder: @escaping (Date) -> some MonthLabel) -> Self { changing(path: \.monthLabel, to: builder) }
func weekdaysView(_ builder: @escaping () -> some WeekdaysView) -> Self { changing(path: \.weekdaysView, to: builder) }
func dayView(_ builder: @escaping (Date, Bool, Binding<Date?>?, Binding<MDateRange?>?) -> some DayView) -> Self { changing(path: \.dayView, to: builder) }
}

// MARK: - View Customisation
public extension CalendarConfig {
func monthsViewBackground(_ value: Color) -> Self { changing(path: \.monthsViewBackground, to: value) }
}

// MARK: - Modifiers
public extension CalendarConfig {
func scrollTo(date: Date?) -> Self { changing(path: \.scrollDate, to: date) }
Expand All @@ -35,6 +50,13 @@ public extension CalendarConfig {

// MARK: - Internal
public struct CalendarConfig: Configurable { public init() {}
private(set) var monthLabelDaysSpacing: CGFloat = 12
private(set) var monthsPadding: (top: CGFloat, bottom: CGFloat) = (12, 24)
private(set) var monthsSpacing: CGFloat = 24
private(set) var daysSpacing: (vertical: CGFloat, horizontal: CGFloat) = (2, 0)

private(set) var monthsViewBackground: Color = .clear

private(set) var monthLabel: (Date) -> any MonthLabel = DefaultMonthLabel.init
private(set) var weekdaysView: () -> any WeekdaysView = DefaultWeekdaysView.init
private(set) var dayView: (Date, Bool, Binding<Date?>?, Binding<MDateRange?>?) -> any DayView = DefaultDayView.init
Expand Down
7 changes: 3 additions & 4 deletions Sources/Public/View Protocols/Public+DayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,25 +53,24 @@ private extension DayView {
func createDefaultDayLabel() -> some View {
Text(getStringFromDay(format: "d"))
.font(.system(size: 14, weight: .medium))
.foregroundColor(isSelected() ? .white : .black)
.foregroundColor(isSelected() ? .backgroundPrimary : .onBackgroundPrimary)
}
func createDefaultSelectionView() -> some View {
Circle()
.fill(.black)
.fill(Color.onBackgroundPrimary)
.transition(.asymmetric(insertion: .scale(scale: 0.52).combined(with: .opacity), removal: .opacity))
.active(if: isSelected())
}
func createDefaultRangeSelectionView() -> some View {
RoundedRectangle(corners: rangeSelectionViewCorners)
.fill(.black.opacity(0.12))
.fill(Color.onBackgroundPrimary.opacity(0.12))
.transition(.opacity)
.active(if: isWithinRange())
}
}
private extension DayView {
func createBodyForCurrentMonth() -> some View {
createContent()
.padding(.vertical, 1)
.frame(maxWidth: .infinity, maxHeight: .infinity)
.aspectRatio(1.0, contentMode: .fit)
.onAppear(perform: onAppear)
Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/View Protocols/Public+MonthLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ private extension MonthLabel {
func createDefaultContent() -> some View {
Text(getString(format: "MMMM y"))
.font(.system(size: 16, weight: .semibold))
.foregroundColor(.black)
.foregroundColor(.onBackgroundPrimary)
}
}

Expand Down
2 changes: 1 addition & 1 deletion Sources/Public/View Protocols/Public+WeekdayLabel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public extension WeekdayLabel {
private extension WeekdayLabel {
func createDefaultContent() -> some View {
Text(getString(with: .veryShort))
.foregroundColor(.gray)
.foregroundColor(.onBackgroundSecondary)
.font(.system(size: 14))
}
}
Expand Down

0 comments on commit b227c1e

Please sign in to comment.