@@ -36,35 +36,25 @@ internal data class SessionDetails(
36
36
*
37
37
* @hide
38
38
*/
39
- internal class SessionGenerator (private var collectEvents : Boolean ) {
40
- private var firstSessionId = " "
41
- private var sessionIndex: Int = - 1
39
+ internal class SessionGenerator (private val collectEvents : Boolean ) {
40
+ private val firstSessionId = generateSessionId()
41
+ private var sessionIndex = 0
42
42
43
- private var thisSession: SessionDetails =
44
- SessionDetails (
45
- sessionId = " " ,
46
- firstSessionId = " " ,
47
- collectEvents,
48
- sessionIndex,
49
- )
43
+ var currentSession =
44
+ SessionDetails (sessionId = " " , firstSessionId = " " , collectEvents, sessionIndex = - 1 )
45
+ private set
50
46
51
- // Generates a new Session ID. If there was already a generated Session ID
52
- // from the last session during the app's lifecycle, it will also set the last Session ID
47
+ /* * Generates a new session. The first session's sessionId will match firstSessionId. */
53
48
fun generateNewSession (): SessionDetails {
54
- val newSessionId = UUID .randomUUID().toString().replace(" -" , " " ).lowercase()
55
-
56
- // If firstSessionId is set, use it. Otherwise set it to the
57
- // first generated Session ID
58
- firstSessionId = firstSessionId.ifEmpty { newSessionId }
59
-
60
- sessionIndex + = 1
61
-
62
- thisSession =
63
- SessionDetails (sessionId = newSessionId, firstSessionId, collectEvents, sessionIndex)
64
-
65
- return thisSession
49
+ currentSession =
50
+ SessionDetails (
51
+ sessionId = if (sessionIndex == 0 ) firstSessionId else generateSessionId(),
52
+ firstSessionId,
53
+ collectEvents,
54
+ sessionIndex++ ,
55
+ )
56
+ return currentSession
66
57
}
67
58
68
- val currentSession: SessionDetails
69
- get() = thisSession
59
+ private fun generateSessionId () = UUID .randomUUID().toString().replace(" -" , " " ).lowercase()
70
60
}
0 commit comments