Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit c0596ecc authored by Guillaume Jacquart's avatar Guillaume Jacquart
Browse files

Merge branch '1222-features_switch_wrong_state' into 'main'

1222: tight toggle feature to UI switch state.

See merge request !132
parents 5a432ecd d94e8043
Loading
Loading
Loading
Loading
Loading
+15 −6
Original line number Diff line number Diff line
@@ -73,16 +73,25 @@ class GetQuickPrivacyStateUseCase(

    val ipScramblingMode: Flow<InternetPrivacyMode> = localStateRepository.internetPrivacyMode

    fun toggleTrackers() {
        localStateRepository.setBlockTrackers(!localStateRepository.blockTrackers.value)
    fun toggleTrackers(enabled: Boolean?) {
        val value = enabled ?: !localStateRepository.blockTrackers.value
        if (value != localStateRepository.blockTrackers.value) {
            localStateRepository.setBlockTrackers(value)
        }
    }

    fun toggleLocation() {
        localStateRepository.setFakeLocationEnabled(!localStateRepository.fakeLocationEnabled.value)
    fun toggleLocation(enabled: Boolean?) {
        val value = enabled ?: !localStateRepository.fakeLocationEnabled.value
        if (value != localStateRepository.fakeLocationEnabled.value) {
            localStateRepository.setFakeLocationEnabled(value)
        }
    }

    fun toggleIpScrambling() {
        localStateRepository.setIpScramblingSetting(!localStateRepository.ipScramblingSetting.value)
    fun toggleIpScrambling(enabled: Boolean?) {
        val value = enabled ?: !localStateRepository.ipScramblingSetting.value
        if (value != localStateRepository.ipScramblingSetting.value) {
            localStateRepository.setIpScramblingSetting(value)
        }
    }

    val otherVpnRunning: SharedFlow<ApplicationDescription> = localStateRepository.otherVpnRunning
+12 −2
Original line number Diff line number Diff line
@@ -30,6 +30,7 @@ import foundation.e.privacymodules.permissions.data.ApplicationDescription
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.callbackFlow
@@ -75,8 +76,17 @@ class IpScramblingStateUseCase(
            }
        }

        coroutineScope.launch {
            internetPrivacyMode.collect { localStateRepository.internetPrivacyMode.value = it }
        coroutineScope.launch(Dispatchers.IO) {
            internetPrivacyMode.collect {
                if (
                    it == REAL_IP &&
                    localStateRepository.internetPrivacyMode.value == REAL_IP_LOADING
                ) {
                    // Wait for orbot to relax before allowing user to reactivate it.
                    delay(1000)
                }
                localStateRepository.internetPrivacyMode.value = it
            }
        }
    }

+18 −3
Original line number Diff line number Diff line
@@ -89,13 +89,25 @@ class DashboardFragment : NavToolbarFragment(R.layout.fragment_dashboard) {
            viewModel.submitAction(Action.ShowMostLeakedApp)
        }
        binding.toggleTrackers.setOnClickListener {
            viewModel.submitAction(Action.ToggleTrackers)
            viewModel.submitAction(
                Action.ToggleTrackers(
                    enabled = binding.toggleTrackers.isChecked
                )
            )
        }
        binding.toggleLocation.setOnClickListener {
            viewModel.submitAction(Action.ToggleLocation)
            viewModel.submitAction(
                Action.ToggleLocation(
                    enabled = binding.toggleLocation.isChecked
                )
            )
        }
        binding.toggleIpscrambling.setOnClickListener {
            viewModel.submitAction(Action.ToggleIpScrambling)
            viewModel.submitAction(
                Action.ToggleIpScrambling(
                    enabled = binding.toggleIpscrambling.isChecked
                )
            )
        }
        binding.myLocation.container.setOnClickListener {
            viewModel.submitAction(Action.ShowFakeMyLocationAction)
@@ -230,6 +242,9 @@ class DashboardFragment : NavToolbarFragment(R.layout.fragment_dashboard) {

        binding.toggleIpscrambling.isChecked = state.ipScramblingMode.isChecked
        val isLoading = state.ipScramblingMode.isLoading
        binding.toggleIpscrambling.isEnabled = (
            state.ipScramblingMode != InternetPrivacyMode.REAL_IP_LOADING
            )

        binding.stateIpAddress.text = getString(
            if (state.ipScramblingMode == InternetPrivacyMode.HIDE_IP) R.string.dashboard_state_ipaddress_on
+7 −6
Original line number Diff line number Diff line
@@ -89,13 +89,14 @@ class DashboardViewModel(
    fun submitAction(action: Action) = viewModelScope.launch {
        when (action) {
            is Action.ToggleTrackers -> {
                getPrivacyStateUseCase.toggleTrackers()
                getPrivacyStateUseCase.toggleTrackers(action.enabled)
                // Add delay here to prevent race condition with trackers state.
                delay(200)
                fetchStatistics().first()
            }
            is Action.ToggleLocation -> getPrivacyStateUseCase.toggleLocation()
            is Action.ToggleIpScrambling -> getPrivacyStateUseCase.toggleIpScrambling()
            is Action.ToggleLocation -> getPrivacyStateUseCase.toggleLocation(action.enabled)
            is Action.ToggleIpScrambling ->
                getPrivacyStateUseCase.toggleIpScrambling(action.enabled)
            is Action.ShowFakeMyLocationAction ->
                _singleEvents.emit(SingleEvent.NavigateToLocationSingleEvent)
            is Action.ShowAppsPermissions ->
@@ -146,9 +147,9 @@ class DashboardViewModel(
    }

    sealed class Action {
        object ToggleTrackers : Action()
        object ToggleLocation : Action()
        object ToggleIpScrambling : Action()
        data class ToggleTrackers(val enabled: Boolean) : Action()
        data class ToggleLocation(val enabled: Boolean) : Action()
        data class ToggleIpScrambling(val enabled: Boolean) : Action()
        object ShowFakeMyLocationAction : Action()
        object ShowInternetActivityPrivacyAction : Action()
        object ShowAppsPermissions : Action()
+9 −3
Original line number Diff line number Diff line
@@ -26,10 +26,15 @@ class WidgetCommandReceiver : BroadcastReceiver() {
    override fun onReceive(context: Context?, intent: Intent?) {
        val getQuickPrivacyStateUseCase = (context?.applicationContext as? AdvancedPrivacyApplication)?.dependencyContainer?.getQuickPrivacyStateUseCase

        val featureEnabled = intent?.extras?.let { bundle ->
            if (bundle.containsKey(PARAM_FEATURE_ENABLED))
                bundle.getBoolean(PARAM_FEATURE_ENABLED)
            else null
        }
        when (intent?.action) {
            ACTION_TOGGLE_TRACKERS -> getQuickPrivacyStateUseCase?.toggleTrackers()
            ACTION_TOGGLE_LOCATION -> getQuickPrivacyStateUseCase?.toggleLocation()
            ACTION_TOGGLE_IPSCRAMBLING -> getQuickPrivacyStateUseCase?.toggleIpScrambling()
            ACTION_TOGGLE_TRACKERS -> getQuickPrivacyStateUseCase?.toggleTrackers(featureEnabled)
            ACTION_TOGGLE_LOCATION -> getQuickPrivacyStateUseCase?.toggleLocation(featureEnabled)
            ACTION_TOGGLE_IPSCRAMBLING -> getQuickPrivacyStateUseCase?.toggleIpScrambling(featureEnabled)
            else -> {}
        }
    }
@@ -38,5 +43,6 @@ class WidgetCommandReceiver : BroadcastReceiver() {
        const val ACTION_TOGGLE_TRACKERS = "toggle_trackers"
        const val ACTION_TOGGLE_LOCATION = "toggle_location"
        const val ACTION_TOGGLE_IPSCRAMBLING = "toggle_ipscrambling"
        const val PARAM_FEATURE_ENABLED = "param_feature_enabled"
    }
}
Loading