Skip to content

Commit

Permalink
Improve how time of birth is handled
Browse files Browse the repository at this point in the history
  • Loading branch information
valeriyvan committed Jan 11, 2021
1 parent 6a6dfdf commit 44c91a6
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 30 deletions.
12 changes: 10 additions & 2 deletions Motivation/AgeView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class AgeView: ScreenSaverView {
animationTimeInterval = 1 / 30

// Recall preferences
birthday = Preferences().birthday
updateBirthday()

// Setup the label
addSubview(textLabel)
Expand Down Expand Up @@ -165,9 +165,17 @@ class AgeView: ScreenSaverView {

/// Birthday changed
@objc private func birthdayDidChange(_ notification: Notification?) {
birthday = Preferences().birthday
updateBirthday()
}

private func updateBirthday() {
if let dateComponents = Preferences().birthdayComponents {
birthday = Calendar.current.date(from: dateComponents)
} else {
birthday = Date()
}
}

/// Update the font for the current size
private func updateFont() {
if birthday != nil {
Expand Down
5 changes: 3 additions & 2 deletions Motivation/Configuration.xib
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
<customObject id="-2" userLabel="File's Owner" customClass="ConfigureSheetController" customModule="Motivation" customModuleProvider="target">
<connections>
<outlet property="birthdatePicker" destination="24i-HD-fhj" id="bHR-UT-Eba"/>
<outlet property="birthdayIncludesTimeToggle" destination="HSJ-VB-Zwi" id="Pjd-It-nKx"/>
<outlet property="lightRadio" destination="SYp-OG-PUb" id="QAx-UK-2Qi"/>
<outlet property="moderateRadio" destination="pTh-rE-L1F" id="Okf-GD-efx"/>
<outlet property="terrifyingRadio" destination="daF-cg-qud" id="Qzr-li-xhu"/>
Expand Down Expand Up @@ -105,8 +106,8 @@ DQ
</textFieldCell>
</textField>
<button translatesAutoresizingMaskIntoConstraints="NO" id="HSJ-VB-Zwi">
<rect key="frame" x="18" y="149" width="52" height="18"/>
<buttonCell key="cell" type="check" title="Time" bezelStyle="regularSquare" imagePosition="left" inset="2" id="3dd-s4-7Zs">
<rect key="frame" x="18" y="149" width="97" height="18"/>
<buttonCell key="cell" type="check" title="Include time" bezelStyle="regularSquare" imagePosition="left" inset="2" id="3dd-s4-7Zs">
<behavior key="behavior" changeContents="YES" doesNotDimImage="YES" lightByContents="YES"/>
<font key="font" metaFont="system"/>
</buttonCell>
Expand Down
55 changes: 47 additions & 8 deletions Motivation/ConfigureSheetController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,29 @@ class ConfigureSheetController: NSObject {

@IBOutlet var window: NSWindow?
@IBOutlet weak var birthdatePicker: NSDatePicker!
@IBOutlet weak var birthdayIncludesTimeToggle: NSButton!
@IBOutlet weak var lightRadio: NSButton!
@IBOutlet weak var moderateRadio: NSButton!
@IBOutlet weak var terrifyingRadio: NSButton!

private var pickerElements: NSDatePicker.ElementFlags {
if birthdayIncludesTime {
return [.yearMonthDay, .hourMinute]
} else {
return [.yearMonthDay]
}
}

private var birthdayIncludesTime: Bool { birthdayIncludesTimeToggle.state.rawValue != 0 }

private var calendarComponents: Set<Calendar.Component> {
if birthdayIncludesTime {
return [.year, .month, .day, .hour, .minute]
} else {
return [.year, .month, .day]
}
}

override init() {
super.init()
let myBundle = Bundle(for: ConfigureSheetController.self)
Expand All @@ -26,26 +45,46 @@ class ConfigureSheetController: NSObject {

override func awakeFromNib() {
super.awakeFromNib()

switch Preferences().motivationLevel {
case .light: lightRadio.state = .on
case .moderate: moderateRadio.state = .on
case .terrifying: terrifyingRadio.state = .on
}
if let birthday = Preferences().birthday {
birthdatePicker.dateValue = birthday

let birthDate: Date
let timeIsIncluded: Bool
if let dateComponents = Preferences().birthdayComponents,
let date = Calendar.current.date(from: dateComponents)
{
birthDate = date
timeIsIncluded = dateComponents.hour != nil
} else {
// Probably, first launch
birthDate = Date()
timeIsIncluded = false
}
birthdatePicker.dateValue = birthDate
birthdayIncludesTimeToggle.state = timeIsIncluded ? .on : .off
birthdatePicker.datePickerElements = pickerElements
}

@IBAction func dateDidChange(_ sender: NSDatePicker) {
Preferences().birthday = sender.dateValue
updateDate()
}

private func updateDate() {
Preferences().birthdayComponents =
Calendar
.current
.dateComponents(calendarComponents,
from: birthdatePicker.dateValue)

}

@IBAction func time(_ sender: NSButton) {
if birthdatePicker.datePickerElements.contains(.hourMinute) {
birthdatePicker.datePickerElements = [.yearMonthDay]
} else {
birthdatePicker.datePickerElements = [.hourMinute, .yearMonthDay]
}
birthdatePicker.datePickerElements = pickerElements
updateDate()
}

@IBAction func close(_ sender: NSButton) {
Expand Down
45 changes: 27 additions & 18 deletions Motivation/Preferences.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,23 +28,31 @@ class Preferences: NSObject {
static var birthdayDidChangeNotificationName = "Preferences.birthdayDidChangeNotification"
static var motivationLevelDidChangeNotificationName = "Preferences.motivationLevelDidChangeNotification"

var birthday: Date? {
get {
let timestamp = defaults?.object(forKey: "Birthday") as? TimeInterval
return timestamp.map { Date(timeIntervalSince1970: $0) }
}

set {
if let date = newValue {
defaults?.set(date.timeIntervalSince1970, forKey: "Birthday")
} else {
defaults?.removeObject(forKey: "Birthday")
}
defaults?.synchronize()

NotificationCenter.default.post(name: Notification.Name(rawValue: type(of: self).birthdayDidChangeNotificationName), object: newValue)
}
}
var birthdayComponents: DateComponents? {
get {
if let data = defaults?.object(forKey: "BirthdayComponents") as? Data,
let dateComponents = try? JSONDecoder().decode(DateComponents.self, from: data) {
return dateComponents
} else if let timestamp = defaults?.object(forKey: "Birthday") as? TimeInterval {
// Trying read Date stored by earlier version
defaults?.removeObject(forKey: "Birthday")
let date = Date(timeIntervalSince1970: timestamp)
// Here we suppose time of birth is not included
return Calendar.current.dateComponents([.year, .month, .day], from: date)
} else {
// Not available. Probably, first launch.
return nil
}
}
set {
if let jsonData = try? JSONEncoder().encode(newValue) {
defaults?.setValue(jsonData, forKey: "BirthdayComponents")
defaults?.synchronize()
}
let notificationName = Notification.Name(rawValue: type(of: self).birthdayDidChangeNotificationName)
NotificationCenter.default.post(name: notificationName, object: newValue)
}
}

var motivationLevel: MotivationLevel {
get {
Expand All @@ -56,7 +64,8 @@ class Preferences: NSObject {
defaults?.set(newValue.rawValue, forKey: "MotivationLevel")
defaults?.synchronize()

NotificationCenter.default.post(name: Notification.Name(rawValue: type(of: self).motivationLevelDidChangeNotificationName), object: newValue.rawValue)
let notificationName = Notification.Name(rawValue: type(of: self).motivationLevelDidChangeNotificationName)
NotificationCenter.default.post(name: notificationName, object: newValue.rawValue)
}
}

Expand Down

0 comments on commit 44c91a6

Please sign in to comment.