simplify dependencies
This commit is contained in:
parent
ec84c3fa70
commit
e3766a229b
3 changed files with 49 additions and 48 deletions
|
@ -20,51 +20,53 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper;
|
||||||
public class TriggerListAdapter {
|
public class TriggerListAdapter {
|
||||||
private final LinearLayout mRootLayout;
|
private final LinearLayout mRootLayout;
|
||||||
private final Context mContext;
|
private final Context mContext;
|
||||||
private final TriggerConnector mRootConnector;
|
private final TriggerConnector rootTrigger;
|
||||||
private final MainApp mainApp;
|
private final MainApp mainApp;
|
||||||
private final ResourceHelper resourceHelper;
|
private final ResourceHelper resourceHelper;
|
||||||
|
|
||||||
public TriggerListAdapter(MainApp mainApp, ResourceHelper resourceHelper, Context context, LinearLayout rootLayout, TriggerConnector rootTrigger) {
|
public TriggerListAdapter(MainApp mainApp, ResourceHelper resourceHelper, LinearLayout rootLayout, TriggerConnector rootTrigger) {
|
||||||
mRootLayout = rootLayout;
|
mRootLayout = rootLayout;
|
||||||
this.mainApp = mainApp;
|
this.mainApp = mainApp;
|
||||||
this.resourceHelper = resourceHelper;
|
this.resourceHelper = resourceHelper;
|
||||||
mContext = context;
|
mContext = rootLayout.getContext();
|
||||||
mRootConnector = rootTrigger;
|
this.rootTrigger = rootTrigger;
|
||||||
build(rootTrigger.scanForActivity(context).getSupportFragmentManager());
|
build(rootLayout.getContext());
|
||||||
}
|
|
||||||
|
|
||||||
public Context getContext() {
|
|
||||||
return mContext;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private void destroy() {
|
private void destroy() {
|
||||||
mRootLayout.removeAllViews();
|
mRootLayout.removeAllViews();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void build(FragmentManager fragmentManager) {
|
private void rebuild(Context context) {
|
||||||
for (int i = 0; i < mRootConnector.size(); ++i) {
|
destroy();
|
||||||
final Trigger trigger = mRootConnector.get(i);
|
build(context);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void build(Context context) {
|
||||||
|
FragmentManager fragmentManager = Trigger.scanForActivity(context).getSupportFragmentManager();
|
||||||
|
for (int i = 0; i < rootTrigger.size(); ++i) {
|
||||||
|
final Trigger trigger = rootTrigger.get(i);
|
||||||
|
|
||||||
// spinner
|
// spinner
|
||||||
if (i > 0) {
|
if (i > 0) {
|
||||||
createSpinner(trigger, fragmentManager);
|
createSpinner(trigger, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
// trigger layout
|
// trigger layout
|
||||||
trigger.generateDialog(mRootLayout);
|
trigger.generateDialog(mRootLayout);
|
||||||
|
|
||||||
// buttons
|
// buttons
|
||||||
createButtons(fragmentManager, trigger);
|
createButtons(mRootLayout, trigger, context);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mRootConnector.size() == 0) {
|
if (rootTrigger.size() == 0) {
|
||||||
Button buttonAdd = new Button(mContext);
|
Button buttonAdd = new Button(mContext);
|
||||||
buttonAdd.setText(resourceHelper.gs(R.string.addnew));
|
buttonAdd.setText(resourceHelper.gs(R.string.addnew));
|
||||||
buttonAdd.setOnClickListener(v -> {
|
buttonAdd.setOnClickListener(v -> {
|
||||||
ChooseTriggerDialog dialog = new ChooseTriggerDialog();
|
ChooseTriggerDialog dialog = new ChooseTriggerDialog();
|
||||||
dialog.setOnClickListener(newTriggerObject -> {
|
dialog.setOnClickListener(newTriggerObject -> {
|
||||||
mRootConnector.add(newTriggerObject);
|
rootTrigger.add(newTriggerObject);
|
||||||
rebuild(fragmentManager);
|
rebuild(context);
|
||||||
});
|
});
|
||||||
dialog.show(fragmentManager, "ChooseTriggerDialog");
|
dialog.show(fragmentManager, "ChooseTriggerDialog");
|
||||||
});
|
});
|
||||||
|
@ -80,7 +82,7 @@ public class TriggerListAdapter {
|
||||||
return spinner;
|
return spinner;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createSpinner(Trigger trigger, FragmentManager fragmentManager) {
|
private void createSpinner(Trigger trigger, Context context) {
|
||||||
final TriggerConnector connector = trigger.getConnector();
|
final TriggerConnector connector = trigger.getConnector();
|
||||||
final int initialPosition = connector.getConnectorType().ordinal();
|
final int initialPosition = connector.getConnectorType().ordinal();
|
||||||
Spinner spinner = createSpinner();
|
Spinner spinner = createSpinner();
|
||||||
|
@ -97,7 +99,7 @@ public class TriggerListAdapter {
|
||||||
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
|
||||||
if (position != initialPosition) {
|
if (position != initialPosition) {
|
||||||
// connector type changed
|
// connector type changed
|
||||||
changeConnector(fragmentManager, trigger, connector, TriggerConnector.Type.values()[position]);
|
changeConnector(context, trigger, connector, TriggerConnector.Type.values()[position]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -108,7 +110,8 @@ public class TriggerListAdapter {
|
||||||
mRootLayout.addView(spinner);
|
mRootLayout.addView(spinner);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void createButtons(FragmentManager fragmentManager, Trigger trigger) {
|
private void createButtons(LinearLayout rootLayout, Trigger trigger, Context context) {
|
||||||
|
FragmentManager fragmentManager = Trigger.scanForActivity(context).getSupportFragmentManager();
|
||||||
// do not create buttons for TriggerConnector
|
// do not create buttons for TriggerConnector
|
||||||
if (trigger instanceof TriggerConnector) {
|
if (trigger instanceof TriggerConnector) {
|
||||||
return;
|
return;
|
||||||
|
@ -118,7 +121,6 @@ public class TriggerListAdapter {
|
||||||
LinearLayout buttonLayout = new LinearLayout(mContext);
|
LinearLayout buttonLayout = new LinearLayout(mContext);
|
||||||
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
|
buttonLayout.setOrientation(LinearLayout.HORIZONTAL);
|
||||||
buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
|
||||||
mRootLayout.addView(buttonLayout);
|
|
||||||
|
|
||||||
// Button [-]
|
// Button [-]
|
||||||
Button buttonRemove = new Button(mContext);
|
Button buttonRemove = new Button(mContext);
|
||||||
|
@ -126,7 +128,8 @@ public class TriggerListAdapter {
|
||||||
buttonRemove.setOnClickListener(v -> {
|
buttonRemove.setOnClickListener(v -> {
|
||||||
final TriggerConnector connector = trigger.getConnector();
|
final TriggerConnector connector = trigger.getConnector();
|
||||||
connector.remove(trigger);
|
connector.remove(trigger);
|
||||||
connector.simplify().rebuildView(fragmentManager);
|
connector.simplify();
|
||||||
|
rebuild(context);
|
||||||
});
|
});
|
||||||
buttonLayout.addView(buttonRemove);
|
buttonLayout.addView(buttonRemove);
|
||||||
|
|
||||||
|
@ -139,7 +142,8 @@ public class TriggerListAdapter {
|
||||||
dialog.setOnClickListener(newTriggerObject -> {
|
dialog.setOnClickListener(newTriggerObject -> {
|
||||||
TriggerConnector connector = trigger.getConnector();
|
TriggerConnector connector = trigger.getConnector();
|
||||||
connector.add(connector.pos(trigger) + 1, newTriggerObject);
|
connector.add(connector.pos(trigger) + 1, newTriggerObject);
|
||||||
connector.simplify().rebuildView(fragmentManager);
|
connector.simplify();
|
||||||
|
rebuild(context);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
buttonLayout.addView(buttonAdd);
|
buttonLayout.addView(buttonAdd);
|
||||||
|
@ -150,17 +154,14 @@ public class TriggerListAdapter {
|
||||||
buttonCopy.setOnClickListener(v -> {
|
buttonCopy.setOnClickListener(v -> {
|
||||||
TriggerConnector connector = trigger.getConnector();
|
TriggerConnector connector = trigger.getConnector();
|
||||||
connector.add(connector.pos(trigger) + 1, trigger.duplicate());
|
connector.add(connector.pos(trigger) + 1, trigger.duplicate());
|
||||||
connector.simplify().rebuildView(fragmentManager);
|
connector.simplify();
|
||||||
|
rebuild(context);
|
||||||
});
|
});
|
||||||
buttonLayout.addView(buttonCopy);
|
buttonLayout.addView(buttonCopy);
|
||||||
|
rootLayout.addView(buttonLayout);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void rebuild(FragmentManager fragmentManager) {
|
public void changeConnector(final Context context, final Trigger trigger, final TriggerConnector connector, final TriggerConnector.Type newConnectorType) {
|
||||||
destroy();
|
|
||||||
build(fragmentManager);
|
|
||||||
}
|
|
||||||
|
|
||||||
public void changeConnector(final FragmentManager fragmentManager, final Trigger trigger, final TriggerConnector connector, final TriggerConnector.Type newConnectorType) {
|
|
||||||
if (connector.size() > 2) {
|
if (connector.size() > 2) {
|
||||||
// split connector
|
// split connector
|
||||||
int pos = connector.pos(trigger) - 1;
|
int pos = connector.pos(trigger) - 1;
|
||||||
|
@ -178,6 +179,7 @@ public class TriggerListAdapter {
|
||||||
} else {
|
} else {
|
||||||
connector.setType(newConnectorType);
|
connector.setType(newConnectorType);
|
||||||
}
|
}
|
||||||
connector.simplify().rebuildView(fragmentManager);
|
connector.simplify();
|
||||||
|
rebuild(context);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -45,19 +45,23 @@ abstract class Trigger(val mainApp: MainApp) {
|
||||||
abstract fun icon(): Optional<Int?>
|
abstract fun icon(): Optional<Int?>
|
||||||
abstract fun duplicate(): Trigger
|
abstract fun duplicate(): Trigger
|
||||||
|
|
||||||
|
|
||||||
|
companion object {
|
||||||
|
@JvmStatic
|
||||||
|
fun scanForActivity(cont: Context?): AppCompatActivity? {
|
||||||
|
if (cont == null) return null
|
||||||
|
else if (cont is AppCompatActivity) return cont
|
||||||
|
else if (cont is ContextWrapper) return scanForActivity(cont.baseContext)
|
||||||
|
return null
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
open fun generateDialog(root: LinearLayout) {
|
open fun generateDialog(root: LinearLayout) {
|
||||||
val title = TextView(root.context)
|
val title = TextView(root.context)
|
||||||
title.setText(friendlyName())
|
title.setText(friendlyName())
|
||||||
root.addView(title)
|
root.addView(title)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun scanForActivity(cont: Context?): AppCompatActivity? {
|
|
||||||
if (cont == null) return null
|
|
||||||
else if (cont is AppCompatActivity) return cont
|
|
||||||
else if (cont is ContextWrapper) return scanForActivity(cont.baseContext)
|
|
||||||
return null
|
|
||||||
}
|
|
||||||
|
|
||||||
fun instantiate(obj: JSONObject): Trigger? {
|
fun instantiate(obj: JSONObject): Trigger? {
|
||||||
try {
|
try {
|
||||||
val type = obj.getString("type")
|
val type = obj.getString("type")
|
||||||
|
|
|
@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers
|
||||||
import android.view.ViewGroup
|
import android.view.ViewGroup
|
||||||
import android.widget.LinearLayout
|
import android.widget.LinearLayout
|
||||||
import androidx.annotation.StringRes
|
import androidx.annotation.StringRes
|
||||||
import androidx.fragment.app.FragmentManager
|
|
||||||
import com.google.common.base.Optional
|
import com.google.common.base.Optional
|
||||||
import info.nightscout.androidaps.MainApp
|
import info.nightscout.androidaps.MainApp
|
||||||
import info.nightscout.androidaps.R
|
import info.nightscout.androidaps.R
|
||||||
|
@ -132,19 +131,15 @@ class TriggerConnector(mainApp: MainApp) : Trigger(mainApp) {
|
||||||
|
|
||||||
override fun duplicate(): Trigger = TriggerConnector(mainApp, connectorType)
|
override fun duplicate(): Trigger = TriggerConnector(mainApp, connectorType)
|
||||||
|
|
||||||
private var adapter: TriggerListAdapter? = null
|
|
||||||
|
|
||||||
fun rebuildView(fragmentManager: FragmentManager) = adapter?.rebuild(fragmentManager)
|
|
||||||
|
|
||||||
override fun generateDialog(root: LinearLayout) {
|
override fun generateDialog(root: LinearLayout) {
|
||||||
val padding = resourceHelper.dpToPx(5)
|
val padding = resourceHelper.dpToPx(5)
|
||||||
root.setPadding(padding, padding, padding, padding)
|
root.setPadding(padding, padding, padding, padding)
|
||||||
root.setBackgroundResource(R.drawable.border_automation_unit)
|
root.setBackgroundResource(R.drawable.border_automation_unit)
|
||||||
val triggerListLayout = LinearLayout(root.context)
|
val listLayout = LinearLayout(root.context)
|
||||||
triggerListLayout.orientation = LinearLayout.VERTICAL
|
listLayout.orientation = LinearLayout.VERTICAL
|
||||||
triggerListLayout.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
listLayout.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
|
||||||
root.addView(triggerListLayout)
|
root.addView(listLayout)
|
||||||
adapter = TriggerListAdapter(mainApp, resourceHelper, root.context, triggerListLayout, this)
|
TriggerListAdapter(mainApp, resourceHelper, listLayout, this)
|
||||||
}
|
}
|
||||||
|
|
||||||
fun simplify(): TriggerConnector { // simplify children
|
fun simplify(): TriggerConnector { // simplify children
|
||||||
|
|
Loading…
Reference in a new issue