added iOS source code
[wl-app.git] / iOS / Pods / MatomoTracker / MatomoTracker / Session.swift
1 //
2 //  Session.swift
3 //  PiwikTracker
4 //
5 //  Created by Cornelius Horstmann on 26.02.17.
6 //  Copyright © 2017 PIWIK. All rights reserved.
7 //
8
9 import Foundation
10
11 struct Session {
12     /// The number of sessions of the current user.
13     /// api-key: _idvc
14     let sessionsCount: Int
15     
16     /// The timestamp of the previous visit.
17     /// Discussion: Should this be now for the first request?
18     /// api-key: _viewts
19     let lastVisit: Date
20     
21     /// The timestamp of the fist visit.
22     /// Discussion: Should this be now for the first request?
23     /// api-key: _idts
24     let firstVisit: Date
25 }
26
27 extension Session {
28     static func current(in matomoUserDefaults: MatomoUserDefaults) -> Session {
29         let firstVisit: Date
30         var matomoUserDefaults = matomoUserDefaults
31         if let existingFirstVisit = matomoUserDefaults.firstVisit {
32             firstVisit = existingFirstVisit
33         } else {
34             firstVisit = Date()
35             matomoUserDefaults.firstVisit = firstVisit
36         }
37         let sessionCount = matomoUserDefaults.totalNumberOfVisits
38         let lastVisit = matomoUserDefaults.previousVisit ?? Date()
39         return Session(sessionsCount: sessionCount, lastVisit: lastVisit, firstVisit: firstVisit)
40     }
41 }