Cleanup extra content handling (#1536)

* BasicBottomSheet: remove unnecessary custom background

Fixes: 88c9a0d487 ("app: refactor M3 themes and styles")

* FieldItemAdapter: fix typo ClipBoard -> Clipboard

* FieldItemAdapter: fix RV binding idempotency

* app: refactor FieldItemAdapter population logic

* DecryptActivityV2: wire in missing 'show password' toggle
This commit is contained in:
Harsh Shandilya 2021-11-07 20:50:25 +05:30 committed by GitHub
parent 223960d8d3
commit 67e70e5936
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 38 additions and 41 deletions

View file

@ -19,7 +19,7 @@ import dev.msfjarvis.aps.databinding.ItemFieldBinding
class FieldItemAdapter(
private var fieldItemList: List<FieldItem>,
private val showPassword: Boolean,
private val copyTextToClipBoard: (text: String?) -> Unit,
private val copyTextToClipboard: (text: String?) -> Unit,
) : RecyclerView.Adapter<FieldItemAdapter.FieldItemViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FieldItemViewHolder {
@ -28,7 +28,7 @@ class FieldItemAdapter(
}
override fun onBindViewHolder(holder: FieldItemViewHolder, position: Int) {
holder.bind(fieldItemList[position], showPassword, copyTextToClipBoard)
holder.bind(fieldItemList[position], showPassword, copyTextToClipboard)
}
override fun getItemCount(): Int {
@ -50,15 +50,10 @@ class FieldItemAdapter(
notifyItemChanged(otpItemPosition)
}
fun updateItems(itemList: List<FieldItem>) {
fieldItemList = itemList
notifyDataSetChanged()
}
class FieldItemViewHolder(itemView: View, val binding: ItemFieldBinding) :
RecyclerView.ViewHolder(itemView) {
fun bind(fieldItem: FieldItem, showPassword: Boolean, copyTextToClipBoard: (String?) -> Unit) {
fun bind(fieldItem: FieldItem, showPassword: Boolean, copyTextToClipboard: (String?) -> Unit) {
with(binding) {
itemText.hint = fieldItem.key
itemTextContainer.hint = fieldItem.key
@ -70,19 +65,23 @@ class FieldItemAdapter(
endIconDrawable =
ContextCompat.getDrawable(itemView.context, R.drawable.ic_content_copy)
endIconMode = TextInputLayout.END_ICON_CUSTOM
setEndIconOnClickListener { copyTextToClipBoard(itemText.text.toString()) }
setEndIconOnClickListener { copyTextToClipboard(itemText.text.toString()) }
}
itemText.transformationMethod = null
}
FieldItem.ActionType.HIDE -> {
itemTextContainer.apply {
endIconMode = TextInputLayout.END_ICON_PASSWORD_TOGGLE
setOnClickListener { copyTextToClipBoard(itemText.text.toString()) }
setOnClickListener { copyTextToClipboard(itemText.text.toString()) }
}
itemText.apply {
if (!showPassword) {
transformationMethod = PasswordTransformationMethod.getInstance()
}
setOnClickListener { copyTextToClipBoard(itemText.text.toString()) }
transformationMethod =
if (!showPassword) {
PasswordTransformationMethod.getInstance()
} else {
null
}
setOnClickListener { copyTextToClipboard(itemText.text.toString()) }
}
}
}

View file

@ -179,9 +179,6 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound {
runCatching {
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
val entry = passwordEntryFactory.create(lifecycleScope, outputStream.toByteArray())
val items = arrayListOf<FieldItem>()
val adapter =
FieldItemAdapter(emptyList(), showPassword) { text -> copyTextToClipboard(text) }
if (settings.getBoolean(PreferenceKeys.COPY_ON_DECRYPT, false)) {
copyPasswordToClipboard(entry.password)
@ -190,17 +187,13 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound {
passwordEntry = entry
invalidateOptionsMenu()
val items = arrayListOf<FieldItem>()
if (!entry.password.isNullOrBlank()) {
items.add(FieldItem.createPasswordField(entry.password!!))
}
if (entry.hasTotp()) {
launch {
items.add(FieldItem.createOtpField(entry.totp.value))
entry.totp.collect { code ->
withContext(Dispatchers.Main) { adapter.updateOTPCode(code) }
}
}
items.add(FieldItem.createOtpField(entry.totp.value))
}
if (!entry.username.isNullOrBlank()) {
@ -211,8 +204,17 @@ class DecryptActivity : BasePgpActivity(), OpenPgpServiceConnection.OnBound {
items.add(FieldItem(key, value, FieldItem.ActionType.COPY))
}
val adapter =
FieldItemAdapter(items, showPassword) { text -> copyTextToClipboard(text) }
binding.recyclerView.adapter = adapter
adapter.updateItems(items)
if (entry.hasTotp()) {
lifecycleScope.launch {
entry.totp.collect { code ->
withContext(Dispatchers.Main) { adapter.updateOTPCode(code) }
}
}
}
}
.onFailure { e -> logcat(ERROR) { e.asLog() } }
}

View file

@ -20,6 +20,7 @@ import dev.msfjarvis.aps.injection.password.PasswordEntryFactory
import dev.msfjarvis.aps.ui.adapters.FieldItemAdapter
import dev.msfjarvis.aps.util.extensions.unsafeLazy
import dev.msfjarvis.aps.util.extensions.viewBinding
import dev.msfjarvis.aps.util.settings.PreferenceKeys
import java.io.ByteArrayOutputStream
import java.io.File
import javax.inject.Inject
@ -142,23 +143,18 @@ class DecryptActivityV2 : BasePgpActivity() {
}
startAutoDismissTimer()
val showPassword = settings.getBoolean(PreferenceKeys.SHOW_PASSWORD, true)
val entry = passwordEntryFactory.create(lifecycleScope, result.toByteArray())
passwordEntry = entry
invalidateOptionsMenu()
val items = arrayListOf<FieldItem>()
val adapter = FieldItemAdapter(emptyList(), true) { text -> copyTextToClipboard(text) }
if (!entry.password.isNullOrBlank()) {
items.add(FieldItem.createPasswordField(entry.password!!))
}
if (entry.hasTotp()) {
lifecycleScope.launch {
items.add(FieldItem.createOtpField(entry.totp.value))
entry.totp.collect { code ->
withContext(Dispatchers.Main) { adapter.updateOTPCode(code) }
}
}
items.add(FieldItem.createOtpField(entry.totp.value))
}
if (!entry.username.isNullOrBlank()) {
@ -169,8 +165,16 @@ class DecryptActivityV2 : BasePgpActivity() {
items.add(FieldItem(key, value, FieldItem.ActionType.COPY))
}
val adapter = FieldItemAdapter(items, showPassword) { text -> copyTextToClipboard(text) }
binding.recyclerView.adapter = adapter
adapter.updateItems(items)
if (entry.hasTotp()) {
lifecycleScope.launch {
entry.totp.collect { code ->
withContext(Dispatchers.Main) { adapter.updateOTPCode(code) }
}
}
}
}
}

View file

@ -6,7 +6,6 @@
package dev.msfjarvis.aps.ui.dialogs
import android.content.Context
import android.graphics.drawable.GradientDrawable
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
@ -20,13 +19,11 @@ import com.google.android.material.bottomsheet.BottomSheetDialog
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
import dev.msfjarvis.aps.R
import dev.msfjarvis.aps.databinding.BasicBottomSheetBinding
import dev.msfjarvis.aps.ui.dialogs.BasicBottomSheet.Builder
import dev.msfjarvis.aps.util.extensions.resolveAttribute
import dev.msfjarvis.aps.util.extensions.viewBinding
/**
* [BottomSheetDialogFragment] that exposes a simple [androidx.appcompat.app.AlertDialog] like API
* through [Builder] to create a similar UI, just at the bottom of the screen.
* through [BasicBottomSheet.Builder] to create a similar UI, just at the bottom of the screen.
*/
class BasicBottomSheet
private constructor(
@ -100,11 +97,6 @@ private constructor(
}
}
)
val gradientDrawable =
GradientDrawable().apply {
setColor(requireContext().resolveAttribute(android.R.attr.windowBackground))
}
view.background = gradientDrawable
}
override fun dismiss() {