Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 0 additions & 38 deletions Automattic-Tracks-iOS/ABTesting/ExPlat.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,6 @@ import Cocoa
return ttlDate.timeIntervalSinceReferenceDate - Date().timeIntervalSinceReferenceDate
}

private(set) var scheduledTimer: Timer?

public init(configuration: ExPlatConfiguration,
service: ExPlatService? = nil) {
self.service = service ?? ExPlatService(configuration: configuration)
Expand Down Expand Up @@ -50,7 +48,6 @@ import Cocoa
public func refreshIfNeeded(completion: (() -> Void)? = nil) {
guard ttl > 0 else {
completion?()
scheduleRefresh()
return
}

Expand All @@ -73,7 +70,6 @@ import Cocoa
var ttlDate = Date()
ttlDate.addTimeInterval(TimeInterval(assignments.ttl))
UserDefaults.standard.setValue(ttlDate, forKey: self.ttlDateKey)
self.scheduleRefresh()

completion?()
}
Expand All @@ -95,41 +91,15 @@ import Cocoa
}
}

private func scheduleRefresh() {
if ttl > 0 {
scheduledTimer?.invalidate()

/// Schedule the refresh on a background thread
DispatchQueue.global(qos: .background).async { [weak self] in
guard let `self` = self else {
return
}

self.scheduledTimer = Timer.scheduledTimer(withTimeInterval: self.ttl, repeats: true) { [weak self] timer in
self?.refresh()
timer.invalidate()
}

RunLoop.current.run()
}


} else {
refresh()
}
}

/// Check if the app is entering background and/or foreground
/// and start/stop the timers
///
private func subscribeToNotifications() {
let notificationCenter = NotificationCenter.default

#if os(iOS) || os(watchOS) || os(tvOS)
notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: UIApplication.willEnterForegroundNotification, object: nil)
#elseif os(macOS)
notificationCenter.addObserver(self, selector: #selector(applicationDidEnterBackground), name: NSApplication.didResignActiveNotification, object: nil)
notificationCenter.addObserver(self, selector: #selector(applicationWillEnterForeground), name: NSApplication.willBecomeActiveNotification, object: nil)
#endif
}
Expand All @@ -138,20 +108,12 @@ import Cocoa
let notificationCenter = NotificationCenter.default

#if os(iOS) || os(watchOS) || os(tvOS)
notificationCenter.removeObserver(self, name: UIApplication.didEnterBackgroundNotification, object: nil)
notificationCenter.removeObserver(self, name: UIApplication.willEnterForegroundNotification, object: nil)
#elseif os(macOS)
notificationCenter.removeObserver(self, name: NSApplication.didResignActiveNotification, object: nil)
notificationCenter.removeObserver(self, name: NSApplication.willBecomeActiveNotification, object: nil)
#endif
}

/// When the app goes to background stop the timer
///
@objc private func applicationDidEnterBackground() {
scheduledTimer?.invalidate()
}

/// When the app enter foreground refresh the assignments or
/// start the timer
///
Expand Down
2 changes: 1 addition & 1 deletion Automattic-Tracks-iOS/TracksConstants.m
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#import "TracksConstants.h"

NSString *const TracksErrorDomain = @"TracksErrorDomain";
NSString *const TracksLibraryVersion = @"0.8.1";
NSString *const TracksLibraryVersion = @"0.8.2";
19 changes: 0 additions & 19 deletions Automattic-Tracks-iOSTests/Tests/ABTesting/ExPlatTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -46,25 +46,6 @@ class ExPlatTests: XCTestCase {

wait(for: [expectation], timeout: 2.0)
}

// Schedule a timer to automatically refresh
//
func testScheduleRefresh() {
let expectation = XCTestExpectation(description: "Automatically refresh")
let serviceMock = ExPlatServiceMock()
let abTesting = ExPlat(configuration: exPlatTestConfiguration, service: serviceMock)
abTesting.refresh {

DispatchQueue.main.asyncAfter(deadline: .now() + 0.1) {
XCTAssertTrue(abTesting.scheduledTimer!.isValid)
XCTAssertEqual(round(abTesting.scheduledTimer!.timeInterval), 60)
expectation.fulfill()
}

}

wait(for: [expectation], timeout: 2.0)
}
}

private class ExPlatServiceMock: ExPlatService {
Expand Down