@@ -10,23 +10,36 @@ import Foundation
1010
1111final class InboxZeroLoader {
1212
13- private let url = URL ( string: " http://githawk.com/holidays.json " ) !
13+ // [year/"fixed": [month: [day: [key:value]]]
14+ private typealias SerializedType = [ String : [ String : [ String : [ String : String ] ] ] ]
1415 private let path : String = {
1516 let path = NSSearchPathForDirectoriesInDomains ( . documentDirectory, . userDomainMask, true ) [ 0 ]
1617 return " \( path) /holidays.json "
1718 } ( )
18- private typealias SerializedType = [ String : [ String : [ String : String ] ] ]
19+
1920 private lazy var json : SerializedType = {
2021 return NSKeyedUnarchiver . unarchiveObject ( withFile: path) as? SerializedType ?? [ : ]
2122 } ( )
23+
2224 private let session : URLSession = {
2325 let config = URLSessionConfiguration . default
2426 config. requestCachePolicy = . reloadIgnoringLocalCacheData
2527 config. urlCache = nil
2628 return URLSession . init ( configuration: config)
2729 } ( )
2830
31+ private var url : URL ? {
32+ return URL (
33+ string: " https://raw.githubusercontent.com/GitHawkApp/Holidays/master/locales/holidays- \( Locale . current. identifier) .json "
34+ )
35+ }
36+
2937 func load( completion: @escaping ( Bool ) -> Void ) {
38+ guard let url = self . url else {
39+ completion ( false )
40+ return
41+ }
42+
3043 let path = self . path
3144 session. dataTask ( with: url) { [ weak self] ( data, response, error) in
3245 if let data = data,
@@ -50,11 +63,16 @@ final class InboxZeroLoader {
5063 }
5164
5265 func message( date: Date = Date ( ) ) -> ( emoji: String , message: String ) {
53- let components = Calendar . current. dateComponents ( [ . day, . month] , from: date)
54- guard let day = components. day, let month = components. month else {
55- return fallback
66+ let components = Calendar . current. dateComponents ( [ . day, . month, . year] , from: date)
67+ guard let day = components. day,
68+ let month = components. month,
69+ let year = components. year else {
70+ return fallback
5671 }
57- if let monthData = json [ " \( month) " ] ,
72+
73+ // either key on the current year or "fixed" (for permanent dates)
74+ if let yearData = json [ " \( year) " ] ?? json [ " fixed " ] ,
75+ let monthData = yearData [ " \( month) " ] ,
5876 let data = monthData [ " \( day) " ] ,
5977 let emoji = data [ " emoji " ] ,
6078 let message = data [ " message " ] {
0 commit comments