From 2c60ec44e516bd055028a1ffb12d72324662774b Mon Sep 17 00:00:00 2001 From: Andries Smit Date: Wed, 23 Mar 2022 11:56:46 +0100 Subject: [PATCH] feat: request protection from menu --- .../configBuilder/ConfigBuilderFragment.kt | 64 +++++++++++-------- .../maintenance/MaintenanceFragment.kt | 51 +++++++++------ .../profile/local/LocalProfileFragment.kt | 43 ++++++++----- 3 files changed, 94 insertions(+), 64 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt index 0bf7dcfc30..96095067d6 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt @@ -10,23 +10,26 @@ import android.widget.* import androidx.annotation.StringRes import androidx.core.content.ContextCompat import dagger.android.support.DaggerFragment -import info.nightscout.androidaps.interfaces.Config import info.nightscout.androidaps.R import info.nightscout.androidaps.activities.PreferencesActivity +import info.nightscout.androidaps.activities.SingleFragmentActivity import info.nightscout.androidaps.databinding.ConfigbuilderFragmentBinding import info.nightscout.androidaps.events.EventRebuildTabs -import info.nightscout.androidaps.interfaces.* +import info.nightscout.androidaps.extensions.toVisibility +import info.nightscout.androidaps.interfaces.ActivePlugin +import info.nightscout.androidaps.interfaces.Config +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginType import info.nightscout.androidaps.plugins.bus.RxBus import info.nightscout.androidaps.plugins.configBuilder.events.EventConfigBuilderUpdateGui import info.nightscout.androidaps.utils.FabricPrivacy -import io.reactivex.rxjava3.kotlin.plusAssign -import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.utils.buildHelper.BuildHelper import info.nightscout.androidaps.utils.protection.ProtectionCheck +import info.nightscout.androidaps.utils.protection.ProtectionCheck.Protection.PREFERENCES import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers import io.reactivex.rxjava3.disposables.CompositeDisposable -import java.util.* +import io.reactivex.rxjava3.kotlin.plusAssign import javax.inject.Inject class ConfigBuilderFragment : DaggerFragment() { @@ -44,48 +47,36 @@ class ConfigBuilderFragment : DaggerFragment() { private var disposable: CompositeDisposable = CompositeDisposable() private val pluginViewHolders = ArrayList() - + private var inMenu = false + private var queryingProtection = false private var _binding: ConfigbuilderFragmentBinding? = null - // This property is only valid between onCreateView and - // onDestroyView. + // This property is only valid between onCreateView and onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { _binding = ConfigbuilderFragmentBinding.inflate(inflater, container, false) return binding.root } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - - if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) - binding.mainLayout.visibility = View.GONE - else - binding.unlock.visibility = View.GONE - - binding.unlock.setOnClickListener { - activity?.let { activity -> - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, { - activity.runOnUiThread { - binding.mainLayout.visibility = View.VISIBLE - binding.unlock.visibility = View.GONE - } - }) - } - } + val parentClass = this.activity?.let { it::class.java } + inMenu = parentClass == SingleFragmentActivity::class.java + updateProtectedUi() + binding.unlock.setOnClickListener { queryProtection() } } @Synchronized override fun onResume() { super.onResume() + if (inMenu) queryProtection() else updateProtectedUi() disposable += rxBus .toObservable(EventConfigBuilderUpdateGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - for (pluginViewHolder in pluginViewHolders) pluginViewHolder.update() - }, fabricPrivacy::logException) + for (pluginViewHolder in pluginViewHolders) pluginViewHolder.update() + }, fabricPrivacy::logException) updateGUI() } @@ -215,4 +206,21 @@ class ConfigBuilderFragment : DaggerFragment() { return type == PluginType.GENERAL || type == PluginType.CONSTRAINTS || type == PluginType.LOOP } } + + private fun updateProtectedUi() { + val isLocked = protectionCheck.isLocked(PREFERENCES) + binding.mainLayout.visibility = isLocked.not().toVisibility() + binding.unlock.visibility = isLocked.toVisibility() + } + + private fun queryProtection() { + val isLocked = protectionCheck.isLocked(PREFERENCES) + if (isLocked && !queryingProtection) { + activity?.let { activity -> + queryingProtection = true + val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } } + protectionCheck.queryProtection(activity, PREFERENCES, doUpdate, doUpdate, doUpdate) + } + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt index 42ceaabc6c..c927072d83 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/maintenance/MaintenanceFragment.kt @@ -7,6 +7,7 @@ import android.view.View import android.view.ViewGroup import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.SingleFragmentActivity import info.nightscout.androidaps.dana.database.DanaHistoryDatabase import info.nightscout.androidaps.database.AppRepository import info.nightscout.androidaps.database.entities.UserEntry.Action @@ -14,6 +15,7 @@ import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.databinding.MaintenanceFragmentBinding import info.nightscout.androidaps.diaconn.database.DiaconnHistoryDatabase import info.nightscout.androidaps.events.EventPreferenceChange +import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.insight.database.InsightDatabase import info.nightscout.androidaps.interfaces.DataSyncSelector import info.nightscout.androidaps.interfaces.ImportExportPrefs @@ -28,6 +30,7 @@ import info.nightscout.androidaps.plugins.pump.omnipod.dash.history.database.Das import info.nightscout.androidaps.plugins.pump.omnipod.eros.history.database.ErosHistoryDatabase import info.nightscout.androidaps.utils.alertDialogs.OKDialog import info.nightscout.androidaps.utils.protection.ProtectionCheck +import info.nightscout.androidaps.utils.protection.ProtectionCheck.Protection.PREFERENCES import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers import io.reactivex.rxjava3.core.Completable.fromAction @@ -57,11 +60,11 @@ class MaintenanceFragment : DaggerFragment() { @Inject lateinit var overviewData: OverviewData private val compositeDisposable = CompositeDisposable() - + private var inMenu = false + private var queryingProtection = false private var _binding: MaintenanceFragmentBinding? = null - // This property is only valid between onCreateView and - // onDestroyView. + // This property is only valid between onCreateView and onDestroyView. private val binding get() = _binding!! override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { @@ -71,6 +74,9 @@ class MaintenanceFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + val parentClass = this.activity?.let { it::class.java } + inMenu = parentClass == SingleFragmentActivity::class.java + updateProtectedUi() binding.logSend.setOnClickListener { maintenancePlugin.sendLogs() } binding.logDelete.setOnClickListener { uel.log(Action.DELETE_LOGS, Sources.Maintenance) @@ -131,22 +137,12 @@ class MaintenanceFragment : DaggerFragment() { } } - if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) { - binding.mainLayout.visibility = View.GONE - } else { - binding.unlock.visibility = View.GONE - } + binding.unlock.setOnClickListener { queryProtection() } + } - binding.unlock.setOnClickListener { - activity?.let { activity -> - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, { - activity.runOnUiThread { - binding.mainLayout.visibility = View.VISIBLE - binding.unlock.visibility = View.GONE - } - }) - } - } + override fun onResume() { + super.onResume() + if (inMenu) queryProtection() else updateProtectedUi() } @Synchronized @@ -155,4 +151,21 @@ class MaintenanceFragment : DaggerFragment() { compositeDisposable.clear() _binding = null } -} \ No newline at end of file + + private fun updateProtectedUi() { + val isLocked = protectionCheck.isLocked(PREFERENCES) + binding.mainLayout.visibility = isLocked.not().toVisibility() + binding.unlock.visibility = isLocked.toVisibility() + } + + private fun queryProtection() { + val isLocked = protectionCheck.isLocked(PREFERENCES) + if (isLocked && !queryingProtection) { + activity?.let { activity -> + queryingProtection = true + val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } } + protectionCheck.queryProtection(activity, PREFERENCES, doUpdate, doUpdate, doUpdate) + } + } + } +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt index ce79ba80e5..2e43996c91 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/profile/local/LocalProfileFragment.kt @@ -12,12 +12,14 @@ import android.widget.ArrayAdapter import dagger.android.support.DaggerFragment import info.nightscout.androidaps.Constants import info.nightscout.androidaps.R +import info.nightscout.androidaps.activities.SingleFragmentActivity import info.nightscout.androidaps.data.ProfileSealed import info.nightscout.androidaps.database.entities.UserEntry.Action import info.nightscout.androidaps.database.entities.UserEntry.Sources import info.nightscout.androidaps.database.entities.ValueWithUnit import info.nightscout.androidaps.databinding.LocalprofileFragmentBinding import info.nightscout.androidaps.dialogs.ProfileSwitchDialog +import info.nightscout.androidaps.extensions.toVisibility import info.nightscout.androidaps.interfaces.ActivePlugin import info.nightscout.androidaps.interfaces.GlucoseUnit import info.nightscout.androidaps.interfaces.Profile @@ -56,7 +58,8 @@ class LocalProfileFragment : DaggerFragment() { @Inject lateinit var uel: UserEntryLogger private var disposable: CompositeDisposable = CompositeDisposable() - + private var inMenu = false + private var queryingProtection = false private var basalView: TimeListEdit? = null private val save = Runnable { @@ -99,6 +102,9 @@ class LocalProfileFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) + val parentClass = this.activity?.let { it::class.java } + inMenu = parentClass == SingleFragmentActivity::class.java + updateProtectedUi() // activate DIA tab processVisibilityOnClick(binding.diaTab) binding.diaPlaceholder.visibility = View.VISIBLE @@ -125,22 +131,7 @@ class LocalProfileFragment : DaggerFragment() { } binding.dia.editText?.id?.let { binding.diaLabel.labelFor = it } - if (protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES)) { - binding.mainLayout.visibility = View.GONE - } else { - binding.unlock.visibility = View.GONE - } - - binding.unlock.setOnClickListener { - activity?.let { activity -> - protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, { - activity.runOnUiThread { - binding.mainLayout.visibility = View.VISIBLE - binding.unlock.visibility = View.GONE - } - }) - } - } + binding.unlock.setOnClickListener { queryProtection() } } fun build() { @@ -345,6 +336,7 @@ class LocalProfileFragment : DaggerFragment() { @Synchronized override fun onResume() { super.onResume() + if (inMenu) queryProtection() else updateProtectedUi() disposable += rxBus .toObservable(EventLocalProfileChanged::class.java) .observeOn(aapsSchedulers.main) @@ -421,4 +413,21 @@ class LocalProfileFragment : DaggerFragment() { binding.basal.visibility = View.GONE binding.target.visibility = View.GONE } + + private fun updateProtectedUi() { + val isLocked = protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES) + binding.mainLayout.visibility = isLocked.not().toVisibility() + binding.unlock.visibility = isLocked.toVisibility() + } + + private fun queryProtection() { + val isLocked = protectionCheck.isLocked(ProtectionCheck.Protection.PREFERENCES) + if (isLocked && !queryingProtection) { + activity?.let { activity -> + queryingProtection = true + val doUpdate = { activity.runOnUiThread { queryingProtection = false; updateProtectedUi() } } + protectionCheck.queryProtection(activity, ProtectionCheck.Protection.PREFERENCES, doUpdate, doUpdate, doUpdate) + } + } + } }