forked from RaffiKian/RKCalendar
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Example.txt
179 lines (145 loc) · 7.53 KB
/
Example.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
import SwiftUI
import RKCalendar
enum TestOptions: String {
case singleDate
case dateRange
case multiDate
case disabled
case timeSetting
case weekly
case horizontal
}
extension TestOptions: Identifiable {
var id: RawValue { rawValue }
}
struct ContentView : View {
@State var isPresented: TestOptions?
@StateObject var rkManager1 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .singleDate)
@StateObject var rkManager2 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .dateRange) // automatically goes to mode=2 after start selection, and vice versa.
@StateObject var rkManager3 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .multiDate)
@StateObject var rkManager4 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .singleDate)
@StateObject var rkManager5 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .singleDate)
@StateObject var rkManager6 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .singleDate)
@StateObject var rkManager7 = RKManager(calendar: Calendar.current, minimumDate: Date().addingTimeInterval(-60*60*24*60), maximumDate: Date().addingTimeInterval(60*60*24*90), mode: .singleDate)
var body: some View {
VStack (spacing: 10) {
Group {
Button(action: { isPresented = .singleDate }) {
Text("Example 1 - Single Date Selection").foregroundColor(.blue)
}
Text(getTextFromDate(rkManager1.selectedDate))
Button(action: { isPresented = .dateRange }) {
VStack {
Text("Example 2 - Range of Dates Selection").foregroundColor(.blue)
Text("(end date > start date)").foregroundColor(.blue)
}
}
VStack {
Text(getTextFromDate(rkManager2.startDate))
Text(getTextFromDate(rkManager2.endDate))
}
Button(action: { isPresented = .multiDate }) {
Text("Example 3 - Multiple Dates Selection ").foregroundColor(.blue)
}
datesView(dates: rkManager3.selectedDates)
}
Group {
Button(action: { isPresented = .disabled }) {
Text("Example 4 - Disabled Dates Setting").foregroundColor(.blue)
}
datesView(dates: rkManager4.disabledDates)
Button(action: { isPresented = .timeSetting }) {
Text("Example 5 - Time setting on long press").foregroundColor(.blue)
}
Text(getTextFromDateTime(rkManager5.selectedDate))
Button(action: { isPresented = .weekly }) {
Text("Example 6 - Weekly view").foregroundColor(.blue)
}
Text(getTextFromDate(rkManager6.selectedDate))
Button(action: { isPresented = .horizontal }) {
Text("Example 7 - Horizontal view with paging").foregroundColor(.blue)
}
Text(getTextFromDate(rkManager7.selectedDate))
}
}
.sheet(item: $isPresented) { selection in
switch selection {
case .singleDate: RKCalendarView().environmentObject(rkManager1)
case .dateRange: RKCalendarView().environmentObject(rkManager2)
case .multiDate: RKCalendarView().environmentObject(rkManager3)
case .disabled: RKCalendarView().environmentObject(rkManager4)
case .timeSetting: RKCalendarView().environmentObject(rkManager5)
case .weekly: RKCalendarView().environmentObject(rkManager6)
case .horizontal: RKCalendarView().environmentObject(rkManager7)
}
}
.onAppear(perform: startUp)
.navigationViewStyle(StackNavigationViewStyle())
}
func datesView(dates: [Date], _ withTime: Bool = false) -> some View {
ScrollView (.horizontal) {
HStack {
ForEach(dates, id: \.self) { date in
withTime ? Text(getTextFromDateTime(date)) : Text(getTextFromDate(date))
}
}
}.padding(.horizontal, 5)
}
func startUp() {
// example of horizontal view
// rkManager1.isVertical = false
// example of pre-setting selected dates
let testOnDates = [Date().addingTimeInterval(60*60*24), Date().addingTimeInterval(60*60*24*2)]
rkManager3.selectedDates.append(contentsOf: testOnDates)
// example of some foreground colors
rkManager3.colors.weekdayHeaderColor = Color.blue
rkManager3.colors.monthHeaderColor = Color.green
rkManager3.colors.textColor = Color.blue
rkManager3.colors.disabledColor = Color.red
// example of pre-setting disabled dates
let testOffDates = [
Date().addingTimeInterval(60*60*24*4),
Date().addingTimeInterval(60*60*24*5),
Date().addingTimeInterval(60*60*24*7)]
rkManager4.disabledDates.append(contentsOf: testOffDates)
// example of allowing time (hh:mm) to be set and displayed on a long press
// example to display in Japanese
rkManager5.locale = Locale(identifier: "ja")
rkManager5.displayTime = true
// example of weekly view with "paging"
rkManager6.isVertical = false
rkManager6.isWeeklyView = true
rkManager6.isContinuous = false
rkManager6.colors.weekdayHeaderColor = Color.blue
rkManager6.colors.monthHeaderColor = Color.green
// example of horizontal monthly view with "paging"
rkManager7.isVertical = false
rkManager7.isContinuous = false
rkManager7.colors.weekdayHeaderColor = Color.blue
rkManager7.colors.monthHeaderColor = Color.green
}
func getDateTimeAsString(_ date: Date?) -> String {
if date == nil { return "" }
let format = DateFormatter()
format.dateFormat = "yyyy-MM-dd-HH-mm"
format.timeZone = TimeZone.current
format.locale = Locale.current
return format.string(from: date!)
}
func getTextFromDate(_ date: Date?) -> String {
if date == nil { return "" }
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.locale = Locale.current
formatter.dateFormat = "EEEE, MMMM d, yyyy"
return formatter.string(from: date!)
}
func getTextFromDateTime(_ date: Date?) -> String {
if date == nil { return "" }
let formatter = DateFormatter()
formatter.timeZone = TimeZone.current
formatter.locale = Locale.current
formatter.dateFormat = "EEEE, MMMM d HH:mm, yyyy"
return formatter.string(from: date!)
}
}