diff --git a/app/build.gradle.kts b/app/build.gradle.kts index 06e1f6a..b55793c 100644 --- a/app/build.gradle.kts +++ b/app/build.gradle.kts @@ -6,12 +6,12 @@ plugins { android { namespace = "it.danieleverducci.lunatracker" - compileSdk = 34 + compileSdk = 36 defaultConfig { applicationId = "it.danieleverducci.lunatracker" minSdk = 21 - targetSdk = 34 + targetSdk = 36 versionCode = 7 versionName = "0.9" @@ -31,6 +31,12 @@ android { sourceCompatibility = JavaVersion.VERSION_11 targetCompatibility = JavaVersion.VERSION_11 } + dependenciesInfo { + // Disables dependency metadata when building APKs. + includeInApk = false + // Disables dependency metadata when building Android App Bundles. + includeInBundle = false + } kotlinOptions { jvmTarget = "11" } @@ -40,7 +46,6 @@ android { } dependencies { - implementation(libs.androidx.core.ktx) implementation(libs.androidx.lifecycle.runtime.ktx) implementation(libs.androidx.activity.compose) @@ -51,7 +56,7 @@ dependencies { implementation(libs.androidx.material3) implementation(libs.androidx.appcompat) implementation(libs.androidx.recyclerview) - implementation("com.github.thegrizzlylabs:sardine-android:v0.9") + implementation(libs.sardine.android) implementation(libs.material) testImplementation(libs.junit) androidTestImplementation(libs.androidx.junit) diff --git a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt index d2900e9..4a1d8d4 100644 --- a/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt +++ b/app/src/main/java/it/danieleverducci/lunatracker/MainActivity.kt @@ -6,13 +6,16 @@ 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 import android.view.ViewGroup import android.widget.AdapterView import android.widget.ArrayAdapter +import android.widget.Button import android.widget.EditText +import android.widget.LinearLayout import android.widget.NumberPicker import android.widget.PopupWindow import android.widget.Spinner @@ -49,6 +52,9 @@ class MainActivity : AppCompatActivity() { const val TAG = "MainActivity" const val UPDATE_EVERY_SECS: Long = 30 const val DEBUG_CHECK_LOGBOOK_CONSISTENCY = false + // list of all events + var allEvents = arrayListOf() + var currentPopupItems = listOf() } var logbook: Logbook? = null @@ -62,7 +68,7 @@ class MainActivity : AppCompatActivity() { val updateListRunnable: Runnable = Runnable { if (logbook != null && !pauseLogbookUpdate) loadLogbook(logbook!!.name) - handler.postDelayed(updateListRunnable, 1000*60) + handler.postDelayed(updateListRunnable, 1000 * 60) } var logbookRepo: LogbookRepository? = null var showingOverflowPopupWindow = false @@ -81,34 +87,10 @@ class MainActivity : AppCompatActivity() { recyclerView.setLayoutManager(LinearLayoutManager(applicationContext)) // Set listeners - findViewById(R.id.logbooks_add_button).setOnClickListener { showAddLogbookDialog(true) } - findViewById(R.id.button_bottle).setOnClickListener { askBabyBottleContent() } - findViewById(R.id.button_food).setOnClickListener { askNotes(LunaEvent(LunaEvent.TYPE_FOOD)) } - findViewById(R.id.button_nipple_left).setOnClickListener { logEvent( - LunaEvent( - LunaEvent.TYPE_BREASTFEEDING_LEFT_NIPPLE - ) - ) } - findViewById(R.id.button_nipple_both).setOnClickListener { logEvent( - LunaEvent( - LunaEvent.TYPE_BREASTFEEDING_BOTH_NIPPLE - ) - ) } - findViewById(R.id.button_nipple_right).setOnClickListener { logEvent( - LunaEvent( - LunaEvent.TYPE_BREASTFEEDING_RIGHT_NIPPLE - ) - ) } - findViewById(R.id.button_change_poo).setOnClickListener { logEvent( - LunaEvent( - LunaEvent.TYPE_DIAPERCHANGE_POO - ) - ) } - findViewById(R.id.button_change_pee).setOnClickListener { logEvent( - LunaEvent( - LunaEvent.TYPE_DIAPERCHANGE_PEE - ) - ) } + findViewById(R.id.logbooks_add_button).setOnClickListener { + showAddLogbookDialog(true) + } + val moreButton = findViewById(R.id.button_more) moreButton.setOnClickListener { showOverflowPopupWindow(moreButton) @@ -124,15 +106,19 @@ class MainActivity : AppCompatActivity() { loadLogbookList() } findViewById(R.id.button_sync).setOnClickListener { - loadLogbookList() + if (logbook != null) { + loadLogbook(logbook!!.name) + } else { + loadLogbookList() + } } } private fun setListAdapter(items: ArrayList) { val adapter = LunaEventRecyclerAdapter(this, items) - adapter.onItemClickListener = object: LunaEventRecyclerAdapter.OnItemClickListener { + adapter.onItemClickListener = object : LunaEventRecyclerAdapter.OnItemClickListener { override fun onItemClick(event: LunaEvent) { - showEventDetailDialog(event, items) + showEventDetailDialog(event) } } recyclerView.adapter = adapter @@ -148,7 +134,79 @@ class MainActivity : AppCompatActivity() { if (logbook == null) Log.w(TAG, "showLogbook(): logbook is null!") - setListAdapter(logbook?.logs ?: arrayListOf()) + allEvents = logbook?.logs ?: arrayListOf() + setListAdapter(allEvents) + } + + private fun populateHeaderMenu() { + val settingsRepository = LocalSettingsRepository(this) + val dynamicMenu = settingsRepository.loadDynamicMenu() + val eventTypeStats = mutableMapOf() + + if (dynamicMenu) { + // populate frequency map from all events of the last two weeks + val lastWeekTime = (System.currentTimeMillis() / 1000) - (14 * 24 * 60 * 60) + allEvents.forEach { + if (it.time > lastWeekTime) { + eventTypeStats[it.type] = 1 + (eventTypeStats[it.type] ?: 0) + } + } + } + + // sort all event types by frequency and ordinal + val eventTypesSorted = LunaEvent.Type.entries.toList().sortedWith( + compareBy({ -1 * (eventTypeStats[it] ?: 0) }, { it.ordinal }) + ).filter { it != LunaEvent.Type.UNKNOWN } + + val usedEventCount = eventTypeStats.count { it.value > 0 } + val maxButtonCount = if (dynamicMenu) { usedEventCount } else { 7 } + + val row1 = findViewById(R.id.linear_layout_row1) + val row1Button1 = findViewById(R.id.button1_row1) + val row1Button2 = findViewById(R.id.button2_row1) + + val row2 = findViewById(R.id.linear_layout_row2) + val row2Button1 = findViewById(R.id.button1_row2) + val row2Button2 = findViewById(R.id.button2_row2) + val row2Button3 = findViewById(R.id.button3_row2) + + val row3 = findViewById(R.id.linear_layout_row3) + val row3Button1 = findViewById(R.id.button1_row3) + val row3Button2 = findViewById(R.id.button2_row3) + + // hide all rows/buttons (except row 3) + for (view in listOf(row1, row1Button1, row1Button2, + row2, row2Button1, row2Button2, row2Button3, + row3, row3Button1, row3Button2)) { + view.visibility = View.GONE + } + row3.visibility = View.VISIBLE + + var showCounter = 0 + + fun show(vararg tvs: TextView) { + for (tv in tvs) { + val type = eventTypesSorted[showCounter] + tv.text = LunaEvent.getHeaderEmoji(applicationContext, type) + tv.setOnClickListener { showCreateDialog(type) } + tv.visibility = View.VISIBLE + // show parent row + (tv.parent as View).visibility = View.VISIBLE + showCounter += 1 + } + } + + when (maxButtonCount) { + 0 -> { } // ignore - show empty row3 + 1 -> show(row3Button1) + 2 -> show(row3Button1, row3Button2) + 3 -> show(row1Button1, row3Button1) + 4, 5, 6 -> show(row1Button1, row1Button2, row3Button1, row3Button2) + else -> show(row1Button1, row1Button2, row2Button1, row2Button2, row2Button3, row3Button1, row3Button2) + } + + // store left over events for popup menu + currentPopupItems = eventTypesSorted.subList(showCounter, eventTypesSorted.size) } override fun onStart() { @@ -171,12 +229,6 @@ class MainActivity : AppCompatActivity() { signature = settingsRepository.loadSignature() - val noBreastfeeding = settingsRepository.loadNoBreastfeeding() - findViewById(R.id.layout_nipples).visibility = when (noBreastfeeding) { - true -> View.GONE - false -> View.VISIBLE - } - // Update list dates recyclerView.adapter?.notifyDataSetChanged() @@ -195,118 +247,515 @@ class MainActivity : AppCompatActivity() { super.onStop() } - fun askBabyBottleContent() { - // Show number picker dialog - val localSettings = LocalSettingsRepository(this) + fun addBabyBottleEvent(event: LunaEvent) { + setToPreviousQuantity(event) + askBabyBottleContent(event, true) { + saveEvent(event) + } + } + + fun askBabyBottleContent(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { 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) + val dialogView = layoutInflater.inflate(R.layout.dialog_edit_bottle, null) + d.setTitle(event.getDialogTitle(this)) + d.setMessage(event.getDialogMessage(this)) 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.maxValue = 34 // "340 + numberPicker.displayedValues = ((10..340 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) + numberPicker.value = event.quantity / 10 + + val numberPickerUnit = dialogView.findViewById(R.id.dialog_number_picker_unit) + numberPickerUnit.text = NumericUtils(this).measurement_unit_liquid_base + + val dateTV = dialogView.findViewById(R.id.dialog_date_picker) + val pickedTime = dateTimePicker(event.time, dateTV) + + if (!showTime) { + dateTV.visibility = View.GONE } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + event.time = pickedTime.time.time / 1000 + event.quantity = numberPicker.value * 10 + onPositive() + dialogInterface.dismiss() + } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + } + val alertDialog = d.create() alertDialog.show() } - fun askWeightValue() { + fun addWeightEvent(event: LunaEvent) { + setToPreviousQuantity(event) + askWeightValue(event, true) { saveEvent(event) } + } + + fun askWeightValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { // Show number picker dialog val d = AlertDialog.Builder(this) - val dialogView = layoutInflater.inflate(R.layout.number_edit_dialog, null) - d.setTitle(R.string.log_weight_dialog_title) - d.setMessage(R.string.log_weight_dialog_description) + val dialogView = layoutInflater.inflate(R.layout.dialog_edit_weight, null) + d.setTitle(event.getDialogTitle(this)) + d.setMessage(event.getDialogMessage(this)) d.setView(dialogView) + val weightET = dialogView.findViewById(R.id.dialog_number_edittext) + weightET.setText(event.quantity.toString()) + + val unitTV = dialogView.findViewById(R.id.dialog_number_unit) + unitTV.text = NumericUtils(this).measurement_unit_weight_base + + val dateTV = dialogView.findViewById(R.id.dialog_date_picker) + val pickedTime = dateTimePicker(event.time, dateTV) + + if (!showTime) { + dateTV.visibility = View.GONE + } + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> val weight = weightET.text.toString().toIntOrNull() - if (weight != null) - logEvent(LunaEvent(LunaEvent.TYPE_WEIGHT, weight)) - else + if (weight != null) { + event.time = pickedTime.time.time / 1000 + event.quantity = weight + onPositive() + } else { Toast.makeText(this, R.string.toast_integer_error, Toast.LENGTH_SHORT).show() + } + + dialogInterface.dismiss() } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + } + val alertDialog = d.create() alertDialog.show() } - fun askTemperatureValue() { + fun addHeightEvent(event: LunaEvent) { + setToPreviousQuantity(event) + askHeightValue(event, true) { saveEvent(event) } + } + + fun askHeightValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { // Show number picker dialog val d = AlertDialog.Builder(this) - val dialogView = layoutInflater.inflate(R.layout.temperature_dialog, null) - d.setTitle(R.string.log_temperature_dialog_title) - d.setMessage(R.string.log_temperature_dialog_description) + val dialogView = layoutInflater.inflate(R.layout.dialog_edit_height, null) + d.setTitle(event.getDialogTitle(this)) + d.setMessage(event.getDialogMessage(this)) d.setView(dialogView) + + val heightET = dialogView.findViewById(R.id.dialog_number_edittext) + heightET.setText(event.quantity.toString()) + + val unitTV = dialogView.findViewById(R.id.dialog_number_unit) + unitTV.text = NumericUtils(this).measurement_unit_height_base + + val dateTV = dialogView.findViewById(R.id.dialog_date_picker) + val pickedTime = dateTimePicker(event.time, dateTV) + + if (!showTime) { + dateTV.visibility = View.GONE + } + + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + val height = heightET.text.toString().toIntOrNull() + if (height != null) { + event.time = pickedTime.time.time / 1000 + event.quantity = height + onPositive() + } else { + Toast.makeText(this, R.string.toast_integer_error, Toast.LENGTH_SHORT).show() + } + + dialogInterface.dismiss() + } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + } + + val alertDialog = d.create() + alertDialog.show() + } + + fun addTemperatureEvent(event: LunaEvent) { + setToPreviousQuantity(event) + askTemperatureValue(event, true) { saveEvent(event) } + } + + fun askTemperatureValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { + // Show number picker dialog + val d = AlertDialog.Builder(this) + val dialogView = layoutInflater.inflate(R.layout.dialog_edit_temperature, null) + d.setTitle(event.getDialogTitle(this)) + d.setMessage(event.getDialogMessage(this)) + d.setView(dialogView) + val tempSlider = dialogView.findViewById(R.id.dialog_temperature_value) - val range = NumericUtils(this).getValidEventQuantityRange(LunaEvent.TYPE_TEMPERATURE)!! + val range = NumericUtils(this).getValidEventQuantityRange(LunaEvent.Type.TEMPERATURE)!! tempSlider.valueFrom = range.first.toFloat() tempSlider.valueTo = range.second.toFloat() - tempSlider.value = range.third.toFloat() - val tempTextView = dialogView.findViewById(R.id.dialog_temperature_display) - tempTextView.text = range.third.toString() - tempSlider.addOnChangeListener({s, v, b -> tempTextView.text = v.toString()}) - d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> - val temperature = (tempSlider.value * 10).toInt() // In tenth of a grade - logEvent(LunaEvent(LunaEvent.TYPE_TEMPERATURE, temperature)) + tempSlider.value = if (event.quantity == 0) { + range.third.toFloat() // default + } else { + event.quantity.toFloat() / 10 } - d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> dialogInterface.dismiss() } + + val dateTV = dialogView.findViewById(R.id.dialog_date_picker) + val pickedTime = dateTimePicker(event.time, dateTV) + if (!showTime) { + dateTV.visibility = View.GONE + } + + val tempTextView = dialogView.findViewById(R.id.dialog_temperature_display) + tempTextView.text = tempSlider.value.toString() + tempSlider.addOnChangeListener({ s, v, b -> tempTextView.text = v.toString() }) + + d.setPositiveButton(android.R.string.ok) { dialogInterface, i -> + event.time = pickedTime.time.time / 1000 + event.quantity = (tempSlider.value * 10).toInt() // temperature in tenth of a grade + onPositive() + dialogInterface.dismiss() + } + + d.setNegativeButton(android.R.string.cancel) { dialogInterface, i -> + dialogInterface.dismiss() + } + val alertDialog = d.create() alertDialog.show() } - fun askPukeValue() { + // Pick a date/time. + fun dateTimePicker(time: Long, dateTextView: TextView, onChange: (Long) -> Unit = {}): Calendar { + dateTextView.text = DateUtils.formatDateTime(time) + + val dateTime = Calendar.getInstance() + dateTime.time = Date(time * 1000) + dateTextView.setOnClickListener { + // Show datetime picker + val startYear = dateTime.get(Calendar.YEAR) + val startMonth = dateTime.get(Calendar.MONTH) + val startDay = dateTime.get(Calendar.DAY_OF_MONTH) + val startHour = dateTime.get(Calendar.HOUR_OF_DAY) + val startMinute = dateTime.get(Calendar.MINUTE) + + DatePickerDialog(this, { _, year, month, day -> + TimePickerDialog( + this, + { _, hour, minute -> + dateTime.set(year, month, day, hour, minute) + dateTextView.text = DateUtils.formatDateTime(dateTime.time.time / 1000) + onChange.invoke(dateTime.time.time / 1000) + }, + startHour, + startMinute, + android.text.format.DateFormat.is24HourFormat(this@MainActivity) + ).show() + }, startYear, startMonth, startDay).show() + } + + return dateTime + } + + fun addDurationEvent(event: LunaEvent) { + askDurationEvent(event, true) { saveEvent(event) } + } + + fun askDurationEvent(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) { val d = AlertDialog.Builder(this) - val dialogView = layoutInflater.inflate(R.layout.puke_dialog, null) - d.setTitle(R.string.log_puke_dialog_title) - d.setMessage(R.string.log_puke_dialog_description) + val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null) + d.setTitle(event.getDialogTitle(this)) d.setView(dialogView) - 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) + val durationTV = dialogView.findViewById(R.id.dialog_date_duration) + val datePickerBeginTV = dialogView.findViewById(R.id.dialog_date_picker_begin) + val datePickerEndTV = dialogView.findViewById(R.id.dialog_date_picker_end) + val dateDelimiterTV = dialogView.findViewById(R.id.dialog_date_range_delimiter) + val durationButtons = dialogView.findViewById(R.id.duration_buttons) + val durationNowButton = dialogView.findViewById