Skip to content

Commit

Permalink
Bugfixes and the first result that can be called stable��
Browse files Browse the repository at this point in the history
However, not all TODOs have been explained and solved
  • Loading branch information
RareScrap committed Jan 20, 2021
1 parent b4fa5b9 commit d78bfe5
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 17 deletions.
19 changes: 10 additions & 9 deletions Source/DayView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,19 +61,20 @@ public final class DayView: UIView, TimelinePagerViewDelegate {
}

public var calendar: Calendar = Calendar.autoupdatingCurrent {
didSet {
// TODO: Should we observe calendar in state rather then change it manualy?
didSet { // TODO: willSet?
dayHeaderView.calendar = self.calendar
timelinePagerView.calendar = self.calendar

let date = self.state?.selectedDate ?? Date()
let newState = DayViewState(date: date, calendar: calendar)
newState.move(to: date)
state = newState
// recalculate the selectedDate taking into account
// the difference between new and old timezones
let oldCalendar = state!.calendar
let selectedDate = (self.state?.selectedDate ?? Date())
.dateOnly(calendar: calendar, oldCalendar: oldCalendar)

// TODO: Shound we redraw dayView and its subvies? Or we should do this when calendar in state was changed?
// setNeedsDisplay()
// self.subviews.forEach { $0.setNeedsDisplay() }
// setting a new state
let newState = DayViewState(date: selectedDate, calendar: calendar)
state = newState
newState.move(to: selectedDate) // TODO: Why I added this :I?
}
}

Expand Down
2 changes: 2 additions & 0 deletions Source/DayViewState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ public protocol DayViewStateUpdating: AnyObject {

public final class DayViewState {
public private(set) var calendar: Calendar
// willSet {} // TODO: I think this needs to be implemented, cause timezone can change without creating a new state object
public private(set) var selectedDate: Date
private var clients = [DayViewStateUpdating]()

Expand All @@ -17,6 +18,7 @@ public final class DayViewState {

public func move(to date: Date) {
let date = date.dateOnly(calendar: calendar)
// if (date == selectedDate) { return } // TODO: If necessary? Just trying to make sure that willMoveTo not be called if we are already on this page
notify(clients: clients, moveTo: date)
selectedDate = date
}
Expand Down
8 changes: 8 additions & 0 deletions Source/Extensions/Date+DateOnly.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ extension Date {
let returnValue = calendar.date(from: newComponents)
return returnValue!
}

/// Cuts off the time, leaving the beginning of the day for the new time zone
func dateOnly(calendar: Calendar, oldCalendar: Calendar) -> Date {
var newDate = self.dateOnly(calendar: oldCalendar)
let diff = oldCalendar.timeZone.secondsFromGMT() - calendar.timeZone.secondsFromGMT()
newDate.addTimeInterval(TimeInterval(diff))
return newDate
}
}
16 changes: 15 additions & 1 deletion Source/Header/DayHeaderView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@ import DateToolsSwift

public final class DayHeaderView: UIView, DaySelectorDelegate, DayViewStateUpdating, UIPageViewControllerDataSource, UIPageViewControllerDelegate {
public private(set) var daysInWeek = 7
public var calendar: Calendar
public var calendar: Calendar {
didSet {
daySymbolsView.calendar = calendar
swipeLabelView.calendar = calendar
configure() // TODO: Should I do it that way or better just to set a new vc without adding subview (like in state#didSet)?
}
}

private var style = DayHeaderStyle()
private var currentSizeClass = UIUserInterfaceSizeClass.compact
Expand All @@ -16,6 +22,14 @@ public final class DayHeaderView: UIView, DaySelectorDelegate, DayViewStateUpdat
didSet {
state?.subscribe(client: self)
swipeLabelView.state = state

// Fixes the "jump" of the pager from today to the selected date. This is noticeable
// when we are on a date outside of today's week, and try to change the time zone.
let previousSelectedDate = state!.selectedDate // day selected before timezone change
let vc = makeSelectorController(startDate: beginningOfWeek(previousSelectedDate))
vc.selectedDate = previousSelectedDate
currentWeekdayIndex = vc.selectedIndex
pagingViewController.setViewControllers([vc], direction: .forward, animated: false, completion: nil)
}
}

Expand Down
10 changes: 9 additions & 1 deletion Source/Header/DaySymbolsView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,15 @@ import UIKit

public final class DaySymbolsView: UIView {
public private(set) var daysInWeek = 7
private var calendar = Calendar.autoupdatingCurrent
public var calendar = Calendar.autoupdatingCurrent {
didSet {
// TODO: I honestly don't know if I should be doing this. Changes to
// the timezone should not affect the displayed days of the week,
// however, just in case, I am reconfiguring this view
configure()
setNeedsLayout()
}
}
private var labels = [UILabel]()
private var style: DaySymbolsStyle = DaySymbolsStyle()

Expand Down
8 changes: 7 additions & 1 deletion Source/Header/SwipeLabelView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,13 @@ public final class SwipeLabelView: UIView, DayViewStateUpdating {
case Backward
}

public private(set) var calendar = Calendar.autoupdatingCurrent
public var calendar = Calendar.autoupdatingCurrent {
didSet {
// I doubt that someone will use this view without a state, but
// I consider it necessary to update text label for such a situation
updateLabelText()
}
}
public weak var state: DayViewState? {
willSet(newValue) {
state?.unsubscribe(client: self)
Expand Down
29 changes: 24 additions & 5 deletions Source/Timeline/TimelinePagerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,11 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr

public var calendar: Calendar = Calendar.autoupdatingCurrent {
didSet {
pagingViewController.viewControllers?.forEach {
// changing timezone in loaded pages
pagingViewController.children.forEach {
let vc = $0 as! TimelineContainerController
let oldCalendar = vc.timeline.calendar
vc.timeline.date = vc.timeline.date.dateOnly(calendar: calendar, oldCalendar: oldCalendar)
vc.timeline.calendar = calendar
}
}
Expand Down Expand Up @@ -180,7 +183,11 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr

private func updateTimeline(_ timeline: TimelineView) {
guard let dataSource = dataSource else {return}
let date = timeline.date.dateOnly(calendar: calendar)
// I don't know under what circumstances, but I know for sure that
// a situation is possible when these calendars have different time
// zones. It is in this case that the call below will prevent a bug
// that leads to "jumps" after days when you swipe to the left
let date = timeline.date.dateOnly(calendar: calendar, oldCalendar: timeline.calendar)
let events = dataSource.eventsForDate(date)
let day = TimePeriod(beginning: date,
chunk: TimeChunk.dateComponents(days: 1))
Expand Down Expand Up @@ -444,7 +451,7 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
// MARK: UIPageViewControllerDataSource

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerBefore viewController: UIViewController) -> UIViewController? {
guard let containerController = viewController as? TimelineContainerController else {return nil}
guard let containerController = viewController as? TimelineContainerController else {return nil}
let previousDate = containerController.timeline.date.add(TimeChunk.dateComponents(days: -1), calendar: calendar)
let vc = configureTimelineController(date: previousDate)
let offset = (pageViewController.viewControllers?.first as? TimelineContainerController)?.container.contentOffset
Expand All @@ -453,7 +460,7 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
}

public func pageViewController(_ pageViewController: UIPageViewController, viewControllerAfter viewController: UIViewController) -> UIViewController? {
guard let containerController = viewController as? TimelineContainerController else {return nil}
guard let containerController = viewController as? TimelineContainerController else {return nil}
let nextDate = containerController.timeline.date.add(TimeChunk.dateComponents(days: 1), calendar: calendar)
let vc = configureTimelineController(date: nextDate)
let offset = (pageViewController.viewControllers?.first as? TimelineContainerController)?.container.contentOffset
Expand All @@ -466,7 +473,19 @@ public final class TimelinePagerView: UIView, UIGestureRecognizerDelegate, UIScr
public func pageViewController(_ pageViewController: UIPageViewController, didFinishAnimating finished: Bool, previousViewControllers: [UIViewController], transitionCompleted completed: Bool) {
guard completed else {return}
if let timelineContainerController = pageViewController.viewControllers?.first as? TimelineContainerController {
let selectedDate = timelineContainerController.timeline.date
// Unfortunately, I cannot explain this change in detail. I fiddled
// with a bug in which some days were skipped when swiping, completely
// desperate and wrote this. To my surprise, this solved the problem.
// I'll do some research on the reasons later.

// Попытка пофиксить сбой хедера с перескоком через число
var selectedDate = timelineContainerController.timeline.date
selectedDate = selectedDate.dateOnly(calendar: self.state!.calendar, oldCalendar: timelineContainerController.container.timeline.calendar)

// попытка пофиксить баг с уменьшением и свайпом
timelineContainerController.timeline.date = selectedDate // Я хз как и какие последствия оно имеет, но это сработало
timelineContainerController.timeline.calendar = self.state!.calendar

delegate?.timelinePager(timelinePager: self, willMoveTo: selectedDate)
state?.client(client: self, didMoveTo: selectedDate)
scrollToFirstEventIfNeeded()
Expand Down

0 comments on commit d78bfe5

Please sign in to comment.