Skip to content

Commit c9694b0

Browse files
committed
Rework JetpackStatsWidget
1 parent 815edf2 commit c9694b0

14 files changed

+108
-190
lines changed

WordPress/JetpackStatsWidgets/Cache/HomeWidgetCache.swift Modules/Sources/JetpackStatsWidgetsCore/HomeWidgetCache.swift

+10-19
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
import Foundation
2-
import JetpackStatsWidgetsCore
32

43
/// Cache manager that stores `HomeWidgetData` values in a plist file, contained in the specified security application group and with the specified file name.
54
/// The corresponding dictionary is always in the form `[Int: T]`, where the `Int` key is the SiteID, and the `T` value is any `HomeWidgetData` instance.
6-
struct HomeWidgetCache<T: HomeWidgetData> {
5+
public struct HomeWidgetCache<T: HomeWidgetData> {
76
let fileName: String
87
let appGroup: String
98

10-
init(fileName: String = T.filename, appGroup: String) {
9+
public init(fileName: String = T.filename, appGroup: String) {
1110
self.fileName = fileName
1211
self.appGroup = appGroup
1312
}
@@ -27,40 +26,32 @@ struct HomeWidgetCache<T: HomeWidgetData> {
2726
return directoryURL.appendingPathComponent(fileName)
2827
}
2928

30-
func read() throws -> [Int: T]? {
31-
32-
guard let fileURL,
33-
FileManager.default.fileExists(atPath: fileURL.path) else {
34-
return nil
29+
public func read() throws -> [Int: T]? {
30+
guard let fileURL, FileManager.default.fileExists(atPath: fileURL.path) else {
31+
return nil
3532
}
3633

3734
let data = try Data(contentsOf: fileURL)
3835
return try PropertyListDecoder().decode([Int: T].self, from: data)
3936
}
4037

41-
func write(items: [Int: T]) throws {
42-
43-
guard let fileURL else {
44-
return
45-
}
38+
public func write(items: [Int: T]) throws {
39+
guard let fileURL else { return }
4640

4741
let encodedData = try PropertyListEncoder().encode(items)
4842
try encodedData.write(to: fileURL)
4943
}
5044

51-
func setItem(item: T) throws {
45+
public func setItem(item: T) throws {
5246
var cachedItems = try read() ?? [Int: T]()
5347

5448
cachedItems[item.siteID] = item
5549

5650
try write(items: cachedItems)
5751
}
5852

59-
func delete() throws {
60-
61-
guard let fileURL else {
62-
return
63-
}
53+
public func delete() throws {
54+
guard let fileURL else { return }
6455
try FileManager.default.removeItem(at: fileURL)
6556
}
6657

WordPress/Classes/Stores/StatsWidgetsStore.swift

+63-43
Original file line numberDiff line numberDiff line change
@@ -186,18 +186,18 @@ private extension StatsWidgetsStore {
186186
var initializedWeekdays: [ThisWeekWidgetDay] {
187187
var days = [ThisWeekWidgetDay]()
188188
for index in 0...7 {
189-
days.insert(ThisWeekWidgetDay(date: NSCalendar.current.date(byAdding: .day,
190-
value: -index,
191-
to: Date()) ?? Date(),
192-
viewsCount: 0,
193-
dailyChangePercent: 0),
194-
at: index)
189+
let day = ThisWeekWidgetDay(
190+
date: NSCalendar.current.date(byAdding: .day, value: -index, to: Date()) ?? Date(),
191+
viewsCount: 0,
192+
dailyChangePercent: 0
193+
)
194+
days.insert(day, at: index)
195195
}
196196
return days
197197
}
198198

199199
func refreshStats<T: HomeWidgetData>(type: T.Type) -> [Int: T]? {
200-
guard let currentData = T.read() else {
200+
guard let currentData = getCachedItems(for: T.self) else {
201201
return nil
202202
}
203203
let updatedSiteList = (try? BlogQuery().hostedByWPCom(true).blogs(in: coreDataStack.mainContext)) ?? []
@@ -223,33 +223,39 @@ private extension StatsWidgetsStore {
223223

224224
let stats = (existingSite as? HomeWidgetTodayData)?.stats ?? TodayWidgetStats()
225225

226-
sitesList[blogID.intValue] = HomeWidgetTodayData(siteID: blogID.intValue,
227-
siteName: siteName,
228-
url: siteURL,
229-
timeZone: timeZone,
230-
date: date,
231-
stats: stats) as? T
226+
sitesList[blogID.intValue] = HomeWidgetTodayData(
227+
siteID: blogID.intValue,
228+
siteName: siteName,
229+
url: siteURL,
230+
timeZone: timeZone,
231+
date: date,
232+
stats: stats
233+
) as? T
232234
} else if type == HomeWidgetAllTimeData.self {
233235

234236
let stats = (existingSite as? HomeWidgetAllTimeData)?.stats ?? AllTimeWidgetStats()
235237

236-
sitesList[blogID.intValue] = HomeWidgetAllTimeData(siteID: blogID.intValue,
237-
siteName: siteName,
238-
url: siteURL,
239-
timeZone: timeZone,
240-
date: date,
241-
stats: stats) as? T
238+
sitesList[blogID.intValue] = HomeWidgetAllTimeData(
239+
siteID: blogID.intValue,
240+
siteName: siteName,
241+
url: siteURL,
242+
timeZone: timeZone,
243+
date: date,
244+
stats: stats
245+
) as? T
242246

243247
} else if type == HomeWidgetThisWeekData.self {
244248

245249
let stats = (existingSite as? HomeWidgetThisWeekData)?.stats ?? ThisWeekWidgetStats(days: initializedWeekdays)
246250

247-
sitesList[blogID.intValue] = HomeWidgetThisWeekData(siteID: blogID.intValue,
248-
siteName: siteName,
249-
url: siteURL,
250-
timeZone: timeZone,
251-
date: date,
252-
stats: stats) as? T
251+
sitesList[blogID.intValue] = HomeWidgetThisWeekData(
252+
siteID: blogID.intValue,
253+
siteName: siteName,
254+
url: siteURL,
255+
timeZone: timeZone,
256+
date: date,
257+
stats: stats
258+
) as? T
253259
}
254260
}
255261
return newData
@@ -265,26 +271,40 @@ private extension StatsWidgetsStore {
265271
let title = (element.title ?? url).isEmpty ? url : element.title ?? url
266272
let timeZone = blog.timeZone
267273
if type == HomeWidgetTodayData.self {
268-
result[blogID.intValue] = HomeWidgetTodayData(siteID: blogID.intValue,
269-
siteName: title,
270-
url: url,
271-
timeZone: timeZone ?? TimeZone.current,
272-
date: Date(timeIntervalSinceReferenceDate: 0),
273-
stats: TodayWidgetStats()) as? T
274+
result[blogID.intValue] = HomeWidgetTodayData(
275+
siteID: blogID.intValue,
276+
siteName: title,
277+
url: url,
278+
timeZone: timeZone ?? TimeZone.current,
279+
date: Date(
280+
timeIntervalSinceReferenceDate: 0
281+
),
282+
stats: TodayWidgetStats()
283+
) as? T
274284
} else if type == HomeWidgetAllTimeData.self {
275-
result[blogID.intValue] = HomeWidgetAllTimeData(siteID: blogID.intValue,
276-
siteName: title,
277-
url: url,
278-
timeZone: timeZone ?? TimeZone.current,
279-
date: Date(timeIntervalSinceReferenceDate: 0),
280-
stats: AllTimeWidgetStats()) as? T
285+
result[blogID.intValue] = HomeWidgetAllTimeData(
286+
siteID: blogID.intValue,
287+
siteName: title,
288+
url: url,
289+
timeZone: timeZone ?? TimeZone.current,
290+
date: Date(
291+
timeIntervalSinceReferenceDate: 0
292+
),
293+
stats: AllTimeWidgetStats()
294+
) as? T
281295
} else if type == HomeWidgetThisWeekData.self {
282-
result[blogID.intValue] = HomeWidgetThisWeekData(siteID: blogID.intValue,
283-
siteName: title,
284-
url: url,
285-
timeZone: timeZone ?? TimeZone.current,
286-
date: Date(timeIntervalSinceReferenceDate: 0),
287-
stats: ThisWeekWidgetStats(days: initializedWeekdays)) as? T
296+
result[blogID.intValue] = HomeWidgetThisWeekData(
297+
siteID: blogID.intValue,
298+
siteName: title,
299+
url: url,
300+
timeZone: timeZone ?? TimeZone.current,
301+
date: Date(
302+
timeIntervalSinceReferenceDate: 0
303+
),
304+
stats: ThisWeekWidgetStats(
305+
days: initializedWeekdays
306+
)
307+
) as? T
288308
}
289309
}
290310
}

WordPress/Jetpack/WidgetConfiguration.swift

-33
This file was deleted.

WordPress/JetpackIntents/SitesDataProvider.swift

+12
Original file line numberDiff line numberDiff line change
@@ -73,3 +73,15 @@ class SitesDataProvider {
7373
}
7474
}
7575
}
76+
77+
private extension HomeWidgetData {
78+
static func read(from cache: HomeWidgetCache<Self>? = nil) -> [Int: Self]? {
79+
80+
let cache = cache ?? HomeWidgetCache<Self>(fileName: Self.filename, appGroup: BuildSettings.current.appGroupName)
81+
do {
82+
return try cache.read()
83+
} catch {
84+
return nil
85+
}
86+
}
87+
}

WordPress/JetpackStatsWidgets/LocalizationConfiguration.swift

-7
This file was deleted.

WordPress/JetpackStatsWidgets/LockScreenWidgets/LockScreenSiteListProvider.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
import CocoaLumberjackSwift
13
import WidgetKit
24
import SwiftUI
35
import BuildSettingsKit

WordPress/JetpackStatsWidgets/LockScreenWidgets/ViewProvider/LockScreenMultiStatWidgetViewProvider.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -35,11 +35,11 @@ struct LockScreenMultiStatWidgetViewProvider<WidgetData: HomeWidgetData>: LockSc
3535
let message: String
3636
switch widgetKind {
3737
case .today:
38-
message = AppConfiguration.Widget.Localization.unconfiguredViewTodayTitle
38+
message = LocalizableStrings.unconfiguredViewJetpackTodayTitle
3939
case .allTime:
40-
message = AppConfiguration.Widget.Localization.unconfiguredViewAllTimeTitle
40+
message = LocalizableStrings.unconfiguredViewJetpackAllTimeTitle
4141
case .thisWeek:
42-
message = AppConfiguration.Widget.Localization.unconfiguredViewThisWeekTitle
42+
message = LocalizableStrings.unconfiguredViewJetpackThisWeekTitle
4343
}
4444
let viewModel = LockScreenUnconfiguredViewModel(message: message)
4545
return LockScreenUnconfiguredView(viewModel: viewModel)

WordPress/JetpackStatsWidgets/LockScreenWidgets/ViewProvider/LockScreenSingleStatWidgetViewProvider.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ struct LockScreenSingleStatWidgetViewProvider<WidgetData: HomeWidgetData>: LockS
2727
let message: String
2828
switch widgetKind {
2929
case .today:
30-
message = AppConfiguration.Widget.Localization.unconfiguredViewTodayTitle
30+
message = LocalizableStrings.unconfiguredViewJetpackTodayTitle
3131
case .allTime:
32-
message = AppConfiguration.Widget.Localization.unconfiguredViewAllTimeTitle
32+
message = LocalizableStrings.unconfiguredViewJetpackAllTimeTitle
3333
case .thisWeek:
34-
message = AppConfiguration.Widget.Localization.unconfiguredViewThisWeekTitle
34+
message = LocalizableStrings.unconfiguredViewJetpackThisWeekTitle
3535
}
3636
let viewModel = LockScreenUnconfiguredViewModel(message: message)
3737
return LockScreenUnconfiguredView(viewModel: viewModel)
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
import CocoaLumberjackSwift
13
import JetpackStatsWidgetsCore
24
import BuildSettingsKit
35

@@ -6,9 +8,7 @@ import BuildSettingsKit
68
extension HomeWidgetData {
79

810
static func read(from cache: HomeWidgetCache<Self>? = nil) -> [Int: Self]? {
9-
10-
let cache = cache ?? HomeWidgetCache<Self>(fileName: Self.filename,
11-
appGroup: BuildSettings.current.appGroupName)
11+
let cache = cache ?? makeCache()
1212
do {
1313
return try cache.read()
1414
} catch {
@@ -17,37 +17,16 @@ extension HomeWidgetData {
1717
}
1818
}
1919

20-
static func write(items: [Int: Self], to cache: HomeWidgetCache<Self>? = nil) {
21-
22-
let cache = cache ?? HomeWidgetCache<Self>(fileName: Self.filename,
23-
appGroup: BuildSettings.current.appGroupName)
24-
25-
do {
26-
try cache.write(items: items)
27-
} catch {
28-
DDLogError("HomeWidgetToday: Failed writing data: \(error.localizedDescription)")
29-
}
30-
}
31-
32-
static func delete(cache: HomeWidgetCache<Self>? = nil) {
33-
let cache = cache ?? HomeWidgetCache<Self>(fileName: Self.filename,
34-
appGroup: BuildSettings.current.appGroupName)
35-
36-
do {
37-
try cache.delete()
38-
} catch {
39-
DDLogError("HomeWidgetToday: Failed deleting data: \(error.localizedDescription)")
40-
}
41-
}
42-
4320
static func setItem(item: Self, to cache: HomeWidgetCache<Self>? = nil) {
44-
let cache = cache ?? HomeWidgetCache<Self>(fileName: Self.filename,
45-
appGroup: BuildSettings.current.appGroupName)
46-
21+
let cache = cache ?? makeCache()
4722
do {
4823
try cache.setItem(item: item)
4924
} catch {
5025
DDLogError("HomeWidgetToday: Failed writing data item: \(error.localizedDescription)")
5126
}
5227
}
28+
29+
private static func makeCache() -> HomeWidgetCache<Self> {
30+
HomeWidgetCache<Self>(fileName: Self.filename, appGroup: BuildSettings.current.appGroupName)
31+
}
5332
}

WordPress/JetpackStatsWidgets/Remote service/StatsWidgetsService.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
import CocoaLumberjackSwift
13
import SFHFKeychainUtils
24
import BuildSettingsKit
35
import WordPressKit

WordPress/JetpackStatsWidgets/SiteListProvider.swift

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import Foundation
2+
import CocoaLumberjackSwift
13
import WidgetKit
24
import BuildSettingsKit
35
import SwiftUI

WordPress/JetpackStatsWidgets/Supporting Files/JetpackStatsWidgets-Bridging-Header.h

-1
This file was deleted.

WordPress/JetpackStatsWidgets/Views/UnconfiguredView.swift

+3-3
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@ struct UnconfiguredView: View {
1919
case .loggedOut(let widgetKind):
2020
switch widgetKind {
2121
case .today:
22-
return AppConfiguration.Widget.Localization.unconfiguredViewTodayTitle
22+
return LocalizableStrings.unconfiguredViewJetpackTodayTitle
2323
case .allTime:
24-
return AppConfiguration.Widget.Localization.unconfiguredViewAllTimeTitle
24+
return LocalizableStrings.unconfiguredViewJetpackAllTimeTitle
2525
case .thisWeek:
26-
return AppConfiguration.Widget.Localization.unconfiguredViewThisWeekTitle
26+
return LocalizableStrings.unconfiguredViewJetpackThisWeekTitle
2727
}
2828
case .noSite(let widgetKind):
2929
switch widgetKind {

0 commit comments

Comments
 (0)