Skip to content
This repository was archived by the owner on Sep 20, 2023. It is now read-only.

Commit 2e1b55e

Browse files
viktoraslBasThomas
authored andcommitted
Support inbox zero fixed holidays (#2629)
1 parent 1e7d5d9 commit 2e1b55e

File tree

3 files changed

+98
-7
lines changed

3 files changed

+98
-7
lines changed

Classes/Systems/InboxZeroLoader.swift

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import Foundation
1111
final class InboxZeroLoader {
1212

1313
// [year/"fixed": [month: [day: [key:value]]]
14-
private typealias SerializedType = [String: [String: [String: [String: String]]]]
14+
typealias SerializedType = [String: [String: [String: [String: String]]]]
1515
private let path: String = {
1616
let path = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true)[0]
1717
return "\(path)/holidays.json"
@@ -34,6 +34,10 @@ final class InboxZeroLoader {
3434
]).url
3535
}
3636

37+
init(json: SerializedType? = nil) {
38+
json.flatMap { self.json = $0 }
39+
}
40+
3741
func load(completion: @escaping (Bool) -> Void) {
3842
guard let url = self.url else {
3943
completion(false)
@@ -62,6 +66,14 @@ final class InboxZeroLoader {
6266
return ("🎉", NSLocalizedString("Inbox zero!", comment: ""))
6367
}
6468

69+
// either key on the current year or "fixed" (for permanent dates)
70+
private func holiday(year: Int, month: Int, day: Int) -> [String: String]? {
71+
let holidays: (String) -> [String: String]? = {
72+
return self.json[$0]?["\(month)"]?["\(day)"]
73+
}
74+
return holidays("\(year)") ?? holidays("fixed")
75+
}
76+
6577
func message(date: Date = Date()) -> (emoji: String, message: String) {
6678
let components = Calendar.current.dateComponents([.day, .month, .year], from: date)
6779
guard let day = components.day,
@@ -70,12 +82,9 @@ final class InboxZeroLoader {
7082
return fallback
7183
}
7284

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)"],
76-
let data = monthData["\(day)"],
77-
let emoji = data["emoji"],
78-
let message = data["message"] {
85+
if let holiday = holiday(year: year, month: month, day: day),
86+
let emoji = holiday["emoji"],
87+
let message = holiday["message"] {
7988
return (emoji, message)
8089
}
8190
return fallback

Freetime.xcodeproj/project.pbxproj

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
objects = {
88

99
/* Begin PBXBuildFile section */
10+
03C127B8220993300062F7C9 /* InboxZeroLoaderTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 03C127B7220993300062F7C9 /* InboxZeroLoaderTests.swift */; };
1011
0F9440FD32236514CD7215E9 /* Pods_Freetime.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = ECF622FFD773FDA73297C0D0 /* Pods_Freetime.framework */; };
1112
15F28F992108DBA6006421B6 /* SplashView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15F28F982108DBA6006421B6 /* SplashView.swift */; };
1213
290056F3210028B20046EAE5 /* UIViewController+MenuDone.swift in Sources */ = {isa = PBXBuildFile; fileRef = 290056F2210028B20046EAE5 /* UIViewController+MenuDone.swift */; };
@@ -593,6 +594,8 @@
593594
/* End PBXCopyFilesBuildPhase section */
594595

595596
/* Begin PBXFileReference section */
597+
03A0EC6221F8F60B008C9CE2 /* RepositoryOverviewHeaderViewModels.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RepositoryOverviewHeaderViewModels.swift; sourceTree = "<group>"; };
598+
03C127B7220993300062F7C9 /* InboxZeroLoaderTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = InboxZeroLoaderTests.swift; sourceTree = "<group>"; };
596599
07C7DC5A7A907BE73BCA95AC /* Pods-FreetimeTests.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FreetimeTests.debug.xcconfig"; path = "Pods/Target Support Files/Pods-FreetimeTests/Pods-FreetimeTests.debug.xcconfig"; sourceTree = "<group>"; };
597600
0F26A46A43D11F8D04E17D4A /* Pods-FreetimeWatch Extension.testflight.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FreetimeWatch Extension.testflight.xcconfig"; path = "Pods/Target Support Files/Pods-FreetimeWatch Extension/Pods-FreetimeWatch Extension.testflight.xcconfig"; sourceTree = "<group>"; };
598601
15F28F982108DBA6006421B6 /* SplashView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SplashView.swift; sourceTree = "<group>"; };
@@ -1744,6 +1747,7 @@
17441747
295F52A61EF1B9D2000B53CF /* Test.md */,
17451748
291E987C21973FA700E5EED9 /* URLBuilderTests.swift */,
17461749
29827D7321AA5DA300A1B293 /* ViewControllerTestUtil.swift */,
1750+
03C127B7220993300062F7C9 /* InboxZeroLoaderTests.swift */,
17471751
);
17481752
path = FreetimeTests;
17491753
sourceTree = "<group>";
@@ -3425,6 +3429,7 @@
34253429
DC60C6D51F983DF800241271 /* IssueLabelCellTests.swift in Sources */,
34263430
49FE18FF204B6508001681E8 /* SequenceTests.swift in Sources */,
34273431
DC5C02C31F9C6D0B00E80B9F /* SearchRecentStoreTests.swift in Sources */,
3432+
03C127B8220993300062F7C9 /* InboxZeroLoaderTests.swift in Sources */,
34283433
9870B9081FC74E300009719C /* SecretsTests.swift in Sources */,
34293434
29EDFE7C1F65C580005BCCEB /* SplitViewTests.swift in Sources */,
34303435
2986B35E1FD462AA00E3CFC6 /* FilePathTests.swift in Sources */,
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
//
2+
// InboxZeroLoaderTests.swift
3+
// FreetimeTests
4+
//
5+
// Created by Viktoras Laukevicius on 05/02/2019.
6+
// Copyright © 2019 Ryan Nystrom. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import Freetime
11+
12+
extension Date {
13+
static func date(from: [Calendar.Component : Int]) -> Date {
14+
var dc = DateComponents()
15+
dc.calendar = Calendar.current
16+
for (comp, val) in from {
17+
switch comp {
18+
case .year: dc.year = val
19+
case .month: dc.month = val
20+
case .day: dc.day = val
21+
case .hour: dc.hour = val
22+
case .minute: dc.minute = val
23+
case .second: dc.second = val
24+
default: break
25+
}
26+
}
27+
return dc.date!
28+
}
29+
}
30+
31+
class InboxZeroLoaderTests: XCTestCase {
32+
33+
private var loader: InboxZeroLoader!
34+
35+
override func setUp() {
36+
loader = InboxZeroLoader(json: [
37+
"2019": [
38+
"2": [
39+
"5": [
40+
"emoji": "🐷",
41+
"message": "新年好 / 新年好"
42+
]
43+
]
44+
],
45+
"fixed": [
46+
"4": [
47+
"1": [
48+
"emoji": "🤡",
49+
"message": "Inbox ∞"
50+
]
51+
]
52+
]
53+
])
54+
}
55+
56+
override func tearDown() {
57+
loader = nil
58+
}
59+
60+
func test_particularDate() {
61+
let date = Date.date(from: [.year: 2019, .month: 4, .day: 1, .hour: 13])
62+
let message = loader.message(date: date)
63+
XCTAssertEqual(message.emoji, "🤡")
64+
}
65+
66+
func test_fixedDate() {
67+
let date = Date.date(from: [.year: 2019, .month: 2, .day: 5, .hour: 13])
68+
let message = loader.message(date: date)
69+
XCTAssertEqual(message.emoji, "🐷")
70+
}
71+
72+
func test_defaultMessage() {
73+
let date = Date.date(from: [.year: 2019, .month: 4, .day: 2, .hour: 13])
74+
let message = loader.message(date: date)
75+
XCTAssertEqual(message.emoji, "🎉")
76+
}
77+
}

0 commit comments

Comments
 (0)