From d2c01501a81f5221b2678d45c29bff281e08a914 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Tue, 6 Aug 2019 20:46:35 +0200 Subject: [PATCH] ConfigBuilderFragment -> kotlin --- .../androidaps/interfaces/PluginBase.java | 7 +- .../configBuilder/ConfigBuilderFragment.java | 180 ------------------ .../configBuilder/ConfigBuilderFragment.kt | 87 +++++++++ .../configBuilder/ConfigBuilderPlugin.java | 55 ++++++ .../androidaps/setupwizard/SWDefinition.java | 4 +- .../setupwizard/elements/SWPlugin.java | 2 +- .../res/layout/configbuilder_fragment.xml | 4 +- 7 files changed, 151 insertions(+), 188 deletions(-) delete mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.java create mode 100644 app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt diff --git a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java index a421147240..7b1d0c6175 100644 --- a/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java +++ b/app/src/main/java/info/nightscout/androidaps/interfaces/PluginBase.java @@ -8,6 +8,7 @@ import androidx.fragment.app.FragmentActivity; import info.nightscout.androidaps.R; import info.nightscout.androidaps.events.EventConfigBuilderChange; import info.nightscout.androidaps.events.EventRefreshGui; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.configBuilder.EventConfigBuilderUpdateGui; import info.nightscout.androidaps.utils.SP; import org.slf4j.Logger; @@ -64,7 +65,7 @@ public abstract class PluginBase { log.debug("First time HW pump allowed!"); }) .setNegativeButton(R.string.cancel, (dialog, id) -> { - MainApp.bus().post(new EventConfigBuilderUpdateGui()); + RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); if (L.isEnabled(L.PUMP)) log.debug("User does not allow switching to HW pump!"); }); @@ -78,11 +79,11 @@ public abstract class PluginBase { private void performPluginSwitch(boolean enabled, PluginType type) { setPluginEnabled(type, enabled); setFragmentVisible(type, enabled); - ConfigBuilderFragment.processOnEnabledCategoryChanged(this, getType()); + ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(this, getType()); ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled"); MainApp.bus().post(new EventRefreshGui()); MainApp.bus().post(new EventConfigBuilderChange()); - MainApp.bus().post(new EventConfigBuilderUpdateGui()); + RxBus.INSTANCE.send(new EventConfigBuilderUpdateGui()); ConfigBuilderPlugin.getPlugin().logPluginStatus(); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.java deleted file mode 100644 index e60d18e247..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.java +++ /dev/null @@ -1,180 +0,0 @@ -package info.nightscout.androidaps.plugins.configBuilder; - - -import android.app.Activity; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.Button; -import android.widget.LinearLayout; -import android.widget.ScrollView; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.annotation.StringRes; - -import com.squareup.otto.Subscribe; - -import java.util.ArrayList; -import java.util.List; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.interfaces.APSInterface; -import info.nightscout.androidaps.interfaces.BgSourceInterface; -import info.nightscout.androidaps.interfaces.ConstraintsInterface; -import info.nightscout.androidaps.interfaces.InsulinInterface; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.interfaces.ProfileInterface; -import info.nightscout.androidaps.interfaces.PumpInterface; -import info.nightscout.androidaps.interfaces.SensitivityInterface; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.insulin.InsulinOrefRapidActingPlugin; -import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; -import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; -import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; -import info.nightscout.androidaps.utils.FabricPrivacy; -import info.nightscout.androidaps.utils.PasswordProtection; - - -public class ConfigBuilderFragment extends SubscriberFragment { - - private List pluginViewHolders = new ArrayList<>(); - - @BindView(R.id.categories) - LinearLayout categories; - - @BindView(R.id.main_layout) - ScrollView mainLayout; - @BindView(R.id.unlock) - Button unlock; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - try { - View view = inflater.inflate(R.layout.configbuilder_fragment, container, false); - unbinder = ButterKnife.bind(this, view); - - if (PasswordProtection.isLocked("settings_password")) - mainLayout.setVisibility(View.GONE); - else unlock.setVisibility(View.GONE); - - createViews(); - - return view; - } catch (Exception e) { - FabricPrivacy.logException(e); - } - - return null; - } - - @OnClick(R.id.unlock) - void onClickUnlock() { - PasswordProtection.QueryPassword(getContext(), R.string.settings_password, "settings_password", () -> { - mainLayout.setVisibility(View.VISIBLE); - unlock.setVisibility(View.GONE); - }, null); - } - - @Override - protected void updateGUI() { - for (PluginViewHolder pluginViewHolder : pluginViewHolders) pluginViewHolder.update(); - } - - @Subscribe - public void on(EventConfigBuilderUpdateGui e) { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(this::updateGUI); - } - - private void createViews() { - createViewsForPlugins(R.string.configbuilder_profile, R.string.configbuilder_profile_description, PluginType.PROFILE, MainApp.getSpecificPluginsVisibleInListByInterface(ProfileInterface.class, PluginType.PROFILE)); - createViewsForPlugins(R.string.configbuilder_insulin, R.string.configbuilder_insulin_description, PluginType.INSULIN, MainApp.getSpecificPluginsVisibleInListByInterface(InsulinInterface.class, PluginType.INSULIN)); - createViewsForPlugins(R.string.configbuilder_bgsource, R.string.configbuilder_bgsource_description, PluginType.BGSOURCE, MainApp.getSpecificPluginsVisibleInListByInterface(BgSourceInterface.class, PluginType.BGSOURCE)); - createViewsForPlugins(R.string.configbuilder_pump, R.string.configbuilder_pump_description, PluginType.PUMP, MainApp.getSpecificPluginsVisibleInList(PluginType.PUMP)); - createViewsForPlugins(R.string.configbuilder_sensitivity, R.string.configbuilder_sensitivity_description, PluginType.SENSITIVITY, MainApp.getSpecificPluginsVisibleInListByInterface(SensitivityInterface.class, PluginType.SENSITIVITY)); - createViewsForPlugins(R.string.configbuilder_aps, R.string.configbuilder_aps_description, PluginType.APS, MainApp.getSpecificPluginsVisibleInList(PluginType.APS)); - createViewsForPlugins(R.string.configbuilder_loop, R.string.configbuilder_loop_description, PluginType.LOOP, MainApp.getSpecificPluginsVisibleInList(PluginType.LOOP)); - createViewsForPlugins(R.string.constraints, R.string.configbuilder_constraints_description, PluginType.CONSTRAINTS, MainApp.getSpecificPluginsVisibleInListByInterface(ConstraintsInterface.class, PluginType.CONSTRAINTS)); - createViewsForPlugins(R.string.configbuilder_treatments, R.string.configbuilder_treatments_description, PluginType.TREATMENT, MainApp.getSpecificPluginsVisibleInList(PluginType.TREATMENT)); - createViewsForPlugins(R.string.configbuilder_general, R.string.configbuilder_general_description, PluginType.GENERAL, MainApp.getSpecificPluginsVisibleInList(PluginType.GENERAL)); - } - - private void createViewsForPlugins(@StringRes int title, @StringRes int description, PluginType pluginType, List plugins) { - if (plugins.size() == 0) return; - LinearLayout parent = (LinearLayout) getLayoutInflater().inflate(R.layout.configbuilder_single_category, null); - ((TextView) parent.findViewById(R.id.category_title)).setText(MainApp.gs(title)); - ((TextView) parent.findViewById(R.id.category_description)).setText(MainApp.gs(description)); - LinearLayout pluginContainer = parent.findViewById(R.id.category_plugins); - for (PluginBase plugin: plugins) { - PluginViewHolder pluginViewHolder = new PluginViewHolder(this, pluginType, plugin); - pluginContainer.addView(pluginViewHolder.getBaseView()); - pluginViewHolders.add(pluginViewHolder); - } - categories.addView(parent); - } - - public static void processOnEnabledCategoryChanged(PluginBase changedPlugin, PluginType type) { - ArrayList pluginsInCategory = null; - switch (type) { - // Multiple selection allowed - case GENERAL: - case CONSTRAINTS: - case LOOP: - break; - // Single selection allowed - case INSULIN: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class); - break; - case SENSITIVITY: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); - break; - case APS: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class); - break; - case PROFILE: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); - break; - case BGSOURCE: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); - break; - case TREATMENT: - case PUMP: - pluginsInCategory = MainApp.getSpecificPluginsListByInterface(PumpInterface.class); - break; - } - if (pluginsInCategory != null) { - boolean newSelection = changedPlugin.isEnabled(type); - if (newSelection) { // new plugin selected -> disable others - for (PluginBase p : pluginsInCategory) { - if (p.getName().equals(changedPlugin.getName())) { - // this is new selected - } else { - p.setPluginEnabled(type, false); - p.setFragmentVisible(type, false); - } - } - } else { // enable first plugin in list - if (type == PluginType.PUMP) - VirtualPumpPlugin.getPlugin().setPluginEnabled(type, true); - else if (type == PluginType.INSULIN) - InsulinOrefRapidActingPlugin.getPlugin().setPluginEnabled(type, true); - else if (type == PluginType.SENSITIVITY) - SensitivityOref0Plugin.getPlugin().setPluginEnabled(type, true); - else if (type == PluginType.PROFILE) - NSProfilePlugin.getPlugin().setPluginEnabled(type, true); - else - pluginsInCategory.get(0).setPluginEnabled(type, true); - } - } - } - -} 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 new file mode 100644 index 0000000000..2e31bf9c59 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderFragment.kt @@ -0,0 +1,87 @@ +package info.nightscout.androidaps.plugins.configBuilder + + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.LinearLayout +import android.widget.TextView +import androidx.annotation.StringRes +import androidx.fragment.app.Fragment +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.interfaces.* +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.utils.FabricPrivacy +import info.nightscout.androidaps.utils.PasswordProtection +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import kotlinx.android.synthetic.main.configbuilder_fragment.* +import java.util.* + +class ConfigBuilderFragment : Fragment() { + + private var disposable: CompositeDisposable = CompositeDisposable() + private val pluginViewHolders = ArrayList() + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.configbuilder_fragment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + if (PasswordProtection.isLocked("settings_password")) + configbuilder_main_layout.visibility = View.GONE + else + unlock.visibility = View.GONE + + unlock.setOnClickListener { + PasswordProtection.QueryPassword(context, R.string.settings_password, "settings_password", { + configbuilder_main_layout.visibility = View.VISIBLE + unlock.visibility = View.GONE + }, null) + } + + createViews() + + disposable.add(RxBus + .toObservable(EventConfigBuilderUpdateGui::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + for (pluginViewHolder in pluginViewHolders) pluginViewHolder.update() + }, { + FabricPrivacy.logException(it) + })) + } + + private fun createViews() { + createViewsForPlugins(R.string.configbuilder_profile, R.string.configbuilder_profile_description, PluginType.PROFILE, MainApp.getSpecificPluginsVisibleInListByInterface(ProfileInterface::class.java, PluginType.PROFILE)) + createViewsForPlugins(R.string.configbuilder_insulin, R.string.configbuilder_insulin_description, PluginType.INSULIN, MainApp.getSpecificPluginsVisibleInListByInterface(InsulinInterface::class.java, PluginType.INSULIN)) + createViewsForPlugins(R.string.configbuilder_bgsource, R.string.configbuilder_bgsource_description, PluginType.BGSOURCE, MainApp.getSpecificPluginsVisibleInListByInterface(BgSourceInterface::class.java, PluginType.BGSOURCE)) + createViewsForPlugins(R.string.configbuilder_pump, R.string.configbuilder_pump_description, PluginType.PUMP, MainApp.getSpecificPluginsVisibleInList(PluginType.PUMP)) + createViewsForPlugins(R.string.configbuilder_sensitivity, R.string.configbuilder_sensitivity_description, PluginType.SENSITIVITY, MainApp.getSpecificPluginsVisibleInListByInterface(SensitivityInterface::class.java, PluginType.SENSITIVITY)) + createViewsForPlugins(R.string.configbuilder_aps, R.string.configbuilder_aps_description, PluginType.APS, MainApp.getSpecificPluginsVisibleInList(PluginType.APS)) + createViewsForPlugins(R.string.configbuilder_loop, R.string.configbuilder_loop_description, PluginType.LOOP, MainApp.getSpecificPluginsVisibleInList(PluginType.LOOP)) + createViewsForPlugins(R.string.constraints, R.string.configbuilder_constraints_description, PluginType.CONSTRAINTS, MainApp.getSpecificPluginsVisibleInListByInterface(ConstraintsInterface::class.java, PluginType.CONSTRAINTS)) + createViewsForPlugins(R.string.configbuilder_treatments, R.string.configbuilder_treatments_description, PluginType.TREATMENT, MainApp.getSpecificPluginsVisibleInList(PluginType.TREATMENT)) + createViewsForPlugins(R.string.configbuilder_general, R.string.configbuilder_general_description, PluginType.GENERAL, MainApp.getSpecificPluginsVisibleInList(PluginType.GENERAL)) + } + + private fun createViewsForPlugins(@StringRes title: Int, @StringRes description: Int, pluginType: PluginType, plugins: List) { + if (plugins.size == 0) return + val parent = layoutInflater.inflate(R.layout.configbuilder_single_category, null) as LinearLayout + (parent.findViewById(R.id.category_title) as TextView).text = MainApp.gs(title) + (parent.findViewById(R.id.category_description) as TextView).text = MainApp.gs(description) + val pluginContainer = parent.findViewById(R.id.category_plugins) + for (plugin in plugins) { + val pluginViewHolder = PluginViewHolder(this, pluginType, plugin) + pluginContainer.addView(pluginViewHolder.baseView) + pluginViewHolders.add(pluginViewHolder) + } + configbuilder_categories.addView(parent) + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java index 364e8dba81..b73c535147 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/configBuilder/ConfigBuilderPlugin.java @@ -21,6 +21,7 @@ import info.nightscout.androidaps.interfaces.PumpInterface; import info.nightscout.androidaps.interfaces.SensitivityInterface; import info.nightscout.androidaps.logging.L; import info.nightscout.androidaps.plugins.insulin.InsulinOrefRapidActingPlugin; +import info.nightscout.androidaps.plugins.profile.ns.NSProfilePlugin; import info.nightscout.androidaps.plugins.pump.virtual.VirtualPumpPlugin; import info.nightscout.androidaps.plugins.sensitivity.SensitivityOref0Plugin; import info.nightscout.androidaps.queue.CommandQueue; @@ -405,4 +406,58 @@ public class ConfigBuilderPlugin extends PluginBase { return found; } + public void processOnEnabledCategoryChanged(PluginBase changedPlugin, PluginType type) { + ArrayList pluginsInCategory = null; + switch (type) { + // Multiple selection allowed + case GENERAL: + case CONSTRAINTS: + case LOOP: + break; + // Single selection allowed + case INSULIN: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(InsulinInterface.class); + break; + case SENSITIVITY: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(SensitivityInterface.class); + break; + case APS: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(APSInterface.class); + break; + case PROFILE: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(ProfileInterface.class); + break; + case BGSOURCE: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(BgSourceInterface.class); + break; + case TREATMENT: + case PUMP: + pluginsInCategory = MainApp.getSpecificPluginsListByInterface(PumpInterface.class); + break; + } + if (pluginsInCategory != null) { + boolean newSelection = changedPlugin.isEnabled(type); + if (newSelection) { // new plugin selected -> disable others + for (PluginBase p : pluginsInCategory) { + if (p.getName().equals(changedPlugin.getName())) { + // this is new selected + } else { + p.setPluginEnabled(type, false); + p.setFragmentVisible(type, false); + } + } + } else { // enable first plugin in list + if (type == PluginType.PUMP) + VirtualPumpPlugin.getPlugin().setPluginEnabled(type, true); + else if (type == PluginType.INSULIN) + InsulinOrefRapidActingPlugin.getPlugin().setPluginEnabled(type, true); + else if (type == PluginType.SENSITIVITY) + SensitivityOref0Plugin.getPlugin().setPluginEnabled(type, true); + else if (type == PluginType.PROFILE) + NSProfilePlugin.getPlugin().setPluginEnabled(type, true); + else + pluginsInCategory.get(0).setPluginEnabled(type, true); + } + } + } } diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java index b39f2209b3..a66a2dc11c 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/SWDefinition.java @@ -169,7 +169,7 @@ public class SWDefinition { .action(() -> { NSClientPlugin.getPlugin().setPluginEnabled(PluginType.GENERAL, true); NSClientPlugin.getPlugin().setFragmentVisible(PluginType.GENERAL, true); - ConfigBuilderFragment.processOnEnabledCategoryChanged(NSClientPlugin.getPlugin(), PluginType.GENERAL); + ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(NSClientPlugin.getPlugin(), PluginType.GENERAL); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); MainApp.bus().post(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate(true)); @@ -386,7 +386,7 @@ public class SWDefinition { .action(() -> { LoopPlugin.getPlugin().setPluginEnabled(PluginType.LOOP, true); LoopPlugin.getPlugin().setFragmentVisible(PluginType.LOOP, true); - ConfigBuilderFragment.processOnEnabledCategoryChanged(LoopPlugin.getPlugin(), PluginType.LOOP); + ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(LoopPlugin.getPlugin(), PluginType.LOOP); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); MainApp.bus().post(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate(true)); diff --git a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java index 060102ba9c..8c0be61598 100644 --- a/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java +++ b/app/src/main/java/info/nightscout/androidaps/setupwizard/elements/SWPlugin.java @@ -86,7 +86,7 @@ public class SWPlugin extends SWItem { PluginBase plugin = (PluginBase) rb.getTag(); plugin.setPluginEnabled(pType, rb.isChecked()); plugin.setFragmentVisible(pType, rb.isChecked() && makeVisible); - ConfigBuilderFragment.processOnEnabledCategoryChanged(plugin, pType); + ConfigBuilderPlugin.getPlugin().processOnEnabledCategoryChanged(plugin, pType); ConfigBuilderPlugin.getPlugin().storeSettings("SetupWizard"); MainApp.bus().post(new EventConfigBuilderChange()); MainApp.bus().post(new EventSWUpdate()); diff --git a/app/src/main/res/layout/configbuilder_fragment.xml b/app/src/main/res/layout/configbuilder_fragment.xml index 75608be43a..954bc314a1 100644 --- a/app/src/main/res/layout/configbuilder_fragment.xml +++ b/app/src/main/res/layout/configbuilder_fragment.xml @@ -12,12 +12,12 @@ android:text="@string/unlock_settings" />