-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Basic changes to enable custom Queues * Added an example implementation of a custom queue to the ios Example app * Fixed failing Specs * Fixed the macOS example application * Added a Changelog entry * Fixed the tvOS example application
- Loading branch information
Showing
21 changed files
with
140 additions
and
172 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import Foundation | ||
import MatomoTracker | ||
|
||
public final class UserDefaultsQueue: NSObject, Queue { | ||
private var items: [Event] { | ||
didSet { | ||
if autoSave { | ||
try? UserDefaultsQueue.write(items, to: userDefaults) | ||
} | ||
} | ||
} | ||
private let userDefaults: UserDefaults | ||
private let autoSave: Bool | ||
|
||
init(_ userDefaults: UserDefaults, autoSave: Bool = false) { | ||
self.userDefaults = userDefaults | ||
self.autoSave = autoSave | ||
self.items = (try? UserDefaultsQueue.readEvents(from: userDefaults)) ?? [] | ||
super.init() | ||
} | ||
|
||
public var eventCount: Int { | ||
return items.count | ||
} | ||
|
||
public func enqueue(events: [Event], completion: (()->())?) { | ||
items.append(contentsOf: events) | ||
completion?() | ||
} | ||
|
||
public func first(limit: Int, completion: (_ items: [Event])->()) { | ||
let amount = [limit,eventCount].min()! | ||
let dequeuedItems = Array(items[0..<amount]) | ||
completion(dequeuedItems) | ||
} | ||
|
||
public func remove(events: [Event], completion: ()->()) { | ||
items = items.filter({ event in !events.contains(where: { eventToRemove in eventToRemove.uuid == event.uuid })}) | ||
completion() | ||
} | ||
|
||
public func save() throws { | ||
try UserDefaultsQueue.write(items, to: userDefaults) | ||
} | ||
} | ||
|
||
extension UserDefaultsQueue { | ||
|
||
private static let userDefaultsKey = "UserDefaultsQueue.items" | ||
|
||
private static func readEvents(from userDefaults: UserDefaults) throws -> [Event] { | ||
guard let data = userDefaults.data(forKey: userDefaultsKey) else { return [] } | ||
let decoder = JSONDecoder() | ||
return try decoder.decode([Event].self, from: data) | ||
} | ||
|
||
private static func write(_ events: [Event], to userDefaults: UserDefaults) throws { | ||
let encoder = JSONEncoder() | ||
let data = try encoder.encode(events) | ||
userDefaults.set(data, forKey: userDefaultsKey) | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
import MatomoTracker | ||
|
||
extension MatomoTracker { | ||
static let shared: MatomoTracker = { | ||
let matomoTracker = MatomoTracker(siteId: "23", baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!) | ||
matomoTracker.logger = DefaultLogger(minLevel: .info) | ||
matomoTracker.migrateFromFourPointFourSharedInstance() | ||
return matomoTracker | ||
}() | ||
|
||
private func migrateFromFourPointFourSharedInstance() { | ||
guard !UserDefaults.standard.bool(forKey: "migratedFromFourPointFourSharedInstance") else { return } | ||
copyFromOldSharedInstance() | ||
UserDefaults.standard.set(true, forKey: "migratedFromFourPointFourSharedInstance") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import Foundation | ||
import MatomoTracker | ||
|
||
extension MatomoTracker { | ||
static let shared: MatomoTracker = { | ||
let matomoTracker = MatomoTracker(siteId: "23", baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!) | ||
matomoTracker.logger = DefaultLogger(minLevel: .info) | ||
matomoTracker.migrateFromFourPointFourSharedInstance() | ||
return matomoTracker | ||
}() | ||
|
||
private func migrateFromFourPointFourSharedInstance() { | ||
guard !UserDefaults.standard.bool(forKey: "migratedFromFourPointFourSharedInstance") else { return } | ||
copyFromOldSharedInstance() | ||
UserDefaults.standard.set(true, forKey: "migratedFromFourPointFourSharedInstance") | ||
} | ||
} |
Oops, something went wrong.