Skip to content

Commit

Permalink
Bugfix/callback (#5)
Browse files Browse the repository at this point in the history
* updated elegantpages to fix bug with willdisplaymonth being called multiple times at calendar creation

* fixed bug where a month wouldn't show if the end date was exactly the start of that month

* updated the examples

* lowered the swift tools version

* updated the examples
  • Loading branch information
ThasianX authored Aug 1, 2020
1 parent 49ee3d2 commit 112afd0
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 282 deletions.
266 changes: 24 additions & 242 deletions Example/Example.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/ThasianX/ElegantPages",
"state": {
"branch": null,
"revision": "7eb457484e04375d7e6a3ca442a98cbe75ead7b0",
"version": "1.4.0"
"revision": "47d166799f2313ad9e47c9c22687021d396d940f",
"version": "1.4.1"
}
}
]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Kevin Li - 11:47 AM - 6/13/20

import ElegantCalendar
import SwiftUI

struct ExampleCalendarView: View {
Expand All @@ -10,15 +11,15 @@ struct ExampleCalendarView: View {

@State private var calendarTheme: CalendarTheme = .royalBlue

init(ascVisits: [Visit]) {
init(ascVisits: [Visit], initialMonth: Date?) {
let configuration = CalendarConfiguration(
calendar: currentCalendar,
startDate: ascVisits.first!.arrivalDate,
endDate: ascVisits.last!.arrivalDate)

calendarManager = ElegantCalendarManager(
configuration: configuration,
initialMonth: .daysFromToday(30))
initialMonth: initialMonth)

visitsByDay = Dictionary(
grouping: ascVisits,
Expand Down Expand Up @@ -87,6 +88,6 @@ extension ExampleCalendarView: ElegantCalendarDelegate {

struct ExampleCalendarView_Previews: PreviewProvider {
static var previews: some View {
ExampleCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)))
ExampleCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)), initialMonth: nil)
}
}
9 changes: 9 additions & 0 deletions Example/Example/Calendar with Accessory View/Visit.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import SwiftUI

let currentCalendar = Calendar.current
let screen = UIScreen.main.bounds

struct Visit {

Expand Down Expand Up @@ -99,3 +100,11 @@ fileprivate extension Color {
}

}

fileprivate extension DateComponents {

static var everyDay: DateComponents {
DateComponents(hour: 0, minute: 0, second: 0)
}

}
9 changes: 6 additions & 3 deletions Example/Example/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ struct ContentView: View {
// ExampleYearlyCalendarView(
// ascVisits: Visit.mocks(
// start: .daysFromToday(-30*36),
// end: .daysFromToday(30*36)))
// end: .daysFromToday(30*36)),
// initialYear: .daysFromToday(365))

// ExampleMonthlyCalendarView(
// ascVisits: Visit.mocks(
// start: .daysFromToday(-30*36),
// end: .daysFromToday(30*36)))
// end: .daysFromToday(30*36)),
// initialMonth: .daysFromToday(30))

ExampleCalendarView(
ascVisits: Visit.mocks(
start: .daysFromToday(-30*36),
end: .daysFromToday(30*36)))
end: .daysFromToday(30*36)),
initialMonth: Date())

// ExampleSelectionView()
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Kevin Li - 5:51 PM - 6/14/20

import ElegantCalendar
import SwiftUI

struct ExampleMonthlyCalendarView: View {
Expand All @@ -10,12 +11,12 @@ struct ExampleMonthlyCalendarView: View {

@State private var calendarTheme: CalendarTheme = .royalBlue

init(ascVisits: [Visit]) {
init(ascVisits: [Visit], initialMonth: Date?) {
let configuration = CalendarConfiguration(calendar: currentCalendar,
startDate: ascVisits.first!.arrivalDate,
endDate: ascVisits.last!.arrivalDate)
calendarManager = MonthlyCalendarManager(configuration: configuration,
initialMonth: .daysFromToday(30))
initialMonth: initialMonth)
visitsByDay = Dictionary(grouping: ascVisits, by: { currentCalendar.startOfDay(for: $0.arrivalDate) })

calendarManager.datasource = self
Expand Down Expand Up @@ -73,6 +74,6 @@ extension ExampleMonthlyCalendarView: MonthlyCalendarDelegate {

struct ExampleMonthlyCalendarView_Previews: PreviewProvider {
static var previews: some View {
ExampleYearlyCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)))
ExampleYearlyCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)), initialYear: nil)
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Kevin Li - 4:49 PM - 6/14/20

import ElegantCalendar
import SwiftUI

struct ExampleYearlyCalendarView: View {
Expand All @@ -10,12 +11,12 @@ struct ExampleYearlyCalendarView: View {

@State private var calendarTheme: CalendarTheme = .royalBlue

init(ascVisits: [Visit]) {
init(ascVisits: [Visit], initialYear: Date?) {
let configuration = CalendarConfiguration(calendar: currentCalendar,
startDate: ascVisits.first!.arrivalDate,
endDate: ascVisits.last!.arrivalDate)
calendarManager = YearlyCalendarManager(configuration: configuration,
initialYear: .daysFromToday(365))
initialYear: initialYear)
visitsByDay = Dictionary(grouping: ascVisits, by: { currentCalendar.startOfDay(for: $0.arrivalDate) })

calendarManager.delegate = self
Expand Down Expand Up @@ -53,6 +54,6 @@ extension ExampleYearlyCalendarView: YearlyCalendarDelegate {

struct ExampleYearlyCalendarView_Previews: PreviewProvider {
static var previews: some View {
ExampleYearlyCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)))
ExampleYearlyCalendarView(ascVisits: Visit.mocks(start: .daysFromToday(-365*2), end: .daysFromToday(365*2)), initialYear: nil)
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Kevin Li - 8:33 PM - 6/24/20

import ElegantPages
import ElegantCalendar
import SwiftUI

fileprivate let turnAnimation: Animation = .spring(response: 0.4, dampingFraction: 0.95)
Expand Down
1 change: 1 addition & 0 deletions Example/Example/Shared/ChangeThemeButton.swift
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
// Kevin Li - 7:42 PM - 7/14/20

import ElegantCalendar
import SwiftUI

struct ChangeThemeButton: View {
Expand Down
39 changes: 39 additions & 0 deletions Example/Example/Shared/Date+Additions.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Kevin Li - 12:05 PM - 8/1/20

import Foundation

extension Date {

static func daysFromToday(_ days: Int) -> Date {
Date().addingTimeInterval(TimeInterval(60*60*24*days))
}

}

extension Date {

var fullDate: String {
DateFormatter.fullDate.string(from: self)
}

var timeOnlyWithPadding: String {
DateFormatter.timeOnlyWithPadding.string(from: self)
}

}

extension DateFormatter {

static var fullDate: DateFormatter {
let formatter = DateFormatter()
formatter.dateFormat = "EEEE, MMM d, yyyy"
return formatter
}

static let timeOnlyWithPadding: DateFormatter = {
let formatter = DateFormatter()
formatter.dateFormat = "h:mm a"
return formatter
}()

}
21 changes: 0 additions & 21 deletions Example/Example/Shared/Date+FullDate.swift

This file was deleted.

58 changes: 58 additions & 0 deletions Example/Example/Shared/LightDarkThemePreview.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// Kevin Li - 12:08 PM - 8/1/20

import SwiftUI

struct LightDarkThemePreview<Preview: View>: View {

let preview: Preview

var body: some View {
Group {
LightThemePreview {
self.preview
}

DarkThemePreview {
self.preview
}
}
}

init(@ViewBuilder preview: @escaping () -> Preview) {
self.preview = preview()
}

}

struct LightThemePreview<Preview: View>: View {

let preview: Preview

var body: some View {
preview
.previewLayout(.sizeThatFits)
.colorScheme(.light)
}

init(@ViewBuilder preview: @escaping () -> Preview) {
self.preview = preview()
}

}

struct DarkThemePreview<Preview: View>: View {

let preview: Preview

var body: some View {
preview
.previewLayout(.sizeThatFits)
.colorScheme(.dark)
.background(Color.black.edgesIgnoringSafeArea(.all))
}

init(@ViewBuilder preview: @escaping () -> Preview) {
self.preview = preview()
}

}
4 changes: 2 additions & 2 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
"repositoryURL": "https://github.com/ThasianX/ElegantPages",
"state": {
"branch": null,
"revision": "7eb457484e04375d7e6a3ca442a98cbe75ead7b0",
"version": "1.4.0"
"revision": "47d166799f2313ad9e47c9c22687021d396d940f",
"version": "1.4.1"
}
}
]
Expand Down
4 changes: 2 additions & 2 deletions Package.swift
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// swift-tools-version:5.2
// swift-tools-version:5.1

import PackageDescription

Expand All @@ -13,7 +13,7 @@ let package = Package(
targets: ["ElegantCalendar"]),
],
dependencies: [
.package(url: "https://github.com/ThasianX/ElegantPages", from: "1.4.0")
.package(url: "https://github.com/ThasianX/ElegantPages", from: "1.4.1")
],
targets: [
.target(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@ import SwiftUI

extension Calendar {

func endOfDay(for date: Date) -> Date {
var components = DateComponents()
components.day = 1
components.second = -1
return self.date(byAdding: components, to: startOfDay(for: date))!
}

func isDate(_ date1: Date, equalTo date2: Date, toGranularities components: Set<Calendar.Component>) -> Bool {
components.reduce(into: true) { isEqual, component in
isEqual = isEqual && isDate(date1, equalTo: date2, toGranularity: component)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public class MonthlyCalendarManager: ObservableObject, ConfigurationDirectAccess

let months = configuration.calendar.generateDates(
inside: DateInterval(start: configuration.startDate,
end: configuration.endDate),
end: configuration.calendar.endOfDay(for: configuration.endDate)),
matching: .firstDayOfEveryMonth)

self.months = configuration.ascending ? months : months.reversed()
Expand Down

0 comments on commit 112afd0

Please sign in to comment.