Skip to content

Commit 32b59c2

Browse files
Replaced DateComponentsFormatter with dummy formatter for Linux Compatibility
Removed (and invalidated) telegram bot token from scheme
1 parent ebed156 commit 32b59c2

File tree

6 files changed

+83
-17
lines changed

6 files changed

+83
-17
lines changed

NotifierBot/Sources/Notifier/Commands/InfoCommand.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ struct InfoCommand: Command {
5858
}
5959
var durationString = ""
6060
if let restDuration = e.unmuteDate?.timeIntervalSince(Date()) {
61-
durationString = JFUtils.muteDurationFormatter.string(from: restDuration) ?? ""
61+
durationString = SharedUtils.muteDurationFormatter.string(from: restDuration) ?? ""
6262
}
6363
lines.append("- Muted: \(e.isMuted ? "Yes (\(durationString) hours remaining)" : "No")".escaped())
6464
return lines.joined(separator: "\n")

NotifierBot/Sources/Notifier/JFUtils.swift

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -74,13 +74,6 @@ struct JFUtils {
7474
return list.joined(separator: "\n")
7575
}
7676

77-
static var muteDurationFormatter: DateComponentsFormatter {
78-
let f = DateComponentsFormatter()
79-
f.unitsStyle = .abbreviated
80-
f.allowedUnits = [.year, .day, .hour, .minute]
81-
return f
82-
}
83-
8477
}
8578

8679
extension Dispatcher {

Shared/Sources/Shared/PermissionHandler.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//
2-
// File 2.swift
2+
// PermissionHandler.swift
33
//
44
//
55
// Created by Jonas Frey on 01.05.21.
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
//
2+
// SharedUtils.swift
3+
//
4+
//
5+
// Created by Jonas Frey on 01.04.22.
6+
//
7+
8+
import Foundation
9+
10+
public struct SharedUtils {
11+
12+
/// Dummy formatter that replaces the DateComponentsFormatter with a fixed configuration
13+
/// DateComponentsFormatter is not yet available on Linux, so we have to use this one instead
14+
public struct DummyFormatter {
15+
16+
let fullUnits: Bool
17+
18+
public init(fullUnits: Bool = false) {
19+
self.fullUnits = fullUnits
20+
}
21+
22+
public func string(from ti: TimeInterval) -> String? {
23+
var components: [String] = []
24+
var rest = ti
25+
let years = rest / (365 * 24 * 60 * 60)
26+
rest = rest.truncatingRemainder(dividingBy: (365 * 24 * 60 * 60))
27+
if years > 0 {
28+
components.append(durationString(Int(years), .year))
29+
}
30+
31+
let days = rest / (24 * 60 * 60)
32+
rest = rest.truncatingRemainder(dividingBy: (24 * 60 * 60))
33+
if days > 0 {
34+
components.append(durationString(Int(days), .day))
35+
}
36+
37+
let hours = rest / (60 * 60)
38+
rest = rest.truncatingRemainder(dividingBy: (60 * 60))
39+
if hours > 0 {
40+
components.append(durationString(Int(hours), .hour))
41+
}
42+
43+
let minutes = rest / 60
44+
rest = rest.truncatingRemainder(dividingBy: 60)
45+
if minutes > 0 {
46+
components.append(durationString(Int(minutes), .minute))
47+
}
48+
49+
return components.joined(separator: " ")
50+
}
51+
52+
private enum Unit: String {
53+
case year
54+
case day
55+
case hour
56+
case minute
57+
}
58+
59+
private func durationString(_ amount: Int, _ unit: Unit) -> String {
60+
return "\(amount) \(unitString(amount, unit))"
61+
}
62+
63+
private func unitString(_ amount: Int, _ unit: Unit) -> String {
64+
if !fullUnits {
65+
return .init(unit.rawValue.first!)
66+
}
67+
return unit.rawValue.appending(amount == 1 ? "" : "s")
68+
}
69+
}
70+
71+
public static var muteDurationFormatter: DummyFormatter {
72+
// let f = DateComponentsFormatter()
73+
// f.unitsStyle = .abbreviated
74+
// f.allowedUnits = [.year, .day, .hour, .minute]
75+
// return f
76+
return DummyFormatter(fullUnits: false)
77+
}
78+
79+
}

urlwatcher/.swiftpm/xcode/xcshareddata/xcschemes/urlwatcher.xcscheme

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@
5757
value = "true"
5858
isEnabled = "YES">
5959
</EnvironmentVariable>
60-
<EnvironmentVariable
61-
key = "TELEGRAM_BOT_TOKEN"
62-
value = "5221316043:AAF-_gfAxbHVrW2HDfplfSYz3psPtseLVKM"
63-
isEnabled = "YES">
64-
</EnvironmentVariable>
6560
</EnvironmentVariables>
6661
</LaunchAction>
6762
<ProfileAction

urlwatcher/Sources/urlwatcher/Functions.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,8 @@ func notifyError(entry: URLEntry) throws {
176176
return
177177
}
178178
print("Error taking screenshot. Notifying user...")
179-
let f = DateComponentsFormatter()
180-
f.allowedUnits = [.year, .day, .hour, .minute, .second]
181-
f.unitsStyle = .full
179+
// TODO: Replace with DateComponentsFormatter when available on Linux
180+
let f = SharedUtils.DummyFormatter(fullUnits: true)
182181
let durationString = f.string(from: kErrorReportDuration) ?? "some time"
183182
try sendTelegramMessage("The entry '\(entry.name)' failed to capture a screenshot for at least \(durationString).",
184183
to: Int(entry.chatID))

0 commit comments

Comments
 (0)