Commit 04f0f45b by Demid Merzlyakov

The NavigationBar time is now the current time in the specified location.

parent 84a11e67
......@@ -94,6 +94,9 @@
"tabBar.radar" = "radar";
"tabBar.menu" = "menu";
//Navigation bar
"navigationBar.noCityAvailable" = "N/A";
// Search
"search.add" = "add";
"search.popularCities" = "Popular cities";
......
......@@ -15,10 +15,22 @@ class NavigationCityButton: UIControl {
private let downArrowImageView = UIImageView()
private let timeLabel = UILabel()
private let kSpaceForDownArrow:CGFloat = 10
private static let timeFormatter:DateFormatter = {
private var timer: Timer?
private var timeZone: TimeZone? {
didSet {
if oldValue != timeZone {
if let timeZone = timeZone {
timeFormatter.timeZone = timeZone
}
updateTime()
}
}
}
private let timeFormatter:DateFormatter = {
let fmt = DateFormatter()
fmt.dateFormat = "h:mm a"
fmt.timeStyle = .short
fmt.dateFormat = .none
return fmt
}()
......@@ -57,19 +69,42 @@ class NavigationCityButton: UIControl {
}
public func configure(with location:Location?) {
guard let loc = location else {
self.cityLabel.text = "N/A"
self.cityLabel.text = location?.cityName ?? "navigationBar.noCityAvailable".localized()
self.timeZone = location?.timeZone
configureTimer()
}
private func updateTime() {
if timeZone != nil {
self.timeLabel.text = timeFormatter.string(from: Date())
}
else {
self.timeLabel.text = nil
return
}
self.cityLabel.text = loc.cityName
if let today = loc.today?.date {
NavigationCityButton.timeFormatter.timeZone = loc.timeZone
self.timeLabel.text = NavigationCityButton.timeFormatter.string(from: today)
}
private func configureTimer() {
if let timeZone = timeZone {
if timer == nil {
var calendar = Calendar(identifier: .gregorian)
calendar.timeZone = timeZone
var dateComponents = calendar.dateComponents([.year, .month, .day, .hour, .minute, .second], from: Date())
if let oldMinutes = dateComponents.minute {
dateComponents.minute = oldMinutes + 1 // dateComponents handle rolling over 60.
}
dateComponents.second = 0
let firstFireDate = calendar.date(from: dateComponents) ?? Date().addingTimeInterval(5)
let newTimer = Timer(fire: firstFireDate, interval: 30.0, repeats: true, block: { [weak self] _ in
self?.updateTime()
})
RunLoop.main.add(newTimer, forMode: .common)
self.timer = newTimer
}
}
else {
self.timeLabel.text = nil
timer?.invalidate()
timer = nil
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment