Skip to content

Commit

Permalink
Feature/137 custom queues (#279)
Browse files Browse the repository at this point in the history
* 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
brototyp authored Dec 12, 2018
1 parent eac3be1 commit a19879b
Show file tree
Hide file tree
Showing 21 changed files with 140 additions and 172 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Changelog

## Unreleased
* **feature** Added the possibility to implement custom queues. [#137](https://github.com/matomo-org/matomo-sdk-ios/issues/137)
* **improvement** Updated to Swift 4.2
* **bugfix** Added default values for items when tracking orders. [#276](https://github.com/matomo-org/matomo-sdk-ios/issues/276)

Expand Down
16 changes: 0 additions & 16 deletions Example/ios/iOS Example/CustomVariablesViewController.h

This file was deleted.

38 changes: 0 additions & 38 deletions Example/ios/iOS Example/CustomVariablesViewController.m

This file was deleted.

12 changes: 3 additions & 9 deletions Example/ios/iOS Example/MatomoTracker+SharedInstance.swift
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
//
// PiwikTracker+SharedInstance.swift
// ios
//
// Created by Cornelius Horstmann on 14.12.17.
// Copyright © 2017 Mattias Levin. All rights reserved.
//

import Foundation
import MatomoTracker

extension MatomoTracker {
static let shared: MatomoTracker = {
let matomoTracker = MatomoTracker(siteId: "23", baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!)
let queue = UserDefaultsQueue(UserDefaults.standard, autoSave: true)
let dispatcher = URLSessionDispatcher(baseURL: URL(string: "https://demo2.matomo.org/piwik.php")!)
let matomoTracker = MatomoTracker(siteId: "23", queue: queue, dispatcher: dispatcher)
matomoTracker.logger = DefaultLogger(minLevel: .info)
matomoTracker.migrateFromFourPointFourSharedInstance()
return matomoTracker
Expand Down
63 changes: 63 additions & 0 deletions Example/ios/iOS Example/UserDefaultsQueue.swift
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)
}

}
8 changes: 4 additions & 4 deletions Example/ios/ios.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
1F72DA731E62E78E00EFF764 /* ConfigurationViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F72DA721E62E78E00EFF764 /* ConfigurationViewController.swift */; };
1F9C69F31F89254200728626 /* CustomTrackingParametersViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F9C69F21F89254200728626 /* CustomTrackingParametersViewController.swift */; };
1FA444982047A16800F7E37E /* SearchViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FA444972047A16800F7E37E /* SearchViewController.swift */; };
1FC2A0E52195A10A0039EE0C /* UserDefaultsQueue.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC2A0E42195A10A0039EE0C /* UserDefaultsQueue.swift */; };
1FC89ADD1FE2A2A5002EEB27 /* MatomoTracker+SharedInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC89ADC1FE2A2A5002EEB27 /* MatomoTracker+SharedInstance.swift */; };
1FDC91771F1A648C0046F506 /* CustomDimensionsViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FDC91711F1A648C0046F506 /* CustomDimensionsViewController.swift */; };
1FDC91791F1A648C0046F506 /* ObjectiveCCompatibilityChecker.m in Sources */ = {isa = PBXBuildFile; fileRef = 1FDC91741F1A648C0046F506 /* ObjectiveCCompatibilityChecker.m */; };
Expand Down Expand Up @@ -83,6 +84,7 @@
1F72DA721E62E78E00EFF764 /* ConfigurationViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = ConfigurationViewController.swift; sourceTree = "<group>"; };
1F9C69F21F89254200728626 /* CustomTrackingParametersViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CustomTrackingParametersViewController.swift; sourceTree = "<group>"; };
1FA444972047A16800F7E37E /* SearchViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SearchViewController.swift; sourceTree = "<group>"; };
1FC2A0E42195A10A0039EE0C /* UserDefaultsQueue.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserDefaultsQueue.swift; sourceTree = "<group>"; };
1FC89ADC1FE2A2A5002EEB27 /* MatomoTracker+SharedInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MatomoTracker+SharedInstance.swift"; sourceTree = "<group>"; };
1FDC91711F1A648C0046F506 /* CustomDimensionsViewController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CustomDimensionsViewController.swift; sourceTree = "<group>"; };
1FDC91731F1A648C0046F506 /* ObjectiveCCompatibilityChecker.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ObjectiveCCompatibilityChecker.h; sourceTree = "<group>"; };
Expand All @@ -105,8 +107,6 @@
CD5F3BED18678C9000D04E03 /* CoreLocation.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CoreLocation.framework; path = Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.9.sdk/System/Library/Frameworks/CoreLocation.framework; sourceTree = DEVELOPER_DIR; };
CD7ABB131B0FD0D2002312DA /* DownloadViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = DownloadViewController.h; sourceTree = "<group>"; };
CD7ABB141B0FD0D2002312DA /* DownloadViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = DownloadViewController.m; sourceTree = "<group>"; };
CD91BDD219A1382900A4B80D /* CustomVariablesViewController.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = CustomVariablesViewController.h; sourceTree = "<group>"; };
CD91BDD319A1382900A4B80D /* CustomVariablesViewController.m */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.objc; path = CustomVariablesViewController.m; sourceTree = "<group>"; };
CD93EC8B17E76E290062BE20 /* ios.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = ios.app; sourceTree = BUILT_PRODUCTS_DIR; };
CD93EC9117E76E290062BE20 /* PiwikTrackeriOSDemo-Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = "PiwikTrackeriOSDemo-Info.plist"; sourceTree = "<group>"; };
CD93EC9317E76E290062BE20 /* en */ = {isa = PBXFileReference; lastKnownFileType = text.plist.strings; name = en; path = en.lproj/InfoPlist.strings; sourceTree = "<group>"; };
Expand Down Expand Up @@ -209,8 +209,6 @@
1FFE0C262095C0E400DE23B1 /* CampaignViewController.swift */,
EB82ABD720DA125100083494 /* ContentViewController.swift */,
1F7001BE216B6F47007A9355 /* GoalsViewController.swift */,
CD91BDD219A1382900A4B80D /* CustomVariablesViewController.h */,
CD91BDD319A1382900A4B80D /* CustomVariablesViewController.m */,
CD7ABB131B0FD0D2002312DA /* DownloadViewController.h */,
CD7ABB141B0FD0D2002312DA /* DownloadViewController.m */,
CDEDC027189518B00054CF73 /* EcommerceViewController.h */,
Expand All @@ -228,6 +226,7 @@
1FDC91741F1A648C0046F506 /* ObjectiveCCompatibilityChecker.m */,
1F72DA6D1E61ECAF00EFF764 /* ios-Bridging-Header.h */,
1FC89ADC1FE2A2A5002EEB27 /* MatomoTracker+SharedInstance.swift */,
1FC2A0E42195A10A0039EE0C /* UserDefaultsQueue.swift */,
);
path = "iOS Example";
sourceTree = "<group>";
Expand Down Expand Up @@ -402,6 +401,7 @@
1F7001BF216B6F47007A9355 /* GoalsViewController.swift in Sources */,
24F6AE8F1F61FDE200C6C22C /* UserIDViewController.swift in Sources */,
EB82ABD820DA125100083494 /* ContentViewController.swift in Sources */,
1FC2A0E52195A10A0039EE0C /* UserDefaultsQueue.swift in Sources */,
1FFE0C272095C0E500DE23B1 /* CampaignViewController.swift in Sources */,
1F03BC9B1E86CF770002F0AD /* ScreenViewController.swift in Sources */,
1F72DA711E62E55200EFF764 /* MenuViewController.swift in Sources */,
Expand Down
8 changes: 4 additions & 4 deletions Example/macos/macos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
1F8801981EBFBBCC00707352 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F8801971EBFBBCC00707352 /* ViewController.swift */; };
1F88019A1EBFBBCC00707352 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1F8801991EBFBBCC00707352 /* Assets.xcassets */; };
1F88019D1EBFBBCC00707352 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1F88019B1EBFBBCC00707352 /* Main.storyboard */; };
1F963077201B3AB0007B2AE7 /* MatomoTracker+SharedInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F963076201B3AB0007B2AE7 /* MatomoTracker+SharedInstance.swift */; };
1FC2A0E92195C6340039EE0C /* MatomoTracker+SharedInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC2A0E82195C6340039EE0C /* MatomoTracker+SharedInstance.swift */; };
E9C171B28E33B897204F7A14 /* Pods_example_macos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 9A25A82DCA8469434D57EE94 /* Pods_example_macos.framework */; };
/* End PBXBuildFile section */

Expand All @@ -22,7 +22,7 @@
1F8801991EBFBBCC00707352 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
1F88019C1EBFBBCC00707352 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
1F88019E1EBFBBCC00707352 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1F963076201B3AB0007B2AE7 /* MatomoTracker+SharedInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "MatomoTracker+SharedInstance.swift"; path = "../../ios/iOS Example/MatomoTracker+SharedInstance.swift"; sourceTree = "<group>"; };
1FC2A0E82195C6340039EE0C /* MatomoTracker+SharedInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MatomoTracker+SharedInstance.swift"; sourceTree = "<group>"; };
6E5F466A5CEB9E4EE4BB5F02 /* Pods-example-macos.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-example-macos.debug.xcconfig"; path = "../../Pods/Target Support Files/Pods-example-macos/Pods-example-macos.debug.xcconfig"; sourceTree = "<group>"; };
7ABE41F256CAD8BC6F7A7450 /* Pods-example-macos.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-example-macos.release.xcconfig"; path = "../../Pods/Target Support Files/Pods-example-macos/Pods-example-macos.release.xcconfig"; sourceTree = "<group>"; };
9A25A82DCA8469434D57EE94 /* Pods_example_macos.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_example_macos.framework; sourceTree = BUILT_PRODUCTS_DIR; };
Expand Down Expand Up @@ -71,7 +71,7 @@
children = (
1F8801951EBFBBCC00707352 /* AppDelegate.swift */,
1F8801971EBFBBCC00707352 /* ViewController.swift */,
1F963076201B3AB0007B2AE7 /* MatomoTracker+SharedInstance.swift */,
1FC2A0E82195C6340039EE0C /* MatomoTracker+SharedInstance.swift */,
1F8801991EBFBBCC00707352 /* Assets.xcassets */,
1F88019B1EBFBBCC00707352 /* Main.storyboard */,
1F88019E1EBFBBCC00707352 /* Info.plist */,
Expand Down Expand Up @@ -201,7 +201,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1F963077201B3AB0007B2AE7 /* MatomoTracker+SharedInstance.swift in Sources */,
1FC2A0E92195C6340039EE0C /* MatomoTracker+SharedInstance.swift in Sources */,
1F8801981EBFBBCC00707352 /* ViewController.swift in Sources */,
1F8801961EBFBBCC00707352 /* AppDelegate.swift in Sources */,
);
Expand Down
17 changes: 17 additions & 0 deletions Example/macos/macos/MatomoTracker+SharedInstance.swift
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")
}
}
8 changes: 4 additions & 4 deletions Example/tvos/tvos.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
1F38EBEB1EE55AE50021FBF8 /* ViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F38EBEA1EE55AE50021FBF8 /* ViewController.swift */; };
1F38EBEE1EE55AE50021FBF8 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = 1F38EBEC1EE55AE50021FBF8 /* Main.storyboard */; };
1F38EBF01EE55AE50021FBF8 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 1F38EBEF1EE55AE50021FBF8 /* Assets.xcassets */; };
1F963079201B3AD1007B2AE7 /* MatomoTracker+SharedInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1F963078201B3AD1007B2AE7 /* MatomoTracker+SharedInstance.swift */; };
1FC2A0EB2195C98B0039EE0C /* MatomoTracker+SharedInstance.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1FC2A0EA2195C98B0039EE0C /* MatomoTracker+SharedInstance.swift */; };
607D4B3ABA064DC5DA050144 /* Pods_example_tvos.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 0907CCC9B4E1BA377380424F /* Pods_example_tvos.framework */; };
/* End PBXBuildFile section */

Expand All @@ -23,7 +23,7 @@
1F38EBED1EE55AE50021FBF8 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = "<group>"; };
1F38EBEF1EE55AE50021FBF8 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = "<group>"; };
1F38EBF11EE55AE50021FBF8 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
1F963078201B3AD1007B2AE7 /* MatomoTracker+SharedInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; name = "MatomoTracker+SharedInstance.swift"; path = "../../ios/iOS Example/MatomoTracker+SharedInstance.swift"; sourceTree = "<group>"; };
1FC2A0EA2195C98B0039EE0C /* MatomoTracker+SharedInstance.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = "MatomoTracker+SharedInstance.swift"; sourceTree = "<group>"; };
2CE39FB3D2EC21407896280A /* Pods-example-tvos.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-example-tvos.release.xcconfig"; path = "../../Pods/Target Support Files/Pods-example-tvos/Pods-example-tvos.release.xcconfig"; sourceTree = "<group>"; };
918636219B28741F62F0C1D0 /* Pods-example-tvos.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-example-tvos.debug.xcconfig"; path = "../../Pods/Target Support Files/Pods-example-tvos/Pods-example-tvos.debug.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -70,9 +70,9 @@
1F38EBE71EE55AE50021FBF8 /* tvos */ = {
isa = PBXGroup;
children = (
1F963078201B3AD1007B2AE7 /* MatomoTracker+SharedInstance.swift */,
1F38EBE81EE55AE50021FBF8 /* AppDelegate.swift */,
1F38EBEA1EE55AE50021FBF8 /* ViewController.swift */,
1FC2A0EA2195C98B0039EE0C /* MatomoTracker+SharedInstance.swift */,
1F38EBEC1EE55AE50021FBF8 /* Main.storyboard */,
1F38EBEF1EE55AE50021FBF8 /* Assets.xcassets */,
1F38EBF11EE55AE50021FBF8 /* Info.plist */,
Expand Down Expand Up @@ -200,7 +200,7 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
1F963079201B3AD1007B2AE7 /* MatomoTracker+SharedInstance.swift in Sources */,
1FC2A0EB2195C98B0039EE0C /* MatomoTracker+SharedInstance.swift in Sources */,
1F38EBEB1EE55AE50021FBF8 /* ViewController.swift in Sources */,
1F38EBE91EE55AE50021FBF8 /* AppDelegate.swift in Sources */,
);
Expand Down
17 changes: 17 additions & 0 deletions Example/tvos/tvos/MatomoTracker+SharedInstance.swift
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")
}
}
Loading

0 comments on commit a19879b

Please sign in to comment.