From 63605e3702f3211ecfec30e7017bc5f9b3ca9bf9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Felix=20N=C3=BCsse?= Date: Sun, 24 Nov 2024 19:33:16 +0100 Subject: [PATCH] show timestamp on click --- .../handler/volume/VolumeState.kt | 19 ++------------- .../timedsilence/ui/LogEntryAdapter.kt | 23 +++++++++++++++++-- .../felixnuesse/timedsilence/util/DateUtil.kt | 16 ++++++++++--- app/src/main/res/drawable/icon_list_alt.xml | 7 ++++++ app/src/main/res/layout/activity_main.xml | 2 +- app/src/main/res/layout/adapter_log_entry.xml | 1 + .../timedsilence/ExampleUnitTest.kt | 17 -------------- 7 files changed, 45 insertions(+), 40 deletions(-) create mode 100644 app/src/main/res/drawable/icon_list_alt.xml delete mode 100644 app/src/test/java/de/felixnuesse/timedsilence/ExampleUnitTest.kt diff --git a/app/src/main/java/de/felixnuesse/timedsilence/handler/volume/VolumeState.kt b/app/src/main/java/de/felixnuesse/timedsilence/handler/volume/VolumeState.kt index c9c61c6d..a3e4bd09 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/handler/volume/VolumeState.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/handler/volume/VolumeState.kt @@ -13,12 +13,6 @@ import de.felixnuesse.timedsilence.Constants.Companion.REASON_UNDEFINED import de.felixnuesse.timedsilence.Constants.Companion.REASON_WIFI import de.felixnuesse.timedsilence.util.DateUtil import kotlinx.serialization.Serializable -import java.time.LocalDate -import java.time.LocalDateTime -import java.time.LocalTime -import java.time.ZoneId -import java.time.format.DateTimeFormatter -import java.time.format.FormatStyle @Serializable @@ -27,9 +21,9 @@ class VolumeState( var state: Int ) { - @PrimaryKey(autoGenerate = true) + @PrimaryKey @ColumnInfo(name = "id") - var id: Long = 0 + var id: Long = System.currentTimeMillis() companion object { @@ -103,15 +97,6 @@ class VolumeState( return DateUtil.getDate(endTime, "HH:mm") } - private fun getFormattedDate(time: Long): String { - var today = LocalDate.now(ZoneId.systemDefault()) - var todayMidnight = LocalDateTime.of(today, LocalTime.MIDNIGHT) - val timestamp = todayMidnight.plusMinutes(time) - - var shortFormat = DateTimeFormatter.ofLocalizedTime(FormatStyle.SHORT) - return timestamp.toLocalTime().format(shortFormat) - } - fun stateString(): String { return when (state) { TIME_SETTING_SILENT -> "SILENT" diff --git a/app/src/main/java/de/felixnuesse/timedsilence/ui/LogEntryAdapter.kt b/app/src/main/java/de/felixnuesse/timedsilence/ui/LogEntryAdapter.kt index 6cc09d50..77f09982 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/ui/LogEntryAdapter.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/ui/LogEntryAdapter.kt @@ -1,16 +1,22 @@ package de.felixnuesse.timedsilence.ui; -import androidx.recyclerview.widget.RecyclerView +import android.content.Context import android.view.LayoutInflater import android.view.ViewGroup +import android.widget.Toast import androidx.appcompat.content.res.AppCompatResources +import androidx.recyclerview.widget.RecyclerView import de.felixnuesse.timedsilence.R import de.felixnuesse.timedsilence.databinding.AdapterLogEntryBinding import de.felixnuesse.timedsilence.handler.volume.VolumeState import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_LOUD import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_SILENT import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_VIBRATE -import kotlin.collections.ArrayList +import de.felixnuesse.timedsilence.util.DateUtil +import java.time.ZoneId +import java.time.format.DateTimeFormatter +import java.time.format.FormatStyle +import java.util.Locale /** @@ -60,6 +66,19 @@ class LogEntryAdapter(private val entries: List) : RecyclerView.Ada holder.logView.logTitle.text = logentry.reasonSource holder.logView.logContent.text = logentry.getReason() + holder.logView.card.setOnClickListener { showTimestamp(logentry.id, context) } + holder.logView.logTitle.setOnClickListener { showTimestamp(logentry.id, context) } + holder.logView.logContent.setOnClickListener { showTimestamp(logentry.id, context) } + holder.logView.stateIcon.setOnClickListener { showTimestamp(logentry.id, context) } + } + + private fun showTimestamp(inMs: Long, context: Context) { + val format = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM) + val text = DateUtil.format(inMs, format) + Toast.makeText( + context, text, + Toast.LENGTH_LONG + ).show() } // Return the size of your dataset (invoked by the layout manager) diff --git a/app/src/main/java/de/felixnuesse/timedsilence/util/DateUtil.kt b/app/src/main/java/de/felixnuesse/timedsilence/util/DateUtil.kt index 502c67b2..a63d74bf 100644 --- a/app/src/main/java/de/felixnuesse/timedsilence/util/DateUtil.kt +++ b/app/src/main/java/de/felixnuesse/timedsilence/util/DateUtil.kt @@ -1,15 +1,17 @@ package de.felixnuesse.timedsilence.util -import androidx.annotation.RequiresApi +import java.lang.System.currentTimeMillis import java.text.SimpleDateFormat import java.time.Duration import java.time.Instant import java.time.LocalDate import java.time.LocalDateTime import java.time.LocalTime +import java.time.Month import java.time.ZoneId -import java.time.temporal.Temporal -import java.util.* +import java.time.format.DateTimeFormatter +import java.util.Calendar +import java.util.Locale /** @@ -91,6 +93,14 @@ class DateUtil{ val midnight: LocalTime = LocalTime.MIDNIGHT return LocalDateTime.of(date.toLocalDate(), midnight) } + + fun format(milliSeconds: Long, format: DateTimeFormatter): String { + val localDateTime = Instant.ofEpochMilli(milliSeconds) + .atZone(ZoneId.systemDefault()) + .toLocalDateTime() + val dateTime = localDateTime.atZone(ZoneId.systemDefault()) + return dateTime.format(format) + } } } \ No newline at end of file diff --git a/app/src/main/res/drawable/icon_list_alt.xml b/app/src/main/res/drawable/icon_list_alt.xml new file mode 100644 index 00000000..c1c563b2 --- /dev/null +++ b/app/src/main/res/drawable/icon_list_alt.xml @@ -0,0 +1,7 @@ + + + + + + + diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 52aef679..e2546b06 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -73,7 +73,7 @@ diff --git a/app/src/main/res/layout/adapter_log_entry.xml b/app/src/main/res/layout/adapter_log_entry.xml index f06a9657..33e848ff 100644 --- a/app/src/main/res/layout/adapter_log_entry.xml +++ b/app/src/main/res/layout/adapter_log_entry.xml @@ -6,6 +6,7 @@ android:layout_height="wrap_content">