2018-07-07 07:15:16 +02:00
|
|
|
package jp.juggler.subwaytooter
|
|
|
|
|
|
|
|
import android.app.Activity
|
|
|
|
import android.content.Intent
|
2018-12-01 00:02:18 +01:00
|
|
|
import android.os.Bundle
|
2023-01-14 11:19:01 +01:00
|
|
|
import android.widget.ArrayAdapter
|
|
|
|
import android.widget.CheckBox
|
2021-05-27 04:15:59 +02:00
|
|
|
import androidx.appcompat.app.AppCompatActivity
|
2023-01-14 11:19:01 +01:00
|
|
|
import androidx.core.view.children
|
2021-05-27 04:15:59 +02:00
|
|
|
import jp.juggler.subwaytooter.api.ApiPath
|
2023-01-14 11:19:01 +01:00
|
|
|
import jp.juggler.subwaytooter.api.TootApiResult
|
|
|
|
import jp.juggler.subwaytooter.api.entity.*
|
2021-05-27 04:15:59 +02:00
|
|
|
import jp.juggler.subwaytooter.api.runApiTask
|
2021-06-28 09:09:00 +02:00
|
|
|
import jp.juggler.subwaytooter.column.ColumnType
|
2023-01-14 05:02:41 +01:00
|
|
|
import jp.juggler.subwaytooter.databinding.ActKeywordFilterBinding
|
2023-01-14 11:19:01 +01:00
|
|
|
import jp.juggler.subwaytooter.databinding.LvKeywordFilterBinding
|
2021-05-27 04:15:59 +02:00
|
|
|
import jp.juggler.subwaytooter.table.AcctColor
|
2018-07-07 07:15:16 +02:00
|
|
|
import jp.juggler.subwaytooter.table.SavedAccount
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.coroutine.launchMain
|
2023-01-14 11:19:01 +01:00
|
|
|
import jp.juggler.util.data.*
|
2023-01-13 13:22:25 +01:00
|
|
|
import jp.juggler.util.log.LogCategory
|
|
|
|
import jp.juggler.util.log.showToast
|
|
|
|
import jp.juggler.util.network.toPostRequestBuilder
|
|
|
|
import jp.juggler.util.network.toPut
|
|
|
|
import jp.juggler.util.network.toRequestBody
|
2018-07-07 07:15:16 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
class ActKeywordFilter : AppCompatActivity() {
|
2021-05-27 04:15:59 +02:00
|
|
|
|
|
|
|
companion object {
|
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private val log = LogCategory("ActKeywordFilter")
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private const val EXTRA_ACCOUNT_DB_ID = "account_db_id"
|
|
|
|
private const val EXTRA_FILTER_ID = "filter_id"
|
|
|
|
private const val EXTRA_INITIAL_PHRASE = "initial_phrase"
|
|
|
|
|
|
|
|
private const val STATE_EXPIRE_SPINNER = "expire_spinner"
|
|
|
|
private const val STATE_EXPIRE_AT = "expire_at"
|
|
|
|
private const val STATE_KEYWORDS = "keywords"
|
|
|
|
private const val STATE_DELETE_IDS = "deleteIds"
|
2021-05-27 04:15:59 +02:00
|
|
|
|
|
|
|
fun open(
|
|
|
|
activity: Activity,
|
|
|
|
ai: SavedAccount,
|
2021-06-20 15:12:25 +02:00
|
|
|
filterId: EntityId? = null,
|
|
|
|
initialPhrase: String? = null,
|
2021-05-27 04:15:59 +02:00
|
|
|
) {
|
|
|
|
val intent = Intent(activity, ActKeywordFilter::class.java)
|
|
|
|
intent.putExtra(EXTRA_ACCOUNT_DB_ID, ai.db_id)
|
2021-06-20 15:12:25 +02:00
|
|
|
filterId?.putTo(intent, EXTRA_FILTER_ID)
|
|
|
|
initialPhrase?.notEmpty()?.let { intent.putExtra(EXTRA_INITIAL_PHRASE, it) }
|
2021-05-27 04:15:59 +02:00
|
|
|
activity.startActivity(intent)
|
|
|
|
}
|
|
|
|
|
2021-06-28 09:09:00 +02:00
|
|
|
private val expire_duration_list = intArrayOf(
|
2021-05-27 04:15:59 +02:00
|
|
|
-1, // dont change
|
|
|
|
0, // unlimited
|
|
|
|
1800,
|
|
|
|
3600,
|
|
|
|
3600 * 6,
|
|
|
|
3600 * 12,
|
|
|
|
86400,
|
|
|
|
86400 * 7
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
private lateinit var account: SavedAccount
|
|
|
|
|
2023-01-14 05:02:41 +01:00
|
|
|
private val views by lazy {
|
|
|
|
ActKeywordFilterBinding.inflate(layoutInflater)
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 05:02:41 +01:00
|
|
|
// private lateinit var tvAccount: TextView
|
|
|
|
// private lateinit var etPhrase: EditText
|
|
|
|
// private lateinit var cbContextHome: CheckBox
|
|
|
|
// private lateinit var cbContextNotification: CheckBox
|
|
|
|
// private lateinit var cbContextPublic: CheckBox
|
|
|
|
// private lateinit var cbContextThread: CheckBox
|
|
|
|
// private lateinit var cbContextProfile: CheckBox
|
|
|
|
//
|
|
|
|
// private lateinit var cbFilterIrreversible: CheckBox
|
|
|
|
// private lateinit var cbFilterWordMatch: CheckBox
|
|
|
|
// private lateinit var tvExpire: TextView
|
|
|
|
// private lateinit var spExpire: Spinner
|
2021-05-27 04:15:59 +02:00
|
|
|
|
|
|
|
private var loading = false
|
|
|
|
private var density: Float = 1f
|
2021-06-20 15:12:25 +02:00
|
|
|
private var filterId: EntityId? = null
|
|
|
|
private var filterExpire: Long = 0L
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private val deleteIds = HashSet<String>()
|
|
|
|
|
2021-05-27 04:15:59 +02:00
|
|
|
///////////////////////////////////////////////////
|
|
|
|
|
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
App1.setActivityTheme(this)
|
|
|
|
|
|
|
|
val intent = this.intent
|
|
|
|
|
|
|
|
// filter ID の有無はUIに影響するのでinitUIより先に初期化する
|
2021-06-20 15:12:25 +02:00
|
|
|
this.filterId = EntityId.from(intent, EXTRA_FILTER_ID)
|
2021-05-27 04:15:59 +02:00
|
|
|
|
|
|
|
val a = SavedAccount.loadAccount(this, intent.getLongExtra(EXTRA_ACCOUNT_DB_ID, -1L))
|
|
|
|
if (a == null) {
|
|
|
|
finish()
|
|
|
|
return
|
|
|
|
}
|
|
|
|
this.account = a
|
|
|
|
|
|
|
|
initUI()
|
|
|
|
|
|
|
|
showAccount()
|
|
|
|
|
|
|
|
if (savedInstanceState == null) {
|
2021-06-20 15:12:25 +02:00
|
|
|
if (filterId != null) {
|
2021-05-27 04:15:59 +02:00
|
|
|
startLoading()
|
|
|
|
} else {
|
2023-01-14 11:19:01 +01:00
|
|
|
views.spExpire.setSelection(1)
|
|
|
|
val initialText = intent.getStringExtra(EXTRA_INITIAL_PHRASE)?.trim() ?: ""
|
|
|
|
views.etTitle.setText(initialText)
|
|
|
|
addKeywordArea(TootFilterKeyword(keyword = initialText))
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
} else {
|
2023-01-14 11:19:01 +01:00
|
|
|
|
|
|
|
savedInstanceState.getStringArrayList(STATE_DELETE_IDS)
|
|
|
|
?.let { deleteIds.addAll(it) }
|
|
|
|
|
|
|
|
savedInstanceState.getStringArrayList(STATE_KEYWORDS)
|
|
|
|
?.mapNotNull { it?.decodeJsonObject() }
|
|
|
|
?.forEach {
|
|
|
|
try {
|
|
|
|
addKeywordArea(TootFilterKeyword(it))
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
log.e(ex, "can't decode TootFilterKeyword")
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
val iv = savedInstanceState.getInt(STATE_EXPIRE_SPINNER, -1)
|
|
|
|
if (iv != -1) {
|
2023-01-14 11:19:01 +01:00
|
|
|
views.spExpire.setSelection(iv)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
2021-06-20 15:12:25 +02:00
|
|
|
filterExpire = savedInstanceState.getLong(STATE_EXPIRE_AT, filterExpire)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onSaveInstanceState(outState: Bundle) {
|
|
|
|
super.onSaveInstanceState(outState)
|
|
|
|
if (!loading) {
|
2023-01-14 05:02:41 +01:00
|
|
|
outState.putInt(STATE_EXPIRE_SPINNER, views.spExpire.selectedItemPosition)
|
2021-06-20 15:12:25 +02:00
|
|
|
outState.putLong(STATE_EXPIRE_AT, filterExpire)
|
2023-01-14 11:19:01 +01:00
|
|
|
|
|
|
|
outState.putStringArrayList(STATE_DELETE_IDS, ArrayList<String>(deleteIds))
|
|
|
|
|
|
|
|
views.llKeywords.children
|
|
|
|
.mapNotNull { (it.tag as? VhKeyword)?.encodeJson()?.toString() }
|
|
|
|
.toList()
|
|
|
|
.let { outState.putStringArrayList(STATE_KEYWORDS, ArrayList<String>(it)) }
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun initUI() {
|
|
|
|
title = getString(
|
2021-06-20 15:12:25 +02:00
|
|
|
when (filterId) {
|
|
|
|
null -> R.string.keyword_filter_new
|
|
|
|
else -> R.string.keyword_filter_edit
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
this.density = resources.displayMetrics.density
|
2023-01-14 05:02:41 +01:00
|
|
|
setContentView(views.root)
|
2021-05-27 04:15:59 +02:00
|
|
|
App1.initEdgeToEdge(this)
|
|
|
|
|
2023-01-13 13:22:25 +01:00
|
|
|
fixHorizontalPadding(findViewById(R.id.svContent))
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
views.btnSave.setOnClickListener { save() }
|
|
|
|
views.btnAddKeyword.setOnClickListener {
|
|
|
|
val ti = TootInstance.getCached(account)
|
|
|
|
when {
|
|
|
|
ti == null ->
|
|
|
|
showToast(true, "can't get server information")
|
|
|
|
!ti.versionGE(TootInstance.VERSION_4_0_0) && views.llKeywords.childCount >= 1 ->
|
|
|
|
showToast(true, "before mastodon 4.0, allowed 1 keyword per 1 filter.")
|
|
|
|
else -> addKeywordArea(TootFilterKeyword(keyword = ""))
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
val captionList = arrayOf(
|
2021-05-27 04:15:59 +02:00
|
|
|
getString(R.string.dont_change),
|
|
|
|
getString(R.string.filter_expire_unlimited),
|
|
|
|
getString(R.string.filter_expire_30min),
|
|
|
|
getString(R.string.filter_expire_1hour),
|
|
|
|
getString(R.string.filter_expire_6hour),
|
|
|
|
getString(R.string.filter_expire_12hour),
|
|
|
|
getString(R.string.filter_expire_1day),
|
|
|
|
getString(R.string.filter_expire_1week)
|
|
|
|
)
|
2021-06-20 15:12:25 +02:00
|
|
|
val adapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, captionList)
|
2021-05-27 04:15:59 +02:00
|
|
|
adapter.setDropDownViewResource(R.layout.lv_spinner_dropdown)
|
2023-01-14 05:02:41 +01:00
|
|
|
views.spExpire.adapter = adapter
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun showAccount() {
|
2023-01-14 05:02:41 +01:00
|
|
|
views.tvAccount.text = AcctColor.getNicknameWithColor(account.acct)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun startLoading() {
|
|
|
|
loading = true
|
|
|
|
launchMain {
|
|
|
|
var resultFilter: TootFilter? = null
|
|
|
|
runApiTask(account) { client ->
|
2023-01-14 11:19:01 +01:00
|
|
|
|
|
|
|
// try v2
|
|
|
|
var result = client.request("${ApiPath.PATH_FILTERS_V2}/$filterId")
|
|
|
|
result?.jsonObject?.let {
|
|
|
|
try {
|
|
|
|
resultFilter = TootFilter(it)
|
|
|
|
return@runApiTask result
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
log.e(ex, "parse error.")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result?.response?.code == 404) {
|
|
|
|
// try v1
|
|
|
|
result = client.request("${ApiPath.PATH_FILTERS}/$filterId")
|
|
|
|
result?.jsonObject?.let {
|
|
|
|
try {
|
2021-05-27 04:15:59 +02:00
|
|
|
resultFilter = TootFilter(it)
|
2023-01-14 11:19:01 +01:00
|
|
|
return@runApiTask result
|
|
|
|
} catch (ex: Throwable) {
|
|
|
|
log.e(ex, "parse error.")
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 11:19:01 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
result
|
2021-05-27 04:15:59 +02:00
|
|
|
}?.let { result ->
|
|
|
|
loading = false
|
|
|
|
when (val filter = resultFilter) {
|
|
|
|
null -> {
|
|
|
|
showToast(true, result.error ?: "?")
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
else -> onLoadComplete(filter)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// キャンセル時はloadingはtrueのまま
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun onLoadComplete(filter: TootFilter) {
|
|
|
|
loading = false
|
|
|
|
|
2021-06-20 15:12:25 +02:00
|
|
|
filterExpire = filter.time_expires_at
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 05:02:41 +01:00
|
|
|
setContextChecked(filter, views.cbContextHome, TootFilterContext.Home)
|
|
|
|
setContextChecked(filter, views.cbContextNotification, TootFilterContext.Notifications)
|
|
|
|
setContextChecked(filter, views.cbContextPublic, TootFilterContext.Public)
|
|
|
|
setContextChecked(filter, views.cbContextThread, TootFilterContext.Thread)
|
|
|
|
setContextChecked(filter, views.cbContextProfile, TootFilterContext.Account)
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
views.rgAction.check(if (filter.hide) views.rbHide.id else views.rbWarn.id)
|
|
|
|
|
|
|
|
if (filter.keywords.isEmpty()) {
|
|
|
|
filter.keywords = listOf(TootFilterKeyword(keyword = ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
filter.keywords.forEach { addKeywordArea(it) }
|
|
|
|
|
|
|
|
views.etTitle.setText(
|
|
|
|
filter.title.notEmpty() ?: filter.keywords.firstOrNull()?.keyword ?: ""
|
|
|
|
)
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 05:02:41 +01:00
|
|
|
views.tvExpire.text = if (filter.time_expires_at == 0L) {
|
2021-05-27 04:15:59 +02:00
|
|
|
getString(R.string.filter_expire_unlimited)
|
|
|
|
} else {
|
|
|
|
TootStatus.formatTime(this, filter.time_expires_at, false)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-14 05:02:41 +01:00
|
|
|
private fun setContextChecked(filter: TootFilter, cb: CheckBox, fc: TootFilterContext) {
|
|
|
|
cb.isChecked = filter.hasContext(fc)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private fun save() {
|
|
|
|
if (loading) return
|
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
val vhList = views.llKeywords.children.mapNotNull { it.tag as? VhKeyword }.toList()
|
|
|
|
if (vhList.isEmpty() || vhList.any { it.keyword.isEmpty() }) {
|
|
|
|
showToast(true, R.string.filter_keyword_empty)
|
|
|
|
return
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
val title = views.etTitle.text.toString().trim()
|
|
|
|
if (title.isEmpty()) {
|
|
|
|
showToast(true, R.string.filter_title_empty)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
launchMain {
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
var result = saveV2(vhList)
|
|
|
|
if (result?.response?.code == 404) {
|
|
|
|
result = saveV1(vhList)
|
|
|
|
}
|
|
|
|
result ?: return@launchMain // cancelled
|
2023-01-14 05:02:41 +01:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
val error = result.error
|
|
|
|
if (error != null) {
|
|
|
|
showToast(true, result.error)
|
|
|
|
} else {
|
|
|
|
val appState = App1.prepare(applicationContext, "ActKeywordFilter.save()")
|
|
|
|
for (column in appState.columnList) {
|
|
|
|
if (column.type == ColumnType.KEYWORD_FILTER && column.accessInfo == account) {
|
|
|
|
column.filterReloadRequired = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private fun filterParamBase() = buildJsonObject {
|
|
|
|
fun JsonArray.putContextChecked(cb: CheckBox, fc: TootFilterContext) {
|
|
|
|
if (cb.isChecked) add(fc.apiName)
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
put("context", JsonArray().apply {
|
|
|
|
putContextChecked(views.cbContextHome, TootFilterContext.Home)
|
|
|
|
putContextChecked(views.cbContextNotification, TootFilterContext.Notifications)
|
|
|
|
putContextChecked(views.cbContextPublic, TootFilterContext.Public)
|
|
|
|
putContextChecked(views.cbContextThread, TootFilterContext.Thread)
|
|
|
|
putContextChecked(views.cbContextProfile, TootFilterContext.Account)
|
|
|
|
})
|
|
|
|
|
|
|
|
when (val seconds = expire_duration_list
|
|
|
|
.elementAtOrNull(views.spExpire.selectedItemPosition)
|
|
|
|
?: -1
|
|
|
|
) {
|
|
|
|
// dont change
|
|
|
|
-1 -> Unit
|
|
|
|
|
|
|
|
// unlimited
|
|
|
|
0 -> when {
|
|
|
|
// already unlimited. don't change.
|
|
|
|
filterExpire <= 0L -> Unit
|
|
|
|
// XXX: currently there is no way to remove expires from existing filter.
|
|
|
|
else -> put("expires_in", Int.MAX_VALUE)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
// set seconds
|
|
|
|
else -> put("expires_in", seconds)
|
|
|
|
}
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private suspend fun saveV1(vhList: List<VhKeyword>): TootApiResult? {
|
|
|
|
if (vhList.size != 1) return TootApiResult("V1 API allow only 1 keyword.")
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
val params = filterParamBase().apply {
|
|
|
|
put("irreversible", views.rgAction.checkedRadioButtonId == views.rbHide.id)
|
|
|
|
val vh = vhList.first()
|
|
|
|
put("phrase", vh.keyword)
|
|
|
|
put("whole_word", vh.wholeWord)
|
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
return runApiTask(account) { client ->
|
|
|
|
if (filterId == null) {
|
|
|
|
client.request(
|
|
|
|
ApiPath.PATH_FILTERS,
|
|
|
|
params.toPostRequestBuilder()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
client.request(
|
|
|
|
"${ApiPath.PATH_FILTERS}/$filterId",
|
|
|
|
params.toRequestBody().toPut()
|
|
|
|
)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 11:19:01 +01:00
|
|
|
}
|
2021-05-27 04:15:59 +02:00
|
|
|
|
2023-01-14 11:19:01 +01:00
|
|
|
private suspend fun saveV2(vhList: List<VhKeyword>): TootApiResult? {
|
|
|
|
val params = filterParamBase().apply {
|
|
|
|
put(
|
|
|
|
"filter_action", when {
|
|
|
|
views.rbHide.isChecked -> "hide"
|
|
|
|
else -> "warn"
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
2023-01-14 11:19:01 +01:00
|
|
|
)
|
|
|
|
put("keywords_attributes", buildJsonArray {
|
|
|
|
vhList.forEach { vh ->
|
|
|
|
add(buildJsonObject {
|
|
|
|
put("keyword", vh.keyword)
|
|
|
|
put("whole_word", vh.wholeWord)
|
|
|
|
vh.id?.let { put("id", it) }
|
|
|
|
})
|
|
|
|
}
|
|
|
|
deleteIds.forEach { id ->
|
|
|
|
add(buildJsonObject {
|
|
|
|
put("id", id)
|
|
|
|
put("_destroy", id)
|
|
|
|
})
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
2023-01-14 11:19:01 +01:00
|
|
|
})
|
|
|
|
}
|
|
|
|
return runApiTask(account) { client ->
|
|
|
|
if (filterId == null) {
|
|
|
|
client.request(
|
|
|
|
ApiPath.PATH_FILTERS_V2,
|
|
|
|
params.toPostRequestBuilder()
|
|
|
|
)
|
|
|
|
} else {
|
|
|
|
client.request(
|
|
|
|
"${ApiPath.PATH_FILTERS_V2}/$filterId",
|
|
|
|
params.toRequestBody().toPut()
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun addKeywordArea(keyword: TootFilterKeyword) {
|
|
|
|
views.llKeywords.addView(VhKeyword(fk = keyword).views.root)
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun deleteKeywordArea(vh: VhKeyword) {
|
|
|
|
views.llKeywords.children.find { it.tag == vh }
|
|
|
|
?.let { views.llKeywords.removeView(it) }
|
|
|
|
vh.id?.let { deleteIds.add(it) }
|
|
|
|
}
|
|
|
|
|
|
|
|
private inner class VhKeyword(
|
|
|
|
val fk: TootFilterKeyword,
|
|
|
|
val views: LvKeywordFilterBinding = LvKeywordFilterBinding.inflate(layoutInflater),
|
|
|
|
) {
|
|
|
|
init {
|
|
|
|
views.root.tag = this
|
|
|
|
views.etKeyword.setText(fk.keyword.trim())
|
|
|
|
views.cbFilterWordMatch.isChecked = fk.whole_word
|
|
|
|
|
|
|
|
views.btnDelete.setOnClickListener {
|
|
|
|
deleteKeywordArea(this)
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
|
|
|
}
|
2023-01-14 11:19:01 +01:00
|
|
|
|
|
|
|
// onSaveInstanceや保存時に呼ばれる
|
|
|
|
fun encodeJson() =
|
|
|
|
fk.encodeNewParam(newKeyword = keyword, newWholeWord = wholeWord)
|
|
|
|
|
|
|
|
val keyword: String
|
|
|
|
get() = views.etKeyword.text.toString().trim()
|
|
|
|
|
|
|
|
val wholeWord: Boolean
|
|
|
|
get() = views.cbFilterWordMatch.isChecked
|
|
|
|
|
|
|
|
val id: String?
|
|
|
|
get() = fk.id?.toString()?.notEmpty()
|
2021-05-27 04:15:59 +02:00
|
|
|
}
|
2018-07-07 07:15:16 +02:00
|
|
|
}
|