Rename UserPushStoreFactory.create to UserPushStoreFactory.getOrCreate for code clarity.

This commit is contained in:
Benoit Marty
2024-04-02 16:23:57 +02:00
committed by Benoit Marty
parent e18e5f1cc5
commit 52c039d676
11 changed files with 13 additions and 13 deletions
@@ -48,7 +48,7 @@ class NotificationSettingsPresenter @Inject constructor(
) : Presenter<NotificationSettingsState> { ) : Presenter<NotificationSettingsState> {
@Composable @Composable
override fun present(): NotificationSettingsState { override fun present(): NotificationSettingsState {
val userPushStore = remember { userPushStoreFactory.create(matrixClient.sessionId) } val userPushStore = remember { userPushStoreFactory.getOrCreate(matrixClient.sessionId) }
val systemNotificationsEnabled: MutableState<Boolean> = remember { val systemNotificationsEnabled: MutableState<Boolean> = remember {
mutableStateOf(systemNotificationsEnabledProvider.notificationsEnabled()) mutableStateOf(systemNotificationsEnabledProvider.notificationsEnabled())
} }
@@ -35,7 +35,7 @@ class DefaultGetCurrentPushProvider @Inject constructor(
.value .value
.navigationState .navigationState
.currentSessionId() .currentSessionId()
?.let { pushStoreFactory.create(it) } ?.let { pushStoreFactory.getOrCreate(it) }
?.getPushProviderName() ?.getPushProviderName()
} }
} }
@@ -49,7 +49,7 @@ class DefaultPushService @Inject constructor(
* Get current push provider, compare with provided one, then unregister and register if different, and store change. * Get current push provider, compare with provided one, then unregister and register if different, and store change.
*/ */
override suspend fun registerWith(matrixClient: MatrixClient, pushProvider: PushProvider, distributor: Distributor) { override suspend fun registerWith(matrixClient: MatrixClient, pushProvider: PushProvider, distributor: Distributor) {
val userPushStore = userPushStoreFactory.create(matrixClient.sessionId) val userPushStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
val currentPushProviderName = userPushStore.getPushProviderName() val currentPushProviderName = userPushStore.getPushProviderName()
if (currentPushProviderName != pushProvider.name) { if (currentPushProviderName != pushProvider.name) {
// Unregister previous one if any // Unregister previous one if any
@@ -63,7 +63,7 @@ class PushersManager @Inject constructor(
* Register a pusher to the server if not done yet. * Register a pusher to the server if not done yet.
*/ */
override suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String) { override suspend fun registerPusher(matrixClient: MatrixClient, pushKey: String, gateway: String) {
val userDataStore = userPushStoreFactory.create(matrixClient.sessionId) val userDataStore = userPushStoreFactory.getOrCreate(matrixClient.sessionId)
if (userDataStore.getCurrentRegisteredPushKey() == pushKey) { if (userDataStore.getCurrentRegisteredPushKey() == pushKey) {
Timber.tag(loggerTag.value) Timber.tag(loggerTag.value)
.d("Unnecessary to register again the same pusher, but do it in case the pusher has been removed from the server") .d("Unnecessary to register again the same pusher, but do it in case the pusher has been removed from the server")
@@ -122,7 +122,7 @@ class DefaultPushHandler @Inject constructor(
return return
} }
val userPushStore = userPushStoreFactory.create(userId) val userPushStore = userPushStoreFactory.getOrCreate(userId)
if (!userPushStore.getNotificationEnabledForDevice().first()) { if (!userPushStore.getNotificationEnabledForDevice().first()) {
// TODO We need to check if this is an incoming call // TODO We need to check if this is an incoming call
Timber.tag(loggerTag.value).i("Notification are disabled for this device, ignore push.") Timber.tag(loggerTag.value).i("Notification are disabled for this device, ignore push.")
@@ -44,7 +44,7 @@ class FirebaseNewTokenHandler @Inject constructor(
sessionStore.getAllSessions().toUserList() sessionStore.getAllSessions().toUserList()
.map { SessionId(it) } .map { SessionId(it) }
.forEach { userId -> .forEach { userId ->
val userDataStore = userPushStoreFactory.create(userId) val userDataStore = userPushStoreFactory.getOrCreate(userId)
if (userDataStore.getPushProviderName() == FirebaseConfig.NAME) { if (userDataStore.getPushProviderName() == FirebaseConfig.NAME) {
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client -> matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
pusherSubscriber.registerPusher(client, firebaseToken, FirebaseConfig.PUSHER_HTTP_URL) pusherSubscriber.registerPusher(client, firebaseToken, FirebaseConfig.PUSHER_HTTP_URL)
@@ -40,7 +40,7 @@ class UnifiedPushNewGatewayHandler @Inject constructor(
val userId = pushClientSecret.getUserIdFromSecret(clientSecret) ?: return Unit.also { val userId = pushClientSecret.getUserIdFromSecret(clientSecret) ?: return Unit.also {
Timber.w("Unable to retrieve session") Timber.w("Unable to retrieve session")
} }
val userDataStore = userPushStoreFactory.create(userId) val userDataStore = userPushStoreFactory.getOrCreate(userId)
if (userDataStore.getPushProviderName() == UnifiedPushConfig.NAME) { if (userDataStore.getPushProviderName() == UnifiedPushConfig.NAME) {
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client -> matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
pusherSubscriber.registerPusher(client, endpoint, pushGateway) pusherSubscriber.registerPusher(client, endpoint, pushGateway)
@@ -22,5 +22,5 @@ import io.element.android.libraries.matrix.api.core.SessionId
* Store data related to push about a user. * Store data related to push about a user.
*/ */
interface UserPushStoreFactory { interface UserPushStoreFactory {
fun create(userId: SessionId): UserPushStore fun getOrCreate(userId: SessionId): UserPushStore
} }
@@ -40,11 +40,11 @@ class DefaultUserPushStoreFactoryTest {
val userPushStoreFactory = DefaultUserPushStoreFactory(context, NoOpSessionObserver()) val userPushStoreFactory = DefaultUserPushStoreFactory(context, NoOpSessionObserver())
var userPushStore1: UserPushStore? = null var userPushStore1: UserPushStore? = null
val thread1 = thread { val thread1 = thread {
userPushStore1 = userPushStoreFactory.create(sessionId) userPushStore1 = userPushStoreFactory.getOrCreate(sessionId)
} }
var userPushStore2: UserPushStore? = null var userPushStore2: UserPushStore? = null
val thread2 = thread { val thread2 = thread {
userPushStore2 = userPushStoreFactory.create(sessionId) userPushStore2 = userPushStoreFactory.getOrCreate(sessionId)
} }
thread1.join() thread1.join()
thread2.join() thread2.join()
@@ -41,7 +41,7 @@ class DefaultUserPushStoreFactory @Inject constructor(
// We can have only one class accessing a single data store, so keep a cache of them. // We can have only one class accessing a single data store, so keep a cache of them.
private val cache = ConcurrentHashMap<SessionId, UserPushStore>() private val cache = ConcurrentHashMap<SessionId, UserPushStore>()
override fun create(userId: SessionId): UserPushStore { override fun getOrCreate(userId: SessionId): UserPushStore {
return cache.getOrPut(userId) { return cache.getOrPut(userId) {
UserPushStoreDataStore( UserPushStoreDataStore(
context = context, context = context,
@@ -60,6 +60,6 @@ class DefaultUserPushStoreFactory @Inject constructor(
override suspend fun onSessionDeleted(userId: String) { override suspend fun onSessionDeleted(userId: String) {
// Delete the store // Delete the store
create(SessionId(userId)).reset() getOrCreate(SessionId(userId)).reset()
} }
} }
@@ -21,7 +21,7 @@ import io.element.android.libraries.pushstore.api.UserPushStore
import io.element.android.libraries.pushstore.api.UserPushStoreFactory import io.element.android.libraries.pushstore.api.UserPushStoreFactory
class FakeUserPushStoreFactory : UserPushStoreFactory { class FakeUserPushStoreFactory : UserPushStoreFactory {
override fun create(userId: SessionId): UserPushStore { override fun getOrCreate(userId: SessionId): UserPushStore {
return FakeUserPushStore() return FakeUserPushStore()
} }
} }