Persist log view preferences

This commit is contained in:
ajp_anton
2026-07-23 09:28:10 +00:00
parent 4fc03a43a7
commit 6c00b9f659
3 changed files with 46 additions and 6 deletions
@@ -8,6 +8,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
import se.ajpanton.notificationlog.data.EncryptedNotificationLogStore
import se.ajpanton.notificationlog.databinding.FragmentViewLogsBinding
import se.ajpanton.notificationlog.model.NotificationLogEntry
import se.ajpanton.notificationlog.settings.LogField
import se.ajpanton.notificationlog.settings.LogViewSettingsStore
import se.ajpanton.notificationlog.settings.TimestampZone
import java.text.DateFormat
import java.util.Date
@@ -44,16 +47,24 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
private fun render(view: FragmentViewLogsBinding, entries: List<NotificationLogEntry>) {
view.logRows.removeAllViews()
view.emptyView.visibility = if (entries.isEmpty()) View.VISIBLE else View.GONE
val settings = LogViewSettingsStore(requireContext()).load()
entries.forEach { entry ->
view.logRows.addView(TextView(requireContext()).apply {
text = listOfNotNull(
DateFormat.getDateTimeInstance().format(Date(entry.recordedAtEpochMillis)),
entry.appName,
entry.action.name.lowercase().replace('_', ' '),
entry.contents,
).joinToString(" · ")
text = settings.order.filter { it in settings.visibleFields }.mapNotNull { field -> when (field) {
LogField.TIMESTAMP -> timestamp(entry.recordedAtEpochMillis, settings.timestampZone)
LogField.APP_NAME -> entry.appName
LogField.PACKAGE_NAME -> entry.packageName
LogField.ACTION -> entry.action.name.lowercase().replace('_', ' ')
LogField.CONTENTS -> entry.contents
} }.joinToString(" · ")
setPadding(0, 12, 0, 12)
})
}
}
private fun timestamp(value: Long, zone: TimestampZone): String {
val format = DateFormat.getDateTimeInstance()
if (zone == TimestampZone.UTC) format.timeZone = java.util.TimeZone.getTimeZone("UTC")
return format.format(Date(value))
}
}
@@ -0,0 +1,10 @@
package se.ajpanton.notificationlog.settings
enum class LogField { TIMESTAMP, APP_NAME, PACKAGE_NAME, ACTION, CONTENTS }
enum class TimestampZone { LOCAL_NOW, UTC, EVENT_LOCAL }
data class LogViewSettings(
val visibleFields: Set<LogField> = setOf(LogField.TIMESTAMP, LogField.APP_NAME, LogField.ACTION, LogField.CONTENTS),
val order: List<LogField> = LogField.entries,
val timestampZone: TimestampZone = TimestampZone.LOCAL_NOW,
)
@@ -0,0 +1,19 @@
package se.ajpanton.notificationlog.settings
import android.content.Context
import androidx.core.content.edit
class LogViewSettingsStore(context: Context) {
private val prefs = context.getSharedPreferences("log-view-settings", Context.MODE_PRIVATE)
fun load() = LogViewSettings(
visibleFields = prefs.getStringSet("visible", LogViewSettings().visibleFields.map { it.name }.toSet())!!
.map(LogField::valueOf).toSet(),
order = prefs.getString("order", null)?.split(',')?.map(LogField::valueOf) ?: LogField.entries,
timestampZone = TimestampZone.valueOf(prefs.getString("zone", TimestampZone.LOCAL_NOW.name)!!),
)
fun save(settings: LogViewSettings) = prefs.edit {
putStringSet("visible", settings.visibleFields.map { it.name }.toSet())
putString("order", settings.order.joinToString(",") { it.name })
putString("zone", settings.timestampZone.name)
}
}