diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index afdd5b8..3d4f000 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -128,6 +128,10 @@ class MainActivity : AppCompatActivity() { } } + private fun getAllEvents(): ArrayList { + return logbook?.logs ?: arrayListOf() + } + private fun setListAdapter(items: ArrayList) { val adapter = LunaEventRecyclerAdapter(this, items) adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener { @@ -283,6 +287,9 @@ class MainActivity : AppCompatActivity() { } fun askNotes(lunaEvent: LunaEvent) { + val allEvents = getAllEvents() + val useQuantity = (lunaEvent.type != LunaEvent.TYPE_NOTE && lunaEvent.type != LunaEvent.TYPE_CUSTOM) + val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.dialog_notes, null) d.setTitle(lunaEvent.getTypeDescription(this)) @@ -290,11 +297,66 @@ class MainActivity : AppCompatActivity() { d.setView(dialogView) val notesET = dialogView.findViewById(R.id.notes_edittext) val qtyET = dialogView.findViewById(R.id.notes_qty_edittext) - if (lunaEvent.type == LunaEvent.TYPE_NOTE || lunaEvent.type == LunaEvent.TYPE_CUSTOM) + + val nextTextView = dialogView.findViewById(R.id.notes_template_next) + val clearTextView = dialogView.findViewById(R.id.notes_clear) + val prevTextView = dialogView.findViewById(R.id.notes_template_prev) + + fun updateContent(current: LunaEvent) { + val prevEvent = getPreviousSameEvent(current, allEvents) + val nextEvent = getNextSameEvent(current, allEvents) + + notesET.setText(current.notes) + if (useQuantity) { + qtyET.setText(current.quantity.toString()) + } + + if (nextEvent != null) { + nextTextView.setOnClickListener { + notesET.setText(nextEvent.notes) + if (useQuantity) { + qtyET.setText(nextEvent.quantity.toString()) + } + updateContent(nextEvent) + } + nextTextView.alpha = 1.0f + } else { + nextTextView.setOnClickListener {} + nextTextView.alpha = 0.5f + } + + if (prevEvent != null) { + prevTextView.setOnClickListener { + notesET.setText(prevEvent.notes) + if (useQuantity) { + qtyET.setText(prevEvent.quantity.toString()) + } + updateContent(prevEvent) + } + prevTextView.alpha = 1.0f + } else { + prevTextView.setOnClickListener {} + prevTextView.alpha = 0.5f + } + } + + clearTextView.setOnClickListener { + notesET.setText("") + if (useQuantity) { + qtyET.setText("") + } + updateContent(lunaEvent) + } + + if (!useQuantity) { qtyET.visibility = View.GONE + } + + updateContent(lunaEvent) + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> val qtyStr = qtyET.text.toString() - if (qtyStr.isNotEmpty()) { + if (qtyStr.isNotEmpty() && useQuantity) { val qty = qtyStr.toIntOrNull() if (qty == null) { Toast.makeText(this, R.string.toast_integer_error, Toast.LENGTH_SHORT).show() diff --git a/app/src/main/res/layout/dialog_notes.xml b/app/src/main/res/layout/dialog_notes.xml index 6d30a5e..ef0efea 100644 --- a/app/src/main/res/layout/dialog_notes.xml +++ b/app/src/main/res/layout/dialog_notes.xml @@ -24,4 +24,33 @@ android:hint="@string/log_notes_dialog_note_hint" android:background="@drawable/textview_background"/> + + + + + + + + \ No newline at end of file