chinwag-android/app/src/main/java/com/keylesspalace/tusky/adapter/AccountFieldEditAdapter.kt
Konrad Pozniak b976fe5296
full sdk 34 support (#4224)
builds upon work from #4082 

Additionally fixes some deprecations and adds support for [predictive
back](https://developer.android.com/guide/navigation/custom-back/predictive-back-gesture).
I also refactored how the activity transitions work because they are
closely related to predictive back. The awkward
`finishWithoutSlideOutAnimation` is gone, activities that have been
started with slide in will now automatically close with slide out.

To test predictive back you need an emulator or device with Sdk 34
(Android 14) and then enable it in the developer settings.

Predictive back requires the back action to be determined before it
actually occurs so the system can play the right predictive animation,
which made a few reorganisations necessary.

closes #4082 
closes #4005 
unlocks a bunch of dependency upgrades that require sdk 34

---------

Co-authored-by: Goooler <wangzongler@gmail.com>
2024-02-23 10:27:19 +01:00

109 lines
3.8 KiB
Kotlin

/* 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
import androidx.core.widget.doAfterTextChanged
import androidx.recyclerview.widget.RecyclerView
import com.keylesspalace.tusky.databinding.ItemEditFieldBinding
import com.keylesspalace.tusky.entity.StringField
import com.keylesspalace.tusky.util.BindingHolder
import com.keylesspalace.tusky.util.fixTextSelection
class AccountFieldEditAdapter(
var onFieldsChanged: () -> Unit = { }
) : RecyclerView.Adapter<BindingHolder<ItemEditFieldBinding>>() {
private val fieldData = mutableListOf<MutableStringPair>()
private var maxNameLength: Int? = null
private var maxValueLength: Int? = null
fun setFields(fields: List<StringField>) {
fieldData.clear()
fields.forEach { field ->
fieldData.add(MutableStringPair(field.name, field.value))
}
if (fieldData.isEmpty()) {
fieldData.add(MutableStringPair("", ""))
}
notifyDataSetChanged()
}
fun setFieldLimits(maxNameLength: Int?, maxValueLength: Int?) {
this.maxNameLength = maxNameLength
this.maxValueLength = maxValueLength
notifyDataSetChanged()
}
fun getFieldData(): List<StringField> {
return fieldData.map {
StringField(it.first, it.second)
}
}
fun addField() {
fieldData.add(MutableStringPair("", ""))
notifyItemInserted(fieldData.size - 1)
}
override fun getItemCount() = fieldData.size
override fun onCreateViewHolder(
parent: ViewGroup,
viewType: Int
): BindingHolder<ItemEditFieldBinding> {
val binding = ItemEditFieldBinding.inflate(
LayoutInflater.from(parent.context),
parent,
false
)
return BindingHolder(binding)
}
override fun onBindViewHolder(holder: BindingHolder<ItemEditFieldBinding>, position: Int) {
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
}
holder.binding.accountFieldNameText.doAfterTextChanged { newText ->
fieldData.getOrNull(holder.bindingAdapterPosition)?.first = newText.toString()
onFieldsChanged()
}
holder.binding.accountFieldValueText.doAfterTextChanged { newText ->
fieldData.getOrNull(holder.bindingAdapterPosition)?.second = newText.toString()
onFieldsChanged()
}
// Ensure the textview contents are selectable
holder.binding.accountFieldNameText.fixTextSelection()
holder.binding.accountFieldValueText.fixTextSelection()
}
class MutableStringPair(var first: String, var second: String)
}