2018-01-04 19:52:25 +01:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.os.Bundle
|
|
|
|
import android.view.View
|
|
|
|
import android.view.ViewGroup
|
2023-01-14 21:37:23 +01:00
|
|
|
import android.widget.BaseAdapter
|
2021-05-22 00:03:16 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2023-01-14 21:37:23 +01:00
|
|
|
import jp.juggler.subwaytooter.databinding.ActWordListBinding
|
|
|
|
import jp.juggler.subwaytooter.databinding.LvMuteAppBinding
|
|
|
|
import jp.juggler.subwaytooter.dialog.DlgConfirm.confirm
|
2021-05-22 00:03:16 +02:00
|
|
|
import jp.juggler.subwaytooter.table.MutedApp
|
2022-12-27 03:54:52 +01:00
|
|
|
import jp.juggler.util.backPressed
|
2023-01-14 21:37:23 +01:00
|
|
|
import jp.juggler.util.coroutine.launchAndShowError
|
|
|
|
import jp.juggler.util.data.cast
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.log.LogCategory
|
2023-01-14 21:37:23 +01:00
|
|
|
import jp.juggler.util.ui.setNavigationBack
|
2018-01-04 19:52:25 +01:00
|
|
|
|
|
|
|
class ActMutedApp : AppCompatActivity() {
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
private val log = LogCategory("ActMutedApp")
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
private val views by lazy {
|
|
|
|
ActWordListBinding.inflate(layoutInflater)
|
|
|
|
}
|
|
|
|
|
|
|
|
private val listAdapter by lazy { MyListAdapter() }
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
2022-09-10 23:09:26 +02:00
|
|
|
backPressed {
|
|
|
|
setResult(RESULT_OK)
|
|
|
|
finish()
|
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
App1.setActivityTheme(this)
|
|
|
|
initUI()
|
|
|
|
loadData()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initUI() {
|
2023-01-14 21:37:23 +01:00
|
|
|
setContentView(views.root)
|
|
|
|
setSupportActionBar(views.toolbar)
|
|
|
|
setNavigationBack(views.toolbar)
|
|
|
|
fixHorizontalMargin(views.llContent)
|
|
|
|
views.listView.adapter = listAdapter
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadData() {
|
2023-01-14 21:37:23 +01:00
|
|
|
listAdapter.items = buildList {
|
|
|
|
try {
|
|
|
|
MutedApp.createCursor().use { cursor ->
|
|
|
|
val idxId = cursor.getColumnIndex(MutedApp.COL_ID)
|
|
|
|
val idxName = cursor.getColumnIndex(MutedApp.COL_NAME)
|
|
|
|
while (cursor.moveToNext()) {
|
|
|
|
val item = MyItem(
|
|
|
|
id = cursor.getLong(idxId),
|
|
|
|
name = cursor.getString(idxName)
|
|
|
|
)
|
|
|
|
add(item)
|
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
2023-01-14 21:37:23 +01:00
|
|
|
} catch (ex: Throwable) {
|
|
|
|
log.e(ex, "loadData failed.")
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 21:37:23 +01:00
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
private fun delete(item: MyItem?) {
|
|
|
|
item ?: return
|
|
|
|
launchAndShowError {
|
|
|
|
confirm(R.string.delete_confirm, item.name)
|
|
|
|
MutedApp.delete(item.name)
|
|
|
|
listAdapter.remove(item)
|
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// リスト要素のデータ
|
2023-01-14 21:37:23 +01:00
|
|
|
private class MyItem(val id: Long, val name: String)
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
// リスト要素のViewHolder
|
2023-01-14 21:37:23 +01:00
|
|
|
private inner class MyViewHolder(parent: ViewGroup?) {
|
|
|
|
val views = LvMuteAppBinding.inflate(layoutInflater, parent, false)
|
|
|
|
var lastItem: MyItem? = null
|
2021-05-22 00:03:16 +02:00
|
|
|
|
|
|
|
init {
|
2023-01-14 21:37:23 +01:00
|
|
|
views.root.tag = this
|
|
|
|
views.btnDelete.setOnClickListener { delete(lastItem) }
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
fun bind(item: MyItem?) {
|
|
|
|
item ?: return
|
|
|
|
lastItem = item
|
|
|
|
views.tvName.text = item.name
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
private inner class MyListAdapter : BaseAdapter() {
|
|
|
|
var items: List<MyItem> = emptyList()
|
|
|
|
set(value) {
|
|
|
|
field = value
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
fun remove(item: MyItem) {
|
|
|
|
items = items.filter { it != item }
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
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
|
2021-05-22 00:03:16 +02:00
|
|
|
|
2023-01-14 21:37:23 +01:00
|
|
|
override fun isEnabled(position: Int) = false
|
2021-05-22 00:03:16 +02:00
|
|
|
}
|
2018-01-04 19:52:25 +01:00
|
|
|
}
|