PluginViewHolder -> kotlin

This commit is contained in:
Milos Kozak 2019-08-06 20:27:17 +02:00
parent 9b6312a495
commit 63e45fd549
9 changed files with 122 additions and 149 deletions

View file

@ -45,39 +45,44 @@ public abstract class PluginBase {
// Default always calls invoke
// Plugins that have special constraints if they get switched to may override this method
public void switchAllowed(boolean newState, FragmentActivity activity) {
performPluginSwitch(newState);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
performPluginSwitch(newState, type);
}
protected void confirmPumpPluginActivation(boolean newState, FragmentActivity activity) {
boolean allowHardwarePump = SP.getBoolean("allow_hardware_pump", false);
if (allowHardwarePump || activity == null) {
performPluginSwitch(newState);
protected void confirmPumpPluginActivation(boolean newState, FragmentActivity activity, PluginType type) {
if (type == PluginType.PUMP) {
boolean allowHardwarePump = SP.getBoolean("allow_hardware_pump", false);
if (allowHardwarePump || activity == null) {
performPluginSwitch(newState, type);
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, (dialog, id) -> {
performPluginSwitch(newState, type);
SP.putBoolean("allow_hardware_pump", true);
if (L.isEnabled(L.PUMP))
log.debug("First time HW pump allowed!");
})
.setNegativeButton(R.string.cancel, (dialog, id) -> {
MainApp.bus().post(new EventConfigBuilderUpdateGui());
if (L.isEnabled(L.PUMP))
log.debug("User does not allow switching to HW pump!");
});
builder.create().show();
}
} else {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage(R.string.allow_hardware_pump_text)
.setPositiveButton(R.string.yes, (dialog, id) -> {
performPluginSwitch(newState);
SP.putBoolean("allow_hardware_pump", true);
if (L.isEnabled(L.PUMP))
log.debug("First time HW pump allowed!");
})
.setNegativeButton(R.string.cancel, (dialog, id) -> {
MainApp.bus().post(new EventConfigBuilderUpdateGui());
if (L.isEnabled(L.PUMP))
log.debug("User does not allow switching to HW pump!");
});
builder.create().show();
performPluginSwitch(newState, type);
}
}
private void performPluginSwitch(boolean enabled) {
setPluginEnabled(getType(), enabled);
setFragmentVisible(getType(), enabled);
private void performPluginSwitch(boolean enabled, PluginType type) {
setPluginEnabled(type, enabled);
setFragmentVisible(type, enabled);
ConfigBuilderFragment.processOnEnabledCategoryChanged(this, getType());
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxEnabled");
MainApp.bus().post(new EventRefreshGui());
MainApp.bus().post(new EventConfigBuilderChange());
MainApp.bus().post(new EventConfigBuilderUpdateGui());
ConfigBuilderPlugin.getPlugin().logPluginStatus();
}

View file

@ -83,13 +83,6 @@ public class ConfigBuilderFragment extends SubscriberFragment {
}, null);
}
@Override
public void onDestroyView() {
super.onDestroyView();
for (PluginViewHolder pluginViewHolder : pluginViewHolders) pluginViewHolder.unbind();
pluginViewHolders.clear();
}
@Override
protected void updateGUI() {
for (PluginViewHolder pluginViewHolder : pluginViewHolders) pluginViewHolder.update();
@ -99,9 +92,7 @@ public class ConfigBuilderFragment extends SubscriberFragment {
public void on(EventConfigBuilderUpdateGui e) {
Activity activity = getActivity();
if (activity != null)
activity.runOnUiThread(() -> {
updateGUI();
});
activity.runOnUiThread(this::updateGUI);
}
private void createViews() {

View file

@ -1,106 +0,0 @@
package info.nightscout.androidaps.plugins.configBuilder;
import android.content.Intent;
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import info.nightscout.androidaps.MainApp;
import info.nightscout.androidaps.R;
import info.nightscout.androidaps.activities.PreferencesActivity;
import info.nightscout.androidaps.events.EventRefreshGui;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.utils.PasswordProtection;
public class PluginViewHolder {
private Unbinder unbinder;
private PluginType pluginType;
private PluginBase plugin;
private ConfigBuilderFragment fragment;
LinearLayout baseView;
@BindView(R.id.plugin_enabled_exclusive)
RadioButton enabledExclusive;
@BindView(R.id.plugin_enabled_inclusive)
CheckBox enabledInclusive;
@BindView(R.id.plugin_name)
TextView pluginName;
@BindView(R.id.plugin_description)
TextView pluginDescription;
@BindView(R.id.plugin_preferences)
ImageButton pluginPreferences;
@BindView(R.id.plugin_visibility)
CheckBox pluginVisibility;
public PluginViewHolder(ConfigBuilderFragment fragment, PluginType pluginType, PluginBase plugin) {
this.pluginType = pluginType;
this.plugin = plugin;
this.fragment = fragment;
baseView = (LinearLayout) fragment.getLayoutInflater().inflate(R.layout.configbuilder_single_plugin, null);
unbinder = ButterKnife.bind(this, baseView);
update();
}
public LinearLayout getBaseView() {
return baseView;
}
public void update() {
enabledExclusive.setVisibility(areMultipleSelectionsAllowed(pluginType) ? View.GONE : View.VISIBLE);
enabledInclusive.setVisibility(areMultipleSelectionsAllowed(pluginType) ? View.VISIBLE : View.GONE);
enabledExclusive.setChecked(plugin.isEnabled(pluginType));
enabledInclusive.setChecked(plugin.isEnabled(pluginType));
enabledInclusive.setEnabled(!plugin.pluginDescription.alwaysEnabled);
enabledExclusive.setEnabled(!plugin.pluginDescription.alwaysEnabled);
pluginName.setText(plugin.getName());
if (plugin.getDescription() == null) pluginDescription.setVisibility(View.GONE);
else {
pluginDescription.setVisibility(View.VISIBLE);
pluginDescription.setText(plugin.getDescription());
}
pluginPreferences.setVisibility(plugin.getPreferencesId() == -1 || !plugin.isEnabled(pluginType) ? View.INVISIBLE : View.VISIBLE);
pluginVisibility.setVisibility(plugin.hasFragment() ? View.VISIBLE : View.INVISIBLE);
pluginVisibility.setEnabled(!(plugin.pluginDescription.neverVisible || plugin.pluginDescription.alwaysVisible) && plugin.isEnabled(pluginType));
pluginVisibility.setChecked(plugin.isFragmentVisible());
}
@OnClick(R.id.plugin_visibility)
void onVisibilityChanged() {
plugin.setFragmentVisible(pluginType, pluginVisibility.isChecked());
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxVisible");
MainApp.bus().post(new EventRefreshGui());
ConfigBuilderPlugin.getPlugin().logPluginStatus();
}
@OnClick({R.id.plugin_enabled_exclusive, R.id.plugin_enabled_inclusive})
void onEnabledChanged() {
plugin.switchAllowed(enabledExclusive.getVisibility() == View.VISIBLE ? enabledExclusive.isChecked() : enabledInclusive.isChecked(), fragment.getActivity());
}
@OnClick(R.id.plugin_preferences)
void onPluginPreferencesClicked() {
PasswordProtection.QueryPassword(fragment.getContext(), R.string.settings_password, "settings_password", () -> {
Intent i = new Intent(fragment.getContext(), PreferencesActivity.class);
i.putExtra("id", plugin.getPreferencesId());
fragment.startActivity(i);
}, null);
}
public void unbind() {
unbinder.unbind();
}
private boolean areMultipleSelectionsAllowed(PluginType type) {
return type == PluginType.GENERAL || type == PluginType.CONSTRAINTS ||type == PluginType.LOOP;
}
}

View file

@ -0,0 +1,82 @@
package info.nightscout.androidaps.plugins.configBuilder
import android.content.Intent
import android.view.View
import android.widget.*
import info.nightscout.androidaps.MainApp
import info.nightscout.androidaps.R
import info.nightscout.androidaps.activities.PreferencesActivity
import info.nightscout.androidaps.events.EventRefreshGui
import info.nightscout.androidaps.interfaces.PluginBase
import info.nightscout.androidaps.interfaces.PluginType
import info.nightscout.androidaps.utils.PasswordProtection
class PluginViewHolder internal constructor(private val fragment: ConfigBuilderFragment,
private val pluginType: PluginType,
private val plugin: PluginBase) {
val baseView: LinearLayout = fragment.layoutInflater.inflate(R.layout.configbuilder_single_plugin, null) as LinearLayout
private val enabledExclusive: RadioButton
private val enabledInclusive: CheckBox
private val pluginName: TextView
private val pluginDescription: TextView
private val pluginPreferences: ImageButton
private val pluginVisibility: CheckBox
init {
enabledExclusive = baseView.findViewById(R.id.plugin_enabled_exclusive)
enabledInclusive = baseView.findViewById(R.id.plugin_enabled_inclusive)
pluginName = baseView.findViewById(R.id.plugin_name)
pluginDescription = baseView.findViewById(R.id.plugin_description)
pluginPreferences = baseView.findViewById(R.id.plugin_preferences)
pluginVisibility = baseView.findViewById(R.id.plugin_visibility)
pluginVisibility.setOnClickListener {
plugin.setFragmentVisible(pluginType, pluginVisibility.isChecked)
ConfigBuilderPlugin.getPlugin().storeSettings("CheckedCheckboxVisible")
MainApp.bus().post(EventRefreshGui())
ConfigBuilderPlugin.getPlugin().logPluginStatus()
}
enabledExclusive.setOnClickListener {
plugin.switchAllowed(if (enabledExclusive.visibility == View.VISIBLE) enabledExclusive.isChecked else enabledInclusive.isChecked, fragment.activity, pluginType)
}
enabledInclusive.setOnClickListener {
plugin.switchAllowed(if (enabledExclusive.visibility == View.VISIBLE) enabledExclusive.isChecked else enabledInclusive.isChecked, fragment.activity, pluginType)
}
pluginPreferences.setOnClickListener {
PasswordProtection.QueryPassword(fragment.context, R.string.settings_password, "settings_password", {
val i = Intent(fragment.context, PreferencesActivity::class.java)
i.putExtra("id", plugin.preferencesId)
fragment.startActivity(i)
}, null)
}
update()
}
fun update() {
enabledExclusive.visibility = if (areMultipleSelectionsAllowed(pluginType)) View.GONE else View.VISIBLE
enabledInclusive.visibility = if (areMultipleSelectionsAllowed(pluginType)) View.VISIBLE else View.GONE
enabledExclusive.isChecked = plugin.isEnabled(pluginType)
enabledInclusive.isChecked = plugin.isEnabled(pluginType)
enabledInclusive.isEnabled = !plugin.pluginDescription.alwaysEnabled
enabledExclusive.isEnabled = !plugin.pluginDescription.alwaysEnabled
pluginName.text = plugin.name
if (plugin.description == null)
pluginDescription.visibility = View.GONE
else {
pluginDescription.visibility = View.VISIBLE
pluginDescription.text = plugin.description
}
pluginPreferences.visibility = if (plugin.preferencesId == -1 || !plugin.isEnabled(pluginType)) View.INVISIBLE else View.VISIBLE
pluginVisibility.visibility = if (plugin.hasFragment()) View.VISIBLE else View.INVISIBLE
pluginVisibility.isEnabled = !(plugin.pluginDescription.neverVisible || plugin.pluginDescription.alwaysVisible) && plugin.isEnabled(pluginType)
pluginVisibility.isChecked = plugin.isFragmentVisible
}
private fun areMultipleSelectionsAllowed(type: PluginType): Boolean {
return type == PluginType.GENERAL || type == PluginType.CONSTRAINTS || type == PluginType.LOOP
}
}

View file

@ -162,8 +162,8 @@ public class ComboPlugin extends PluginBase implements PumpInterface, Constraint
}
@Override
public void switchAllowed(boolean newState, FragmentActivity activity) {
confirmPumpPluginActivation(newState, activity);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
confirmPumpPluginActivation(newState, activity, type);
}
@Override

View file

@ -27,6 +27,7 @@ import info.nightscout.androidaps.events.EventAppExit;
import info.nightscout.androidaps.interfaces.ConstraintsInterface;
import info.nightscout.androidaps.interfaces.PluginBase;
import info.nightscout.androidaps.interfaces.PluginDescription;
import info.nightscout.androidaps.interfaces.PluginType;
import info.nightscout.androidaps.interfaces.PumpDescription;
import info.nightscout.androidaps.interfaces.PumpInterface;
import info.nightscout.androidaps.logging.L;
@ -108,8 +109,8 @@ public abstract class PumpPluginAbstract extends PluginBase implements PumpInter
public abstract void onStartCustomActions();
@Override
public void switchAllowed(boolean newState, FragmentActivity activity) {
confirmPumpPluginActivation(newState, activity);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
confirmPumpPluginActivation(newState, activity, type);
}
/**

View file

@ -79,8 +79,8 @@ public abstract class AbstractDanaRPlugin extends PluginBase implements PumpInte
}
@Override
public void switchAllowed(boolean newState, FragmentActivity activity) {
confirmPumpPluginActivation(newState, activity);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
confirmPumpPluginActivation(newState, activity, type);
}
@Override

View file

@ -126,8 +126,8 @@ public class DanaRSPlugin extends PluginBase implements PumpInterface, DanaRInte
}
@Override
public void switchAllowed(boolean newState, FragmentActivity activity) {
confirmPumpPluginActivation(newState, activity);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
confirmPumpPluginActivation(newState, activity, type);
}
private ServiceConnection mConnection = new ServiceConnection() {

View file

@ -250,8 +250,8 @@ public class LocalInsightPlugin extends PluginBase implements PumpInterface, Con
}
@Override
public void switchAllowed(boolean newState, FragmentActivity activity) {
confirmPumpPluginActivation(newState, activity);
public void switchAllowed(boolean newState, FragmentActivity activity, PluginType type) {
confirmPumpPluginActivation(newState, activity, type);
}
@Override