diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index afdd5b8..79b7c09 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -6,6 +6,7 @@ import android.content.DialogInterface import android.content.Intent import android.os.Bundle import android.os.Handler +import android.text.Editable import android.util.Log import android.view.LayoutInflater import android.view.View @@ -21,6 +22,7 @@ import android.widget.Toast import androidx.appcompat.app.AlertDialog import androidx.appcompat.app.AppCompatActivity import androidx.core.content.ContextCompat +import androidx.core.widget.doOnTextChanged import androidx.recyclerview.widget.LinearLayoutManager import androidx.recyclerview.widget.RecyclerView import com.google.android.material.progressindicator.LinearProgressIndicator @@ -29,6 +31,7 @@ import com.thegrizzlylabs.sardineandroid.impl.SardineException import it.danieleverducci.lunatracker.adapters.LunaEventRecyclerAdapter import it.danieleverducci.lunatracker.entities.Logbook import it.danieleverducci.lunatracker.entities.LunaEvent +import it.danieleverducci.lunatracker.entities.LunaEvent.Companion.TYPE_BABY_BOTTLE import it.danieleverducci.lunatracker.repository.FileLogbookRepository import it.danieleverducci.lunatracker.repository.LocalSettingsRepository import it.danieleverducci.lunatracker.repository.LogbookListObtainedListener @@ -132,7 +135,7 @@ class MainActivity : AppCompatActivity() { val adapter = LunaEventRecyclerAdapter(this, items) adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener { override fun onItemClick(event: LunaEvent) { - showEventDetailDialog(event, items) + showEventDetailDialog(event) } } recyclerView.adapter = adapter @@ -195,25 +198,75 @@ class MainActivity : AppCompatActivity() { super.onStop() } - fun askBabyBottleContent() { - // Show number picker dialog - val localSettings = LocalSettingsRepository(this) + fun getAllEvents(): ArrayList { + return logbook?.logs ?: arrayListOf() + } + + fun askBabyBottleContent(existingEvent: LunaEvent? = null, onEnd: (() -> Unit)? = null) { + val isNewEvent = (existingEvent != null) + val event = if (existingEvent == null) { + LunaEvent(TYPE_BABY_BOTTLE) + } else { + existingEvent + } + + val previous = getPreviousSameEvent(event, getAllEvents()) val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.number_picker_dialog, null) d.setTitle(R.string.log_bottle_dialog_title) d.setMessage(R.string.log_bottle_dialog_description) d.setView(dialogView) + val numberPicker = dialogView.findViewById(R.id.dialog_number_picker) numberPicker.minValue = 1 // "10" numberPicker.maxValue = 25 // "250 numberPicker.displayedValues = ((10..250 step 10).map { it.toString() }.toTypedArray()) numberPicker.wrapSelectorWheel = false - numberPicker.value = localSettings.loadBabyBottleContent() - d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> - logEvent(LunaEvent(LunaEvent.TYPE_BABY_BOTTLE, numberPicker.value * 10)) - localSettings.saveBabyBottleContent(numberPicker.value) + if (previous != null) { + numberPicker.value = previous.quantity / 10 } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + + val dateTextView = dialogView.findViewById(R.id.dialog_date_picker) + dateTextView.text = + String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) + + val pickedDateTime = Calendar.getInstance() + val currentDateTime = Calendar.getInstance() + currentDateTime.time = Date(event.time * 1000) + dateTextView.setOnClickListener { + // Show datetime picker + val startYear = currentDateTime.get(Calendar.YEAR) + val startMonth = currentDateTime.get(Calendar.MONTH) + val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH) + val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY) + val startMinute = currentDateTime.get(Calendar.MINUTE) + + DatePickerDialog(this, { _, year, month, day -> + TimePickerDialog(this, { _, hour, minute -> + pickedDateTime.set(year, month, day, hour, minute) + val time = pickedDateTime.time.time / 1000 + dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(time)) + }, startHour, startMinute, android.text.format.DateFormat.is24HourFormat(this@MainActivity)).show() + }, startYear, startMonth, startDay).show() + } + + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + event.quantity = numberPicker.value * 10 + event.time = pickedDateTime.time.time / 1000 // Seconds since epoch + if (isNewEvent) { + logEvent(event) + } + logbook?.sort() + recyclerView.adapter?.notifyDataSetChanged() + saveLogbook() + onEnd?.invoke() + } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + onEnd?.invoke() + } + val alertDialog = d.create() alertDialog.show() } @@ -262,7 +315,14 @@ class MainActivity : AppCompatActivity() { alertDialog.show() } - fun askPukeValue() { + fun askPukeValue(existingEvent: LunaEvent? = null, onEnd: (() -> Unit)? = null) { + val isNewEvent = (existingEvent != null) + val event = if (existingEvent == null) { + LunaEvent(LunaEvent.TYPE_PUKE) + } else { + existingEvent + } + val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.puke_dialog, null) d.setTitle(R.string.log_puke_dialog_title) @@ -271,42 +331,162 @@ class MainActivity : AppCompatActivity() { val spinner = dialogView.findViewById(R.id.dialog_puke_value) spinner.adapter = ArrayAdapter.createFromResource(this, R.array.AmountLabels, android.R.layout.simple_spinner_dropdown_item) - spinner.setSelection(1) + spinner.setSelection(event.quantity) d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> - val pos = spinner.selectedItemPosition - logEvent(LunaEvent(LunaEvent.TYPE_PUKE, pos)) + event.quantity = spinner.selectedItemPosition + if (isNewEvent) { + logEvent(event) + } + onEnd?.invoke() + } + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + onEnd?.invoke } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } val alertDialog = d.create() alertDialog.show() } - fun askNotes(lunaEvent: LunaEvent) { + // Ask to edit events to be edited (only affects date) + fun askPlain(existingEvent: LunaEvent? = null, onEnd: (() -> Unit)? = null) { + val isNewEvent = (existingEvent != null) + val event = if (existingEvent == null) { + LunaEvent(LunaEvent.TYPE_NOTE) + } else { + existingEvent + } + + val d = AlertDialog.Builder(this) + val dialogView = layoutInflater.inflate(R.layout.dialog_plain_event, null) + d.setTitle(event.getTypeDescription(this)) + d.setMessage(event.getDialogMessage(this)) + d.setView(dialogView) + + val dateET = dialogView.findViewById(R.id.notes_date_picker) + + val pickedDateTime = Calendar.getInstance() + val currentDateTime = Calendar.getInstance() + currentDateTime.time = Date(event.time * 1000) + + dateET.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) + dateET.setOnClickListener { + // Show datetime picker + val startYear = currentDateTime.get(Calendar.YEAR) + val startMonth = currentDateTime.get(Calendar.MONTH) + val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH) + val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY) + val startMinute = currentDateTime.get(Calendar.MINUTE) + + DatePickerDialog(this, { _, year, month, day -> + TimePickerDialog(this, { _, hour, minute -> + pickedDateTime.set(year, month, day, hour, minute) + val time = pickedDateTime.time.time / 1000 + dateET.setText(String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(time))) + }, startHour, startMinute, android.text.format.DateFormat.is24HourFormat(this@MainActivity)).show() + }, startYear, startMonth, startDay).show() + } + + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + // Save event and move it to the right position in the logbook + event.time = pickedDateTime.time.time / 1000 // Seconds since epoch + if (isNewEvent) { + logEvent(event) + } + + logbook?.sort() + recyclerView.adapter?.notifyDataSetChanged() + saveLogbook() + + onEnd?.invoke() + } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + onEnd?.invoke() + } + + val alertDialog = d.create() + alertDialog.show() + } + + fun askNotes(existingEvent: LunaEvent? = null, onEnd: (() -> Unit)? = null) { + val isNewEvent = (existingEvent != null) + val event = if (existingEvent == null) { + LunaEvent(LunaEvent.TYPE_NOTE) + } else { + existingEvent + } + val useQuantity = (event.type != LunaEvent.TYPE_NOTE && event.type != LunaEvent.TYPE_CUSTOM) + val d = AlertDialog.Builder(this) val dialogView = layoutInflater.inflate(R.layout.dialog_notes, null) - d.setTitle(lunaEvent.getTypeDescription(this)) - d.setMessage(lunaEvent.getDialogMessage(this)) + d.setTitle(event.getTypeDescription(this)) + d.setMessage(event.getDialogMessage(this)) 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 dateET = dialogView.findViewById(R.id.notes_date_picker) + + val pickedDateTime = Calendar.getInstance() + val currentDateTime = Calendar.getInstance() + currentDateTime.time = Date(event.time * 1000) + + dateET.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) + dateET.setOnClickListener { + // Show datetime picker + val startYear = currentDateTime.get(Calendar.YEAR) + val startMonth = currentDateTime.get(Calendar.MONTH) + val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH) + val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY) + val startMinute = currentDateTime.get(Calendar.MINUTE) + + DatePickerDialog(this, { _, year, month, day -> + TimePickerDialog(this, { _, hour, minute -> + pickedDateTime.set(year, month, day, hour, minute) + val time = pickedDateTime.time.time / 1000 + dateET.setText(String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(time))) + }, startHour, startMinute, android.text.format.DateFormat.is24HourFormat(this@MainActivity)).show() + }, startYear, startMonth, startDay).show() + } + + notesET.setText(event.notes) + + if (useQuantity) { + qtyET.setText(event.quantity.toString()) + } else { qtyET.visibility = View.GONE + } + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> - val qtyStr = qtyET.text.toString() - if (qtyStr.isNotEmpty()) { - val qty = qtyStr.toIntOrNull() - if (qty == null) { + if (useQuantity) { + val qty = qtyET.text.toString().toIntOrNull() + if (qty != null) { + event.quantity = qty + } else { Toast.makeText(this, R.string.toast_integer_error, Toast.LENGTH_SHORT).show() + onEnd?.invoke() return@setPositiveButton } - lunaEvent.quantity = qty } - val notes = notesET.text.toString() - lunaEvent.notes = notes - logEvent(lunaEvent) + + event.notes = notesET.text.toString() + event.time = pickedDateTime.time.time / 1000 // Seconds since epoch + + if (isNewEvent) { + logEvent(event) + } + logbook?.sort() + recyclerView.adapter?.notifyDataSetChanged() + saveLogbook() + onEnd?.invoke() } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + onEnd?.invoke() + } + val alertDialog = d.create() alertDialog.show() } @@ -359,55 +539,57 @@ class MainActivity : AppCompatActivity() { return nextEvent } - fun showEventDetailDialog(event: LunaEvent, items: ArrayList) { + fun showEventDetailDialog(event: LunaEvent) { // Do not update list while the detail is shown, to avoid changing the object below while it is changed by the user pauseLogbookUpdate = true val d = AlertDialog.Builder(this) d.setTitle(R.string.dialog_event_detail_title) val dialogView = layoutInflater.inflate(R.layout.dialog_event_detail, null) - dialogView.findViewById(R.id.dialog_event_detail_type_emoji).text = event.getTypeEmoji(this) - dialogView.findViewById(R.id.dialog_event_detail_type_description).text = event.getTypeDescription(this) - dialogView.findViewById(R.id.dialog_event_detail_type_quantity).text = - NumericUtils(this).formatEventQuantity(event) - dialogView.findViewById(R.id.dialog_event_detail_type_notes).text = event.notes + val emojiTextView = dialogView.findViewById(R.id.dialog_event_detail_type_emoji) + val descriptionTextView = dialogView.findViewById(R.id.dialog_event_detail_type_description) + val quantityTextView = dialogView.findViewById(R.id.dialog_event_detail_type_quantity) + val notesTextView = dialogView.findViewById(R.id.dialog_event_detail_type_notes) - val currentDateTime = Calendar.getInstance() - currentDateTime.time = Date(event.time * 1000) + emojiTextView.text = event.getTypeEmoji(this) + descriptionTextView.text = event.getTypeDescription(this) + quantityTextView.text = NumericUtils(this).formatEventQuantity(event) + notesTextView.text = event.notes val dateTextView = dialogView.findViewById(R.id.dialog_event_detail_type_date) dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) - dateTextView.setOnClickListener { - // Show datetime picker - val startYear = currentDateTime.get(Calendar.YEAR) - val startMonth = currentDateTime.get(Calendar.MONTH) - val startDay = currentDateTime.get(Calendar.DAY_OF_MONTH) - val startHour = currentDateTime.get(Calendar.HOUR_OF_DAY) - val startMinute = currentDateTime.get(Calendar.MINUTE) - - DatePickerDialog(this, { _, year, month, day -> - TimePickerDialog(this, { _, hour, minute -> - val pickedDateTime = Calendar.getInstance() - pickedDateTime.set(year, month, day, hour, minute) - // Save event and move it to the right position in the logbook - event.time = pickedDateTime.time.time / 1000 // Seconds since epoch - dateTextView.text = String.format(getString(R.string.dialog_event_detail_datetime_icon), DateUtils.formatDateTime(event.time)) - logbook?.sort() - recyclerView.adapter?.notifyDataSetChanged() - saveLogbook() - }, startHour, startMinute, android.text.format.DateFormat.is24HourFormat(this@MainActivity)).show() - }, startYear, startMonth, startDay).show() - } d.setView(dialogView) d.setPositiveButton(R.string.dialog_event_detail_close_button) { dialogInterface, i -> dialogInterface.dismiss() } d.setNeutralButton(R.string.dialog_event_detail_delete_button) { dialogInterface, i -> deleteEvent(event) } + d.setNegativeButton(R.string.dialog_event_detail_edit_button) { dialogInterface, i -> + Log.d("MainActivity", "negative button pressed: ${event.type}") + when (event.type) { + TYPE_BABY_BOTTLE -> askBabyBottleContent(event, { showEventDetailDialog(event) }) + LunaEvent.TYPE_WEIGHT -> {} + LunaEvent.TYPE_BREASTFEEDING_LEFT_NIPPLE, + LunaEvent.TYPE_BREASTFEEDING_BOTH_NIPPLE, + LunaEvent.TYPE_BREASTFEEDING_RIGHT_NIPPLE, + LunaEvent.TYPE_ENEMA, + LunaEvent.TYPE_BATH, + LunaEvent.TYPE_COLIC, + LunaEvent.TYPE_DIAPERCHANGE_POO, + LunaEvent.TYPE_DIAPERCHANGE_PEE -> { askPlain(event, { showEventDetailDialog(event) }) } + LunaEvent.TYPE_MEDICINE -> askNotes(event, { showEventDetailDialog(event) }) + LunaEvent.TYPE_NOTE -> askNotes(event, { showEventDetailDialog(event) }) + LunaEvent.TYPE_TEMPERATURE, //todo + LunaEvent.TYPE_FOOD -> askNotes(event, { showEventDetailDialog(event) }) + LunaEvent.TYPE_PUKE -> askPukeValue(event, { showEventDetailDialog(event) }) + LunaEvent.TYPE_CUSTOM -> {} // todo? + else -> {} + } + } val alertDialog = d.create() alertDialog.show() alertDialog.getButton(DialogInterface.BUTTON_NEUTRAL).setTextColor(ContextCompat.getColor(this, R.color.danger)) - alertDialog.setOnDismissListener({ + alertDialog.setOnDismissListener { // Resume logbook update pauseLogbookUpdate = false - }) + } // show optional signature if (event.signature.isNotEmpty()) { @@ -420,8 +602,9 @@ class MainActivity : AppCompatActivity() { val previousTextView = dialogView.findViewById(R.id.dialog_event_previous) val nextTextView = dialogView.findViewById(R.id.dialog_event_next) - val nextEvent = getNextSameEvent(event, items) - val previousEvent = getPreviousSameEvent(event, items) + val allEvents = getAllEvents() + val nextEvent = getNextSameEvent(event, allEvents) + val previousEvent = getPreviousSameEvent(event, allEvents) if (previousEvent != null) { val emoji = previousEvent.getTypeEmoji(applicationContext) @@ -429,7 +612,7 @@ class MainActivity : AppCompatActivity() { previousTextView.text = String.format("⬅️ %s %s", emoji, time) previousTextView.setOnClickListener { alertDialog.cancel() - showEventDetailDialog(previousEvent, items) + showEventDetailDialog(previousEvent) } } else { previousTextView.visibility = View.GONE @@ -441,7 +624,7 @@ class MainActivity : AppCompatActivity() { nextTextView.text = String.format("%s %s ➡️", time, emoji) nextTextView.setOnClickListener { alertDialog.cancel() - showEventDetailDialog(nextEvent, items) + showEventDetailDialog(nextEvent) } } else { nextTextView.visibility = View.GONE diff --git a/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt b/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt index 1462820..9431194 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/repository/LocalSettingsRepository.kt @@ -23,14 +23,6 @@ class LocalSettingsRepository(val context: Context) { sharedPreferences = context.getSharedPreferences(SHARED_PREFS_FILE_NAME, MODE_PRIVATE) } - fun saveBabyBottleContent(content: Int) { - sharedPreferences.edit { putInt(SHARED_PREFS_BB_CONTENT, content) } - } - - fun loadBabyBottleContent(): Int { - return sharedPreferences.getInt(SHARED_PREFS_BB_CONTENT, 1) - } - fun saveSignature(content: String) { sharedPreferences.edit { putString(SHARED_PREFS_SIGNATURE, content) } } diff --git a/app/src/main/res/layout/dialog_event_detail.xml b/app/src/main/res/layout/dialog_event_detail.xml index 137baa9..dc5b5fd 100644 --- a/app/src/main/res/layout/dialog_event_detail.xml +++ b/app/src/main/res/layout/dialog_event_detail.xml @@ -1,6 +1,5 @@ + android:text="@string/dialog_event_detail_datetime_icon" /> + + \ No newline at end of file diff --git a/app/src/main/res/layout/number_picker_dialog.xml b/app/src/main/res/layout/number_picker_dialog.xml index ce2aa8b..e2f441a 100644 --- a/app/src/main/res/layout/number_picker_dialog.xml +++ b/app/src/main/res/layout/number_picker_dialog.xml @@ -3,17 +3,33 @@ xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" - android:layout_gravity="center" - android:gravity="center"> + android:orientation="vertical"> - + + + + + + + + android:layout_gravity="center" + android:layout_marginEnd="20dp"/> + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index c612ca8..d1f1ee9 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -15,6 +15,8 @@ Puke Select the amount + Edit + 🍼 🥣 ⚖️