From e3766a229be8ea20028fb12def2a7607826c48f2 Mon Sep 17 00:00:00 2001 From: Milos Kozak Date: Sun, 5 Jan 2020 20:20:11 +0100 Subject: [PATCH] simplify dependencies --- .../dialogs/TriggerListAdapter.java | 64 ++++++++++--------- .../general/automation/triggers/Trigger.kt | 18 ++++-- .../automation/triggers/TriggerConnector.kt | 15 ++--- 3 files changed, 49 insertions(+), 48 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java index 93f5378529..78b285e5d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/TriggerListAdapter.java @@ -20,51 +20,53 @@ import info.nightscout.androidaps.utils.resources.ResourceHelper; public class TriggerListAdapter { private final LinearLayout mRootLayout; private final Context mContext; - private final TriggerConnector mRootConnector; + private final TriggerConnector rootTrigger; private final MainApp mainApp; 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; this.mainApp = mainApp; this.resourceHelper = resourceHelper; - mContext = context; - mRootConnector = rootTrigger; - build(rootTrigger.scanForActivity(context).getSupportFragmentManager()); - } - - public Context getContext() { - return mContext; + mContext = rootLayout.getContext(); + this.rootTrigger = rootTrigger; + build(rootLayout.getContext()); } private void destroy() { mRootLayout.removeAllViews(); } - private void build(FragmentManager fragmentManager) { - for (int i = 0; i < mRootConnector.size(); ++i) { - final Trigger trigger = mRootConnector.get(i); + private void rebuild(Context context) { + destroy(); + 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 if (i > 0) { - createSpinner(trigger, fragmentManager); + createSpinner(trigger, context); } // trigger layout trigger.generateDialog(mRootLayout); // buttons - createButtons(fragmentManager, trigger); + createButtons(mRootLayout, trigger, context); } - if (mRootConnector.size() == 0) { + if (rootTrigger.size() == 0) { Button buttonAdd = new Button(mContext); buttonAdd.setText(resourceHelper.gs(R.string.addnew)); buttonAdd.setOnClickListener(v -> { ChooseTriggerDialog dialog = new ChooseTriggerDialog(); dialog.setOnClickListener(newTriggerObject -> { - mRootConnector.add(newTriggerObject); - rebuild(fragmentManager); + rootTrigger.add(newTriggerObject); + rebuild(context); }); dialog.show(fragmentManager, "ChooseTriggerDialog"); }); @@ -80,7 +82,7 @@ public class TriggerListAdapter { return spinner; } - private void createSpinner(Trigger trigger, FragmentManager fragmentManager) { + private void createSpinner(Trigger trigger, Context context) { final TriggerConnector connector = trigger.getConnector(); final int initialPosition = connector.getConnectorType().ordinal(); Spinner spinner = createSpinner(); @@ -97,7 +99,7 @@ public class TriggerListAdapter { public void onItemSelected(AdapterView parent, View view, int position, long id) { if (position != initialPosition) { // 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); } - 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 if (trigger instanceof TriggerConnector) { return; @@ -118,7 +121,6 @@ public class TriggerListAdapter { LinearLayout buttonLayout = new LinearLayout(mContext); buttonLayout.setOrientation(LinearLayout.HORIZONTAL); buttonLayout.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)); - mRootLayout.addView(buttonLayout); // Button [-] Button buttonRemove = new Button(mContext); @@ -126,7 +128,8 @@ public class TriggerListAdapter { buttonRemove.setOnClickListener(v -> { final TriggerConnector connector = trigger.getConnector(); connector.remove(trigger); - connector.simplify().rebuildView(fragmentManager); + connector.simplify(); + rebuild(context); }); buttonLayout.addView(buttonRemove); @@ -139,7 +142,8 @@ public class TriggerListAdapter { dialog.setOnClickListener(newTriggerObject -> { TriggerConnector connector = trigger.getConnector(); connector.add(connector.pos(trigger) + 1, newTriggerObject); - connector.simplify().rebuildView(fragmentManager); + connector.simplify(); + rebuild(context); }); }); buttonLayout.addView(buttonAdd); @@ -150,17 +154,14 @@ public class TriggerListAdapter { buttonCopy.setOnClickListener(v -> { TriggerConnector connector = trigger.getConnector(); connector.add(connector.pos(trigger) + 1, trigger.duplicate()); - connector.simplify().rebuildView(fragmentManager); + connector.simplify(); + rebuild(context); }); buttonLayout.addView(buttonCopy); + rootLayout.addView(buttonLayout); } - public void rebuild(FragmentManager fragmentManager) { - destroy(); - build(fragmentManager); - } - - public void changeConnector(final FragmentManager fragmentManager, final Trigger trigger, final TriggerConnector connector, final TriggerConnector.Type newConnectorType) { + public void changeConnector(final Context context, final Trigger trigger, final TriggerConnector connector, final TriggerConnector.Type newConnectorType) { if (connector.size() > 2) { // split connector int pos = connector.pos(trigger) - 1; @@ -178,6 +179,7 @@ public class TriggerListAdapter { } else { connector.setType(newConnectorType); } - connector.simplify().rebuildView(fragmentManager); + connector.simplify(); + rebuild(context); } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt index 4db2cc5bd3..782121af3d 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/Trigger.kt @@ -45,19 +45,23 @@ abstract class Trigger(val mainApp: MainApp) { abstract fun icon(): Optional 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) { val title = TextView(root.context) title.setText(friendlyName()) 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? { try { val type = obj.getString("type") diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.kt index 2e74124d19..cd0913e6c4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/triggers/TriggerConnector.kt @@ -3,7 +3,6 @@ package info.nightscout.androidaps.plugins.general.automation.triggers import android.view.ViewGroup import android.widget.LinearLayout import androidx.annotation.StringRes -import androidx.fragment.app.FragmentManager import com.google.common.base.Optional import info.nightscout.androidaps.MainApp import info.nightscout.androidaps.R @@ -132,19 +131,15 @@ class TriggerConnector(mainApp: MainApp) : Trigger(mainApp) { override fun duplicate(): Trigger = TriggerConnector(mainApp, connectorType) - private var adapter: TriggerListAdapter? = null - - fun rebuildView(fragmentManager: FragmentManager) = adapter?.rebuild(fragmentManager) - override fun generateDialog(root: LinearLayout) { val padding = resourceHelper.dpToPx(5) root.setPadding(padding, padding, padding, padding) root.setBackgroundResource(R.drawable.border_automation_unit) - val triggerListLayout = LinearLayout(root.context) - triggerListLayout.orientation = LinearLayout.VERTICAL - triggerListLayout.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) - root.addView(triggerListLayout) - adapter = TriggerListAdapter(mainApp, resourceHelper, root.context, triggerListLayout, this) + val listLayout = LinearLayout(root.context) + listLayout.orientation = LinearLayout.VERTICAL + listLayout.layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT) + root.addView(listLayout) + TriggerListAdapter(mainApp, resourceHelper, listLayout, this) } fun simplify(): TriggerConnector { // simplify children