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 75f1d2fdc0
commit 8971a6c0fb
11 changed files with 13 additions and 13 deletions

View File

@@ -48,7 +48,7 @@ class NotificationSettingsPresenter @Inject constructor(
) : Presenter<NotificationSettingsState> {
@Composable
override fun present(): NotificationSettingsState {
val userPushStore = remember { userPushStoreFactory.create(matrixClient.sessionId) }
val userPushStore = remember { userPushStoreFactory.getOrCreate(matrixClient.sessionId) }
val systemNotificationsEnabled: MutableState<Boolean> = remember {
mutableStateOf(systemNotificationsEnabledProvider.notificationsEnabled())
}

View File

@@ -35,7 +35,7 @@ class DefaultGetCurrentPushProvider @Inject constructor(
.value
.navigationState
.currentSessionId()
?.let { pushStoreFactory.create(it) }
?.let { pushStoreFactory.getOrCreate(it) }
?.getPushProviderName()
}
}

View File

@@ -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.
*/
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()
if (currentPushProviderName != pushProvider.name) {
// Unregister previous one if any

View File

@@ -63,7 +63,7 @@ class PushersManager @Inject constructor(
* Register a pusher to the server if not done yet.
*/
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) {
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")

View File

@@ -122,7 +122,7 @@ class DefaultPushHandler @Inject constructor(
return
}
val userPushStore = userPushStoreFactory.create(userId)
val userPushStore = userPushStoreFactory.getOrCreate(userId)
if (!userPushStore.getNotificationEnabledForDevice().first()) {
// TODO We need to check if this is an incoming call
Timber.tag(loggerTag.value).i("Notification are disabled for this device, ignore push.")

View File

@@ -44,7 +44,7 @@ class FirebaseNewTokenHandler @Inject constructor(
sessionStore.getAllSessions().toUserList()
.map { SessionId(it) }
.forEach { userId ->
val userDataStore = userPushStoreFactory.create(userId)
val userDataStore = userPushStoreFactory.getOrCreate(userId)
if (userDataStore.getPushProviderName() == FirebaseConfig.NAME) {
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
pusherSubscriber.registerPusher(client, firebaseToken, FirebaseConfig.PUSHER_HTTP_URL)

View File

@@ -40,7 +40,7 @@ class UnifiedPushNewGatewayHandler @Inject constructor(
val userId = pushClientSecret.getUserIdFromSecret(clientSecret) ?: return Unit.also {
Timber.w("Unable to retrieve session")
}
val userDataStore = userPushStoreFactory.create(userId)
val userDataStore = userPushStoreFactory.getOrCreate(userId)
if (userDataStore.getPushProviderName() == UnifiedPushConfig.NAME) {
matrixAuthenticationService.restoreSession(userId).getOrNull()?.use { client ->
pusherSubscriber.registerPusher(client, endpoint, pushGateway)

View File

@@ -22,5 +22,5 @@ import io.element.android.libraries.matrix.api.core.SessionId
* Store data related to push about a user.
*/
interface UserPushStoreFactory {
fun create(userId: SessionId): UserPushStore
fun getOrCreate(userId: SessionId): UserPushStore
}

View File

@@ -40,11 +40,11 @@ class DefaultUserPushStoreFactoryTest {
val userPushStoreFactory = DefaultUserPushStoreFactory(context, NoOpSessionObserver())
var userPushStore1: UserPushStore? = null
val thread1 = thread {
userPushStore1 = userPushStoreFactory.create(sessionId)
userPushStore1 = userPushStoreFactory.getOrCreate(sessionId)
}
var userPushStore2: UserPushStore? = null
val thread2 = thread {
userPushStore2 = userPushStoreFactory.create(sessionId)
userPushStore2 = userPushStoreFactory.getOrCreate(sessionId)
}
thread1.join()
thread2.join()

View File

@@ -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.
private val cache = ConcurrentHashMap<SessionId, UserPushStore>()
override fun create(userId: SessionId): UserPushStore {
override fun getOrCreate(userId: SessionId): UserPushStore {
return cache.getOrPut(userId) {
UserPushStoreDataStore(
context = context,
@@ -60,6 +60,6 @@ class DefaultUserPushStoreFactory @Inject constructor(
override suspend fun onSessionDeleted(userId: String) {
// Delete the store
create(SessionId(userId)).reset()
getOrCreate(SessionId(userId)).reset()
}
}

View File

@@ -21,7 +21,7 @@ import io.element.android.libraries.pushstore.api.UserPushStore
import io.element.android.libraries.pushstore.api.UserPushStoreFactory
class FakeUserPushStoreFactory : UserPushStoreFactory {
override fun create(userId: SessionId): UserPushStore {
override fun getOrCreate(userId: SessionId): UserPushStore {
return FakeUserPushStore()
}
}