Observe session database to be able to detect new user and removed user.
This commit is contained in:
parent
7ff72d480c
commit
35c7bffc45
7 changed files with 191 additions and 6 deletions
|
|
@ -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()
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue