diff --git a/app/src/main/java/info/nightscout/androidaps/MainApp.java b/app/src/main/java/info/nightscout/androidaps/MainApp.java index 6167343b0d..35033447d3 100644 --- a/app/src/main/java/info/nightscout/androidaps/MainApp.java +++ b/app/src/main/java/info/nightscout/androidaps/MainApp.java @@ -209,7 +209,7 @@ public class MainApp extends Application { pluginsList.add(TidepoolPlugin.INSTANCE); pluginsList.add(MaintenancePlugin.initPlugin(this)); if (engineeringMode) - pluginsList.add(AutomationPlugin.getPlugin()); + pluginsList.add(AutomationPlugin.INSTANCE); pluginsList.add(ConfigBuilderPlugin.getPlugin()); diff --git a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java index 031f0819df..90b0da5699 100644 --- a/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java +++ b/app/src/main/java/info/nightscout/androidaps/activities/PreferencesActivity.java @@ -189,7 +189,7 @@ public class PreferencesActivity extends PreferenceActivity implements SharedPre addPreferencesFromResourceIfEnabled(NSClientPlugin.getPlugin(), PluginType.GENERAL); addPreferencesFromResourceIfEnabled(TidepoolPlugin.INSTANCE, PluginType.GENERAL); addPreferencesFromResourceIfEnabled(SmsCommunicatorPlugin.getPlugin(), PluginType.GENERAL); - addPreferencesFromResourceIfEnabled(AutomationPlugin.getPlugin(), PluginType.GENERAL); + addPreferencesFromResourceIfEnabled(AutomationPlugin.INSTANCE, PluginType.GENERAL); addPreferencesFromResource(R.xml.pref_others); addPreferencesFromResource(R.xml.pref_datachoices); diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java deleted file mode 100644 index c43e7e0f44..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.java +++ /dev/null @@ -1,85 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation; - -import android.app.Activity; -import android.os.Bundle; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; -import android.widget.TextView; - -import androidx.annotation.NonNull; -import androidx.recyclerview.widget.LinearLayoutManager; -import androidx.recyclerview.widget.RecyclerView; - -import com.squareup.otto.Subscribe; - -import butterknife.BindView; -import butterknife.ButterKnife; -import butterknife.OnClick; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.plugins.common.SubscriberFragment; -import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; - -public class AutomationFragment extends SubscriberFragment { - - @BindView(R.id.eventListView) - RecyclerView mEventListView; - @BindView(R.id.logView) - TextView mLogView; - - private EventListAdapter mEventListAdapter; - - @Override - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - - View view = inflater.inflate(R.layout.automation_fragment, container, false); - unbinder = ButterKnife.bind(this, view); - - final AutomationPlugin plugin = AutomationPlugin.getPlugin(); - mEventListAdapter = new EventListAdapter(plugin.getAutomationEvents(), getFragmentManager()); - mEventListView.setLayoutManager(new LinearLayoutManager(getContext())); - mEventListView.setAdapter(mEventListAdapter); - - return view; - } - - @Subscribe - public void onEvent(EventAutomationUpdateGui unused) { - updateGUI(); - } - - @Subscribe - public void onEvent(EventAutomationDataChanged unused) { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - mEventListAdapter.notifyDataSetChanged(); - }); - } - - @Override - public void updateGUI() { - Activity activity = getActivity(); - if (activity != null) - activity.runOnUiThread(() -> { - mEventListAdapter.notifyDataSetChanged(); - StringBuilder sb = new StringBuilder(); - for (String l : AutomationPlugin.getPlugin().executionLog) { - sb.append(l); - sb.append("\n"); - } - mLogView.setText(sb.toString()); - }); - } - - @OnClick(R.id.fabAddEvent) - void onClickAddEvent(View unused) { - EditEventDialog dialog = EditEventDialog.newInstance(new AutomationEvent(), true); - if (getFragmentManager() != null) - dialog.show(getFragmentManager(), "EditEventDialog"); - } - -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt new file mode 100644 index 0000000000..97f9fa0ba3 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationFragment.kt @@ -0,0 +1,73 @@ +package info.nightscout.androidaps.plugins.general.automation + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import androidx.fragment.app.Fragment +import androidx.recyclerview.widget.LinearLayoutManager +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui +import io.reactivex.android.schedulers.AndroidSchedulers +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable +import kotlinx.android.synthetic.main.automation_fragment.* + +class AutomationFragment : Fragment() { + + private var disposable: CompositeDisposable = CompositeDisposable() + private var eventListAdapter: EventListAdapter? = null + + operator fun CompositeDisposable.plusAssign(disposable: Disposable) { + add(disposable) + } + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.automation_fragment, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + eventListAdapter = EventListAdapter(AutomationPlugin.automationEvents, fragmentManager) + automation_eventListView.layoutManager = LinearLayoutManager(context) + automation_eventListView.adapter = eventListAdapter + + automation_fabAddEvent.setOnClickListener { + val dialog = EditEventDialog() + val args = Bundle() + args.putString("event", AutomationEvent().toJSON()) + args.putInt("position", -1) // New event + dialog.arguments = args + fragmentManager?.let { dialog.show(it, "EditEventDialog") } + } + + disposable += RxBus + .toObservable(EventAutomationUpdateGui::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + eventListAdapter?.notifyDataSetChanged() + val sb = StringBuilder() + for (l in AutomationPlugin.executionLog) { + sb.append(l) + sb.append("\n") + } + automation_logView.text = sb.toString() + }, {}) + disposable += RxBus + .toObservable(EventAutomationDataChanged::class.java) + .observeOn(AndroidSchedulers.mainThread()) + .subscribe({ + eventListAdapter?.notifyDataSetChanged() + }, {}) + } + + override fun onStop() { + super.onStop() + disposable.clear() + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java deleted file mode 100644 index 1f3ea08166..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.java +++ /dev/null @@ -1,234 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation; - -import android.content.Context; -import android.content.Intent; -import android.os.Handler; - -import com.squareup.otto.Subscribe; - -import org.json.JSONArray; -import org.json.JSONException; -import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.ArrayList; -import java.util.List; - -import info.nightscout.androidaps.MainApp; -import info.nightscout.androidaps.R; -import info.nightscout.androidaps.events.EventChargingState; -import info.nightscout.androidaps.events.EventLocationChange; -import info.nightscout.androidaps.events.EventNetworkChange; -import info.nightscout.androidaps.events.EventPreferenceChange; -import info.nightscout.androidaps.interfaces.PluginBase; -import info.nightscout.androidaps.interfaces.PluginDescription; -import info.nightscout.androidaps.interfaces.PluginType; -import info.nightscout.androidaps.logging.L; -import info.nightscout.androidaps.plugins.general.automation.actions.Action; -import info.nightscout.androidaps.plugins.general.automation.actions.ActionNotification; -import info.nightscout.androidaps.plugins.general.automation.actions.ActionProfileSwitchPercent; -import info.nightscout.androidaps.plugins.general.automation.actions.ActionStartTempTarget; -import info.nightscout.androidaps.plugins.general.automation.actions.ActionStopTempTarget; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; -import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished; -import info.nightscout.androidaps.queue.Callback; -import info.nightscout.androidaps.services.LocationService; -import info.nightscout.androidaps.utils.DateUtil; -import info.nightscout.androidaps.utils.SP; -import info.nightscout.androidaps.utils.T; - -public class AutomationPlugin extends PluginBase { - private static Logger log = LoggerFactory.getLogger(L.AUTOMATION); - - private final String key_AUTOMATION_EVENTS = "AUTOMATION_EVENTS"; - private static AutomationPlugin plugin = null; - - public static AutomationPlugin getPlugin() { - if (plugin == null) - plugin = new AutomationPlugin(); - return plugin; - } - - private final List automationEvents = new ArrayList<>(); - private EventLocationChange eventLocationChange; - private EventChargingState eventChargingState; - private EventNetworkChange eventNetworkChange; - List executionLog = new ArrayList<>(); - - private Handler loopHandler = new Handler(); - private Runnable refreshLoop = new Runnable() { - @Override - public void run() { - processActions(); - loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()); - } - }; - - private AutomationPlugin() { - super(new PluginDescription() - .mainType(PluginType.GENERAL) - .fragmentClass(AutomationFragment.class.getName()) - .pluginName(R.string.automation) - .shortName(R.string.automation_short) - .preferencesId(R.xml.pref_automation) - .description(R.string.automation_description) - ); - } - - @Override - protected void onStart() { - Context context = MainApp.instance().getApplicationContext(); - context.startService(new Intent(context, LocationService.class)); - - MainApp.bus().register(this); - super.onStart(); - loadFromSP(); - loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()); - } - - @Override - protected void onStop() { - loopHandler.removeCallbacks(refreshLoop); - Context context = MainApp.instance().getApplicationContext(); - context.stopService(new Intent(context, LocationService.class)); - - MainApp.bus().unregister(this); - } - - public List getAutomationEvents() { - return automationEvents; - } - - public EventLocationChange getEventLocationChange() { - return eventLocationChange; - } - - public EventChargingState getEventChargingState() { - return eventChargingState; - } - - public EventNetworkChange getEventNetworkChange() { - return eventNetworkChange; - } - - private void storeToSP() { - JSONArray array = new JSONArray(); - try { - for (AutomationEvent event : getAutomationEvents()) { - array.put(new JSONObject(event.toJSON())); - } - } catch (JSONException e) { - e.printStackTrace(); - } - SP.putString(key_AUTOMATION_EVENTS, array.toString()); - } - - private void loadFromSP() { - automationEvents.clear(); - String data = SP.getString(key_AUTOMATION_EVENTS, ""); - if (!data.equals("")) { - try { - JSONArray array = new JSONArray(data); - for (int i = 0; i < array.length(); i++) { - JSONObject o = array.getJSONObject(i); - AutomationEvent event = new AutomationEvent().fromJSON(o.toString()); - automationEvents.add(event); - } - } catch (JSONException e) { - e.printStackTrace(); - } - } - - } - - @Subscribe - public void onEventPreferenceChange(EventPreferenceChange e) { - if (e.isChanged(R.string.key_location)) { - Context context = MainApp.instance().getApplicationContext(); - context.stopService(new Intent(context, LocationService.class)); - context.startService(new Intent(context, LocationService.class)); - } - } - - @Subscribe - public void onEvent(EventAutomationDataChanged e) { - storeToSP(); - } - - @Subscribe - public void onEventLocationChange(EventLocationChange e) { - eventLocationChange = e; - if (e != null) - log.debug(String.format("Grabbed location: %f %f Provider: %s", e.location.getLatitude(), e.location.getLongitude(), e.location.getProvider())); - - processActions(); - } - - @Subscribe - public void onEventChargingState(EventChargingState e) { - eventChargingState = e; - processActions(); - } - - @Subscribe - public void onEventNetworkChange(EventNetworkChange e) { - eventNetworkChange = e; - processActions(); - } - - @Subscribe - public void onEventAutosensCalculationFinished(EventAutosensCalculationFinished e) { - processActions(); - } - - private synchronized void processActions() { - if (!isEnabled(PluginType.GENERAL)) - return; - - if (L.isEnabled(L.AUTOMATION)) - log.debug("processActions"); - for (AutomationEvent event : getAutomationEvents()) { - if (event.getTrigger().shouldRun() && event.getPreconditions().shouldRun()) { - List actions = event.getActions(); - for (Action action : actions) { - action.doAction(new Callback() { - @Override - public void run() { - StringBuilder sb = new StringBuilder(); - sb.append(DateUtil.timeString(DateUtil.now())); - sb.append(" "); - sb.append(result.success ? "☺" : "X"); - sb.append(" "); - sb.append(event.getTitle()); - sb.append(": "); - sb.append(action.shortDescription()); - sb.append(": "); - sb.append(result.comment); - executionLog.add(sb.toString()); - if (L.isEnabled(L.AUTOMATION)) - log.debug("Executed: " + sb.toString()); - MainApp.bus().post(new EventAutomationUpdateGui()); - } - }); - } - event.getTrigger().executed(DateUtil.now()); - } - } - storeToSP(); // save last run time - } - - public final List getActionDummyObjects() { - return new ArrayList() {{ - //add(new ActionLoopDisable()); - //add(new ActionLoopEnable()); - //add(new ActionLoopResume()); - //add(new ActionLoopSuspend()); - add(new ActionStartTempTarget()); - add(new ActionStopTempTarget()); - add(new ActionNotification()); - add(new ActionProfileSwitchPercent()); - }}; - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt new file mode 100644 index 0000000000..5fc3680289 --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/AutomationPlugin.kt @@ -0,0 +1,198 @@ +package info.nightscout.androidaps.plugins.general.automation + +import android.content.Intent +import android.os.Handler +import info.nightscout.androidaps.MainApp +import info.nightscout.androidaps.R +import info.nightscout.androidaps.events.EventChargingState +import info.nightscout.androidaps.events.EventLocationChange +import info.nightscout.androidaps.events.EventNetworkChange +import info.nightscout.androidaps.events.EventPreferenceChange +import info.nightscout.androidaps.interfaces.PluginBase +import info.nightscout.androidaps.interfaces.PluginDescription +import info.nightscout.androidaps.interfaces.PluginType +import info.nightscout.androidaps.logging.L +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.actions.* +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationDataChanged +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui +import info.nightscout.androidaps.plugins.iob.iobCobCalculator.events.EventAutosensCalculationFinished +import info.nightscout.androidaps.queue.Callback +import info.nightscout.androidaps.services.LocationService +import info.nightscout.androidaps.utils.DateUtil +import info.nightscout.androidaps.utils.SP +import info.nightscout.androidaps.utils.T +import io.reactivex.disposables.CompositeDisposable +import io.reactivex.disposables.Disposable +import io.reactivex.schedulers.Schedulers +import org.json.JSONArray +import org.json.JSONException +import org.json.JSONObject +import org.slf4j.LoggerFactory +import java.util.* + +object AutomationPlugin : PluginBase(PluginDescription() + .mainType(PluginType.GENERAL) + .fragmentClass(AutomationFragment::class.qualifiedName) + .pluginName(R.string.automation) + .shortName(R.string.automation_short) + .preferencesId(R.xml.pref_automation) + .description(R.string.automation_description)) { + + private val log = LoggerFactory.getLogger(L.AUTOMATION) + private var disposable: CompositeDisposable = CompositeDisposable() + + private const val key_AUTOMATION_EVENTS = "AUTOMATION_EVENTS" + + val automationEvents = ArrayList() + var eventLocationChange: EventLocationChange? = null + private set + var executionLog: MutableList = ArrayList() + + private val loopHandler = Handler() + private lateinit var refreshLoop: Runnable + + init { + refreshLoop = Runnable { + processActions() + loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) + } + } + + operator fun CompositeDisposable.plusAssign(disposable: Disposable) { + add(disposable) + } + + override fun onStart() { + val context = MainApp.instance().applicationContext + context.startService(Intent(context, LocationService::class.java)) + + MainApp.bus().register(this) + super.onStart() + loadFromSP() + loopHandler.postDelayed(refreshLoop, T.mins(1).msecs()) + + disposable += RxBus + .toObservable(EventPreferenceChange::class.java) + .observeOn(Schedulers.io()) + .subscribe({ e -> + if (e.isChanged(R.string.key_location)) { + val ctx = MainApp.instance().applicationContext + ctx.stopService(Intent(ctx, LocationService::class.java)) + ctx.startService(Intent(ctx, LocationService::class.java)) + } + }, {}) + disposable += RxBus + .toObservable(EventAutomationDataChanged::class.java) + .observeOn(Schedulers.io()) + .subscribe({ storeToSP() }, {}) + disposable += RxBus + .toObservable(EventLocationChange::class.java) + .observeOn(Schedulers.io()) + .subscribe({ e -> + eventLocationChange = e + if (e != null) + log.debug("Grabbed location: $e.location.latitude $e.location.longitude Provider: $e.location.provider") + processActions() + }, {}) + disposable += RxBus + .toObservable(EventChargingState::class.java) + .observeOn(Schedulers.io()) + .subscribe({ processActions() }, {}) + disposable += RxBus + .toObservable(EventNetworkChange::class.java) + .observeOn(Schedulers.io()) + .subscribe({ processActions() }, {}) + disposable += RxBus + .toObservable(EventAutosensCalculationFinished::class.java) + .observeOn(Schedulers.io()) + .subscribe({ processActions() }, {}) + } + + override fun onStop() { + disposable.clear() + loopHandler.removeCallbacks(refreshLoop) + val context = MainApp.instance().applicationContext + context.stopService(Intent(context, LocationService::class.java)) + } + + private fun storeToSP() { + val array = JSONArray() + try { + for (event in automationEvents) { + array.put(JSONObject(event.toJSON())) + } + } catch (e: JSONException) { + e.printStackTrace() + } + + SP.putString(key_AUTOMATION_EVENTS, array.toString()) + } + + private fun loadFromSP() { + automationEvents.clear() + val data = SP.getString(key_AUTOMATION_EVENTS, "") + if (data != "") { + try { + val array = JSONArray(data) + for (i in 0 until array.length()) { + val o = array.getJSONObject(i) + val event = AutomationEvent().fromJSON(o.toString()) + automationEvents.add(event) + } + } catch (e: JSONException) { + e.printStackTrace() + } + } + } + + @Synchronized + private fun processActions() { + if (!isEnabled(PluginType.GENERAL)) + return + + if (L.isEnabled(L.AUTOMATION)) + log.debug("processActions") + for (event in automationEvents) { + if (event.trigger.shouldRun() && event.preconditions.shouldRun()) { + val actions = event.actions + for (action in actions) { + action.doAction(object : Callback() { + override fun run() { + val sb = StringBuilder() + sb.append(DateUtil.timeString(DateUtil.now())) + sb.append(" ") + sb.append(if (result.success) "☺" else "X") + sb.append(" ") + sb.append(event.title) + sb.append(": ") + sb.append(action.shortDescription()) + sb.append(": ") + sb.append(result.comment) + executionLog.add(sb.toString()) + if (L.isEnabled(L.AUTOMATION)) + log.debug("Executed: $sb") + MainApp.bus().post(EventAutomationUpdateGui()) + } + }) + } + event.trigger.executed(DateUtil.now()) + } + } + storeToSP() // save last run time + } + + fun getActionDummyObjects(): List { + return listOf( + //ActionLoopDisable(), + //ActionLoopEnable(), + //ActionLoopResume(), + //ActionLoopSuspend(), + ActionStartTempTarget(), + ActionStopTempTarget(), + ActionNotification(), + ActionProfileSwitchPercent() + ) + } + +} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java index 4bcd4bddfa..4bbd4a0959 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/EventListAdapter.java @@ -1,6 +1,7 @@ package info.nightscout.androidaps.plugins.general.automation; import android.content.Context; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -84,8 +85,14 @@ class EventListAdapter extends RecyclerView.Adapter // edit event holder.rootLayout.setOnClickListener(v -> { - EditEventDialog dialog = EditEventDialog.newInstance(event, false); - dialog.show(mFragmentManager, "EditEventDialog"); + //EditEventDialog dialog = EditEventDialog.Companion.newInstance(event, false); + EditEventDialog dialog = new EditEventDialog(); + Bundle args = new Bundle(); + args.putString("event", event.toJSON()); + args.putInt("position", position); + dialog.setArguments(args); + if (mFragmentManager != null) + dialog.show(mFragmentManager, "EditEventDialog"); }); } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java index 27f7742c00..2a1136fff0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ActionListAdapter.java @@ -1,5 +1,6 @@ package info.nightscout.androidaps.plugins.general.automation.dialogs; +import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -13,18 +14,18 @@ import androidx.recyclerview.widget.RecyclerView; import java.util.List; -import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; public class ActionListAdapter extends RecyclerView.Adapter { - private final List mActionList; - private final FragmentManager mFragmentManager; + private final List actionList; + private final FragmentManager fragmentManager; - public ActionListAdapter(FragmentManager manager, List events) { - this.mActionList = events; - this.mFragmentManager = manager; + public ActionListAdapter(FragmentManager manager, List actions) { + this.actionList = actions; + this.fragmentManager = manager; } @NonNull @@ -36,24 +37,28 @@ public class ActionListAdapter extends RecyclerView.Adapter { if (action.hasDialog()) { - EditActionDialog dialog = EditActionDialog.newInstance(action); - dialog.show(mFragmentManager, "EditActionDialog"); + Bundle args = new Bundle(); + args.putInt("actionPosition", position); + args.putString("action", action.toJSON()); + EditActionDialog dialog = new EditActionDialog(); + dialog.setArguments(args); + dialog.show(fragmentManager, "EditActionDialog"); } }); holder.iconTrash.setOnClickListener(v -> { - mActionList.remove(action); + actionList.remove(action); notifyDataSetChanged(); - MainApp.bus().post(new EventAutomationUpdateGui()); + RxBus.INSTANCE.send(new EventAutomationUpdateGui()); }); } @Override public int getItemCount() { - return mActionList.size(); + return actionList.size(); } static class ViewHolder extends RecyclerView.ViewHolder { diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java index d84761ab8e..19504ec5d4 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/ChooseActionDialog.java @@ -16,6 +16,7 @@ import butterknife.OnClick; import butterknife.Unbinder; import info.nightscout.androidaps.MainApp; import info.nightscout.androidaps.R; +import info.nightscout.androidaps.plugins.bus.RxBus; import info.nightscout.androidaps.plugins.general.automation.AutomationPlugin; import info.nightscout.androidaps.plugins.general.automation.actions.Action; import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationAddAction; @@ -23,10 +24,6 @@ import info.nightscout.androidaps.plugins.general.automation.events.EventAutomat public class ChooseActionDialog extends DialogFragment { - public interface OnClickListener { - void onClick(Action newActionObject); - } - private Unbinder mUnbinder; @BindView(R.id.radioGroup) @@ -37,7 +34,7 @@ public class ChooseActionDialog extends DialogFragment { View view = inflater.inflate(R.layout.automation_dialog_choose_action, container, false); mUnbinder = ButterKnife.bind(this, view); - for (Action a : AutomationPlugin.getPlugin().getActionDummyObjects()) { + for (Action a : AutomationPlugin.INSTANCE.getActionDummyObjects()) { RadioButton radioButton = new RadioButton(getContext()); radioButton.setText(a.friendlyName()); radioButton.setTag(a); @@ -96,8 +93,8 @@ public class ChooseActionDialog extends DialogFragment { @OnClick(R.id.ok) public void onButtonOk(View unused) { dismiss(); - MainApp.bus().post(new EventAutomationAddAction(instantiateAction())); - MainApp.bus().post(new EventAutomationUpdateGui()); + RxBus.INSTANCE.send(new EventAutomationAddAction(instantiateAction())); + RxBus.INSTANCE.send(new EventAutomationUpdateGui()); } @OnClick(R.id.cancel) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.java b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.java deleted file mode 100644 index 24bca41c62..0000000000 --- a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.java +++ /dev/null @@ -1,98 +0,0 @@ -package info.nightscout.androidaps.plugins.general.automation.dialogs; - -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.NonNull; -import androidx.fragment.app.DialogFragment; - -import org.json.JSONException; -import org.json.JSONObject; - -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.plugins.general.automation.actions.Action; -import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateGui; - -public class EditActionDialog extends DialogFragment { - private static Action resultAction; - - private Unbinder mUnbinder; - private Action mAction; - - @BindView(R.id.layout_root) - LinearLayout mRootLayout; - - @BindView(R.id.viewActionTitle) - TextView mViewActionTitle; - - public static EditActionDialog newInstance(Action action) { - Bundle args = new Bundle(); - EditActionDialog fragment = new EditActionDialog(); - fragment.setArguments(args); - resultAction = action; - - return fragment; - } - - public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, - Bundle savedInstanceState) { - View view = inflater.inflate(R.layout.automation_dialog_action, container, false); - mUnbinder = ButterKnife.bind(this, view); - - // get json data for action - String actionData = null; - if (savedInstanceState != null) { - actionData = savedInstanceState.getString("action"); - } - if (actionData == null) { - actionData = resultAction.toJSON(); - } - - // create action from json - try { - mAction = Action.instantiate(new JSONObject(actionData)); - } catch (JSONException e) { - e.printStackTrace(); - } - - if (mAction != null) { - mViewActionTitle.setText(mAction.friendlyName()); - mRootLayout.removeAllViews(); - mAction.generateDialog(mRootLayout); - } - - return view; - } - - @Override - public void onDestroyView() { - mUnbinder.unbind(); - super.onDestroyView(); - } - - @OnClick(R.id.ok) - public void onButtonOk(View unused) { - resultAction.apply(mAction); - dismiss(); - MainApp.bus().post(new EventAutomationUpdateGui()); - } - - @OnClick(R.id.cancel) - public void onButtonCancel(View unused) { - dismiss(); - } - - @Override - public void onSaveInstanceState(Bundle bundle) { - bundle.putString("action", mAction.toJSON()); - } -} diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt new file mode 100644 index 0000000000..0ebc60837c --- /dev/null +++ b/app/src/main/java/info/nightscout/androidaps/plugins/general/automation/dialogs/EditActionDialog.kt @@ -0,0 +1,61 @@ +package info.nightscout.androidaps.plugins.general.automation.dialogs + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import android.widget.Button +import androidx.fragment.app.DialogFragment +import info.nightscout.androidaps.R +import info.nightscout.androidaps.plugins.bus.RxBus +import info.nightscout.androidaps.plugins.general.automation.actions.Action +import info.nightscout.androidaps.plugins.general.automation.events.EventAutomationUpdateAction +import kotlinx.android.synthetic.main.automation_dialog_action.* +import org.json.JSONObject + +class EditActionDialog : DialogFragment() { + + private var action: Action? = null + private var actionPosition: Int = -1 + + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle?): View? { + // load data from bundle + (savedInstanceState ?: arguments)?.let { bundle -> + actionPosition = bundle.getInt("actionPosition", -1) + bundle.getString("action")?.let { action = Action.instantiate(JSONObject(it)) } + } + + dialog.setCanceledOnTouchOutside(false) + return inflater.inflate(R.layout.automation_dialog_action, container, false) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + + action?.let { + automation_actionTitle.setText(it.friendlyName()) + automation_editActionLayout.removeAllViews() + it.generateDialog(automation_editActionLayout) + } + + // OK button + view.findViewById