Skip to content

Commit

Permalink
show timestamp on click
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Nov 24, 2024
1 parent d2f3c99 commit 63605e3
Show file tree
Hide file tree
Showing 7 changed files with 45 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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"
Expand Down
Original file line number Diff line number Diff line change
@@ -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


/**
Expand Down Expand Up @@ -60,6 +66,19 @@ class LogEntryAdapter(private val entries: List<VolumeState>) : 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)
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/java/de/felixnuesse/timedsilence/util/DateUtil.kt
Original file line number Diff line number Diff line change
@@ -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


/**
Expand Down Expand Up @@ -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)
}
}

}
7 changes: 7 additions & 0 deletions app/src/main/res/drawable/icon_list_alt.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:autoMirrored="true" android:height="24dp" android:viewportHeight="24" android:viewportWidth="24" android:width="24dp">

<path android:fillAlpha="0.3" android:fillColor="@android:color/white" android:pathData="M5,19h14L19,5L5,5v14zM11,7h6v2h-6L11,7zM11,11h6v2h-6v-2zM11,15h6v2h-6v-2zM7,7h2v2L7,9L7,7zM7,11h2v2L7,13v-2zM7,15h2v2L7,17v-2z" android:strokeAlpha="0.3"/>

<path android:fillColor="@android:color/white" android:pathData="M11,7h6v2h-6zM11,11h6v2h-6zM11,15h6v2h-6zM7,7h2v2L7,9zM7,11h2v2L7,13zM7,15h2v2L7,17zM20.1,3L3.9,3c-0.5,0 -0.9,0.4 -0.9,0.9v16.2c0,0.4 0.4,0.9 0.9,0.9h16.2c0.4,0 0.9,-0.5 0.9,-0.9L21,3.9c0,-0.5 -0.5,-0.9 -0.9,-0.9zM19,19L5,19L5,5h14v14z"/>

</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:icon="@drawable/icon_verify"
android:icon="@drawable/icon_list_alt"
android:text="@string/main_tab_logs" />
</com.google.android.material.tabs.TabLayout>
</com.google.android.material.appbar.AppBarLayout>
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/layout/adapter_log_entry.xml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
android:layout_height="wrap_content">

<androidx.cardview.widget.CardView
android:id="@+id/card"
style="@style/tertiaryCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
Expand Down
17 changes: 0 additions & 17 deletions app/src/test/java/de/felixnuesse/timedsilence/ExampleUnitTest.kt

This file was deleted.

0 comments on commit 63605e3

Please sign in to comment.