Persist log view preferences
This commit is contained in:
@@ -8,6 +8,9 @@ import com.google.android.material.dialog.MaterialAlertDialogBuilder
|
|||||||
import se.ajpanton.notificationlog.data.EncryptedNotificationLogStore
|
import se.ajpanton.notificationlog.data.EncryptedNotificationLogStore
|
||||||
import se.ajpanton.notificationlog.databinding.FragmentViewLogsBinding
|
import se.ajpanton.notificationlog.databinding.FragmentViewLogsBinding
|
||||||
import se.ajpanton.notificationlog.model.NotificationLogEntry
|
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.text.DateFormat
|
||||||
import java.util.Date
|
import java.util.Date
|
||||||
|
|
||||||
@@ -44,16 +47,24 @@ class ViewLogsFragment : Fragment(R.layout.fragment_view_logs) {
|
|||||||
private fun render(view: FragmentViewLogsBinding, entries: List<NotificationLogEntry>) {
|
private fun render(view: FragmentViewLogsBinding, entries: List<NotificationLogEntry>) {
|
||||||
view.logRows.removeAllViews()
|
view.logRows.removeAllViews()
|
||||||
view.emptyView.visibility = if (entries.isEmpty()) View.VISIBLE else View.GONE
|
view.emptyView.visibility = if (entries.isEmpty()) View.VISIBLE else View.GONE
|
||||||
|
val settings = LogViewSettingsStore(requireContext()).load()
|
||||||
entries.forEach { entry ->
|
entries.forEach { entry ->
|
||||||
view.logRows.addView(TextView(requireContext()).apply {
|
view.logRows.addView(TextView(requireContext()).apply {
|
||||||
text = listOfNotNull(
|
text = settings.order.filter { it in settings.visibleFields }.mapNotNull { field -> when (field) {
|
||||||
DateFormat.getDateTimeInstance().format(Date(entry.recordedAtEpochMillis)),
|
LogField.TIMESTAMP -> timestamp(entry.recordedAtEpochMillis, settings.timestampZone)
|
||||||
entry.appName,
|
LogField.APP_NAME -> entry.appName
|
||||||
entry.action.name.lowercase().replace('_', ' '),
|
LogField.PACKAGE_NAME -> entry.packageName
|
||||||
entry.contents,
|
LogField.ACTION -> entry.action.name.lowercase().replace('_', ' ')
|
||||||
).joinToString(" · ")
|
LogField.CONTENTS -> entry.contents
|
||||||
|
} }.joinToString(" · ")
|
||||||
setPadding(0, 12, 0, 12)
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user