Observe session database to be able to detect new user and removed user.

This commit is contained in:
Benoit Marty
2023-03-31 22:17:19 +02:00
committed by Benoit Marty
parent 62db96476d
commit c52ad084e9
7 changed files with 191 additions and 6 deletions

View File

@@ -17,16 +17,43 @@
package io.element.android.libraries.push.impl.userpushstore
import android.content.Context
import io.element.android.libraries.di.AppScope
import io.element.android.libraries.di.ApplicationContext
import io.element.android.libraries.di.SingleIn
import io.element.android.libraries.sessionstorage.api.observer.SessionListener
import io.element.android.libraries.sessionstorage.api.observer.SessionObserver
import javax.inject.Inject
@SingleIn(AppScope::class)
class UserPushStoreFactory @Inject constructor(
@ApplicationContext private val context: Context,
) {
private val sessionObserver: SessionObserver,
) : SessionListener {
init {
observeSessions()
}
// We can have only one class accessing a single data store, so keep a cache of them.
private val cache = mutableMapOf<String, UserPushStore>()
fun create(userId: String): UserPushStore {
return UserPushStoreDataStore(
context = context,
userId = userId
)
return cache.getOrPut(userId) {
UserPushStoreDataStore(
context = context,
userId = userId
)
}
}
private fun observeSessions() {
sessionObserver.addListener(this)
}
override suspend fun onSessionCreated(userId: String) {
// Nothing to do
}
override suspend fun onSessionDeleted(userId: String) {
// Delete the store
create(userId).reset()
}
}