Created
November 27, 2019 14:20
-
-
Save asobolevsky/499d620880a214164d4b9b9c7eb5f33b to your computer and use it in GitHub Desktop.
This file contains 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
import UIKit | |
protocol CalendarDelegate: class { | |
func calendar(_ calendar: Calendar, willDisplay year: Int) | |
func calendar(_ calendar: Calendar, didSelect date: Date) | |
func calendarShouldChangeYear(_ calendar: Calendar) -> Bool | |
} | |
protocol CalendarDataSource { | |
func calendar(_ calendar: Calendar, eventsFor date: Date) -> [String] | |
func calendar(_ calendar: Calendar, add event: String, to date: Date) | |
} | |
protocol ReminderPresenting { | |
func yearChanged(to year: Int) | |
} | |
class Calendar { | |
var delegate: CalendarDelegate? | |
var dataSource: CalendarDataSource? | |
var selectedDate: Date = Date() | |
var currentYear: Int = 2019 | |
func changeDate(to date: Date) { | |
selectedDate = date | |
delegate?.calendar(self, didSelect: date) | |
if let items = dataSource?.calendar(self, eventsFor: date) { | |
print("Today's events are...") | |
items.forEach { print($0) } | |
} else { | |
print("There are no events for today.") | |
} | |
} | |
func add(event: String) { | |
dataSource?.calendar(self, add: event, to: selectedDate) | |
} | |
func changeYear(to year: Int) { | |
guard delegate?.calendarShouldChangeYear(self) ?? true else { | |
return | |
} | |
delegate?.calendar(self, willDisplay: year) | |
currentYear = year | |
} | |
} | |
class Reminders: ReminderPresenting { | |
var title = "Year: 2019" | |
var calendar = Calendar() | |
init() { | |
let delegate = RemindersCalendarDelegate() | |
delegate.parentController = self | |
calendar.delegate = delegate | |
calendar.dataSource = RemindersCalendarDataSource() | |
} | |
// MARK: - ReminderPresenting | |
func yearChanged(to year: Int) { | |
title = "Year: \(year)" | |
} | |
} | |
class RemindersCalendarDataSource: CalendarDataSource { | |
// MARK: - CalendarDataSource | |
func calendar(_ calendar: Calendar, eventsFor date: Date) -> [String] { | |
return [ "Throw away trash", "Wash the dishes" ] | |
} | |
func calendar(_ calendar: Calendar, add event: String, to date: Date) { | |
print("You are going to \(event) on \(date)") | |
} | |
} | |
class RemindersCalendarDelegate: CalendarDelegate { | |
var parentController: ReminderPresenting? | |
// MARK: - CalendarDelegate | |
func calendarShouldChangeYear(_ calendar: Calendar) -> Bool { | |
return true | |
} | |
func calendar(_ calendar: Calendar, willDisplay year: Int) { | |
parentController?.yearChanged(to: year) | |
} | |
func calendar(_ calendar: Calendar, didSelect date: Date) { | |
print("You selected \(date)") | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment