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

Add date range limit functionality #10

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
8 changes: 4 additions & 4 deletions Example/Podfile.lock
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
PODS:
- SwiftUICalendar (0.1.0)
- SwiftUICalendar (0.1.14)

DEPENDENCIES:
- SwiftUICalendar (from `../`)
Expand All @@ -9,8 +9,8 @@ EXTERNAL SOURCES:
:path: "../"

SPEC CHECKSUMS:
SwiftUICalendar: 6a5a887cb235da91628701a45cbfb58c7af3dbbd
SwiftUICalendar: 5ffef5152b532b94195ff90534863d04b56ad412

PODFILE CHECKSUM: 562a2851462e815457e72c107e0378d2c348b1c4
PODFILE CHECKSUM: cd5f379e7cb0d26d9a299090207ad8521d15ce0b

COCOAPODS: 1.11.0.beta.2
COCOAPODS: 1.12.1
4 changes: 4 additions & 0 deletions Example/SwiftUICalendar.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
607FACDD1AFB9204008FA782 /* Images.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDC1AFB9204008FA782 /* Images.xcassets */; };
607FACE01AFB9204008FA782 /* LaunchScreen.xib in Resources */ = {isa = PBXBuildFile; fileRef = 607FACDE1AFB9204008FA782 /* LaunchScreen.xib */; };
607FACEC1AFB9204008FA782 /* Tests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 607FACEB1AFB9204008FA782 /* Tests.swift */; };
68A314E129FC972D00293579 /* LimitScrollRange.swift in Sources */ = {isa = PBXBuildFile; fileRef = 68A314E029FC972D00293579 /* LimitScrollRange.swift */; };
70CF4A532162E518B75370AB /* Pods_SwiftUICalendar_Tests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 58AFC65DC1653690BF0B4F96 /* Pods_SwiftUICalendar_Tests.framework */; };
F9DEDA2240F94F1B2C25BE97 /* Pods_SwiftUICalendar_Example.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 1D9008D491DE126083E4B2C8 /* Pods_SwiftUICalendar_Example.framework */; };
/* End PBXBuildFile section */
Expand Down Expand Up @@ -58,6 +59,7 @@
607FACE51AFB9204008FA782 /* SwiftUICalendar_Tests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = SwiftUICalendar_Tests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
607FACEA1AFB9204008FA782 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
607FACEB1AFB9204008FA782 /* Tests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Tests.swift; sourceTree = "<group>"; };
68A314E029FC972D00293579 /* LimitScrollRange.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = LimitScrollRange.swift; sourceTree = "<group>"; };
C61EDE4455EFAA021752CF52 /* Pods-SwiftUICalendar_Tests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-SwiftUICalendar_Tests.debug.xcconfig"; path = "Target Support Files/Pods-SwiftUICalendar_Tests/Pods-SwiftUICalendar_Tests.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */

Expand Down Expand Up @@ -91,6 +93,7 @@
2F1877702727964400A6987F /* InformationView.swift */,
2F8BE218272787B500410651 /* SelectionView.swift */,
2F1877802727E8E300A6987F /* InformationWithSelectionView.swift */,
68A314E029FC972D00293579 /* LimitScrollRange.swift */,
);
name = Samples;
sourceTree = "<group>";
Expand Down Expand Up @@ -364,6 +367,7 @@
2F41278A2721092300336C8C /* BasicUseView.swift in Sources */,
607FACD81AFB9204008FA782 /* ViewController.swift in Sources */,
607FACD61AFB9204008FA782 /* AppDelegate.swift in Sources */,
68A314E129FC972D00293579 /* LimitScrollRange.swift in Sources */,
2F8BE219272787B500410651 /* SelectionView.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
106 changes: 106 additions & 0 deletions Example/SwiftUICalendar/LimitScrollRange.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
//
// LimitScrollRange.swift
// SwiftUICalendar_Example
//
// Created by Benson Wong on 2023-04-28.
// Copyright © 2023 CocoaPods. All rights reserved.
//

import SwiftUI
import SwiftUICalendar
struct LimitScrollRange: View {
@ObservedObject var controller: CalendarController
private let range: ClosedRange<YearMonth>

init() {
let yearMonth = YearMonth.current
range = yearMonth.addMonth(value: -5)...yearMonth.addMonth(value: 5)

// for one way ranges
// range = yearMonth.addMonth(value: -6)...yearMonth
// range = yearMonth...yearMonth.addMonth(value: 6)

self.controller = .init(yearMonth, orientation: .horizontal, dateRange: range)
}

var body: some View {
GeometryReader { reader in
VStack(alignment: .center, spacing: 0) {
Text("\(controller.yearMonth.monthShortString), \(String(controller.yearMonth.year))")
.font(.title)
.padding(EdgeInsets(top: 8, leading: 0, bottom: 8, trailing: 0))

HStack(alignment: .center, spacing: 0) {
Spacer()
Button("Drag Lock") {
controller.isLocked = true
}
Spacer()
Button("Drag Unlock") {
controller.isLocked = false
}
Spacer()
}

HStack {
Spacer()
Button("Lower") {
controller.scrollTo(range.lowerBound)
}
Spacer()

Button("Today Fast") {
controller.scrollTo(.current, isAnimate: false)
}
Spacer()

Button("Today") {
controller.scrollTo(.current)
}
Spacer()

Button("Upper") {
controller.scrollTo(range.upperBound)
}
Spacer()
}

HStack(alignment: .center, spacing: 0) {
ForEach(0..<7, id: \.self) { i in
Text(DateFormatter().shortWeekdaySymbols[i])
.font(.headline)
.frame(width: reader.size.width / 7)
}
}
CalendarView(controller) { date in
GeometryReader { geometry in
ZStack(alignment: .center) {
if date.isToday {
Circle()
.padding(4)
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.foregroundColor(.orange)
Text("\(date.day)")
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.font(.system(size: 10, weight: .bold, design: .default))
.foregroundColor(.white)
} else {
Text("\(date.day)")
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.font(.system(size: 10, weight: .light, design: .default))
.opacity(date.isFocusYearMonth == true ? 1 : 0.4)
}
}
}
}
.navigationBarTitle("Limit Scroll Range")
}
}
}
}

struct LimitScrollRange_Previews: PreviewProvider {
static var previews: some View {
LimitScrollRange()
}
}
3 changes: 3 additions & 0 deletions Example/SwiftUICalendar/ViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ struct MainView: View {
NavigationLink(destination: BasicUseView()) {
Text("Basic use")
}
NavigationLink(destination: LimitScrollRange()) {
Text("Limited Scroll Range")
}
NavigationLink(destination: StartWithMondayView()) {
Text("Start With Monday")
}
Expand Down
97 changes: 97 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import SwiftUICalendar
## Features

- Infinite scroll
- Date range limited scroll
- Support horizontal and vertical scroll
- Full custom calendar cell
- Pager lock
Expand Down Expand Up @@ -655,6 +656,102 @@ struct StartMondayView: View {
</p>
</details>

### Limit Date Range

![Limit date range](./img/limit_date_range?raw=true)
<details>
<summary>Show example code</summary>
<p>
```swift
struct LimitScrollRange: View {
@ObservedObject var controller: CalendarController
private let range: ClosedRange<YearMonth>

init() {
let yearMonth = YearMonth.current
range = yearMonth.addMonth(value: -5)...yearMonth.addMonth(value: 5)
self.controller = .init(yearMonth, orientation: .horizontal, dateRange: range)
}

var body: some View {
GeometryReader { reader in
VStack(alignment: .center, spacing: 0) {
Text("\(controller.yearMonth.monthShortString), \(String(controller.yearMonth.year))")
.font(.title)
.padding(EdgeInsets(top: 8, leading: 0, bottom: 8, trailing: 0))

HStack(alignment: .center, spacing: 0) {
Spacer()
Button("Drag Lock") {
controller.isLocked = true
}
Spacer()
Button("Drag Unlock") {
controller.isLocked = false
}
Spacer()
}

HStack {
Spacer()
Button("Lower") {
controller.scrollTo(range.lowerBound)
}
Spacer()

Button("Today Fast") {
controller.scrollTo(.current, isAnimate: false)
}
Spacer()

Button("Today") {
controller.scrollTo(.current)
}
Spacer()

Button("Upper") {
controller.scrollTo(range.upperBound)
}
Spacer()
}

HStack(alignment: .center, spacing: 0) {
ForEach(0..<7, id: \.self) { i in
Text(DateFormatter().shortWeekdaySymbols[i])
.font(.headline)
.frame(width: reader.size.width / 7)
}
}
CalendarView(controller) { date in
GeometryReader { geometry in
ZStack(alignment: .center) {
if date.isToday {
Circle()
.padding(4)
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.foregroundColor(.orange)
Text("\(date.day)")
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.font(.system(size: 10, weight: .bold, design: .default))
.foregroundColor(.white)
} else {
Text("\(date.day)")
.frame(width: geometry.size.width, height: geometry.size.height, alignment: .center)
.font(.system(size: 10, weight: .light, design: .default))
.opacity(date.isFocusYearMonth == true ? 1 : 0.4)
}
}
}
}
.navigationBarTitle("Limit Scroll Range")
}
}
}
}
```
</p>
</details>

## Struct

### CalendarView
Expand Down
Loading