From 7f57ef4177a1bef77e82cc6123aa9691edb56f89 Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Fri, 18 Jun 2021 15:57:46 +0530 Subject: Fix bug with tracker state --- .../privacycentralapp/dummy/TrackersDataSource.kt | 10 ++++++++++ .../features/trackers/TrackerAppsFragment.kt | 13 ++++++++++-- .../features/trackers/TrackersFeature.kt | 18 ++++++++++++++++- .../features/trackers/TrackersFragment.kt | 23 +++++++++++----------- .../features/trackers/TrackersViewModel.kt | 2 ++ 5 files changed, 51 insertions(+), 15 deletions(-) (limited to 'app/src') diff --git a/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt b/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt index 13def5a..7ba7c5b 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt @@ -72,6 +72,16 @@ object TrackersDataSource { this.blockerService = blockerInterface } + fun getTracker(name: String): Tracker? { + try { + return _trackers.value.first { + it.name == name + } + } catch (e: NoSuchElementException) { + return null + } + } + fun toggleTracker(tracker: Tracker, enable: Boolean): Boolean { val result = if (!enable) { blockerService.blockDomain(tracker.domain) diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsFragment.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsFragment.kt index 53c26a9..fff24dc 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsFragment.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsFragment.kt @@ -21,7 +21,7 @@ import android.os.Bundle import android.util.Log import android.view.View import android.widget.Toast -import androidx.fragment.app.activityViewModels +import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -35,7 +35,7 @@ class TrackerAppsFragment : NavToolbarFragment(R.layout.fragment_tracker_apps), MVIView { - private val viewModel: TrackersViewModel by activityViewModels() + private val viewModel: TrackersViewModel by viewModels() private val TAG = "TrackerAppsFragment" @@ -56,6 +56,15 @@ class TrackerAppsFragment : } } } + lifecycleScope.launchWhenStarted { + viewModel.submitAction( + TrackersFeature.Action.ObserveTracker( + requireArguments().getString( + "TRACKER" + ) + ) + ) + } } private fun displayToast(message: String) { diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFeature.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFeature.kt index ec7b67a..9400181 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFeature.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFeature.kt @@ -60,6 +60,7 @@ class TrackersFeature( val tracker: Tracker, val grant: Boolean ) : Action() + data class ObserveTracker(val tracker: String?) : Action() } sealed class Effect { @@ -67,6 +68,7 @@ class TrackersFeature( data class TrackerSelectedEffect(val tracker: Tracker) : Effect() data class TrackerToggleEffect(val result: Boolean) : Effect() data class ErrorEffect(val message: String) : Effect() + data class TrackerLoadedEffect(val tracker: Tracker) : Effect() } companion object { @@ -81,9 +83,11 @@ class TrackersFeature( is Effect.TrackerSelectedEffect -> state.copy(currentSelectedTracker = effect.tracker) is Effect.ErrorEffect -> state is Effect.TrackerToggleEffect -> { - Log.d("Tracker effect", "$state") state } + is Effect.TrackerLoadedEffect -> { + state.copy(currentSelectedTracker = effect.tracker) + } } }, actor = { state, action -> @@ -110,6 +114,18 @@ class TrackersFeature( flowOf(Effect.ErrorEffect("Can't toggle tracker")) } } + is Action.ObserveTracker -> { + if (action.tracker == null) { + flowOf(Effect.ErrorEffect("Null tracker id passed")) + } else { + val tracker = TrackersDataSource.getTracker(action.tracker) + if (tracker != null) { + flowOf(Effect.TrackerLoadedEffect(tracker)) + } else { + flowOf(Effect.ErrorEffect("Can't find tracker with name ${action.tracker}")) + } + } + } } }, singleEventProducer = { _, _, effect -> diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFragment.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFragment.kt index 00fefb6..e3dc941 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFragment.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersFragment.kt @@ -19,9 +19,10 @@ package foundation.e.privacycentralapp.features.trackers import android.os.Bundle import android.view.View -import androidx.fragment.app.activityViewModels +import androidx.core.os.bundleOf import androidx.fragment.app.add import androidx.fragment.app.commit +import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -34,7 +35,7 @@ class TrackersFragment : NavToolbarFragment(R.layout.fragment_trackers), MVIView { - private val viewModel: TrackersViewModel by activityViewModels() + private val viewModel: TrackersViewModel by viewModels() private lateinit var trackersAdapter: TrackersAdapter override fun onCreate(savedInstanceState: Bundle?) { @@ -50,7 +51,13 @@ class TrackersFragment : override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) trackersAdapter = TrackersAdapter { - viewModel.submitAction(TrackersFeature.Action.SetSelectedTracker(it)) + requireActivity().supportFragmentManager.commit { + val bundle = bundleOf("TRACKER" to it.name) + add(R.id.container, args = bundle) + setReorderingAllowed(true) + addToBackStack("trackers") + } + // viewModel.submitAction(TrackersFeature.Action.SetSelectedTracker(it)) } view.findViewById(R.id.recylcer_view_trackers)?.apply { layoutManager = LinearLayoutManager(requireContext()) @@ -62,15 +69,7 @@ class TrackersFragment : override fun getTitle() = getString(R.string.trackers) override fun render(state: TrackersFeature.State) { - if (state.currentSelectedTracker != null) { - requireActivity().supportFragmentManager.commit { - add(R.id.container) - setReorderingAllowed(true) - addToBackStack("trackers") - } - } else { - trackersAdapter.setData(state.trackers) - } + trackersAdapter.setData(state.trackers) } override fun actions(): Flow = viewModel.actions diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersViewModel.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersViewModel.kt index d75d6ec..ee89887 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersViewModel.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackersViewModel.kt @@ -17,6 +17,7 @@ package foundation.e.privacycentralapp.features.trackers +import android.util.Log import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import kotlinx.coroutines.flow.MutableSharedFlow @@ -33,6 +34,7 @@ class TrackersViewModel : ViewModel() { } fun submitAction(action: TrackersFeature.Action) { + Log.d("TrackersViewModel", "submitting action") viewModelScope.launch { _actions.emit(action) } -- cgit v1.2.1