Created
May 8, 2020 14:35
-
-
Save dfrobison/caa675ad68b10945780b326b755021c1 to your computer and use it in GitHub Desktop.
[Calendar without using a collection view] from https://gist.github.com/mecid/f8859ea4bdbd02cf5d440d58e936faec
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| fileprivate extension DateFormatter { | |
| static var month: DateFormatter { | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "MMMM" | |
| return formatter | |
| } | |
| static var monthAndYear: DateFormatter { | |
| let formatter = DateFormatter() | |
| formatter.dateFormat = "MMMM yyyy" | |
| return formatter | |
| } | |
| } | |
| fileprivate extension Calendar { | |
| func generateDates( | |
| inside interval: DateInterval, | |
| matching components: DateComponents | |
| ) -> [Date] { | |
| var dates: [Date] = [] | |
| dates.append(interval.start) | |
| enumerateDates( | |
| startingAfter: interval.start, | |
| matching: components, | |
| matchingPolicy: .nextTime | |
| ) { date, _, stop in | |
| if let date = date { | |
| if date < interval.end { | |
| dates.append(date) | |
| } else { | |
| stop = true | |
| } | |
| } | |
| } | |
| return dates | |
| } | |
| } | |
| struct WeekView<DateView>: View where DateView: View { | |
| @Environment(\.calendar) var calendar | |
| let week: Date | |
| let content: (Date) -> DateView | |
| init(week: Date, @ViewBuilder content: @escaping (Date) -> DateView) { | |
| self.week = week | |
| self.content = content | |
| } | |
| private var days: [Date] { | |
| guard | |
| let weekInterval = calendar.dateInterval(of: .weekOfYear, for: week) | |
| else { return [] } | |
| return calendar.generateDates( | |
| inside: weekInterval, | |
| matching: DateComponents(hour: 0, minute: 0, second: 0) | |
| ) | |
| } | |
| var body: some View { | |
| HStack { | |
| ForEach(days, id: \.self) { date in | |
| HStack { | |
| if self.calendar.isDate(self.week, equalTo: date, toGranularity: .month) { | |
| self.content(date) | |
| } else { | |
| self.content(date).hidden() | |
| } | |
| } | |
| } | |
| } | |
| } | |
| } | |
| struct MonthView<DateView>: View where DateView: View { | |
| @Environment(\.calendar) var calendar | |
| let month: Date | |
| let content: (Date) -> DateView | |
| init(month: Date, @ViewBuilder content: @escaping (Date) -> DateView) { | |
| self.month = month | |
| self.content = content | |
| } | |
| private var weeks: [Date] { | |
| guard | |
| let monthInterval = calendar.dateInterval(of: .month, for: month) | |
| else { return [] } | |
| return calendar.generateDates( | |
| inside: monthInterval, | |
| matching: DateComponents(hour: 0, minute: 0, second: 0, weekday: 1) | |
| ) | |
| } | |
| private var header: some View { | |
| let component = calendar.component(.month, from: month) | |
| let formatter = component == 1 ? DateFormatter.monthAndYear : .month | |
| return Text(formatter.string(from: month)) | |
| .font(.title) | |
| .padding() | |
| } | |
| var body: some View { | |
| VStack { | |
| header | |
| ForEach(weeks, id: \.self) { week in | |
| WeekView(week: week, content: self.content) | |
| } | |
| } | |
| } | |
| } | |
| struct CalendarView<DateView>: View where DateView: View { | |
| @Environment(\.calendar) var calendar | |
| let interval: DateInterval | |
| let content: (Date) -> DateView | |
| init(interval: DateInterval, @ViewBuilder content: @escaping (Date) -> DateView) { | |
| self.interval = interval | |
| self.content = content | |
| } | |
| private var months: [Date] { | |
| calendar.generateDates( | |
| inside: interval, | |
| matching: DateComponents(day: 1, hour: 0, minute: 0, second: 0) | |
| ) | |
| } | |
| var body: some View { | |
| ScrollView(.vertical, showsIndicators: false) { | |
| VStack { | |
| ForEach(months, id: \.self) { month in | |
| MonthView(month: month, content: self.content) | |
| } | |
| } | |
| } | |
| } | |
| } | |
| struct RootView: View { | |
| @Environment(\.calendar) var calendar | |
| private var year: DateInterval { | |
| calendar.dateInterval(of: .year, for: Date())! | |
| } | |
| var body: some View { | |
| CalendarView(interval: year) { date in | |
| Text("30") | |
| .hidden() | |
| .padding(8) | |
| .background(Color.blue) | |
| .clipShape(Circle()) | |
| .padding(.vertical, 4) | |
| .overlay( | |
| Text(String(self.calendar.component(.day, from: date))) | |
| ) | |
| } | |
| } | |
| } | |
| @lailo | |
| lailo commented 2 days ago | |
| @mecid thanks for sharing this gist. | |
| I'm asking myself why you used hidden() and overlay{} in the lines 157 - 165 instead of .frame(). Something like this: | |
| struct RootView: View { | |
| @Environment(\.calendar) var calendar | |
| private var year: DateInterval { | |
| calendar.dateInterval(of: .year, for: Date())! | |
| } | |
| var body: some View { | |
| CalendarView(interval: year) { date in | |
| Text(String(self.calendar.component(.day, from: date))) | |
| .frame(width: 40, height: 40, alignment: .center) | |
| .background(Color.blue) | |
| .clipShape(Circle()) | |
| .padding(.vertical, 4) | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment