added iOS source code
[wl-app.git] / iOS / Pods / MatomoTracker / MatomoTracker / Session.swift
diff --git a/iOS/Pods/MatomoTracker/MatomoTracker/Session.swift b/iOS/Pods/MatomoTracker/MatomoTracker/Session.swift
new file mode 100644 (file)
index 0000000..5c8c954
--- /dev/null
@@ -0,0 +1,41 @@
+//
+//  Session.swift
+//  PiwikTracker
+//
+//  Created by Cornelius Horstmann on 26.02.17.
+//  Copyright © 2017 PIWIK. All rights reserved.
+//
+
+import Foundation
+
+struct Session {
+    /// The number of sessions of the current user.
+    /// api-key: _idvc
+    let sessionsCount: Int
+    
+    /// The timestamp of the previous visit.
+    /// Discussion: Should this be now for the first request?
+    /// api-key: _viewts
+    let lastVisit: Date
+    
+    /// The timestamp of the fist visit.
+    /// Discussion: Should this be now for the first request?
+    /// api-key: _idts
+    let firstVisit: Date
+}
+
+extension Session {
+    static func current(in matomoUserDefaults: MatomoUserDefaults) -> Session {
+        let firstVisit: Date
+        var matomoUserDefaults = matomoUserDefaults
+        if let existingFirstVisit = matomoUserDefaults.firstVisit {
+            firstVisit = existingFirstVisit
+        } else {
+            firstVisit = Date()
+            matomoUserDefaults.firstVisit = firstVisit
+        }
+        let sessionCount = matomoUserDefaults.totalNumberOfVisits
+        let lastVisit = matomoUserDefaults.previousVisit ?? Date()
+        return Session(sessionsCount: sessionCount, lastVisit: lastVisit, firstVisit: firstVisit)
+    }
+}