-
Notifications
You must be signed in to change notification settings - Fork 13
/
KDEDateLabel.swift
174 lines (133 loc) · 3.78 KB
/
KDEDateLabel.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
//
// KDEDateLabel.swift
// KDEDateLabelExample
//
// Created by Kevin DELANNOY on 23/12/14.
// Copyright (c) 2014 Kevin Delannoy. All rights reserved.
//
import UIKit
// MARK: - WeakReferencer
private class WeakReferencer<T: NSObject>: NSObject {
// MARK: Properties
private(set) weak var value: T?
private let _hash: Int
private let _hashValue: Int
// MARK: Initialization
init(value: T) {
self.value = value
self._hash = value.hash
self._hashValue = value.hashValue
super.init()
}
// MARK: NSObject override
override func isEqual(_ object: Any?) -> Bool {
return value?.isEqual(object) ?? false
}
override var hash: Int {
return _hash
}
override var hashValue: Int {
return _hashValue
}
}
// MARK: - DateLabelsHolder
private class DateLabelsHolder: NSObject {
// MARK: Singleton
static var instance = DateLabelsHolder()
// MARK: Properties
private var dateLabels = [WeakReferencer<DateLabel>]()
private var timer: Timer?
// MARK: Initialization
override init() {
super.init()
createNewTimer()
}
// MARK: Label registration
func addReferencer(_ referencer: WeakReferencer<DateLabel>) {
dateLabels.append(referencer)
}
func removeReferencer(_ referencer: WeakReferencer<DateLabel>) {
if let index = dateLabels.index(of: referencer) {
dateLabels.remove(at: index)
}
dateLabels = dateLabels.filter { $0.value != nil }
}
// MARK: Timer
func createNewTimer() {
timer?.invalidate()
let newTimer = Timer(timeInterval: DateLabel.refreshFrequency,
target: self,
selector: #selector(refreshLabels(_:)),
userInfo: nil,
repeats: true)
timer = newTimer
RunLoop.main.add(newTimer, forMode: .commonModes)
}
@objc private func refreshLabels(_: Any) {
dateLabels.forEach { $0.value?.updateText() }
}
}
// MARK: - DateLabel
open class DateLabel: UILabel {
// MARK: Properties
private lazy var holder: WeakReferencer<DateLabel> = {
return WeakReferencer<DateLabel>(value: self)
}()
// MARK: Initialization
public convenience init() {
self.init(frame: .zero)
}
public required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
commonInit()
}
public override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
private func commonInit() {
DateLabelsHolder.instance.addReferencer(holder)
}
// MARK: Deinit
deinit {
DateLabelsHolder.instance.removeReferencer(holder)
}
// MARK: Configuration
open static var refreshFrequency: TimeInterval = 0.2 {
didSet {
DateLabelsHolder.instance.createNewTimer()
}
}
// MARK: Date & Text updating
open var date: Date? = nil {
didSet {
updateText()
}
}
open var dateFormatTextBlock: ((_ date: Date) -> String)? {
didSet {
updateText()
}
}
open var dateFormatAttributedTextBlock: ((_ date: Date) -> NSAttributedString)? {
didSet {
updateText()
}
}
fileprivate func updateText() {
if let date = date {
if let dateFormatAttributedTextBlock = dateFormatAttributedTextBlock {
attributedText = dateFormatAttributedTextBlock(date)
}
else if let dateFormatTextBlock = dateFormatTextBlock {
text = dateFormatTextBlock(date)
}
else {
text = "\(Int(fabs(date.timeIntervalSinceNow)))s ago"
}
}
else {
text = nil
}
}
}