feat: Schlaf-Timer, Verlauf und PC-Schlaf-Timer/-Verlauf in der Fernsteuerung (1.9.0)
Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
@@ -0,0 +1,103 @@
|
||||
package de.fojadrachi.playtube
|
||||
|
||||
import android.app.AlertDialog
|
||||
import android.content.Context
|
||||
import android.text.Editable
|
||||
import android.text.TextWatcher
|
||||
import android.view.View
|
||||
import android.view.ViewGroup
|
||||
import android.widget.ArrayAdapter
|
||||
import android.widget.EditText
|
||||
import android.widget.LinearLayout
|
||||
import android.widget.ListView
|
||||
import android.widget.TextView
|
||||
import java.text.SimpleDateFormat
|
||||
import java.util.Calendar
|
||||
import java.util.Date
|
||||
import java.util.Locale
|
||||
|
||||
/** Verlauf als Liste mit Suchfeld (Handy-Verlauf und Verlauf am PC). */
|
||||
object HistoryDialog {
|
||||
fun show(
|
||||
context: Context,
|
||||
title: String,
|
||||
entries: List<HistoryEntry>,
|
||||
onPick: (HistoryEntry) -> Unit,
|
||||
onClear: (() -> Unit)? = null,
|
||||
) {
|
||||
val pad = (16 * context.resources.displayMetrics.density).toInt()
|
||||
val search = EditText(context).apply {
|
||||
hint = context.getString(R.string.history_search)
|
||||
isSingleLine = true
|
||||
}
|
||||
val empty = TextView(context).apply {
|
||||
text = context.getString(R.string.history_empty)
|
||||
alpha = 0.7f
|
||||
setPadding(0, pad, 0, pad)
|
||||
}
|
||||
var shown = entries
|
||||
val adapter = object : ArrayAdapter<HistoryEntry>(context, android.R.layout.simple_list_item_2, android.R.id.text1, ArrayList(entries)) {
|
||||
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
|
||||
val view = super.getView(position, convertView, parent)
|
||||
val entry = getItem(position) ?: return view
|
||||
view.findViewById<TextView>(android.R.id.text1).text = entry.title
|
||||
view.findViewById<TextView>(android.R.id.text2).text =
|
||||
listOf(entry.artist, timeText(context, entry.playedAt)).filter { it.isNotBlank() }.joinToString(" · ")
|
||||
return view
|
||||
}
|
||||
}
|
||||
val list = ListView(context).apply {
|
||||
this.adapter = adapter
|
||||
layoutParams = LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, (360 * context.resources.displayMetrics.density).toInt())
|
||||
}
|
||||
fun refresh() {
|
||||
val q = search.text.toString().trim().lowercase()
|
||||
shown = entries.filter { q.isEmpty() || it.title.lowercase().contains(q) || it.artist.lowercase().contains(q) }
|
||||
adapter.clear()
|
||||
adapter.addAll(shown)
|
||||
empty.visibility = if (shown.isEmpty()) View.VISIBLE else View.GONE
|
||||
}
|
||||
search.addTextChangedListener(object : TextWatcher {
|
||||
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) = Unit
|
||||
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) = Unit
|
||||
override fun afterTextChanged(s: Editable?) = refresh()
|
||||
})
|
||||
val content = LinearLayout(context).apply {
|
||||
orientation = LinearLayout.VERTICAL
|
||||
setPadding(pad, pad / 2, pad, 0)
|
||||
addView(search)
|
||||
addView(empty)
|
||||
addView(list)
|
||||
}
|
||||
refresh()
|
||||
val builder = AlertDialog.Builder(context)
|
||||
.setTitle(title)
|
||||
.setView(content)
|
||||
.setPositiveButton(R.string.history_close, null)
|
||||
if (onClear != null && entries.isNotEmpty()) {
|
||||
builder.setNeutralButton(R.string.history_clear) { _, _ ->
|
||||
AlertDialog.Builder(context)
|
||||
.setMessage(R.string.history_clear_confirm)
|
||||
.setPositiveButton(R.string.history_clear) { _, _ -> onClear() }
|
||||
.setNegativeButton(android.R.string.cancel, null)
|
||||
.show()
|
||||
}
|
||||
}
|
||||
val dialog = builder.show()
|
||||
list.setOnItemClickListener { _, _, position, _ ->
|
||||
shown.getOrNull(position)?.let(onPick)
|
||||
dialog.dismiss()
|
||||
}
|
||||
}
|
||||
|
||||
/** "Heute, 14:32" bzw. "15.11., 14:32". */
|
||||
private fun timeText(context: Context, millis: Long): String {
|
||||
if (millis <= 0) return ""
|
||||
val time = android.text.format.DateFormat.getTimeFormat(context).format(Date(millis))
|
||||
val then = Calendar.getInstance().apply { timeInMillis = millis }
|
||||
val now = Calendar.getInstance()
|
||||
val sameDay = then.get(Calendar.YEAR) == now.get(Calendar.YEAR) && then.get(Calendar.DAY_OF_YEAR) == now.get(Calendar.DAY_OF_YEAR)
|
||||
return if (sameDay) context.getString(R.string.history_today, time)
|
||||
else SimpleDateFormat("d.M.", Locale.GERMANY).format(Date(millis)) + ", " + time
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user