From e40866d03008ec6dafa0016a31ee4c9e820032df Mon Sep 17 00:00:00 2001 From: Amit Kumar Date: Wed, 16 Jun 2021 01:57:50 +0530 Subject: Fix UI bugs and reformat code --- .../e/privacycentralapp/dummy/DummyDataSource.kt | 1 - .../privacycentralapp/dummy/TrackersDataSource.kt | 10 +++--- .../features/dashboard/DashboardFeature.kt | 2 +- .../features/trackers/TrackerAppsAdapter.kt | 6 ++-- .../features/trackers/TrackerAppsFragment.kt | 38 +++++++++++++++------- .../features/trackers/TrackersFeature.kt | 6 ++-- .../features/trackers/TrackersFragment.kt | 8 ++--- .../features/trackers/TrackersViewModel.kt | 2 +- 8 files changed, 45 insertions(+), 28 deletions(-) (limited to 'app/src/main/java/foundation/e/privacycentralapp') diff --git a/app/src/main/java/foundation/e/privacycentralapp/dummy/DummyDataSource.kt b/app/src/main/java/foundation/e/privacycentralapp/dummy/DummyDataSource.kt index dd6112d..fe61354 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/dummy/DummyDataSource.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/dummy/DummyDataSource.kt @@ -227,5 +227,4 @@ object DummyDataSource { _appsUsingLocationPerm.value = _populatedPermissions.value[permissionId].packagesAllowed } } - } 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 9485a26..07822bb 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/dummy/TrackersDataSource.kt @@ -82,12 +82,14 @@ object TrackersDataSource { if (result) { _trackers.value = _trackers.value.map { if (it.name == tracker.name) { - it.copy(trackedApps = it.trackedApps.map { app -> - app.copy(isEnabled = enable) - }) + it.copy( + trackedApps = it.trackedApps.map { app -> + app.copy(isEnabled = enable) + } + ) } else it } } return result } -} \ No newline at end of file +} diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/dashboard/DashboardFeature.kt b/app/src/main/java/foundation/e/privacycentralapp/features/dashboard/DashboardFeature.kt index a273b88..c26fce1 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/dashboard/DashboardFeature.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/dashboard/DashboardFeature.kt @@ -164,7 +164,7 @@ class DashboardFeature( var activeTrackersCount: Int = 0 outer@ for (tracker in it) { for (app in tracker.trackedApps) { - if(!app.isEnabled) { + if (!app.isEnabled) { continue@outer } } diff --git a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsAdapter.kt b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsAdapter.kt index 04e3f04..ae236b9 100644 --- a/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsAdapter.kt +++ b/app/src/main/java/foundation/e/privacycentralapp/features/trackers/TrackerAppsAdapter.kt @@ -43,8 +43,10 @@ class TrackerAppsAdapter( val view = LayoutInflater.from(parent.context) .inflate(R.layout.item_app_toggle, parent, false) val holder = TrackerViewHolder(view) - holder.toggleBlocker.setOnCheckedChangeListener { _, isChecked -> - listener(tracker, isChecked) + holder.toggleBlocker.setOnClickListener { + if (it is Switch) { + listener(tracker, it.isChecked) + } } return holder } 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 67ae0cc..53c26a9 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 @@ -18,8 +18,10 @@ package foundation.e.privacycentralapp.features.trackers import android.os.Bundle +import android.util.Log +import android.view.View import android.widget.Toast -import androidx.fragment.app.viewModels +import androidx.fragment.app.activityViewModels import androidx.lifecycle.lifecycleScope import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView @@ -33,7 +35,9 @@ class TrackerAppsFragment : NavToolbarFragment(R.layout.fragment_tracker_apps), MVIView { - private val viewModel: TrackersViewModel by viewModels() + private val viewModel: TrackersViewModel by activityViewModels() + + private val TAG = "TrackerAppsFragment" override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) @@ -44,6 +48,11 @@ class TrackerAppsFragment : viewModel.trackersFeature.singleEvents.collect { event -> when (event) { is TrackersFeature.SingleEvent.ErrorEvent -> displayToast(event.error) + is TrackersFeature.SingleEvent.BlockerErrorEvent -> { + displayToast("Couldn't toggle") + // Re-render the current state to reset the switches. + render(viewModel.trackersFeature.state.value) + } } } } @@ -56,19 +65,24 @@ class TrackerAppsFragment : override fun getTitle(): String = getString(R.string.tracker) + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + view.findViewById(R.id.recylcer_view_tracker_apps)?.apply { + layoutManager = LinearLayoutManager(requireContext()) + setHasFixedSize(true) + } + } + override fun render(state: TrackersFeature.State) { + Log.d(TAG, "render() called with: state = $state") state.currentSelectedTracker?.let { tracker -> - view?.findViewById(R.id.recylcer_view_tracker_apps)?.apply { - layoutManager = LinearLayoutManager(requireContext()) - setHasFixedSize(true) - adapter = TrackerAppsAdapter(tracker) { tracker, grant -> - viewModel.submitAction( - TrackersFeature.Action.ToggleTrackerAction( - tracker, - grant - ) + view?.findViewById(R.id.recylcer_view_tracker_apps)?.adapter = TrackerAppsAdapter(tracker) { it, grant -> + viewModel.submitAction( + TrackersFeature.Action.ToggleTrackerAction( + it, + grant ) - } + ) } getToolbar()?.title = tracker.name } 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 f9fbf63..4a08328 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 @@ -40,7 +40,7 @@ class TrackersFeature( actor, reducer, coroutineScope, - { message -> Log.d("PermissionsFeature", message) }, + { message -> Log.d("TrackersFeature", message) }, singleEventProducer ) { data class State( @@ -112,7 +112,9 @@ class TrackersFeature( singleEventProducer = { _, _, effect -> when (effect) { is Effect.ErrorEffect -> SingleEvent.ErrorEvent(effect.message) - is Effect.TrackerToggleEffect -> SingleEvent.BlockerErrorEvent + is Effect.TrackerToggleEffect -> { + if (!effect.result) SingleEvent.BlockerErrorEvent else null + } else -> null } } 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 d0242d3..00fefb6 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,9 @@ package foundation.e.privacycentralapp.features.trackers import android.os.Bundle import android.view.View +import androidx.fragment.app.activityViewModels 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 +34,7 @@ class TrackersFragment : NavToolbarFragment(R.layout.fragment_trackers), MVIView { - private val viewModel: TrackersViewModel by viewModels() + private val viewModel: TrackersViewModel by activityViewModels() private lateinit var trackersAdapter: TrackersAdapter override fun onCreate(savedInstanceState: Bundle?) { @@ -59,9 +59,7 @@ class TrackersFragment : } } - override fun getTitle(): String { - return getString(R.string.trackers) - } + override fun getTitle() = getString(R.string.trackers) override fun render(state: TrackersFeature.State) { if (state.currentSelectedTracker != null) { 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 79ae146..d75d6ec 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 @@ -37,4 +37,4 @@ class TrackersViewModel : ViewModel() { _actions.emit(action) } } -} \ No newline at end of file +} -- cgit v1.2.1