Skip to content

Commit 5079de6

Browse files
authored
Merge pull request #7720 from woocommerce/issue/7710-last-refreshed
Widget: Last refreshed label
2 parents 75754e2 + 0d113e2 commit 5079de6

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

WooCommerce/StoreWidgets/StoreInfoProvider.swift

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ struct StoreInfoData {
4444
/// Conversion at the range (eg: today)
4545
///
4646
var conversion: String
47+
48+
/// Time when the widget was last refreshed (eg: 10.24PM)
49+
///
50+
var updatedTime: String
4751
}
4852

4953
/// Type that provides data entries to the widget system.
@@ -67,7 +71,8 @@ final class StoreInfoProvider: TimelineProvider {
6771
revenue: Self.formattedAmountString(for: 132.234, with: dependencies?.storeCurrencySettings),
6872
visitors: "67",
6973
orders: "23",
70-
conversion: Self.formattedConversionString(for: 23/67)))
74+
conversion: Self.formattedConversionString(for: 23/67),
75+
updatedTime: Self.currentFormattedTime()))
7176
}
7277

7378
/// Quick Snapshot. Required when previewing the widget.
@@ -94,7 +99,8 @@ final class StoreInfoProvider: TimelineProvider {
9499
revenue: Self.formattedAmountString(for: todayStats.revenue, with: dependencies.storeCurrencySettings),
95100
visitors: "\(todayStats.totalVisitors)",
96101
orders: "\(todayStats.totalOrders)",
97-
conversion: Self.formattedConversionString(for: todayStats.conversion)))
102+
conversion: Self.formattedConversionString(for: todayStats.conversion),
103+
updatedTime: Self.currentFormattedTime()))
98104

99105
let reloadDate = Date(timeIntervalSinceNow: reloadInterval)
100106
let timeline = Timeline<StoreInfoEntry>(entries: [entry], policy: .after(reloadDate))
@@ -160,6 +166,15 @@ private extension StoreInfoProvider {
160166
return numberFormatter.string(from: conversionRate as NSNumber) ?? Constants.valuePlaceholderText
161167
}
162168

169+
/// Returns the current time formatted as `10:24 PM` or `22:24` depending on the phone settings.
170+
///
171+
static func currentFormattedTime() -> String {
172+
let timeFormatter = DateFormatter()
173+
timeFormatter.timeStyle = .short
174+
timeFormatter.dateStyle = .none
175+
return timeFormatter.string(from: Date())
176+
}
177+
163178
enum Constants {
164179
static let valuePlaceholderText = "-"
165180
}

WooCommerce/StoreWidgets/StoreInfoWidget.swift

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,10 @@ private struct StoreInfoView: View {
9494
}
9595
.frame(maxWidth: .infinity, alignment: .leading)
9696
}
97+
98+
Text(Localization.updatedAt(entry.updatedTime))
99+
.statRangeStyle()
100+
.frame(maxWidth: .infinity, alignment: .leading)
97101
}
98102
.padding(.horizontal)
99103
}
@@ -187,6 +191,12 @@ private extension StoreInfoView {
187191
value: "Conversion",
188192
comment: "Conversion title label for the store info widget"
189193
)
194+
static func updatedAt(_ updatedTime: String) -> LocalizedString {
195+
let format = AppLocalizedString("storeWidgets.infoView.updatedAt",
196+
value: "as of %1$@",
197+
comment: "Displays the time when the widget was last updated. %1$@ is the time to render.")
198+
return LocalizedString.localizedStringWithFormat(format, updatedTime)
199+
}
190200
}
191201

192202
enum Layout {
@@ -244,7 +254,8 @@ struct StoreWidgets_Previews: PreviewProvider {
244254
revenue: "$132.234",
245255
visitors: "67",
246256
orders: "23",
247-
conversion: "37%")
257+
conversion: "37%",
258+
updatedTime: "10:24 PM")
248259
)
249260
.previewContext(WidgetPreviewContext(family: .systemMedium))
250261

0 commit comments

Comments
 (0)