-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathCommon.swift
69 lines (61 loc) · 2.07 KB
/
Common.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//
// Basic.swift
// Nightlight
//
// Created by David Wu on 7/30/18.
// Copyright © 2018 Gofake1. All rights reserved.
//
import struct CoreLocation.CLLocationCoordinate2D
import Foundation
let _defaults: [String: Any] = [
AppDefaultKind.autoOnMode.rawValue: AutoOnMode.manual.rawValue,
AppDefaultKind.autoOnFromTime.rawValue: 72000,
AppDefaultKind.autoOnToTime.rawValue: 28800
]
let _notificationEmptyCache = NSNotification.Name("emptyCache")
let _nightlight = "net.gofake1.Nightlight"
enum AutoOnMode: String {
case manual
case custom
case sunset
case system
}
enum AppDefaultKind: String {
case autoOnMode
case autoOnFromTime
case autoOnToTime
case autoOnLatitude
case autoOnLongitude
case isOn
}
extension CLLocationCoordinate2D {
func makeDatesForLabel() -> (from: Date, to: Date)? {
let now = Date()
guard let sol = Solar(for: now, coordinate: self), let sunrise = sol.sunrise, let sunset = sol.sunset
else { return nil }
let yesterday = Calendar.autoupdatingCurrent.date(byAdding: .day, value: -1, to: now)!
let tomorrow = Calendar.autoupdatingCurrent.date(byAdding: .day, value: 1, to: now)!
if now < sunrise {
guard let sol = Solar(for: yesterday, coordinate: self), let prevSunset = sol.sunset else { return nil }
return (prevSunset, sunrise)
} else {
guard let sol = Solar(for: tomorrow, coordinate: self), let nextSunrise = sol.sunrise else { return nil }
return (sunset, nextSunrise)
}
}
}
extension UserDefaults {
func doubleIfExists(forDefault default: AppDefaultKind) -> Double? {
if let _ = object(forKey: `default`.rawValue) {
return double(forKey: `default`.rawValue)
} else {
return nil
}
}
func integer(forDefault default: AppDefaultKind) -> Int {
return integer(forKey: `default`.rawValue)
}
func string(forDefault default: AppDefaultKind) -> String? {
return string(forKey: `default`.rawValue)
}
}