Skip to content

Commit 3a04fd5

Browse files
sachin004ncalexan
authored andcommitted
Bug 1154088 - Show remote client last synced time with relative time string.
1 parent d30dc36 commit 3a04fd5

File tree

4 files changed

+141
-2
lines changed

4 files changed

+141
-2
lines changed

Client.xcodeproj/project.pbxproj

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

99
/* Begin PBXBuildFile section */
10+
034C81621AE6032D0050EDA1 /* RelativeDatesTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 034C81611AE6032D0050EDA1 /* RelativeDatesTests.swift */; };
1011
0AE491A61A41C88C0046C724 /* BackForwardListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0AE491A51A41C88C0046C724 /* BackForwardListViewController.swift */; };
1112
0B1C05D71A798B1F004C78B0 /* UIImageViewAligned.m in Sources */ = {isa = PBXBuildFile; fileRef = 0B1C05D61A798B1F004C78B0 /* UIImageViewAligned.m */; };
1213
0B5142CC1AE1BAF50014D0B3 /* UIViewExtensions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 0B5142CB1AE1BAF50014D0B3 /* UIViewExtensions.swift */; };
@@ -1030,6 +1031,7 @@
10301031
/* End PBXCopyFilesBuildPhase section */
10311032

10321033
/* Begin PBXFileReference section */
1034+
034C81611AE6032D0050EDA1 /* RelativeDatesTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = RelativeDatesTests.swift; sourceTree = "<group>"; };
10331035
0AE491A51A41C88C0046C724 /* BackForwardListViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = BackForwardListViewController.swift; sourceTree = "<group>"; };
10341036
0B1C05D51A798B1F004C78B0 /* UIImageViewAligned.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = UIImageViewAligned.h; sourceTree = "<group>"; };
10351037
0B1C05D61A798B1F004C78B0 /* UIImageViewAligned.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = UIImageViewAligned.m; sourceTree = "<group>"; };
@@ -2269,6 +2271,7 @@
22692271
F84B21D61A090F8100AAB793 /* ClientTests */ = {
22702272
isa = PBXGroup;
22712273
children = (
2274+
034C81611AE6032D0050EDA1 /* RelativeDatesTests.swift */,
22722275
F84B21D91A090F8100AAB793 /* ClientTests.swift */,
22732276
281B2BE91ADF4D90002917DC /* MockProfile.swift */,
22742277
D3D488581ABB54CD00A93597 /* FileAccessorTests.swift */,
@@ -3689,6 +3692,7 @@
36893692
D3D488591ABB54CD00A93597 /* FileAccessorTests.swift in Sources */,
36903693
0BA8964B1A250E6500C1010C /* ProfileTest.swift in Sources */,
36913694
0BB5B28D1AC0A6130052877D /* Toolbar.swift in Sources */,
3695+
034C81621AE6032D0050EDA1 /* RelativeDatesTests.swift in Sources */,
36923696
F84B21DA1A090F8100AAB793 /* ClientTests.swift in Sources */,
36933697
281B2BEA1ADF4D90002917DC /* MockProfile.swift in Sources */,
36943698
2F697F7E1A9FD22D009E03AE /* SearchEnginesTests.swift in Sources */,

Client/Frontend/Home/RemoteTabsPanel.swift

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ private let RemoteTabIdentifier = "RemoteTab"
3737
class RemoteTabsPanel: UITableViewController, HomePanel {
3838
weak var homePanelDelegate: HomePanelDelegate? = nil
3939
var profile: Profile!
40+
var timer: NSTimer!
4041

4142
private var clientAndTabs: [ClientAndTabs]?
4243

@@ -60,6 +61,14 @@ class RemoteTabsPanel: UITableViewController, HomePanel {
6061
self.SELrefresh()
6162
}
6263

64+
override func viewDidAppear(animated: Bool) {
65+
timer = NSTimer.scheduledTimerWithTimeInterval(60.0, target: self.tableView, selector: Selector("reloadData"), userInfo: nil, repeats: true)
66+
}
67+
68+
override func viewWillDisappear(animated: Bool) {
69+
timer.invalidate()
70+
}
71+
6372
@objc private func SELrefresh() {
6473
self.refreshControl?.beginRefreshing()
6574

@@ -130,7 +139,7 @@ class RemoteTabsPanel: UITableViewController, HomePanel {
130139
*/
131140
let timestamp = clientTabs.approximateLastSyncTime()
132141
let label = NSLocalizedString("Last synced: %@", comment: "Remote tabs last synced time")
133-
view.detailTextLabel.text = String(format: label, String(timestamp))
142+
view.detailTextLabel.text = String(format: label, timestamp.toNSDate().toRelativeTimeString())
134143
if client.type == "desktop" {
135144
view.imageView.image = UIImage(named: "deviceTypeDesktop")
136145
} else {
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import Foundation
6+
import XCTest
7+
8+
class RelativeDatesTests: XCTestCase {
9+
func testRelativeDates() {
10+
let dateOrig = NSDate()
11+
var date = NSDate(timeInterval: 0, sinceDate: dateOrig)
12+
13+
XCTAssertTrue(date.toRelativeTimeString() == "just now")
14+
15+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
16+
date = date.dateByAddingTimeInterval(-10)
17+
XCTAssertTrue(date.toRelativeTimeString() == "10 seconds ago")
18+
19+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
20+
date = date.dateByAddingTimeInterval(-60)
21+
XCTAssertTrue(date.toRelativeTimeString() == "a minute ago")
22+
23+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
24+
date = date.dateByAddingTimeInterval(-60 * 2)
25+
XCTAssertTrue(date.toRelativeTimeString() == "2 minutes ago")
26+
27+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
28+
date = date.dateByAddingTimeInterval(-60 * 60)
29+
XCTAssertTrue(date.toRelativeTimeString() == "an hour ago")
30+
31+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
32+
date = date.dateByAddingTimeInterval(-60 * 60 * 2)
33+
XCTAssertTrue(date.toRelativeTimeString() == "2 hours ago")
34+
35+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
36+
date = date.dateByAddingTimeInterval(-60 * 60 * 24)
37+
XCTAssertTrue(date.toRelativeTimeString() == "a day ago")
38+
39+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
40+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 2)
41+
XCTAssertTrue(date.toRelativeTimeString() == "2 days ago")
42+
43+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
44+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7)
45+
XCTAssertTrue(date.toRelativeTimeString() == "a week ago")
46+
47+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
48+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7 * 2)
49+
XCTAssertTrue(date.toRelativeTimeString() == "2 weeks ago")
50+
51+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
52+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7 * 5)
53+
XCTAssertTrue(date.toRelativeTimeString() == "a month ago")
54+
55+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
56+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7 * 5 * 2)
57+
XCTAssertTrue(date.toRelativeTimeString() == "2 months ago")
58+
59+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
60+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7 * 5 * 12)
61+
XCTAssertTrue(date.toRelativeTimeString() == "a year ago")
62+
63+
date = NSDate(timeInterval: 0, sinceDate: dateOrig)
64+
date = date.dateByAddingTimeInterval(-60 * 60 * 24 * 7 * 5 * 12 * 2)
65+
XCTAssertTrue(date.toRelativeTimeString() == "2 years ago")
66+
}
67+
}

Utils/TimeConstants.swift

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,59 @@ extension NSDate {
1818
public class func now() -> Timestamp {
1919
return UInt64(1000 * NSDate().timeIntervalSince1970)
2020
}
21+
22+
public func toRelativeTimeString() -> String {
23+
24+
let now = NSDate()
25+
26+
let units = NSCalendarUnit.CalendarUnitSecond | NSCalendarUnit.CalendarUnitMinute | NSCalendarUnit.CalendarUnitDay | NSCalendarUnit.CalendarUnitWeekOfYear | NSCalendarUnit.CalendarUnitMonth | NSCalendarUnit.CalendarUnitYear | NSCalendarUnit.CalendarUnitHour
27+
28+
let components = NSCalendar.currentCalendar().components(units, fromDate: self, toDate: now, options: NSCalendarOptions.allZeros)
29+
30+
if components.year > 0 {
31+
if components.year > 1 {
32+
return String(format: NSLocalizedString("%@ years ago", comment: "relative time"), String(components.year))
33+
} else if components.year == 1 {
34+
return String(format: NSLocalizedString("a year ago", comment: "relative time"))
35+
}
36+
} else if components.month > 0 {
37+
if components.month > 1 {
38+
return String(format: NSLocalizedString("%@ months ago", comment: "relative time"), String(components.month))
39+
} else if components.month == 1 {
40+
return String(format: NSLocalizedString("a month ago", comment: "relative time"))
41+
}
42+
} else if components.weekOfYear > 0 {
43+
if components.weekOfYear > 1 {
44+
return String(format: NSLocalizedString("%@ weeks ago", comment: "relative time"), String(components.weekOfYear))
45+
} else if components.weekOfYear == 1 {
46+
return String(format: NSLocalizedString("a week ago", comment: "relative time"))
47+
}
48+
} else if components.day > 0 {
49+
if components.day > 1 {
50+
return String(format: NSLocalizedString("%@ days ago", comment: "relative time"), String(components.day))
51+
} else if components.day == 1 {
52+
return String(format: NSLocalizedString("a day ago", comment: "relative time"))
53+
}
54+
} else if components.hour > 0 {
55+
if components.hour > 1 {
56+
println(components.hour)
57+
return String(format: NSLocalizedString("%@ hours ago", comment: "relative time"), String(components.hour))
58+
} else if components.hour == 1 {
59+
return String(format: NSLocalizedString("an hour ago", comment: "relative time"))
60+
}
61+
} else if components.minute > 0 {
62+
if components.minute > 1 {
63+
return String(format: NSLocalizedString("%@ minutes ago", comment: "relative time"), String(components.minute))
64+
} else if components.minute == 1 {
65+
return String(format: NSLocalizedString("a minute ago", comment: "relative time"))
66+
}
67+
} else {
68+
if components.second >= 10 {
69+
return String(format: NSLocalizedString("%@ seconds ago", comment: "relative time"), String(components.second))
70+
}
71+
}
72+
return String(format: NSLocalizedString("just now", comment: "relative time"))
73+
}
2174
}
2275

2376
public func decimalSecondsStringToTimestamp(input: String) -> Timestamp? {
@@ -30,4 +83,10 @@ public func decimalSecondsStringToTimestamp(input: String) -> Timestamp? {
3083
public func millisecondsToDecimalSeconds(input: Timestamp) -> String {
3184
let val: Double = Double(input) / 1000
3285
return String(format: "%.2F", val)
33-
}
86+
}
87+
88+
extension Timestamp {
89+
public func toNSDate() -> NSDate {
90+
return NSDate(timeIntervalSince1970: NSTimeInterval(self / 1000))
91+
}
92+
}

0 commit comments

Comments
 (0)