Skip to content

Commit 2650744

Browse files
authored
Merge pull request #3991 from mdholloway/metricsclient
Add MetricsClientBridge for Objective-C compatibility
2 parents 54b0c19 + 02b2a52 commit 2650744

File tree

6 files changed

+71
-39
lines changed

6 files changed

+71
-39
lines changed

WMF Framework/Event Platform/EventPlatformClient.swift

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -52,18 +52,16 @@ import CocoaLumberjackSwift
5252
* - `app_session_id`: the ID of the session at the time of the event when it was
5353
* originally submitted
5454
*/
55-
@objc (WMFEventPlatformClient)
5655
public class EventPlatformClient: NSObject, SamplingControllerDelegate {
5756
// MARK: - Properties
5857

59-
@objc(sharedInstance) public static let shared: EventPlatformClient = {
58+
public static let shared: EventPlatformClient = {
6059
return EventPlatformClient()
6160
}()
6261

6362
// SINGLETONTODO
6463
/// Session for requesting data
6564
let session = MWKDataStore.shared().session
66-
6765
let samplingController: SamplingController
6866
let storageManager: StorageManager?
6967

@@ -223,7 +221,7 @@ public class EventPlatformClient: NSObject, SamplingControllerDelegate {
223221
* This method is called by the application delegate in
224222
* `applicationWillResignActive()` and disables event logging.
225223
*/
226-
@objc public func appInBackground() {
224+
public func appInBackground() {
227225
lastTimestamp = Date()
228226
}
229227
/**
@@ -233,7 +231,7 @@ public class EventPlatformClient: NSObject, SamplingControllerDelegate {
233231
* If it has been more than 15 minutes since the app entered background state,
234232
* a new session is started.
235233
*/
236-
@objc public func appInForeground() {
234+
public func appInForeground() {
237235
if sessionTimedOut() {
238236
resetSession()
239237
}
@@ -246,7 +244,7 @@ public class EventPlatformClient: NSObject, SamplingControllerDelegate {
246244
* session ends when the user (or the OS) has closed the app or when 15
247245
* minutes of inactivity have passed.
248246
*/
249-
@objc public func appWillClose() {
247+
public func appWillClose() {
250248
// Placeholder for any onTerminate logic
251249
}
252250

@@ -268,7 +266,7 @@ public class EventPlatformClient: NSObject, SamplingControllerDelegate {
268266
* This assumes storageManager's deviceID will be reset separately by a
269267
* different owner (EventLoggingService's `reset()` method)
270268
*/
271-
@objc public func reset() {
269+
public func reset() {
272270
resetSession()
273271
}
274272

@@ -387,7 +385,7 @@ public class EventPlatformClient: NSObject, SamplingControllerDelegate {
387385
* Flush the queue of outgoing requests in a first-in-first-out,
388386
* fire-and-forget fashion
389387
*/
390-
private func postAllScheduled(_ completion: (() -> Void)? = nil) {
388+
func postAllScheduled(_ completion: (() -> Void)? = nil) {
391389
guard let storageManager = self.storageManager else {
392390
completion?()
393391
return
@@ -715,29 +713,6 @@ private extension EventPlatformClient {
715713
}
716714
}
717715

718-
//MARK: PeriodicWorker
719-
720-
extension EventPlatformClient: PeriodicWorker {
721-
public func doPeriodicWork(_ completion: @escaping () -> Void) {
722-
guard let storageManager = self.storageManager else {
723-
return
724-
}
725-
storageManager.pruneStaleEvents(completion: {
726-
self.postAllScheduled(completion)
727-
})
728-
}
729-
}
730-
731-
//MARK: BackgroundFetcher
732-
733-
extension EventPlatformClient: BackgroundFetcher {
734-
public func performBackgroundFetch(_ completion: @escaping (UIBackgroundFetchResult) -> Void) {
735-
doPeriodicWork {
736-
completion(.noData)
737-
}
738-
}
739-
}
740-
741716
// MARK: EventInterface
742717

743718
/**
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import Foundation
2+
3+
@objc(WMFMetricsClientBridge)
4+
public class MetricsClientBridge: NSObject {
5+
6+
let client = EventPlatformClient.shared
7+
8+
@objc(sharedInstance) public static let shared: MetricsClientBridge = {
9+
return MetricsClientBridge()
10+
}()
11+
12+
@objc public func appInBackground() {
13+
client.appInBackground()
14+
}
15+
16+
@objc public func appInForeground() {
17+
client.appInForeground()
18+
}
19+
20+
@objc public func appWillClose() {
21+
client.appWillClose()
22+
}
23+
24+
@objc public func reset() {
25+
client.reset()
26+
}
27+
28+
}
29+
30+
//MARK: PeriodicWorker
31+
32+
extension MetricsClientBridge: PeriodicWorker {
33+
public func doPeriodicWork(_ completion: @escaping () -> Void) {
34+
guard let storageManager = self.client.storageManager else {
35+
return
36+
}
37+
storageManager.pruneStaleEvents(completion: {
38+
self.client.postAllScheduled(completion)
39+
})
40+
}
41+
}
42+
43+
//MARK: BackgroundFetcher
44+
45+
extension MetricsClientBridge: BackgroundFetcher {
46+
public func performBackgroundFetch(_ completion: @escaping (UIBackgroundFetchResult) -> Void) {
47+
doPeriodicWork {
48+
completion(.noData)
49+
}
50+
}
51+
}
52+
53+

Wikipedia.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,7 @@
651651
67F73E752267B9070079DEEF /* TalkPageReplyListViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67F73E742267B9070079DEEF /* TalkPageReplyListViewController.swift */; };
652652
67F73E792267B9510079DEEF /* TalkPageTopicNewViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 67F73E782267B9500079DEEF /* TalkPageTopicNewViewController.swift */; };
653653
67F9AE4923AD2F38003D4F5E /* Array+SafeIndex.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7A3EE1532267DC3800709CF6 /* Array+SafeIndex.swift */; };
654+
7004A5BA268CEE680029C46B /* MetricsClientBridge.swift in Sources */ = {isa = PBXBuildFile; fileRef = 7004A5B9268CEE680029C46B /* MetricsClientBridge.swift */; };
654655
702096B9256C3D5700E27041 /* SamplingController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 702096B8256C3D5700E27041 /* SamplingController.swift */; };
655656
70B798142575714100C10BCA /* EventPlatformEvents.xcdatamodeld in Sources */ = {isa = PBXBuildFile; fileRef = 70B798122575714100C10BCA /* EventPlatformEvents.xcdatamodeld */; };
656657
70B79820257577B800C10BCA /* StorageManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = 70B7981F257577B800C10BCA /* StorageManager.swift */; };
@@ -3758,6 +3759,7 @@
37583759
67F73E742267B9070079DEEF /* TalkPageReplyListViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TalkPageReplyListViewController.swift; sourceTree = "<group>"; };
37593760
67F73E782267B9500079DEEF /* TalkPageTopicNewViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = TalkPageTopicNewViewController.swift; sourceTree = "<group>"; };
37603761
67FF8E2C2550BB6F0048E741 /* ArticleAsLivingDocFunnel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ArticleAsLivingDocFunnel.swift; sourceTree = "<group>"; };
3762+
7004A5B9268CEE680029C46B /* MetricsClientBridge.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MetricsClientBridge.swift; sourceTree = "<group>"; };
37613763
702096B8256C3D5700E27041 /* SamplingController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SamplingController.swift; sourceTree = "<group>"; };
37623764
70B798132575714100C10BCA /* EventPlatformEvents.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = EventPlatformEvents.xcdatamodel; sourceTree = "<group>"; };
37633765
70B7981F257577B800C10BCA /* StorageManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StorageManager.swift; sourceTree = "<group>"; };
@@ -6759,6 +6761,7 @@
67596761
70B7982A25758E6D00C10BCA /* EPEventRecord+CoreDataClass.swift */,
67606762
70B7983525758EB800C10BCA /* EPEventRecord+CoreDataProperties.swift */,
67616763
70B798122575714100C10BCA /* EventPlatformEvents.xcdatamodeld */,
6764+
7004A5B9268CEE680029C46B /* MetricsClientBridge.swift */,
67626765
);
67636766
path = "Event Platform";
67646767
sourceTree = "<group>";
@@ -11208,6 +11211,7 @@
1120811211
67A6F13E23BFEF4200736539 /* ArticleCacheController.swift in Sources */,
1120911212
67D6C00A240581ED005709B1 /* CacheItemMigrationPolicy.swift in Sources */,
1121011213
D80ED2591EE178A800CE8C50 /* Gradient.swift in Sources */,
11214+
7004A5BA268CEE680029C46B /* MetricsClientBridge.swift in Sources */,
1121111215
D844DA0A1D6CC5240042D692 /* NSLocale+WMFExtras.swift in Sources */,
1121211216
0042806C25E6E395004945B3 /* FLAnimatedImage.m in Sources */,
1121311217
830177FA1FBF3E490005681C /* ReadingListsAPIController.swift in Sources */,

Wikipedia/Code/AppDelegate.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ - (void)applicationWillEnterForeground:(UIApplication *)application {
9898
- (void)applicationDidBecomeActive:(UIApplication *)application {
9999
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
100100
[self resumeAppIfNecessary];
101-
[[WMFEventPlatformClient sharedInstance] appInForeground];
101+
[[WMFMetricsClientBridge sharedInstance] appInForeground];
102102
}
103103

104104
- (void)application:(UIApplication *)application performActionForShortcutItem:(UIApplicationShortcutItem *)shortcutItem completionHandler:(void (^)(BOOL))completionHandler {
@@ -186,7 +186,7 @@ - (void)applicationWillResignActive:(UIApplication *)application {
186186
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
187187
// Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
188188
[[NSUserDefaults standardUserDefaults] wmf_setAppResignActiveDate:[NSDate date]];
189-
[[WMFEventPlatformClient sharedInstance] appInBackground];
189+
[[WMFMetricsClientBridge sharedInstance] appInBackground];
190190
}
191191

192192
- (void)applicationDidEnterBackground:(UIApplication *)application {
@@ -199,7 +199,7 @@ - (void)applicationDidEnterBackground:(UIApplication *)application {
199199
- (void)applicationWillTerminate:(UIApplication *)application {
200200
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
201201
[self applicationDidEnterBackground:application];
202-
[[WMFEventPlatformClient sharedInstance] appWillClose];
202+
[[WMFMetricsClientBridge sharedInstance] appWillClose];
203203
}
204204

205205
#pragma mark - Background Fetch

Wikipedia/Code/WMFAppViewController.m

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -272,15 +272,15 @@ - (void)setupControllers {
272272
[self.periodicWorkerController add:self.dataStore.readingListsController];
273273
[self.periodicWorkerController add:self.dataStore.remoteNotificationsController];
274274
[self.periodicWorkerController add:[WMFEventLoggingService sharedInstance]];
275-
[self.periodicWorkerController add:[WMFEventPlatformClient sharedInstance]];
275+
[self.periodicWorkerController add:[WMFMetricsClientBridge sharedInstance]];
276276

277277
self.backgroundFetcherController = [[WMFBackgroundFetcherController alloc] init];
278278
self.backgroundFetcherController.delegate = self;
279279
[self.backgroundFetcherController add:self.dataStore.readingListsController];
280280
[self.backgroundFetcherController add:self.dataStore.remoteNotificationsController];
281281
[self.backgroundFetcherController add:(id<WMFBackgroundFetcher>)self.dataStore.feedContentController];
282282
[self.backgroundFetcherController add:[WMFEventLoggingService sharedInstance]];
283-
[self.backgroundFetcherController add:[WMFEventPlatformClient sharedInstance]];
283+
[self.backgroundFetcherController add:[WMFMetricsClientBridge sharedInstance]];
284284
}
285285

286286
- (void)loadMainUI {

Wikipedia/Code/WMFSettingsViewController.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,19 +175,19 @@ - (void)updateStateForMenuItemType:(WMFSettingsMenuItemType)type isSwitchOnValue
175175
switch (type) {
176176
case WMFSettingsMenuItemType_SendUsageReports: {
177177
WMFEventLoggingService *eventLoggingService = [WMFEventLoggingService sharedInstance];
178-
WMFEventPlatformClient *eventPlatformClient = [WMFEventPlatformClient sharedInstance];
178+
WMFMetricsClientBridge *metricsClientBridge = [WMFMetricsClientBridge sharedInstance];
179179
NSUserDefaults.standardUserDefaults.wmf_sendUsageReports = isOn;
180180
if (isOn) {
181181
[eventLoggingService reset];
182-
[eventPlatformClient reset];
182+
[metricsClientBridge reset];
183183
[[WMFDailyStatsLoggingFunnel shared] logAppNumberOfDaysSinceInstall];
184184
[[SessionsFunnel shared] logSessionStart];
185185
[[UserHistoryFunnel shared] logStartingSnapshot];
186186
} else {
187187
[[SessionsFunnel shared] logSessionEnd];
188188
[[UserHistoryFunnel shared] logSnapshot];
189189
[eventLoggingService reset];
190-
[eventPlatformClient reset];
190+
[metricsClientBridge reset];
191191
}
192192
} break;
193193
default:

0 commit comments

Comments
 (0)