Skip to content

Commit 4099eff

Browse files
committed
Provide formatted updated time when dispatching an entry
1 parent b9bb897 commit 4099eff

File tree

2 files changed

+20
-4
lines changed

2 files changed

+20
-4
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: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ private struct StoreInfoView: View {
9595
.frame(maxWidth: .infinity, alignment: .leading)
9696
}
9797

98-
Text(Localization.updatedAt("10:23 PM"))
98+
Text(Localization.updatedAt(entry.updatedTime))
9999
.statRangeStyle()
100100
.frame(maxWidth: .infinity, alignment: .leading)
101101
}
@@ -254,7 +254,8 @@ struct StoreWidgets_Previews: PreviewProvider {
254254
revenue: "$132.234",
255255
visitors: "67",
256256
orders: "23",
257-
conversion: "37%")
257+
conversion: "37%",
258+
updatedTime: "10:24 PM")
258259
)
259260
.previewContext(WidgetPreviewContext(family: .systemMedium))
260261

0 commit comments

Comments
 (0)