208 lines
8.7 KiB
Kotlin
208 lines
8.7 KiB
Kotlin
package com.asio.tapremly.ui
|
|
|
|
import android.content.Intent
|
|
import android.os.Bundle
|
|
import android.text.Editable
|
|
import android.text.TextWatcher
|
|
import android.view.LayoutInflater
|
|
import android.view.View
|
|
import android.view.ViewGroup
|
|
import android.widget.PopupMenu
|
|
import android.widget.TextView
|
|
import androidx.appcompat.app.AlertDialog
|
|
import androidx.recyclerview.widget.LinearLayoutManager
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
import com.asio.tapremly.R
|
|
import com.asio.tapremly.agent.AgentRuntime
|
|
import com.asio.tapremly.agent.Snapshot
|
|
import com.asio.tapremly.core.Device
|
|
import com.asio.tapremly.data.Prefs
|
|
import com.asio.tapremly.databinding.ActivityMainBinding
|
|
|
|
class MainActivity : AgentActivity() {
|
|
private var binding: ActivityMainBinding? = null
|
|
private var last: Snapshot? = null
|
|
private val adapter = DeviceAdapter(
|
|
onDevice = { device ->
|
|
startActivity(Intent(this, DeviceActivity::class.java).putExtra(DeviceActivity.EXTRA_REMOTE, device.id))
|
|
},
|
|
onRename = { id, name -> nameDialog("重命名分组", name) { runtime?.renameGroup(id, it) } },
|
|
onDelete = { id ->
|
|
confirmDialog("删除后,组里的设备会回到未分组。") { runtime?.deleteGroup(id) }
|
|
},
|
|
)
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
super.onCreate(savedInstanceState)
|
|
if (!Prefs(this).loggedIn) return
|
|
val views = ActivityMainBinding.inflate(layoutInflater)
|
|
binding = views
|
|
setContentView(views.root)
|
|
views.list.layoutManager = LinearLayoutManager(this)
|
|
views.list.adapter = adapter
|
|
views.addDevice.setOnClickListener { deviceDialog { name, id -> runtime?.addDevice(name, id) } }
|
|
views.emptyAdd.setOnClickListener { deviceDialog { name, id -> runtime?.addDevice(name, id) } }
|
|
views.addGroup.setOnClickListener { nameDialog("新建分组", "") { runtime?.createGroup(it) } }
|
|
views.more.setOnClickListener { showMore(it) }
|
|
views.refresh.setOnRefreshListener { runtime?.refresh() }
|
|
views.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?) = render()
|
|
})
|
|
}
|
|
|
|
override fun onSnapshot(snapshot: Snapshot) {
|
|
last = snapshot
|
|
binding?.refresh?.isRefreshing = false
|
|
render()
|
|
}
|
|
|
|
override fun onText(fromId: String, text: String) {
|
|
if (isFinishing) return
|
|
AlertDialog.Builder(this)
|
|
.setTitle("收到文字")
|
|
.setMessage("来自 $fromId\n$text")
|
|
.setPositiveButton("知道了", null)
|
|
.show()
|
|
}
|
|
|
|
private fun render() {
|
|
val views = binding ?: return
|
|
val snapshot = last ?: return
|
|
views.link.text = snapshot.linkLabel
|
|
if (snapshot.listError.isBlank()) {
|
|
views.listError.visibility = View.GONE
|
|
} else {
|
|
views.listError.visibility = View.VISIBLE
|
|
views.listError.text = snapshot.listError
|
|
}
|
|
val query = views.search.text.toString()
|
|
val rows = buildRows(snapshot, query)
|
|
adapter.submit(rows)
|
|
val noDevices = snapshot.devices.isEmpty() && query.isBlank()
|
|
views.empty.visibility = if (rows.isEmpty()) View.VISIBLE else View.GONE
|
|
views.emptyText.text = if (query.isBlank()) getString(R.string.empty_devices) else "没有匹配的设备"
|
|
views.emptyAdd.visibility = if (noDevices) View.VISIBLE else View.GONE
|
|
}
|
|
|
|
private fun showMore(anchor: View) {
|
|
PopupMenu(this, anchor).apply {
|
|
menu.add(getString(R.string.trust)).setOnMenuItemClickListener { _ ->
|
|
startActivity(Intent(this@MainActivity, TrustActivity::class.java)); true
|
|
}
|
|
menu.add(getString(R.string.published)).setOnMenuItemClickListener { _ ->
|
|
startActivity(Intent(this@MainActivity, PublishedActivity::class.java)); true
|
|
}
|
|
menu.add(getString(R.string.logs)).setOnMenuItemClickListener { _ ->
|
|
startActivity(Intent(this@MainActivity, LogsActivity::class.java)); true
|
|
}
|
|
menu.add(getString(R.string.inbox)).setOnMenuItemClickListener { _ ->
|
|
startActivity(Intent(this@MainActivity, InboxActivity::class.java)); true
|
|
}
|
|
menu.add(getString(R.string.settings)).setOnMenuItemClickListener { _ ->
|
|
startActivity(Intent(this@MainActivity, SettingsActivity::class.java)); true
|
|
}
|
|
menu.add(getString(R.string.logout)).setOnMenuItemClickListener { _ ->
|
|
confirmDialog("退出后,这部手机不再保持连接。") { runtime?.logout() }; true
|
|
}
|
|
show()
|
|
}
|
|
}
|
|
|
|
private fun buildRows(snapshot: Snapshot, query: String): List<Row> {
|
|
val q = query.trim()
|
|
fun match(device: Device) = q.isEmpty() ||
|
|
device.name.contains(q, ignoreCase = true) ||
|
|
device.id.contains(q, ignoreCase = true)
|
|
val devices = snapshot.devices.filter(::match)
|
|
val known = snapshot.groups.map { it.id }.toSet()
|
|
val out = ArrayList<Row>()
|
|
val ungrouped = devices.filter { it.groupId.isBlank() || it.groupId !in known }
|
|
if (ungrouped.isNotEmpty()) {
|
|
out.add(Row.Header(null, getString(R.string.ungrouped)))
|
|
ungrouped.forEach { out.add(Row.Dev(it)) }
|
|
}
|
|
for (group in snapshot.groups) {
|
|
val members = devices.filter { it.groupId == group.id }
|
|
if (q.isNotEmpty() && members.isEmpty()) continue
|
|
out.add(Row.Header(group.id, group.name))
|
|
members.forEach { out.add(Row.Dev(it)) }
|
|
}
|
|
return out
|
|
}
|
|
}
|
|
|
|
private sealed class Row {
|
|
data class Header(val id: String?, val title: String) : Row()
|
|
data class Dev(val device: Device) : Row()
|
|
}
|
|
|
|
private class DeviceAdapter(
|
|
private val onDevice: (Device) -> Unit,
|
|
private val onRename: (String, String) -> Unit,
|
|
private val onDelete: (String) -> Unit,
|
|
) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
|
|
private val rows = ArrayList<Row>()
|
|
|
|
fun submit(next: List<Row>) {
|
|
rows.clear()
|
|
rows.addAll(next)
|
|
notifyDataSetChanged()
|
|
}
|
|
|
|
override fun getItemViewType(position: Int): Int = if (rows[position] is Row.Header) 0 else 1
|
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
|
|
val inflater = LayoutInflater.from(parent.context)
|
|
return if (viewType == 0) {
|
|
HeaderHolder(inflater.inflate(R.layout.item_group, parent, false))
|
|
} else {
|
|
DeviceHolder(inflater.inflate(R.layout.item_device, parent, false))
|
|
}
|
|
}
|
|
|
|
override fun getItemCount(): Int = rows.size
|
|
|
|
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
|
|
when (val row = rows[position]) {
|
|
is Row.Header -> (holder as HeaderHolder).bind(row, onRename, onDelete)
|
|
is Row.Dev -> (holder as DeviceHolder).bind(row.device, onDevice)
|
|
}
|
|
}
|
|
}
|
|
|
|
private class HeaderHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
private val title: TextView = view.findViewById(R.id.title)
|
|
private val rename: TextView = view.findViewById(R.id.rename)
|
|
private val delete: TextView = view.findViewById(R.id.delete)
|
|
|
|
fun bind(row: Row.Header, onRename: (String, String) -> Unit, onDelete: (String) -> Unit) {
|
|
title.text = row.title
|
|
val editable = row.id != null
|
|
rename.visibility = if (editable) View.VISIBLE else View.GONE
|
|
delete.visibility = if (editable) View.VISIBLE else View.GONE
|
|
rename.setOnClickListener { row.id?.let { onRename(it, row.title) } }
|
|
delete.setOnClickListener { row.id?.let(onDelete) }
|
|
}
|
|
}
|
|
|
|
private class DeviceHolder(view: View) : RecyclerView.ViewHolder(view) {
|
|
private val name: TextView = view.findViewById(R.id.name)
|
|
private val online: TextView = view.findViewById(R.id.online)
|
|
private val endpoint: TextView = view.findViewById(R.id.endpoint)
|
|
|
|
fun bind(device: Device, onDevice: (Device) -> Unit) {
|
|
name.text = device.name
|
|
endpoint.text = device.id
|
|
if (device.online) {
|
|
online.text = itemView.context.getString(R.string.online)
|
|
online.setTextColor(itemView.context.getColor(R.color.ok))
|
|
} else {
|
|
online.text = itemView.context.getString(R.string.offline)
|
|
online.setTextColor(itemView.context.getColor(R.color.muted))
|
|
}
|
|
itemView.setOnClickListener { onDevice(device) }
|
|
}
|
|
}
|