Commit 2929abf1 authored by clairehurst's avatar clairehurst 🌱 Committed by Pier Angelo Vendrame
Browse files

fixup! [android] Implement Android-native Connection Assist UI

Bug 43576: Connection Assist on Android Fast Follows (Bug 41188)
Rename to regions (Bug 43593)
parent 1ea9b1f8
Loading
Loading
Loading
Loading
+17 −17
Original line number Diff line number Diff line
@@ -21,8 +21,8 @@ enum class ConnectAssistUiState(
    @StringRes val internetErrorDescription2: Int? = null,
    @StringRes val titleDescriptionTextStringResource: Int? = R.string.preferences_tor_network_settings_explanation,
    val quickstartSwitchVisible: Boolean,
    val countryDropDownVisible: Boolean,
    @StringRes val countryDropDownDefaultItem: Int = R.string.connection_assist_automatic_country_detection,
    val regionDropDownVisible: Boolean,
    @StringRes val regionDropDownDefaultItem: Int = R.string.connection_assist_automatic_country_detection,
    val torBootstrapButton1Visible: Boolean,
    @StringRes val torBootstrapButton1TextStringResource: Int = R.string.tor_bootstrap_connect,
    val torBootstrapButton1ShouldTryABridge: Boolean = false,
@@ -41,7 +41,7 @@ enum class ConnectAssistUiState(
        titleLargeTextViewVisible = false,
        titleDescriptionVisible = false,
        quickstartSwitchVisible = false,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = false,
        wordmarkLogoVisible = true,
@@ -57,7 +57,7 @@ enum class ConnectAssistUiState(
        titleDescriptionVisible = true,
        titleDescriptionTextStringResource = R.string.preferences_tor_network_settings_explanation,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = true,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.connection_assist_configure_connection_button,
@@ -74,7 +74,7 @@ enum class ConnectAssistUiState(
        titleDescriptionVisible = true,
        titleDescriptionTextStringResource = R.string.preferences_tor_network_settings_explanation,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -94,7 +94,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription = R.string.connection_assist_internet_error_description,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = true,
        torBootstrapButton1TextStringResource = R.string.connection_assist_internet_error_try_again,
        torBootstrapButton2Visible = true,
@@ -114,7 +114,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription = R.string.connection_assist_internet_error_description,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -134,7 +134,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription = R.string.connection_assist_try_a_bridge_description,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = true,
        regionDropDownVisible = true,
        torBootstrapButton1Visible = true,
        torBootstrapButton1TextStringResource = R.string.connection_assist_try_a_bridge_button,
        torBootstrapButton1ShouldTryABridge = true,
@@ -155,7 +155,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription = ChooseRegion.internetErrorDescription,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -177,8 +177,8 @@ enum class ConnectAssistUiState(
        internetErrorDescription2 = R.string.connection_assist_select_country_try_again,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = true,
        countryDropDownDefaultItem = R.string.connection_assist_select_country_or_region,
        regionDropDownVisible = true,
        regionDropDownDefaultItem = R.string.connection_assist_select_country_or_region,
        torBootstrapButton1Visible = true,
        torBootstrapButton1TextStringResource = R.string.connection_assist_try_a_bridge_button,
        torBootstrapButton1ShouldTryABridge = true,
@@ -201,7 +201,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription2 = RegionNotFound.internetErrorDescription2,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -223,8 +223,8 @@ enum class ConnectAssistUiState(
        internetErrorDescription2 = R.string.connection_assist_select_country_try_again,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = true,
        countryDropDownDefaultItem = R.string.connection_assist_select_country_or_region,
        regionDropDownVisible = true,
        regionDropDownDefaultItem = R.string.connection_assist_select_country_or_region,
        torBootstrapButton1Visible = true,
        torBootstrapButton1TextStringResource = R.string.connection_assist_try_a_bridge_button,
        torBootstrapButton1ShouldTryABridge = true,
@@ -247,7 +247,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription2 = ConfirmRegion.internetErrorDescription2,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -268,7 +268,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription2 = R.string.connection_assist_select_country_try_again,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = true,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = false,
        torBootstrapButton2Visible = true,
        torBootstrapButton2TextStringResource = R.string.btn_cancel,
@@ -289,7 +289,7 @@ enum class ConnectAssistUiState(
        internetErrorDescription1 = R.string.connection_assist_final_error_troubleshoot_connection_link,
        titleDescriptionTextStringResource = null,
        quickstartSwitchVisible = false,
        countryDropDownVisible = false,
        regionDropDownVisible = false,
        torBootstrapButton1Visible = true,
        torBootstrapButton1TextStringResource = R.string.connection_assist_configure_connection_button,
        torBootstrapButton1ShouldOpenSettings = true,
+9 −9
Original line number Diff line number Diff line
@@ -60,7 +60,7 @@ class TorConnectionAssistFragment : Fragment(), UserInteractionHandler {
            override fun onReceive(context: Context, intent: Intent) {
                if (intent.action === Intent.ACTION_LOCALE_CHANGED) {
                    Log.v("LocaleReceiver", "received ACTION_LOCALE_CHANGED")
                    torConnectionAssistViewModel.fetchCountryNamesGet()
                    torConnectionAssistViewModel.fetchRegionNames()
                }
            }
        }
@@ -204,14 +204,14 @@ class TorConnectionAssistFragment : Fragment(), UserInteractionHandler {
    }

    private fun setCountryDropDown(screen: ConnectAssistUiState) {
        if (screen.countryDropDownVisible) {
        if (screen.regionDropDownVisible) {
            val spinnerAdapter: ArrayAdapter<String> = initializeSpinner()
            if (binding.countryDropDown.isEmpty()) {
                populateCountryDropDown(spinnerAdapter)
                populateRegionDropDown(spinnerAdapter)
                setOnItemSelectedListener()
            }

            setFirstItemInCountryDropDown(spinnerAdapter, getString(screen.countryDropDownDefaultItem))
            setFirstItemInCountryDropDown(spinnerAdapter, getString(screen.regionDropDownDefaultItem))

            if (screen == ConnectAssistUiState.ChooseRegion || screen == ConnectAssistUiState.ConfirmRegion || screen == ConnectAssistUiState.RegionNotFound) {
                torConnectionAssistViewModel.selectDefaultRegion()
@@ -248,15 +248,15 @@ class TorConnectionAssistFragment : Fragment(), UserInteractionHandler {
        return spinnerAdapter
    }

    private fun populateCountryDropDown(spinnerAdapter: ArrayAdapter<String>) {
        torConnectionAssistViewModel.fetchCountryNamesGet()
    private fun populateRegionDropDown(spinnerAdapter: ArrayAdapter<String>) {
        torConnectionAssistViewModel.fetchRegionNames()
        viewLifecycleOwner.lifecycleScope.launch {
            repeatOnLifecycle(Lifecycle.State.STARTED) {
                torConnectionAssistViewModel.countryCodeNameMap.collect {
                    Log.d(TAG, "countryCodeNameMap: $it")
                torConnectionAssistViewModel.regionCodeNameMap.collect {
                    Log.d(TAG, "regionCodeNameMap: $it")
                    if (it != null) {
                        spinnerAdapter.clear()
                        spinnerAdapter.add(getString(torConnectionAssistViewModel.torConnectScreen.value.countryDropDownDefaultItem))
                        spinnerAdapter.add(getString(torConnectionAssistViewModel.torConnectScreen.value.regionDropDownDefaultItem))
                        spinnerAdapter.addAll(it.values)
                    }
                }
+9 −9
Original line number Diff line number Diff line
@@ -47,15 +47,15 @@ class TorConnectionAssistViewModel(
        components.useCases.tabsUseCases.removeTab.invoke(components.core.store.state.tabs.find {it.getUrl() == "about:"}?.id ?: "")
    }

    fun fetchCountryNamesGet() {
        torAndroidIntegration.countryNamesGet { countryNames : GeckoBundle? ->
            if (countryNames != null) {
                val codes: Array<String> = countryNames.keys()
    fun fetchRegionNames() {
        torAndroidIntegration.regionNamesGet { regionNames : GeckoBundle? ->
            if (regionNames != null) {
                val codes: Array<String> = regionNames.keys()
                val regions = mutableMapOf<String, String>()
                for (code in codes) {
                    regions[code] = countryNames.getString(code)
                    regions[code] = regionNames.getString(code)
                }
                countryCodeNameMap.value = regions
                regionCodeNameMap.value = regions
            }
        }
    }
@@ -73,7 +73,7 @@ class TorConnectionAssistViewModel(
    private val _torConnectScreen = MutableStateFlow(ConnectAssistUiState.Loading)
    internal val torConnectScreen: StateFlow<ConnectAssistUiState> = _torConnectScreen

    val countryCodeNameMap: MutableStateFlow<Map<String, String>?> by lazy {
    val regionCodeNameMap: MutableStateFlow<Map<String, String>?> by lazy {
        MutableStateFlow(null)
    }

@@ -87,7 +87,7 @@ class TorConnectionAssistViewModel(

    fun setCountryCodeToSelectedItem(position: Int) {
        selectedCountryCode.value =
            countryCodeNameMap.value?.keys?.toList()
            regionCodeNameMap.value?.keys?.toList()
                ?.getOrNull(position - 1) ?: "automatic"
        // position - 1 since we have the default/first value of automatic
        Log.d(TAG, "selectedCountryCode = ${selectedCountryCode.value}")
@@ -171,6 +171,6 @@ class TorConnectionAssistViewModel(
    ) {}

    fun button1ShouldBeDisabled(screen: ConnectAssistUiState): Boolean {
        return selectedCountryCode.value == "automatic" && screen.countryDropDownDefaultItem == R.string.connection_assist_select_country_or_region
        return selectedCountryCode.value == "automatic" && screen.regionDropDownDefaultItem == R.string.connection_assist_select_country_or_region
    }
}