Skip to content

Commit 1ef87bf

Browse files
committed
Use the private setter pattern on SessionGenerator.currentSession
1 parent 8acde15 commit 1ef87bf

File tree

1 file changed

+16
-26
lines changed

1 file changed

+16
-26
lines changed

firebase-sessions/src/main/kotlin/com/google/firebase/sessions/SessionGenerator.kt

Lines changed: 16 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -36,35 +36,25 @@ internal data class SessionDetails(
3636
*
3737
* @hide
3838
*/
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
4242

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
5046

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. */
5348
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
6657
}
6758

68-
val currentSession: SessionDetails
69-
get() = thisSession
59+
private fun generateSessionId() = UUID.randomUUID().toString().replace("-", "").lowercase()
7060
}

0 commit comments

Comments
 (0)