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

feat: restart network config on the current network changed during update #1067

Merged
merged 1 commit into from
Mar 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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 @@ -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<Network> = listOf(network)
override var currentNetwork: TariNetwork? = TariNetwork(network, "")
override val defaultNetwork: TariNetwork = tariNetwork
override var supportedNetworks: List<TariNetwork> = listOf(tariNetwork)
override var currentNetwork: TariNetwork? = TariNetwork(network, "", true)
override var ffiNetwork: Network? = network
override var incompatibleNetworkShown: Boolean = false
override var recommendedNetworks: List<Network> = listOf(network)

override fun getAllNetworks(): List<TariNetwork> = listOf()

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,18 @@ package com.tari.android.wallet.data.sharedPrefs.network
import com.tari.android.wallet.application.Network

interface NetworkRepository {
var supportedNetworks: List<Network>
// 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<Network>
var supportedNetworks: List<TariNetwork>

var currentNetwork: TariNetwork?

var ffiNetwork: Network?

var incompatibleNetworkShown : Boolean
fun isCurrentNetworkSupported(): Boolean = supportedNetworks.any { it.network == currentNetwork?.network }

fun getAllNetworks(): List<TariNetwork>
fun setDefaultNetworkAsCurrent() {
currentNetwork = defaultNetwork
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<Network> = listOf(Network.NEXTNET)
override val defaultNetwork = if (DebugConfig.useStagenetNetwork) getStagenet() else getNextnet()

override var recommendedNetworks: List<Network> = listOf(Network.NEXTNET)
override var supportedNetworks: List<TariNetwork> = 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<TariNetwork> = 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)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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)
data class TariNetwork(
val network: Network,
val ticker: String,
val recommended: Boolean = false,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -83,17 +71,13 @@ 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
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

Expand All @@ -117,9 +101,6 @@ class HomeActivity : CommonActivity<ActivityHomeBinding, HomeViewModel>() {
@Inject
lateinit var resourceManager: ResourceManager

@Inject
lateinit var migrationManager: MigrationManager

@Inject
lateinit var tariSettingsRepository: TariSettingsSharedRepository

Expand Down Expand Up @@ -201,12 +182,6 @@ class HomeActivity : CommonActivity<ActivityHomeBinding, HomeViewModel>() {
}
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)
Expand Down Expand Up @@ -309,57 +284,6 @@ class HomeActivity : CommonActivity<ActivityHomeBinding, HomeViewModel>() {
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()) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -26,8 +25,8 @@ class NetworkSelectionViewModel : CommonViewModel() {
@Inject
lateinit var walletServiceLauncher: WalletServiceLauncher

private val _networks = MutableLiveData<MutableList<CommonViewHolderItem>>()
val networks: LiveData<MutableList<CommonViewHolderItem>> = _networks
private val _networks = MutableLiveData<List<CommonViewHolderItem>>()
val networks: LiveData<List<CommonViewHolderItem>> = _networks

private val _recreate = SingleLiveEvent<Unit>()
val recreate: LiveData<Unit> = _recreate
Expand All @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ class NetworkTariViewHolder(view: ItemNetworkBinding) : CommonViewHolder<Network
override fun bind(item: NetworkViewHolderItem) {
super.bind(item)

val recommendedText = if (item.isRecommended) " " + itemView.context.getString(R.string.all_settings_select_network_recommended) else ""
val recommendedText = if (item.network.recommended) " " + itemView.context.getString(R.string.all_settings_select_network_recommended) else ""
val networkText = item.network.network.displayName + recommendedText
ui.tvName.text = networkText

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import com.tari.android.wallet.application.Network
import com.tari.android.wallet.data.sharedPrefs.network.TariNetwork
import com.tari.android.wallet.ui.common.recyclerView.CommonViewHolderItem

class NetworkViewHolderItem(val network: TariNetwork, val isRecommended: Boolean, var currentNetwork: Network) : CommonViewHolderItem() {
class NetworkViewHolderItem(val network: TariNetwork, var currentNetwork: Network) : CommonViewHolderItem() {
override val viewHolderUUID: String = "NetworkViewHolderItem$network"
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import androidx.activity.addCallback
import androidx.appcompat.app.AppCompatActivity
import com.tari.android.wallet.data.WalletConfig
import com.tari.android.wallet.data.sharedPrefs.SharedPrefsRepository
import com.tari.android.wallet.data.sharedPrefs.network.NetworkRepository
import com.tari.android.wallet.data.sharedPrefs.security.SecurityPrefRepository
import com.tari.android.wallet.di.DiContainer.appComponent
import com.tari.android.wallet.service.service.WalletServiceLauncher
Expand All @@ -63,6 +64,9 @@ class SplashActivity : AppCompatActivity() {
@Inject
lateinit var securityPrefRepository: SecurityPrefRepository

@Inject
lateinit var networkRepository: NetworkRepository

@Inject
lateinit var walletServiceLauncher: WalletServiceLauncher

Expand All @@ -76,6 +80,10 @@ class SplashActivity : AppCompatActivity() {
walletServiceLauncher.stopAndDelete()
}

if (!networkRepository.isCurrentNetworkSupported()) {
networkRepository.setDefaultNetworkAsCurrent()
}

val exists = WalletUtil.walletExists(walletConfig) && sharedPrefsRepository.onboardingAuthSetupCompleted
if (WalletUtil.walletExists(walletConfig) && !sharedPrefsRepository.onboardingAuthSetupCompleted) {
// in cases interrupted restoration
Expand Down
9 changes: 7 additions & 2 deletions app/src/main/java/com/tari/android/wallet/util/DebugConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,22 @@ import yat.android.lib.YatIntegration
/**
* Constants used for developing and debugging.
*/
@Suppress("ConstPropertyName", "KotlinConstantConditions")
object DebugConfig {

private const val _mockedTurned = false
val mockedDataEnabled = _mockedTurned && isDebug() // TODO split this flag to multiple different types of mocked data

val isChatEnabled = false
const val isChatEnabled = false

private const val _useYatSandbox = false
val yatEnvironment = if (_useYatSandbox && isDebug()) YatEnvironment.SANDBOX else YatEnvironment.PRODUCTION

val isBlockExplorerEnabled = false
const val isBlockExplorerEnabled = false

// Needed for testing resetting wallet on the network changed
private const val _useStagenetNetwork = false
val useStagenetNetwork = _useStagenetNetwork && isDebug()

private fun isDebug() = BuildConfig.BUILD_TYPE == "debug"
}
Expand Down

This file was deleted.