2018-08-15 20:47:09 +02:00
|
|
|
/* Copyright 2018 Conny Duck
|
|
|
|
*
|
|
|
|
* This file is a part of Tusky.
|
|
|
|
*
|
|
|
|
* This program is free software; you can redistribute it and/or modify it under the terms of the
|
|
|
|
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* Tusky is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
|
|
|
|
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
|
|
|
|
* Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along with Tusky; if not,
|
|
|
|
* see <http://www.gnu.org/licenses>. */
|
|
|
|
|
|
|
|
package com.keylesspalace.tusky.adapter
|
|
|
|
|
|
|
|
import android.view.LayoutInflater
|
|
|
|
import android.view.ViewGroup
|
2023-03-10 20:27:24 +01:00
|
|
|
import androidx.core.widget.doAfterTextChanged
|
2021-03-07 19:24:01 +01:00
|
|
|
import androidx.recyclerview.widget.RecyclerView
|
|
|
|
import com.keylesspalace.tusky.databinding.ItemEditFieldBinding
|
2018-08-15 20:47:09 +02:00
|
|
|
import com.keylesspalace.tusky.entity.StringField
|
2021-03-07 19:24:01 +01:00
|
|
|
import com.keylesspalace.tusky.util.BindingHolder
|
2023-06-11 18:39:48 +02:00
|
|
|
import com.keylesspalace.tusky.util.fixTextSelection
|
2018-08-15 20:47:09 +02:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
class AccountFieldEditAdapter : RecyclerView.Adapter<BindingHolder<ItemEditFieldBinding>>() {
|
2018-08-15 20:47:09 +02:00
|
|
|
|
|
|
|
private val fieldData = mutableListOf<MutableStringPair>()
|
2022-07-26 20:24:50 +02:00
|
|
|
private var maxNameLength: Int? = null
|
|
|
|
private var maxValueLength: Int? = null
|
2018-08-15 20:47:09 +02:00
|
|
|
|
|
|
|
fun setFields(fields: List<StringField>) {
|
|
|
|
fieldData.clear()
|
|
|
|
|
|
|
|
fields.forEach { field ->
|
|
|
|
fieldData.add(MutableStringPair(field.name, field.value))
|
|
|
|
}
|
2021-06-28 21:13:24 +02:00
|
|
|
if (fieldData.isEmpty()) {
|
2018-08-15 20:47:09 +02:00
|
|
|
fieldData.add(MutableStringPair("", ""))
|
|
|
|
}
|
|
|
|
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
2022-07-26 20:24:50 +02:00
|
|
|
fun setFieldLimits(maxNameLength: Int?, maxValueLength: Int?) {
|
|
|
|
this.maxNameLength = maxNameLength
|
|
|
|
this.maxValueLength = maxValueLength
|
|
|
|
notifyDataSetChanged()
|
|
|
|
}
|
|
|
|
|
2018-08-15 20:47:09 +02:00
|
|
|
fun getFieldData(): List<StringField> {
|
|
|
|
return fieldData.map {
|
|
|
|
StringField(it.first, it.second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fun addField() {
|
|
|
|
fieldData.add(MutableStringPair("", ""))
|
|
|
|
notifyItemInserted(fieldData.size - 1)
|
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun getItemCount() = fieldData.size
|
2018-08-15 20:47:09 +02:00
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BindingHolder<ItemEditFieldBinding> {
|
|
|
|
val binding = ItemEditFieldBinding.inflate(LayoutInflater.from(parent.context), parent, false)
|
|
|
|
return BindingHolder(binding)
|
2018-08-15 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2021-03-07 19:24:01 +01:00
|
|
|
override fun onBindViewHolder(holder: BindingHolder<ItemEditFieldBinding>, position: Int) {
|
2022-07-26 20:24:50 +02:00
|
|
|
holder.binding.accountFieldNameText.setText(fieldData[position].first)
|
|
|
|
holder.binding.accountFieldValueText.setText(fieldData[position].second)
|
|
|
|
|
|
|
|
holder.binding.accountFieldNameTextLayout.isCounterEnabled = maxNameLength != null
|
|
|
|
maxNameLength?.let {
|
|
|
|
holder.binding.accountFieldNameTextLayout.counterMaxLength = it
|
|
|
|
}
|
|
|
|
|
|
|
|
holder.binding.accountFieldValueTextLayout.isCounterEnabled = maxValueLength != null
|
|
|
|
maxValueLength?.let {
|
|
|
|
holder.binding.accountFieldValueTextLayout.counterMaxLength = it
|
|
|
|
}
|
2018-08-15 20:47:09 +02:00
|
|
|
|
2023-03-10 20:27:24 +01:00
|
|
|
holder.binding.accountFieldNameText.doAfterTextChanged { newText ->
|
2023-07-06 19:57:35 +02:00
|
|
|
fieldData.getOrNull(holder.bindingAdapterPosition)?.first = newText.toString()
|
2023-03-10 20:27:24 +01:00
|
|
|
}
|
2018-08-15 20:47:09 +02:00
|
|
|
|
2023-03-10 20:27:24 +01:00
|
|
|
holder.binding.accountFieldValueText.doAfterTextChanged { newText ->
|
2023-07-06 19:57:35 +02:00
|
|
|
fieldData.getOrNull(holder.bindingAdapterPosition)?.second = newText.toString()
|
2023-03-10 20:27:24 +01:00
|
|
|
}
|
2023-06-11 18:39:48 +02:00
|
|
|
|
|
|
|
// Ensure the textview contents are selectable
|
|
|
|
holder.binding.accountFieldNameText.fixTextSelection()
|
|
|
|
holder.binding.accountFieldValueText.fixTextSelection()
|
2018-08-15 20:47:09 +02:00
|
|
|
}
|
|
|
|
|
2021-06-28 21:13:24 +02:00
|
|
|
class MutableStringPair(var first: String, var second: String)
|
2018-08-15 20:47:09 +02:00
|
|
|
}
|