diff options
author | Amit Kumar <amitkma@e.email> | 2021-06-18 15:57:46 +0530 |
---|---|---|
committer | Amit Kumar <amitkma@e.email> | 2021-06-18 15:57:46 +0530 |
commit | 7f57ef4177a1bef77e82cc6123aa9691edb56f89 (patch) | |
tree | b9c2efc2d1389331b49724b8bd5731b750e71a09 | |
parent | c5c030693c40c912881feb5ecca5913a83a4bb55 (diff) |
Fix bug with tracker state
6 files changed, 52 insertions, 16 deletions
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<TrackersFeature.State, TrackersFeature.Action> { - 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<TrackersFeature.State, TrackersFeature.Action> { - 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<TrackerAppsFragment>(R.id.container, args = bundle) + setReorderingAllowed(true) + addToBackStack("trackers") + } + // viewModel.submitAction(TrackersFeature.Action.SetSelectedTracker(it)) } view.findViewById<RecyclerView>(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<TrackerAppsFragment>(R.id.container) - setReorderingAllowed(true) - addToBackStack("trackers") - } - } else { - trackersAdapter.setData(state.trackers) - } + trackersAdapter.setData(state.trackers) } override fun actions(): Flow<TrackersFeature.Action> = 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) } diff --git a/flow-mvi/src/main/java/foundation/e/flowmvi/feature/BaseFeature.kt b/flow-mvi/src/main/java/foundation/e/flowmvi/feature/BaseFeature.kt index 8dec0c4..c60373f 100644 --- a/flow-mvi/src/main/java/foundation/e/flowmvi/feature/BaseFeature.kt +++ b/flow-mvi/src/main/java/foundation/e/flowmvi/feature/BaseFeature.kt @@ -120,7 +120,7 @@ open class BaseFeature<State : Any, in Action : Any, in Effect : Any, SingleEven ) { onEach { action -> callerCoroutineScope.launch { - logger.invoke("Received action $action") + logger.invoke("Received action $action $this") actor.invoke(_state.value, action) .onEach { effect -> mutex.withLock { |