Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: app stuck on setting up wire after account creation #2044

Merged
merged 2 commits into from
Sep 8, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@

package com.wire.kalium.logic.feature.call

import co.touchlab.stately.collections.ConcurrentMutableMap
import com.sun.jna.Pointer
import com.wire.kalium.calling.Calling
import com.wire.kalium.calling.ENVIRONMENT_DEFAULT
Expand All @@ -41,13 +40,15 @@ import com.wire.kalium.logic.featureFlags.KaliumConfigs
import com.wire.kalium.logic.util.CurrentPlatform
import com.wire.kalium.logic.util.PlatformContext
import com.wire.kalium.logic.util.PlatformType
import java.util.concurrent.ConcurrentHashMap
import java.util.concurrent.ConcurrentMap

actual class GlobalCallManager(
appContext: PlatformContext
) {

private val callManagerHolder: ConcurrentMutableMap<QualifiedID, CallManager> by lazy {
ConcurrentMutableMap()
private val callManagerHolder: ConcurrentMap<QualifiedID, CallManager> by lazy {
ConcurrentHashMap()
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in jvm we do not need to use statly since java already have ConcurrentHashMap

}

private val calling by lazy {
Expand Down Expand Up @@ -85,22 +86,22 @@ actual class GlobalCallManager(
conversationClientsInCallUpdater: ConversationClientsInCallUpdater,
kaliumConfigs: KaliumConfigs
): CallManager {
return callManagerHolder[userId] ?: CallManagerImpl(
calling = calling,
callRepository = callRepository,
userRepository = userRepository,
currentClientIdProvider = currentClientIdProvider,
selfConversationIdProvider = selfConversationIdProvider,
callMapper = callMapper,
messageSender = messageSender,
conversationRepository = conversationRepository,
federatedIdMapper = federatedIdMapper,
qualifiedIdMapper = qualifiedIdMapper,
videoStateChecker = videoStateChecker,
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
kaliumConfigs = kaliumConfigs
).also {
callManagerHolder[userId] = it
return callManagerHolder.computeIfAbsent(userId) {
CallManagerImpl(
calling = calling,
callRepository = callRepository,
userRepository = userRepository,
currentClientIdProvider = currentClientIdProvider,
selfConversationIdProvider = selfConversationIdProvider,
callMapper = callMapper,
messageSender = messageSender,
conversationRepository = conversationRepository,
federatedIdMapper = federatedIdMapper,
qualifiedIdMapper = qualifiedIdMapper,
videoStateChecker = videoStateChecker,
conversationClientsInCallUpdater = conversationClientsInCallUpdater,
kaliumConfigs = kaliumConfigs
)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.wire.kalium.logic.di

import co.touchlab.stately.collections.ConcurrentMutableMap
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.util.computeIfAbsent
import com.wire.kalium.logic.util.safeComputeIfAbsent
import com.wire.kalium.persistence.db.UserDatabaseBuilder
import com.wire.kalium.persistence.kmmSettings.UserPrefBuilder

Expand All @@ -31,7 +31,7 @@ abstract class UserStorageProvider {
userId: UserId,
platformUserStorageProperties: PlatformUserStorageProperties,
shouldEncryptData: Boolean = true
): UserStorage = inMemoryUserStorage.computeIfAbsent(userId) {
): UserStorage = inMemoryUserStorage.safeComputeIfAbsent(userId) {
create(userId, shouldEncryptData, platformUserStorageProperties)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import co.touchlab.stately.collections.ConcurrentMutableMap
import com.wire.kalium.logic.data.user.UserId
import com.wire.kalium.logic.di.UserStorageProvider
import com.wire.kalium.logic.feature.call.GlobalCallManager
import com.wire.kalium.logic.util.computeIfAbsent
import com.wire.kalium.logic.util.safeComputeIfAbsent

interface UserSessionScopeProvider {
fun get(userId: UserId): UserSessionScope?
Expand All @@ -41,7 +41,7 @@ abstract class UserSessionScopeProviderCommon(
}

override fun getOrCreate(userId: UserId): UserSessionScope =
userScopeStorage.computeIfAbsent(userId) {
userScopeStorage.safeComputeIfAbsent(userId) {
create(userId)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import com.wire.kalium.logic.feature.appVersioning.CheckIfUpdateRequiredUseCaseI
import com.wire.kalium.logic.feature.auth.sso.SSOLoginScope
import com.wire.kalium.logic.feature.auth.verification.RequestSecondFactorVerificationCodeUseCase
import com.wire.kalium.logic.feature.register.RegisterScope
import com.wire.kalium.logic.util.computeIfAbsent
import com.wire.kalium.logic.util.safeComputeIfAbsent
import com.wire.kalium.network.NetworkStateObserver
import com.wire.kalium.network.networkContainer.UnauthenticatedNetworkContainer
import com.wire.kalium.network.session.CertificatePinning
Expand All @@ -59,7 +59,7 @@ class AuthenticationScopeProvider internal constructor(
networkStateObserver: NetworkStateObserver,
certConfig: () -> CertificatePinning
): AuthenticationScope =
authenticationScopeStorage.computeIfAbsent(serverConfig to proxyCredentials) {
authenticationScopeStorage.safeComputeIfAbsent(serverConfig to proxyCredentials) {
AuthenticationScope(
userAgent,
serverConfig,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package com.wire.kalium.logic.util

import co.touchlab.stately.collections.ConcurrentMutableMap
import com.wire.kalium.logic.kaliumLogger
import kotlin.contracts.ExperimentalContracts
import kotlin.contracts.contract
import kotlin.time.Duration
Expand Down Expand Up @@ -86,7 +87,7 @@ fun Int?.isGreaterThan(other: Int?): Boolean {
}

// Convenience method to compute a value if absent in a "Stately" concurrent map
fun <K, V> ConcurrentMutableMap<K, V>.computeIfAbsent(key: K, f: () -> V): V {
fun <K, V> ConcurrentMutableMap<K, V>.safeComputeIfAbsent(key: K, f: () -> V): V {
return this.block {
if (this.containsKey(key)) return@block this[key]!!
val value = f()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* Wire
* Copyright (C) 2023 Wire Swiss GmbH
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see http://www.gnu.org/licenses/.
*/
package com.wire.kalium.logic.util

import co.touchlab.stately.collections.ConcurrentMutableMap
import kotlin.test.Test
import kotlin.test.assertEquals

class ConcurrentMutableMapTest {

@Test
fun givenConcurrentMap_whenSafeComputeIfAbsentIsCalledWith_thenTheSecondIsIgnored() {
val map = ConcurrentMutableMap<String, String>()
map.safeComputeIfAbsent("a") { "c" }
map.safeComputeIfAbsent("a") { "d" }
assertEquals("c", map["a"])
}
}