Skip to content

Commit

Permalink
chore: clean up session object creation, test clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
justin-fiedler committed Mar 19, 2024
1 parent 7d288fa commit c5eac10
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 115 deletions.
8 changes: 4 additions & 4 deletions android/src/main/java/com/amplitude/android/Amplitude.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,19 @@ open class Amplitude(

internal var inForeground = false
private lateinit var androidContextPlugin: AndroidContextPlugin
internal val session: Session = Session(configuration)
internal lateinit var session: Session

val sessionId: Long
get() {
return session.sessionId
return if (session == null) -1 else session.sessionId
}

init {
registerShutdownHook()
}

override fun createTimeline(): Timeline {
return Timeline(logger = logger).also { it.amplitude = this }
return Timeline().also { it.amplitude = this }
}

override fun createIdentityConfiguration(): IdentityConfiguration {
Expand All @@ -54,7 +54,7 @@ open class Amplitude(
}

override suspend fun buildInternal(identityConfiguration: IdentityConfiguration) {
session.configure(configuration as Configuration, storage, store, logger)
session = Session(configuration as Configuration, storage, store, logger)
logger.debug("Configured session. Session=$session")

ApiKeyStorageMigration(this).execute()
Expand Down
4 changes: 1 addition & 3 deletions android/src/main/java/com/amplitude/android/Timeline.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@ import com.amplitude.core.platform.Timeline
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.launch

class Timeline(
private val logger: Logger? = null,
) : Timeline() {
class Timeline : Timeline() {
private val eventMessageChannel: Channel<EventQueueMessage> = Channel(Channel.UNLIMITED)
private lateinit var session: Session

Expand Down
26 changes: 4 additions & 22 deletions android/src/main/java/com/amplitude/android/utilities/Session.kt
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,11 @@ class Session(
var lastEventTime: Long = -1L

init {
readAndSetInitialSessionInfo()
}

fun configure(
configuration: Configuration,
storage: Storage,
state: State? = null,
logger: Logger? = null,
) {
this.configuration = configuration
this.storage = storage
this.state = state
this.logger = logger
readAndSetInitialSessionInfo()
}
val currentSessionId = configuration.sessionId
?: (storage?.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() ?: -1)

fun readAndSetInitialSessionInfo() {
if (configuration.sessionId == null) {
_sessionId.set(
storage?.read(Storage.Constants.PREVIOUS_SESSION_ID)?.toLongOrNull() ?: -1
)
state?.sessionId = _sessionId.get()
}
_sessionId.set(currentSessionId)
state?.sessionId = currentSessionId
lastEventId = storage?.read(Storage.Constants.LAST_EVENT_ID)?.toLongOrNull() ?: 0
lastEventTime = storage?.read(Storage.Constants.LAST_EVENT_TIME)?.toLongOrNull() ?: -1
}
Expand Down
Loading

0 comments on commit c5eac10

Please sign in to comment.