Skip to content

Commit df0463b

Browse files
committed
Add SessionGenerator
1 parent 0358ae9 commit df0463b

File tree

1 file changed

+66
-0
lines changed

1 file changed

+66
-0
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
/*
2+
* Copyright 2023 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.google.firebase.sessions
18+
19+
import java.util.UUID
20+
21+
/**
22+
* [SessionInfo] is a data class responsible for storing information about the
23+
* current Session.
24+
*
25+
* @hide
26+
*/
27+
internal data class SessionInfo(
28+
val sessionId: String,
29+
val firstSessionId: String,
30+
val shouldDispatchEvents: Boolean,
31+
val sessionIndex: Int,
32+
)
33+
34+
/**
35+
* The [SessionGenerator] is responsible for generating the Session ID, and keeping the
36+
* [SessionInfo] up to date with the latest values.
37+
*
38+
* @hide
39+
*/
40+
internal class SessionGenerator(collectEvents: Boolean) {
41+
private var thisSession: SessionInfo = SessionInfo("", "", collectEvents, -1)
42+
43+
private var firstSessionId = ""
44+
private var sessionIndex: Int = -1
45+
private var collectEvents = collectEvents
46+
47+
// Generates a new Session ID. If there was already a generated Session ID
48+
// from the last session during the app's lifecycle, it will also set the last Session ID
49+
fun generateNewSession() {
50+
// val newSessionId = UUID().uuidString.replacingOccurrences(of: "-", with: "").lowercased()
51+
val newSessionId = UUID.randomUUID().toString().replace("-", "").lowercase()
52+
53+
// If firstSessionId is set, use it. Otherwise set it to the
54+
// first generated Session ID
55+
firstSessionId = if (firstSessionId.isEmpty()) newSessionId else firstSessionId
56+
57+
sessionIndex += 1
58+
59+
thisSession = SessionInfo(newSessionId,
60+
firstSessionId,
61+
collectEvents,
62+
sessionIndex)
63+
}
64+
65+
val currentSession: SessionInfo get() = thisSession
66+
}

0 commit comments

Comments
 (0)