SubwayTooter-Android-App/app/src/main/java/jp/juggler/subwaytooter/ActMutedPseudoAccount.kt

120 lines
3.8 KiB
Kotlin
Raw Normal View History

package jp.juggler.subwaytooter
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import android.widget.BaseAdapter
import androidx.appcompat.app.AppCompatActivity
import jp.juggler.subwaytooter.databinding.ActWordListBinding
import jp.juggler.subwaytooter.databinding.LvMuteAppBinding
import jp.juggler.subwaytooter.dialog.DlgConfirm.confirm
import jp.juggler.subwaytooter.table.UserRelation
import jp.juggler.util.backPressed
import jp.juggler.util.coroutine.launchAndShowError
import jp.juggler.util.data.cast
import jp.juggler.util.log.LogCategory
import jp.juggler.util.ui.setNavigationBack
class ActMutedPseudoAccount : AppCompatActivity() {
companion object {
private val log = LogCategory("ActMutedPseudoAccount")
}
private val views by lazy {
ActWordListBinding.inflate(layoutInflater)
}
private val listAdapter by lazy { MyListAdapter() }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
2022-09-10 23:09:26 +02:00
backPressed {
setResult(RESULT_OK)
finish()
}
App1.setActivityTheme(this)
initUI()
loadData()
}
private fun initUI() {
setContentView(views.root)
setSupportActionBar(views.toolbar)
setNavigationBack(views.toolbar)
fixHorizontalMargin(views.llContent)
views.listView.adapter = listAdapter
}
private fun loadData() {
listAdapter.items = buildList {
try {
UserRelation.createCursorPseudoMuted().use { cursor ->
val idxId = UserRelation.COL_ID.getIndex(cursor)
val idxName = UserRelation.COL_WHO_ID.getIndex(cursor)
while (cursor.moveToNext()) {
val item = MyItem(
id = cursor.getLong(idxId),
name = cursor.getString(idxName)
)
add(item)
}
}
} catch (ex: Throwable) {
log.e(ex, "loadData failed.")
}
}
}
private fun delete(item: MyItem?) {
item ?: return
launchAndShowError {
confirm(R.string.delete_confirm, item.name)
UserRelation.deletePseudo(item.id)
listAdapter.remove(item)
}
}
// リスト要素のデータ
private class MyItem(val id: Long, val name: String)
// リスト要素のViewHolder
private inner class MyViewHolder(parent: ViewGroup?) {
val views = LvMuteAppBinding.inflate(layoutInflater, parent, false)
private var lastItem: MyItem? = null
init {
views.root.tag = this
views.btnDelete.setOnClickListener { delete(lastItem) }
}
fun bind(item: MyItem?) {
item ?: return
lastItem = item
views.tvName.text = item.name
}
}
private inner class MyListAdapter : BaseAdapter() {
var items: List<MyItem> = emptyList()
set(value) {
field = value
notifyDataSetChanged()
}
fun remove(item: MyItem) {
items = items.filter { it != item }
}
override fun getCount() = items.size
override fun getItem(position: Int) = items.elementAtOrNull(position)
override fun getItemId(position: Int) = 0L
override fun getView(position: Int, convertView: View?, parent: ViewGroup?) =
(convertView?.tag?.cast() ?: MyViewHolder(parent))
.also { it.bind(items.elementAtOrNull(position)) }
.views.root
override fun isEnabled(position: Int) = false
}
}