Loop: fix custom menu, Automation: unify UI, new menu

This commit is contained in:
Milos Kozak 2022-05-05 16:46:55 +02:00
parent 22dac9b940
commit 3d9b08e5c2
5 changed files with 71 additions and 43 deletions

View file

@ -65,9 +65,11 @@ class LoopFragment : DaggerFragment() {
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)
if (isResumed) {
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 =

View file

@ -21,6 +21,7 @@ import info.nightscout.androidaps.automation.databinding.AutomationFragmentBindi
import info.nightscout.androidaps.database.entities.UserEntry.Action
import info.nightscout.androidaps.database.entities.UserEntry.Sources
import info.nightscout.androidaps.extensions.toVisibility
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.logging.UserEntryLogger
import info.nightscout.androidaps.plugins.bus.RxBus
import info.nightscout.androidaps.plugins.general.automation.dialogs.EditEventDialog
@ -34,7 +35,6 @@ import info.nightscout.androidaps.utils.alertDialogs.OKDialog
import info.nightscout.androidaps.utils.dragHelpers.ItemTouchHelperAdapter
import info.nightscout.androidaps.utils.dragHelpers.OnStartDragListener
import info.nightscout.androidaps.utils.dragHelpers.SimpleItemTouchHelperCallback
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
@ -50,6 +50,13 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
@Inject lateinit var injector: HasAndroidInjector
@Inject lateinit var uel: UserEntryLogger
companion object {
const val ID_MENU_ADD = 3
const val ID_MENU_RUN = 4
const val ID_MENU_EDIT_MOVE = 5
}
private var disposable: CompositeDisposable = CompositeDisposable()
private lateinit var eventListAdapter: EventListAdapter
private lateinit var actionHelper: ActionModeHelper<AutomationEvent>
@ -59,13 +66,14 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
// This property is only valid between onCreateView and onDestroyView.
private val binding get() = _binding!!
@SuppressLint("NotifyDataSetChanged")
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
_binding = AutomationFragmentBinding.inflate(inflater, container, false)
actionHelper = ActionModeHelper(rh, activity)
actionHelper.setUpdateListHandler { binding.eventListView.adapter?.notifyDataSetChanged() }
actionHelper.setOnRemoveHandler { removeSelected(it) }
actionHelper.enableSort = true
setHasOptionsMenu(actionHelper.inMenu)
setHasOptionsMenu(true)
return binding.root
}
@ -75,19 +83,45 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
binding.eventListView.layoutManager = LinearLayoutManager(context)
binding.eventListView.adapter = eventListAdapter
binding.logView.movementMethod = ScrollingMovementMethod()
binding.fabAddEvent.setOnClickListener {
actionHelper.finish()
val dialog = EditEventDialog()
val args = Bundle()
args.putString("event", AutomationEvent(injector).toJSON())
args.putInt("position", -1) // New event
dialog.arguments = args
dialog.show(childFragmentManager, "EditEventDialog")
}
itemTouchHelper.attachToRecyclerView(binding.eventListView)
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
if (isResumed) {
actionHelper.onCreateOptionsMenu(menu, inflater)
menu.removeItem(ID_MENU_ADD)
menu.removeItem(ID_MENU_RUN)
menu.add(Menu.FIRST, ID_MENU_ADD, 0, rh.gs(R.string.add_automation)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
menu.add(Menu.FIRST, ID_MENU_RUN, 0, rh.gs(R.string.run_automations)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
menu.add(Menu.FIRST, ID_MENU_EDIT_MOVE, 0, rh.gs(R.string.remove_sort)).setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER)
menu.setGroupDividerEnabled(true)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean =
if (actionHelper.onOptionsItemSelected(item)) true
else
when (item.itemId) {
ID_MENU_RUN -> {
Thread { automationPlugin.processActions() }.start()
true
}
ID_MENU_ADD -> {
add()
true
}
ID_MENU_EDIT_MOVE -> {
actionHelper.startAction()
true
}
else -> false
}
@SuppressLint("NotifyDataSetChanged")
@Synchronized
override fun onResume() {
@ -120,14 +154,7 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
_binding = null
}
override fun onCreateOptionsMenu(menu: Menu, inflater: MenuInflater) {
super.onCreateOptionsMenu(menu, inflater)
actionHelper.onCreateOptionsMenu(menu, inflater)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean =
actionHelper.onOptionsItemSelected(item)
@SuppressLint("NotifyDataSetChanged")
@Synchronized
private fun updateGui() {
if (_binding == null) return
@ -173,10 +200,13 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
override fun onBindViewHolder(holder: ViewHolder, position: Int) {
val automation = automationPlugin.at(position)
holder.binding.rootLayout.setBackgroundColor(
rh.gac( context,
if (automation.userAction) R.attr.userAction
else if (automation.areActionsValid()) R.attr.validActions
else R.attr.actionsError
rh.gac(
context,
when {
automation.userAction -> R.attr.userAction
automation.areActionsValid() -> R.attr.validActions
else -> R.attr.actionsError
}
)
)
holder.binding.eventTitle.text = automation.title
@ -280,4 +310,13 @@ class AutomationFragment : DaggerFragment(), OnStartDragListener {
}
}
private fun add() {
actionHelper.finish()
EditEventDialog().also {
it.arguments = Bundle().apply {
putString("event", AutomationEvent(injector).toJSON())
putInt("position", -1) // New event
}
}.show(childFragmentManager, "EditEventDialog")
}
}

View file

@ -24,7 +24,6 @@ import info.nightscout.androidaps.services.LocationServiceHelper
import info.nightscout.androidaps.utils.DateUtil
import info.nightscout.androidaps.utils.FabricPrivacy
import info.nightscout.androidaps.utils.T
import info.nightscout.androidaps.interfaces.ResourceHelper
import info.nightscout.androidaps.utils.rx.AapsSchedulers
import info.nightscout.shared.logging.AAPSLogger
import info.nightscout.shared.logging.LTag
@ -180,8 +179,7 @@ class AutomationPlugin @Inject constructor(
automationEvents.add(AutomationEvent(injector).fromJSON(event, 0))
}
@Synchronized
private fun processActions() {
@Synchronized internal fun processActions() {
var commonEventsEnabled = true
if (loop.isSuspended || !(loop as PluginBase).isEnabled()) {
aapsLogger.debug(LTag.AUTOMATION, "Loop deactivated")

View file

@ -21,18 +21,4 @@
android:scrollbars = "vertical"
android:layout_alignParentBottom="true" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fabAddEvent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
android:clickable="true"
android:focusable="true"
android:src="@drawable/ic_add_black_24dp"
tools:ignore="RelativeOverlap"
android:contentDescription="@string/addnew" />
</RelativeLayout>

View file

@ -123,5 +123,8 @@
<string name="confirm_remove_multiple_items">Are you sure you want to remove %1$d items</string>
<string name="sort_label">Sort</string>
<string name="system_automation">System automation</string>
<string name="run_automations">Run automations</string>
<string name="add_automation">Add rule</string>
<string name="remove_sort">Remove/sort</string>
</resources>