From 42614430391e3c28b6249564ce32f0f565ef8787 Mon Sep 17 00:00:00 2001 From: "ingvar.skogen" Date: Thu, 7 Mar 2024 21:17:54 +0100 Subject: [PATCH] Restart network config on the current network changed during update --- .../wallet/application/DeepLinkTest.kt | 11 +-- .../sharedPrefs/network/NetworkRepository.kt | 11 ++- .../network/NetworkRepositoryImpl.kt | 46 ++++++----- .../data/sharedPrefs/network/TariNetwork.kt | 6 +- .../wallet/ui/fragment/home/HomeActivity.kt | 76 ------------------- .../NetworkSelectionViewModel.kt | 10 +-- .../networkItem/NetworkTariViewHolder.kt | 2 +- .../networkItem/NetworkViewHolderItem.kt | 2 +- .../ui/fragment/splash/SplashActivity.kt | 8 ++ .../tari/android/wallet/util/DebugConfig.kt | 9 ++- .../backup/storage/NetworkRepositoryMock.kt | 17 ----- 11 files changed, 59 insertions(+), 139 deletions(-) delete mode 100644 app/src/test/java/com/tari/android/wallet/infrastructure/backup/storage/NetworkRepositoryMock.kt diff --git a/app/src/androidTest/java/com/tari/android/wallet/application/DeepLinkTest.kt b/app/src/androidTest/java/com/tari/android/wallet/application/DeepLinkTest.kt index b1f19b32a..a16e5a6c3 100644 --- a/app/src/androidTest/java/com/tari/android/wallet/application/DeepLinkTest.kt +++ b/app/src/androidTest/java/com/tari/android/wallet/application/DeepLinkTest.kt @@ -68,14 +68,11 @@ class DeepLinkTest { class NetworkRepositoryMock : NetworkRepository { private val network: Network = Network.STAGENET + private val tariNetwork = TariNetwork(network, "xtr", true) - override var supportedNetworks: List = listOf(network) - override var currentNetwork: TariNetwork? = TariNetwork(network, "") + override val defaultNetwork: TariNetwork = tariNetwork + override var supportedNetworks: List = listOf(tariNetwork) + override var currentNetwork: TariNetwork? = TariNetwork(network, "", true) override var ffiNetwork: Network? = network - override var incompatibleNetworkShown: Boolean = false - override var recommendedNetworks: List = listOf(network) - - override fun getAllNetworks(): List = listOf() - } } diff --git a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepository.kt b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepository.kt index f3d63763e..74b1d66f4 100644 --- a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepository.kt +++ b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepository.kt @@ -3,15 +3,18 @@ package com.tari.android.wallet.data.sharedPrefs.network import com.tari.android.wallet.application.Network interface NetworkRepository { - var supportedNetworks: List + // defaultNetwork is the network that will be used if the current network is not set or is not supported + val defaultNetwork: TariNetwork - var recommendedNetworks: List + var supportedNetworks: List var currentNetwork: TariNetwork? var ffiNetwork: Network? - var incompatibleNetworkShown : Boolean + fun isCurrentNetworkSupported(): Boolean = supportedNetworks.any { it.network == currentNetwork?.network } - fun getAllNetworks(): List + fun setDefaultNetworkAsCurrent() { + currentNetwork = defaultNetwork + } } \ No newline at end of file diff --git a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepositoryImpl.kt b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepositoryImpl.kt index 9ffced45f..233db052b 100644 --- a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepositoryImpl.kt +++ b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/NetworkRepositoryImpl.kt @@ -3,42 +3,40 @@ package com.tari.android.wallet.data.sharedPrefs.network import android.content.SharedPreferences import com.tari.android.wallet.application.Network import com.tari.android.wallet.data.repository.SimpleRepository -import com.tari.android.wallet.data.sharedPrefs.delegates.SharedPrefBooleanDelegate import com.tari.android.wallet.data.sharedPrefs.delegates.SharedPrefGsonDelegate +import com.tari.android.wallet.util.DebugConfig class NetworkRepositoryImpl(sharedPrefs: SharedPreferences) : NetworkRepository { - override var supportedNetworks: List = listOf(Network.NEXTNET) + override val defaultNetwork = if (DebugConfig.useStagenetNetwork) getStagenet() else getNextnet() - override var recommendedNetworks: List = listOf(Network.NEXTNET) + override var supportedNetworks: List = if (DebugConfig.useStagenetNetwork) listOf(getStagenet()) else listOf(getNextnet()) - override var currentNetwork by SharedPrefGsonDelegate(sharedPrefs, SimpleRepository(this), Keys.currentNetwork, TariNetwork::class.java) + override var currentNetwork by SharedPrefGsonDelegate( + prefs = sharedPrefs, + commonRepository = SimpleRepository(this), + name = Keys.CURRENT_NETWORK, + type = TariNetwork::class.java, + defValue = defaultNetwork, + ) - init { - try { - currentNetwork!!.network.displayName - } catch (e: Throwable) { - currentNetwork = getNextnet() - } - } - - override var ffiNetwork: Network? by SharedPrefGsonDelegate(sharedPrefs, SimpleRepository(this), formatKey(Keys.ffiNetwork), Network::class.java) - - override var incompatibleNetworkShown by SharedPrefBooleanDelegate(sharedPrefs, SimpleRepository(this), formatKey(Keys.networkIncompatible), false) - - override fun getAllNetworks(): List = listOf(getNextnet()) + override var ffiNetwork: Network? by SharedPrefGsonDelegate( + prefs = sharedPrefs, + commonRepository = SimpleRepository(this), + name = formatKey(Keys.FFI_NETWORK), + type = Network::class.java, + ) object Keys { - const val currentNetwork = "tari_current_network" - const val ffiNetwork = "ffi_tari_current_network" - const val networkIncompatible = "tari_network_incompatible_current_network" + const val CURRENT_NETWORK = "tari_current_network" + const val FFI_NETWORK = "ffi_tari_current_network" } companion object { - private const val mainNetThicker = "XTR" - private const val testNetThicker = "tXTR" + private const val TICKER_MAINNET = "XTR" + private const val TICKER_TESTNET = "tXTR" - fun getStagenet(): TariNetwork = TariNetwork(Network.STAGENET, testNetThicker) - fun getNextnet(): TariNetwork = TariNetwork(Network.NEXTNET, testNetThicker) + fun getStagenet(): TariNetwork = TariNetwork(Network.STAGENET, TICKER_TESTNET) + fun getNextnet(): TariNetwork = TariNetwork(Network.NEXTNET, TICKER_TESTNET, recommended = true) } } \ No newline at end of file diff --git a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/TariNetwork.kt b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/TariNetwork.kt index 5054234a6..d1aad0df5 100644 --- a/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/TariNetwork.kt +++ b/app/src/main/java/com/tari/android/wallet/data/sharedPrefs/network/TariNetwork.kt @@ -2,4 +2,8 @@ package com.tari.android.wallet.data.sharedPrefs.network import com.tari.android.wallet.application.Network -class TariNetwork(val network: Network, val ticker: String) \ No newline at end of file +data class TariNetwork( + val network: Network, + val ticker: String, + val recommended: Boolean = false, +) \ No newline at end of file diff --git a/app/src/main/java/com/tari/android/wallet/ui/fragment/home/HomeActivity.kt b/app/src/main/java/com/tari/android/wallet/ui/fragment/home/HomeActivity.kt index b8c589847..5bb3e9314 100644 --- a/app/src/main/java/com/tari/android/wallet/ui/fragment/home/HomeActivity.kt +++ b/app/src/main/java/com/tari/android/wallet/ui/fragment/home/HomeActivity.kt @@ -43,10 +43,8 @@ import androidx.activity.viewModels import androidx.fragment.app.Fragment import androidx.fragment.app.FragmentManager import androidx.lifecycle.Lifecycle -import androidx.lifecycle.lifecycleScope import androidx.viewpager2.adapter.FragmentStateAdapter import com.tari.android.wallet.R -import com.tari.android.wallet.application.MigrationManager import com.tari.android.wallet.application.deeplinks.DeeplinkHandler import com.tari.android.wallet.application.deeplinks.DeeplinkViewModel import com.tari.android.wallet.data.sharedPrefs.SharedPrefsRepository @@ -55,24 +53,14 @@ import com.tari.android.wallet.data.sharedPrefs.security.SecurityPrefRepository import com.tari.android.wallet.data.sharedPrefs.tariSettings.TariSettingsSharedRepository import com.tari.android.wallet.databinding.ActivityHomeBinding import com.tari.android.wallet.di.DiContainer.appComponent -import com.tari.android.wallet.extension.applyFontStyle import com.tari.android.wallet.extension.observe import com.tari.android.wallet.model.TxId import com.tari.android.wallet.service.connection.ServiceConnectionStatus import com.tari.android.wallet.service.service.WalletServiceLauncher import com.tari.android.wallet.ui.common.CommonActivity import com.tari.android.wallet.ui.common.domain.ResourceManager -import com.tari.android.wallet.ui.component.tari.TariFont -import com.tari.android.wallet.ui.dialog.modular.DialogArgs -import com.tari.android.wallet.ui.dialog.modular.ModularDialog -import com.tari.android.wallet.ui.dialog.modular.ModularDialogArgs -import com.tari.android.wallet.ui.dialog.modular.modules.body.BodyModule -import com.tari.android.wallet.ui.dialog.modular.modules.button.ButtonModule -import com.tari.android.wallet.ui.dialog.modular.modules.button.ButtonStyle -import com.tari.android.wallet.ui.dialog.modular.modules.head.HeadModule import com.tari.android.wallet.ui.extension.parcelable import com.tari.android.wallet.ui.extension.setVisible -import com.tari.android.wallet.ui.extension.string import com.tari.android.wallet.ui.fragment.auth.AuthActivity import com.tari.android.wallet.ui.fragment.chat_list.ChatListFragment import com.tari.android.wallet.ui.fragment.contact_book.root.ContactBookFragment @@ -83,7 +71,6 @@ import com.tari.android.wallet.ui.fragment.home.navigation.TariNavigator.Compani import com.tari.android.wallet.ui.fragment.home.navigation.TariNavigator.Companion.INDEX_HOME import com.tari.android.wallet.ui.fragment.home.navigation.TariNavigator.Companion.INDEX_SETTINGS import com.tari.android.wallet.ui.fragment.home.navigation.TariNavigator.Companion.NO_SMOOTH_SCROLL -import com.tari.android.wallet.ui.fragment.onboarding.activity.OnboardingFlowActivity import com.tari.android.wallet.ui.fragment.settings.allSettings.AllSettingsFragment import com.tari.android.wallet.ui.fragment.settings.themeSelector.TariTheme import com.tari.android.wallet.ui.fragment.splash.SplashActivity @@ -91,9 +78,6 @@ import com.tari.android.wallet.ui.fragment.store.StoreFragment import com.tari.android.wallet.ui.fragment.tx.HomeFragment import com.tari.android.wallet.util.Constants import com.tari.android.wallet.util.DebugConfig -import kotlinx.coroutines.Dispatchers -import kotlinx.coroutines.delay -import kotlinx.coroutines.launch import java.lang.ref.WeakReference import javax.inject.Inject @@ -117,9 +101,6 @@ class HomeActivity : CommonActivity() { @Inject lateinit var resourceManager: ResourceManager - @Inject - lateinit var migrationManager: MigrationManager - @Inject lateinit var tariSettingsRepository: TariSettingsSharedRepository @@ -201,12 +182,6 @@ class HomeActivity : CommonActivity() { } setupUi() subscribeUI() - lifecycleScope.launch(Dispatchers.IO) { - delay(3000) - launch(Dispatchers.Main) { - checkNetworkCompatibility() - } - } if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { requestPermissions(arrayOf(POST_NOTIFICATIONS), 0) @@ -309,57 +284,6 @@ class HomeActivity : CommonActivity() { view.setColorFilter(viewModel.paletteManager.getPurpleBrand(this)) } - private fun checkNetworkCompatibility() { - if (!networkRepository.supportedNetworks.contains(networkRepository.currentNetwork!!.network) && !networkRepository.incompatibleNetworkShown) { - networkRepository.incompatibleNetworkShown = true - displayIncompatibleNetworkDialog() - } - } - - private fun displayIncompatibleNetworkDialog() { - if (this.isFinishing) return - - val description = string(R.string.incompatible_network_description) - .applyFontStyle( - this@HomeActivity, - TariFont.AVENIR_LT_STD_MEDIUM, - listOf( - string(R.string.incompatible_network_description_bold_part_1), - string(R.string.incompatible_network_description_bold_part_2) - ), - TariFont.AVENIR_LT_STD_BLACK - ) - val dialog = ModularDialog(this) - val args = ModularDialogArgs( - DialogArgs(true, canceledOnTouchOutside = false), modules = listOf( - HeadModule(string(R.string.incompatible_network_title)), - BodyModule(null, description), - ButtonModule(string(R.string.incompatible_network_reset_now), ButtonStyle.Normal) { - deleteWallet() - dialog.dismiss() - }, - ButtonModule(string(R.string.incompatible_network_reset_later), ButtonStyle.Close) - ) - ) - dialog.applyArgs(args) - dialog.show() - } - - private fun deleteWallet() { - // delete wallet - goToSplashScreen() - lifecycleScope.launch(Dispatchers.IO) { - walletServiceLauncher.stopAndDelete() - } - } - - private fun goToSplashScreen() { - val intent = Intent(this, OnboardingFlowActivity::class.java) - intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK - startActivity(intent) - finishAffinity() - } - private fun checkScreensDeeplink(intent: Intent) { val screen = intent.getStringExtra(HomeDeeplinkScreens.Key) if (screen.orEmpty().isNotEmpty()) { diff --git a/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/NetworkSelectionViewModel.kt b/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/NetworkSelectionViewModel.kt index b14d7991e..f2fbbfba4 100644 --- a/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/NetworkSelectionViewModel.kt +++ b/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/NetworkSelectionViewModel.kt @@ -5,7 +5,6 @@ import androidx.lifecycle.MutableLiveData import com.tari.android.wallet.R import com.tari.android.wallet.application.WalletState import com.tari.android.wallet.data.WalletConfig -import com.tari.android.wallet.data.sharedPrefs.network.NetworkRepository import com.tari.android.wallet.data.sharedPrefs.network.TariNetwork import com.tari.android.wallet.di.DiContainer import com.tari.android.wallet.event.EventBus @@ -26,8 +25,8 @@ class NetworkSelectionViewModel : CommonViewModel() { @Inject lateinit var walletServiceLauncher: WalletServiceLauncher - private val _networks = MutableLiveData>() - val networks: LiveData> = _networks + private val _networks = MutableLiveData>() + val networks: LiveData> = _networks private val _recreate = SingleLiveEvent() val recreate: LiveData = _recreate @@ -38,10 +37,9 @@ class NetworkSelectionViewModel : CommonViewModel() { } private fun loadData() { - val networks = networkRepository.getAllNetworks() + val networks = networkRepository.supportedNetworks val currentNetwork = networkRepository.currentNetwork!!.network - val recommendedNetworks = networkRepository.recommendedNetworks - _networks.postValue(networks.map { NetworkViewHolderItem(it, recommendedNetworks.contains(it.network), currentNetwork) }.toMutableList()) + _networks.postValue(networks.map { NetworkViewHolderItem(it, currentNetwork) }) } fun selectNetwork(networkViewHolderItem: NetworkViewHolderItem) { diff --git a/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/networkItem/NetworkTariViewHolder.kt b/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/networkItem/NetworkTariViewHolder.kt index aae44734c..4505b3b8a 100644 --- a/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/networkItem/NetworkTariViewHolder.kt +++ b/app/src/main/java/com/tari/android/wallet/ui/fragment/settings/networkSelection/networkItem/NetworkTariViewHolder.kt @@ -11,7 +11,7 @@ class NetworkTariViewHolder(view: ItemNetworkBinding) : CommonViewHolder = listOf(network) - override var currentNetwork: TariNetwork? = TariNetwork(network, "xtr") - override var ffiNetwork: Network? = network - override var incompatibleNetworkShown: Boolean = false - override var recommendedNetworks: List = listOf(network) - - override fun getAllNetworks(): List = listOf() -} \ No newline at end of file