forked from penguin86/luna-tracker
Compare commits
5 Commits
c582fb64b7
...
bottle_tim
| Author | SHA1 | Date | |
|---|---|---|---|
| d76f5cf2ce | |||
| 557a9ab69a | |||
| 620e20aa2e | |||
| 8687d62bac | |||
| 3ae68ffa7b |
@@ -272,6 +272,9 @@ class MainActivity : AppCompatActivity() {
|
||||
numberPicker.wrapSelectorWheel = false
|
||||
numberPicker.value = event.quantity / 10
|
||||
|
||||
val numberPickerUnit = dialogView.findViewById<TextView>(R.id.dialog_number_picker_unit)
|
||||
numberPickerUnit.text = NumericUtils(this).measurement_unit_liquid_base
|
||||
|
||||
val dateTV = dialogView.findViewById<TextView>(R.id.dialog_date_picker)
|
||||
val pickedTime = dateTimePicker(event.time, dateTV)
|
||||
|
||||
@@ -418,11 +421,11 @@ class MainActivity : AppCompatActivity() {
|
||||
return dateTime
|
||||
}
|
||||
|
||||
fun addSleepEvent(event: LunaEvent) {
|
||||
askSleepValue(event, true) { saveEvent(event) }
|
||||
fun addDurationEvent(event: LunaEvent) {
|
||||
askDurationEvent(event, true) { saveEvent(event) }
|
||||
}
|
||||
|
||||
fun askSleepValue(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
|
||||
fun askDurationEvent(event: LunaEvent, showTime: Boolean, onPositive: () -> Unit) {
|
||||
val d = AlertDialog.Builder(this)
|
||||
val dialogView = layoutInflater.inflate(R.layout.dialog_edit_duration, null)
|
||||
d.setTitle(event.getDialogTitle(this))
|
||||
@@ -434,7 +437,7 @@ class MainActivity : AppCompatActivity() {
|
||||
val dateDelimiter = dialogView.findViewById<TextView>(R.id.dialog_date_range_delimiter)
|
||||
val durationButtons = dialogView.findViewById<LinearLayout>(R.id.duration_buttons)
|
||||
val durationNowButton = dialogView.findViewById<Button>(R.id.dialog_date_duration_now)
|
||||
val durationAsleepButton = dialogView.findViewById<Button>(R.id.dialog_date_duration_asleep)
|
||||
val durationClearButton = dialogView.findViewById<Button>(R.id.dialog_date_duration_clear)
|
||||
val durationMinus5Button = dialogView.findViewById<Button>(R.id.dialog_date_duration_minus5)
|
||||
val durationPlus5Button = dialogView.findViewById<Button>(R.id.dialog_date_duration_plus5)
|
||||
|
||||
@@ -442,8 +445,8 @@ class MainActivity : AppCompatActivity() {
|
||||
val invalidDurationTextColor = ContextCompat.getColor(this, R.color.danger)
|
||||
|
||||
// in seconds
|
||||
var sleepStart = event.getStartTime()
|
||||
var sleepEnd = event.getEndTime()
|
||||
var durationStart = event.getStartTime()
|
||||
var durationEnd = event.getEndTime()
|
||||
|
||||
fun isValidTime(timeUnix: Long): Boolean {
|
||||
val now = System.currentTimeMillis() / 1000
|
||||
@@ -454,50 +457,43 @@ class MainActivity : AppCompatActivity() {
|
||||
return (timeBeginUnix <= timeEndUnix) && (timeEndUnix - timeBeginUnix) < (24 * 60 * 60)
|
||||
}
|
||||
|
||||
// prevent printing of seconds
|
||||
fun adjustToMinute(unixTime: Long): Long {
|
||||
return unixTime - (unixTime % 60)
|
||||
}
|
||||
|
||||
fun updateFields() {
|
||||
datePickerBegin.text = DateUtils.formatDateTime(sleepStart)
|
||||
datePickerEnd.text = DateUtils.formatDateTime(sleepEnd)
|
||||
datePickerBegin.text = DateUtils.formatDateTime(durationStart)
|
||||
datePickerEnd.text = DateUtils.formatDateTime(durationEnd)
|
||||
|
||||
durationTextView.setTextColor(currentDurationTextColor)
|
||||
val duration = sleepEnd - sleepStart
|
||||
val duration = durationEnd - durationStart
|
||||
if (duration == 0L) {
|
||||
// baby is sleeping
|
||||
// event is ongoing
|
||||
durationTextView.text = "💤"
|
||||
dateDelimiter.visibility = View.GONE
|
||||
datePickerEnd.visibility = View.GONE
|
||||
} else {
|
||||
durationTextView.text = DateUtils.formatTimeDuration(applicationContext, duration)
|
||||
if (!isValidTimeSpan(sleepStart, sleepEnd)) {
|
||||
if (!isValidTimeSpan(durationStart, durationEnd)) {
|
||||
durationTextView.setTextColor(invalidDurationTextColor)
|
||||
}
|
||||
dateDelimiter.visibility = View.VISIBLE
|
||||
datePickerEnd.visibility = View.VISIBLE
|
||||
}
|
||||
|
||||
datePickerBegin.setTextColor(if (isValidTime(sleepStart)) { currentDurationTextColor } else { invalidDurationTextColor })
|
||||
datePickerEnd.setTextColor(if (isValidTime(sleepEnd)) { currentDurationTextColor } else { invalidDurationTextColor })
|
||||
datePickerBegin.setTextColor(if (isValidTime(durationStart)) { currentDurationTextColor } else { invalidDurationTextColor })
|
||||
datePickerEnd.setTextColor(if (isValidTime(durationEnd)) { currentDurationTextColor } else { invalidDurationTextColor })
|
||||
}
|
||||
|
||||
val pickedDateTimeBegin = dateTimePicker(event.time, datePickerBegin) { time: Long ->
|
||||
sleepStart = adjustToMinute(time)
|
||||
val pickedDateTimeBegin = dateTimePicker(event.time, datePickerBegin) { pickedTime: Long ->
|
||||
durationStart = pickedTime
|
||||
if (datePickerEnd.visibility == View.GONE) {
|
||||
durationEnd = pickedTime
|
||||
}
|
||||
updateFields()
|
||||
}
|
||||
|
||||
val pickedDateTimeEnd = dateTimePicker(event.time + event.quantity, datePickerEnd) { time: Long ->
|
||||
sleepEnd = adjustToMinute(time)
|
||||
val pickedDateTimeEnd = dateTimePicker(event.time + event.quantity, datePickerEnd) { pickedTime: Long ->
|
||||
durationEnd = pickedTime
|
||||
updateFields()
|
||||
}
|
||||
|
||||
sleepStart = adjustToMinute(pickedDateTimeBegin.time.time / 1000)
|
||||
sleepEnd = adjustToMinute(pickedDateTimeEnd.time.time / 1000)
|
||||
|
||||
updateFields()
|
||||
|
||||
if (showTime) {
|
||||
dateDelimiter.visibility = View.GONE
|
||||
datePickerEnd.visibility = View.GONE
|
||||
@@ -512,31 +508,35 @@ class MainActivity : AppCompatActivity() {
|
||||
d.setMessage(event.getDialogMessage(this))
|
||||
}
|
||||
|
||||
durationStart = pickedDateTimeBegin.time.time / 1000
|
||||
durationEnd = pickedDateTimeEnd.time.time / 1000
|
||||
|
||||
updateFields()
|
||||
|
||||
durationMinus5Button.setOnClickListener {
|
||||
sleepEnd = (sleepEnd - 300).coerceAtLeast(sleepStart)
|
||||
durationEnd = (durationEnd - 300).coerceAtLeast(durationStart)
|
||||
updateFields()
|
||||
}
|
||||
|
||||
durationPlus5Button.setOnClickListener {
|
||||
sleepEnd = (sleepEnd + 300).coerceAtLeast(sleepStart)
|
||||
durationEnd = (durationEnd + 300).coerceAtLeast(durationStart)
|
||||
updateFields()
|
||||
}
|
||||
|
||||
durationAsleepButton.setOnClickListener {
|
||||
sleepEnd = sleepStart
|
||||
durationClearButton.setOnClickListener {
|
||||
durationEnd = durationStart
|
||||
updateFields()
|
||||
}
|
||||
|
||||
durationNowButton.setOnClickListener {
|
||||
val now = System.currentTimeMillis() / 1000
|
||||
sleepEnd = adjustToMinute(now)
|
||||
durationEnd = System.currentTimeMillis() / 1000
|
||||
updateFields()
|
||||
}
|
||||
|
||||
d.setPositiveButton(android.R.string.ok) { dialogInterface, i ->
|
||||
if (isValidTime(sleepStart) && isValidTime(sleepEnd) && isValidTimeSpan(sleepStart, sleepEnd)) {
|
||||
event.time = sleepStart
|
||||
event.quantity = (sleepEnd - sleepStart).toInt()
|
||||
if (isValidTime(durationStart) && isValidTime(durationEnd) && isValidTimeSpan(durationStart, durationEnd)) {
|
||||
event.time = durationStart
|
||||
event.quantity = (durationEnd - durationStart).toInt()
|
||||
onPositive()
|
||||
} else {
|
||||
Toast.makeText(this, R.string.toast_date_error, Toast.LENGTH_SHORT).show()
|
||||
@@ -882,7 +882,7 @@ class MainActivity : AppCompatActivity() {
|
||||
LunaEvent.Type.PUKE -> askAmountValue(event, false, updateValues)
|
||||
LunaEvent.Type.TEMPERATURE -> askTemperatureValue(event, false, updateValues)
|
||||
LunaEvent.Type.NOTE -> askNotes(event, false, updateValues)
|
||||
LunaEvent.Type.SLEEP -> askSleepValue(event, false, updateValues)
|
||||
LunaEvent.Type.SLEEP -> askDurationEvent(event, false, updateValues)
|
||||
else -> {
|
||||
Log.w(TAG, "Unexpected type: ${event.type}")
|
||||
}
|
||||
@@ -1309,7 +1309,7 @@ class MainActivity : AppCompatActivity() {
|
||||
LunaEvent.Type.FOOD -> addNoteEvent(event)
|
||||
LunaEvent.Type.PUKE -> addAmountEvent(event)
|
||||
LunaEvent.Type.BATH -> addPlainEvent(event)
|
||||
LunaEvent.Type.SLEEP -> addSleepEvent(event)
|
||||
LunaEvent.Type.SLEEP -> addDurationEvent(event)
|
||||
LunaEvent.Type.UNKNOWN -> {} // ignore
|
||||
}
|
||||
}
|
||||
|
||||
@@ -174,7 +174,7 @@ class LunaEvent: Comparable<LunaEvent> {
|
||||
Type.DIAPERCHANGE_PEE,
|
||||
Type.PUKE -> R.string.log_amount_dialog_description
|
||||
Type.WEIGHT -> R.string.log_weight_dialog_description
|
||||
Type.SLEEP -> R.string.log_sleep_dialog_description
|
||||
Type.SLEEP -> R.string.log_duration_dialog_description
|
||||
else -> R.string.log_unknown_dialog_description
|
||||
}
|
||||
)
|
||||
|
||||
@@ -9,10 +9,15 @@ import java.util.Date
|
||||
class DateUtils {
|
||||
companion object {
|
||||
/**
|
||||
* Format time duration in seconds as e.g. "2 hours, 1 min".
|
||||
* Format time duration in seconds as e.g. "2 hours, 1 min", rounded to minutes.
|
||||
* Used for the duration to the next/previous event in the event details dialog.
|
||||
*/
|
||||
fun formatTimeDuration(context: Context, secondsDiff: Long): String {
|
||||
val adjusted = (secondsDiff + 30) - (secondsDiff + 30) % 60
|
||||
return formatTimeDurationExact(context, adjusted)
|
||||
}
|
||||
|
||||
fun formatTimeDurationExact(context: Context, secondsDiff: Long): String {
|
||||
var seconds = secondsDiff
|
||||
|
||||
val years = (seconds / (365 * 24 * 60 * 60F)).toLong()
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
android:layout_height="wrap_content"/>
|
||||
|
||||
<TextView
|
||||
android:id="@+id/dialog_number_picker_unit"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginStart="10dp"
|
||||
|
||||
@@ -39,11 +39,11 @@
|
||||
android:text="@string/dialog_duration_button_now"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/dialog_date_duration_asleep"
|
||||
android:id="@+id/dialog_date_duration_clear"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_weight="1"
|
||||
android:text="@string/dialog_duration_button_asleep"/>
|
||||
android:text="@string/dialog_duration_button_clear"/>
|
||||
|
||||
<Button
|
||||
android:id="@+id/dialog_date_duration_plus5"
|
||||
|
||||
@@ -130,7 +130,7 @@
|
||||
<string name="log_temperature_dialog_description">Select the temperature:</string>
|
||||
<string name="log_unknown_dialog_description"></string>
|
||||
<string name="log_weight_dialog_description">Insert the weight:</string>
|
||||
<string name="log_sleep_dialog_description">Set sleep duration:</string>
|
||||
<string name="log_duration_dialog_description">Set duration:</string>
|
||||
|
||||
<string name="measurement_unit_liquid_base_metric" translatable="false">ml</string>
|
||||
<string name="measurement_unit_weight_base_metric" translatable="false">g</string>
|
||||
@@ -160,9 +160,9 @@
|
||||
<string name="dialog_event_detail_notes">Notes</string>
|
||||
<string name="dialog_event_detail_signature">by %s</string>
|
||||
|
||||
<string name="dialog_duration_button_asleep">asleep</string>
|
||||
<string name="dialog_duration_button_clear">Clear</string>
|
||||
<string name="dialog_duration_button_minus_5">-5 min</string>
|
||||
<string name="dialog_duration_button_now">now</string>
|
||||
<string name="dialog_duration_button_now">Now</string>
|
||||
<string name="dialog_duration_button_plus_5">+5 min</string>
|
||||
|
||||
<string name="dialog_add_logbook_title">Add logbook</string>
|
||||
|
||||
Reference in New Issue
Block a user