From dc4febb6e4b7b8181f5f7c3e99a185de0bd1be8c Mon Sep 17 00:00:00 2001 From: osodebailar Date: Sun, 24 Apr 2022 18:03:09 +0200 Subject: [PATCH] swiperefresh for loop and aps --- .../plugins/aps/loop/LoopFragment.kt | 54 ++++++++++++++----- .../aps/openAPSAMA/OpenAPSAMAFragment.kt | 54 ++++++++++++++----- .../aps/openAPSSMB/OpenAPSSMBFragment.kt | 46 ++++++++++++---- app/src/main/res/layout/loop_fragment.xml | 25 ++++----- .../main/res/layout/openapsama_fragment.xml | 31 +++++------ 5 files changed, 142 insertions(+), 68 deletions(-) diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopFragment.kt index 1a1ec6fae5..dd524ec047 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/loop/LoopFragment.kt @@ -1,15 +1,12 @@ package info.nightscout.androidaps.plugins.aps.loop import android.os.Bundle -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R import info.nightscout.androidaps.databinding.LoopFragmentBinding import info.nightscout.androidaps.interfaces.Constraint import info.nightscout.androidaps.interfaces.Loop -import info.nightscout.shared.logging.AAPSLogger import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopSetLastRunGui import info.nightscout.androidaps.plugins.aps.loop.events.EventLoopUpdateGui import info.nightscout.androidaps.plugins.bus.RxBus @@ -18,6 +15,7 @@ import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.HtmlHelper import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers +import info.nightscout.shared.logging.AAPSLogger import info.nightscout.shared.sharedPreferences.SP import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.kotlin.plusAssign @@ -34,6 +32,8 @@ class LoopFragment : DaggerFragment() { @Inject lateinit var loop: Loop @Inject lateinit var dateUtil: DateUtil + private val ID_MENU_RUN = 1 + private var disposable: CompositeDisposable = CompositeDisposable() private var _binding: LoopFragmentBinding? = null @@ -42,8 +42,11 @@ class LoopFragment : DaggerFragment() { // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + setHasOptionsMenu(true) _binding = LoopFragmentBinding.inflate(inflater, container, false) return binding.root } @@ -51,12 +54,33 @@ class LoopFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - binding.run.setOnClickListener { - binding.lastrun.text = rh.gs(R.string.executing) - Thread { loop.invoke("Loop button", true) }.start() + with(binding.swipeRefresh) { + setColorSchemeColors(rh.gac(context, R.attr.colorPrimaryDark), rh.gac(context, R.attr.colorPrimary), rh.gac(context, R.attr.colorSecondary)) + setOnRefreshListener { + binding.lastrun.text = rh.gs(info.nightscout.androidaps.R.string.executing) + Thread { loop.invoke("Loop swiperefresh", true) }.start() + } } } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + menu.removeItem(ID_MENU_RUN) + menu.add(Menu.FIRST, ID_MENU_RUN, 0, rh.gs(R.string.openapsma_run)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER) + menu.setGroupDividerEnabled(true) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean = + when (item.itemId) { + ID_MENU_RUN -> { + binding.lastrun.text = rh.gs(R.string.executing) + Thread { loop.invoke("Loop menu", true) }.start() + true + } + + else -> false + } + @Synchronized override fun onResume() { super.onResume() @@ -64,16 +88,16 @@ class LoopFragment : DaggerFragment() { .toObservable(EventLoopUpdateGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - updateGUI() - }, fabricPrivacy::logException) + updateGUI() + }, fabricPrivacy::logException) disposable += rxBus .toObservable(EventLoopSetLastRunGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - clearGUI() - binding.lastrun.text = it.text - }, fabricPrivacy::logException) + clearGUI() + binding.lastrun.text = it.text + }, fabricPrivacy::logException) updateGUI() sp.putBoolean(R.string.key_objectiveuseloop, true) @@ -117,6 +141,7 @@ class LoopFragment : DaggerFragment() { allConstraints.getMostLimitedReasons(aapsLogger) } ?: "" binding.constraints.text = constraints + binding.swipeRefresh.isRefreshing = false } } @@ -133,5 +158,6 @@ class LoopFragment : DaggerFragment() { binding.tbrexecutionTime.text = "" binding.tbrsetbypump.text = "" binding.smbsetbypump.text = "" + binding.swipeRefresh.isRefreshing = false } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.kt index 8ec075f1a9..57c33820e0 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSAMA/OpenAPSAMAFragment.kt @@ -2,14 +2,10 @@ package info.nightscout.androidaps.plugins.aps.openAPSAMA import android.os.Bundle import android.text.TextUtils -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R import info.nightscout.androidaps.databinding.OpenapsamaFragmentBinding -import info.nightscout.shared.logging.AAPSLogger -import info.nightscout.shared.logging.LTag import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateGui import info.nightscout.androidaps.plugins.aps.events.EventOpenAPSUpdateResultGui import info.nightscout.androidaps.plugins.bus.RxBus @@ -18,6 +14,8 @@ import info.nightscout.androidaps.utils.FabricPrivacy import info.nightscout.androidaps.utils.JSONFormatter import info.nightscout.androidaps.utils.resources.ResourceHelper import info.nightscout.androidaps.utils.rx.AapsSchedulers +import info.nightscout.shared.logging.AAPSLogger +import info.nightscout.shared.logging.LTag import io.reactivex.rxjava3.disposables.CompositeDisposable import io.reactivex.rxjava3.kotlin.plusAssign import org.json.JSONArray @@ -37,14 +35,19 @@ class OpenAPSAMAFragment : DaggerFragment() { @Inject lateinit var dateUtil: DateUtil @Inject lateinit var jsonFormatter: JSONFormatter + private val ID_MENU_RUN = 1 + private var _binding: OpenapsamaFragmentBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, - savedInstanceState: Bundle?): View { + override fun onCreateView( + inflater: LayoutInflater, container: ViewGroup?, + savedInstanceState: Bundle? + ): View { + setHasOptionsMenu(true) _binding = OpenapsamaFragmentBinding.inflate(inflater, container, false) return binding.root } @@ -52,11 +55,34 @@ class OpenAPSAMAFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - binding.run.setOnClickListener { - openAPSAMAPlugin.invoke("OpenAPSAMA button", false) + with(binding.swipeRefresh) { + setColorSchemeColors(rh.gac(context, R.attr.colorPrimaryDark), rh.gac(context, R.attr.colorPrimary), rh.gac(context, R.attr.colorSecondary)) + setOnRefreshListener { + binding.lastrun.text = rh.gs(info.nightscout.androidaps.R.string.executing) + openAPSAMAPlugin.invoke("OpenAPSAMA swiperefresh", false) + } } + } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + menu.removeItem(ID_MENU_RUN) + menu.add(Menu.FIRST, ID_MENU_RUN, 0, rh.gs(R.string.openapsma_run)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER) + menu.setGroupDividerEnabled(true) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean = + when (item.itemId) { + ID_MENU_RUN -> { + binding.lastrun.text = rh.gs(R.string.executing) + openAPSAMAPlugin.invoke("OpenAPSAMA menu", false) + true + } + + else -> false + } + @Synchronized override fun onResume() { super.onResume() @@ -65,14 +91,14 @@ class OpenAPSAMAFragment : DaggerFragment() { .toObservable(EventOpenAPSUpdateGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - updateGUI() - }, fabricPrivacy::logException) + updateGUI() + }, fabricPrivacy::logException) disposable += rxBus .toObservable(EventOpenAPSUpdateResultGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - updateResultGUI(it.text) - }, fabricPrivacy::logException) + updateResultGUI(it.text) + }, fabricPrivacy::logException) updateGUI() } @@ -118,6 +144,7 @@ class OpenAPSAMAFragment : DaggerFragment() { openAPSAMAPlugin.lastAutosensResult.let { binding.autosensdata.text = jsonFormatter.format(it.json()) } + binding.swipeRefresh.isRefreshing = false } private fun updateResultGUI(text: String) { @@ -131,5 +158,6 @@ class OpenAPSAMAFragment : DaggerFragment() { binding.scriptdebugdata.text = "" binding.request.text = "" binding.lastrun.text = "" + binding.swipeRefresh.isRefreshing = false } } diff --git a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.kt b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.kt index 8289e60f7c..1d1e6d4f95 100644 --- a/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.kt +++ b/app/src/main/java/info/nightscout/androidaps/plugins/aps/openAPSSMB/OpenAPSSMBFragment.kt @@ -3,9 +3,7 @@ package info.nightscout.androidaps.plugins.aps.openAPSSMB import android.annotation.SuppressLint import android.os.Bundle import android.text.TextUtils -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup +import android.view.* import dagger.android.support.DaggerFragment import info.nightscout.androidaps.R import info.nightscout.androidaps.databinding.OpenapsamaFragmentBinding @@ -39,13 +37,16 @@ class OpenAPSSMBFragment : DaggerFragment() { @Inject lateinit var dateUtil: DateUtil @Inject lateinit var jsonFormatter: JSONFormatter + private val ID_MENU_RUN = 1 + private var _binding: OpenapsamaFragmentBinding? = null // This property is only valid between onCreateView and // onDestroyView. private val binding get() = _binding!! - override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { + setHasOptionsMenu(true) _binding = OpenapsamaFragmentBinding.inflate(inflater, container, false) return binding.root } @@ -53,11 +54,34 @@ class OpenAPSSMBFragment : DaggerFragment() { override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) - binding.run.setOnClickListener { - activePlugin.activeAPS.invoke("OpenAPSSMB button", false) + with(binding.swipeRefresh) { + setColorSchemeColors(rh.gac(context, R.attr.colorPrimaryDark), rh.gac(context, R.attr.colorPrimary), rh.gac(context, R.attr.colorSecondary)) + setOnRefreshListener { + binding.lastrun.text = rh.gs(info.nightscout.androidaps.R.string.executing) + activePlugin.activeAPS.invoke("OpenAPSSMB swiperefresh", false) + } } + } + override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) { + super.onCreateOptionsMenu(menu, inflater) + menu.removeItem(ID_MENU_RUN) + menu.add(Menu.FIRST, ID_MENU_RUN, 0, rh.gs(R.string.openapsma_run)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER) + menu.setGroupDividerEnabled(true) + } + + override fun onOptionsItemSelected(item: MenuItem): Boolean = + when (item.itemId) { + ID_MENU_RUN -> { + binding.lastrun.text = rh.gs(R.string.executing) + activePlugin.activeAPS.invoke("OpenAPSSMB menu", false) + true + } + + else -> false + } + @Synchronized override fun onResume() { super.onResume() @@ -65,14 +89,14 @@ class OpenAPSSMBFragment : DaggerFragment() { .toObservable(EventOpenAPSUpdateGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - updateGUI() - }, fabricPrivacy::logException) + updateGUI() + }, fabricPrivacy::logException) disposable += rxBus .toObservable(EventOpenAPSUpdateResultGui::class.java) .observeOn(aapsSchedulers.main) .subscribe({ - updateResultGUI(it.text) - }, fabricPrivacy::logException) + updateResultGUI(it.text) + }, fabricPrivacy::logException) updateGUI() } @@ -122,6 +146,7 @@ class OpenAPSSMBFragment : DaggerFragment() { openAPSSMBPlugin.lastAutosensResult.let { binding.autosensdata.text = jsonFormatter.format(it.json()) } + binding.swipeRefresh.isRefreshing = false } @Synchronized @@ -137,5 +162,6 @@ class OpenAPSSMBFragment : DaggerFragment() { binding.scriptdebugdata.text = "" binding.request.text = "" binding.lastrun.text = "" + binding.swipeRefresh.isRefreshing = false } } diff --git a/app/src/main/res/layout/loop_fragment.xml b/app/src/main/res/layout/loop_fragment.xml index bdd975b74e..0cc0e604ac 100644 --- a/app/src/main/res/layout/loop_fragment.xml +++ b/app/src/main/res/layout/loop_fragment.xml @@ -1,22 +1,20 @@ - + + + android:paddingTop="2dp"> - - - + + + + diff --git a/app/src/main/res/layout/openapsama_fragment.xml b/app/src/main/res/layout/openapsama_fragment.xml index 8420607244..41cb359aa4 100644 --- a/app/src/main/res/layout/openapsama_fragment.xml +++ b/app/src/main/res/layout/openapsama_fragment.xml @@ -1,29 +1,20 @@ - + + + android:paddingTop="2dp"> - - - - - - - + + +