This repository was archived by the owner on Sep 20, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 387
/
Copy pathRepoInboxRowController.swift
75 lines (64 loc) · 2.67 KB
/
RepoInboxRowController.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
70
71
72
73
74
75
//
// RepoInboxRowController.swift
// FreetimeWatch Extension
//
// Created by Ryan Nystrom on 4/27/18.
// Copyright © 2018 Ryan Nystrom. All rights reserved.
//
import WatchKit
import Foundation
import GitHubAPI
import DateAgo
final class RepoInboxRowController: NSObject {
@IBOutlet var enclosingGroup: WKInterfaceGroup!
@IBOutlet var typeImage: WKInterfaceImage!
@IBOutlet var numberLabel: WKInterfaceLabel!
@IBOutlet var dateLabel: WKInterfaceLabel!
@IBOutlet var titleLabel: WKInterfaceLabel!
func setup(with notification: V3Notification) {
let title = notification.subject.title
titleLabel.setText(title)
dateLabel.setText(notification.updatedAt.agoString(.short))
let imageName: String
switch notification.subject.type {
case .commit: imageName = "git-commit"
case .invitation: imageName = "mail"
case .issue: imageName = "issue-opened"
case .pullRequest: imageName = "git-pull-request"
case .release: imageName = "tag"
case .repo: imageName = "repo"
case .vulnerabilityAlert: imageName = "alert"
case .ci_activity: imageName = "alert"
}
typeImage.setImage(UIImage(named: imageName)?.withRenderingMode(.alwaysTemplate))
let number: String?
if let identifier = notification.subject.identifier {
switch identifier {
case .hash(let h):
number = h.hashDisplay
case .number(let num):
number = "#\(num)"
case .release(let r):
number = r
}
} else {
number = nil
}
if let number = number {
numberLabel.setText(number)
}
let agoString = notification.updatedAt.agoString(.long)
let elements = [number, agoString, title].compactMap { $0 }
let accessibilityLabel: String
// FIXME: Copied from Accessibility.swift; we should put this in a small library probably.
if elements.count == 1, let elements = elements.first {
accessibilityLabel = elements
} else {
accessibilityLabel = elements.reduce("") { "\($0).\n\($1)" }
}
[typeImage, titleLabel, dateLabel, numberLabel].forEach { $0.setIsAccessibilityElement(false) }
enclosingGroup.setAccessibilityTraits(UIAccessibilityTraitStaticText)
enclosingGroup.setIsAccessibilityElement(true)
enclosingGroup.setAccessibilityLabel(accessibilityLabel) // FIXME: We should add the notification type here, probably by using "NotificationType" (only available in GitHawk) or remove that completely and add the localization to V3NotificationType
}
}