From b39b80dc58b75ee5e2b32f4c8d112bb242074ecd Mon Sep 17 00:00:00 2001 From: euxane Date: Wed, 18 Sep 2024 23:41:13 +0200 Subject: dialogs: remove usage of kotlin synthetics --- app/build.gradle | 1 + .../pacien/tincapp/activities/BaseDialogFragment.kt | 8 +------- .../tools/ConfigurationToolDialogFragment.kt | 12 ++++-------- .../EncryptDecryptPrivateKeysToolDialogFragment.kt | 10 +++++----- .../tools/GenerateConfigToolDialogFragment.kt | 10 +++++----- .../configure/tools/JoinNetworkToolDialogFragment.kt | 19 +++++++------------ 6 files changed, 23 insertions(+), 37 deletions(-) diff --git a/app/build.gradle b/app/build.gradle index 7433b49..6fe715c 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -69,6 +69,7 @@ android { buildFeatures { dataBinding = true + viewBinding = true } namespace 'org.pacien.tincapp' diff --git a/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt index 12c2630..4888cd0 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/BaseDialogFragment.kt @@ -19,11 +19,8 @@ package org.pacien.tincapp.activities import android.content.Context -import androidx.annotation.LayoutRes import androidx.fragment.app.DialogFragment import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup /** * @author euxane @@ -31,8 +28,5 @@ import android.view.ViewGroup abstract class BaseDialogFragment : DialogFragment() { protected val parentActivity by lazy { activity as BaseActivity } // getLayoutInflater() calls onCreateDialog. See https://stackoverflow.com/a/15152788 - private val inflater by lazy { activity!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater } - - fun inflate(@LayoutRes layout: Int) = inflater.inflate(layout, null)!! - fun inflate(inflateFunc: (LayoutInflater, ViewGroup?, Boolean) -> View) = inflateFunc(inflater, null, false) + protected val dialogLayoutInflater by lazy { activity!!.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater } } diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt index 18e403a..533faa9 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/ConfigurationToolDialogFragment.kt @@ -18,10 +18,9 @@ package org.pacien.tincapp.activities.configure.tools -import androidx.annotation.LayoutRes import androidx.annotation.StringRes import androidx.appcompat.app.AlertDialog -import android.view.View +import androidx.viewbinding.ViewBinding import java8.util.concurrent.CompletableFuture import org.pacien.tincapp.R import org.pacien.tincapp.activities.BaseDialogFragment @@ -35,14 +34,11 @@ import java.util.regex.Pattern abstract class ConfigurationToolDialogFragment : BaseDialogFragment() { private val networkNamePattern by lazy { Pattern.compile("^[^\\x00/]*$") } - protected fun makeDialog(@LayoutRes layout: Int, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = - makeDialog(inflate(layout), title, applyButton, applyAction) - - protected fun makeDialog(view: View, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (View) -> Unit) = + protected fun makeDialog(viewBinding: ViewBindingT, @StringRes title: Int, @StringRes applyButton: Int, applyAction: (ViewBindingT) -> Unit) = AlertDialog.Builder(parentActivity) .setTitle(title) - .setView(view) - .setPositiveButton(applyButton) { _, _ -> applyAction(view) } + .setView(viewBinding.root) + .setPositiveButton(applyButton) { _, _ -> applyAction(viewBinding) } .setNegativeButton(R.string.generic_action_cancel) { _, _ -> Unit } .create()!! diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt index b58ace4..c583272 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/EncryptDecryptPrivateKeysToolDialogFragment.kt @@ -19,7 +19,7 @@ package org.pacien.tincapp.activities.configure.tools import android.os.Bundle -import kotlinx.android.synthetic.main.configure_tools_dialog_encrypt_decrypt_keys.view.* +import org.pacien.tincapp.databinding.ConfigureToolsDialogEncryptDecryptKeysBinding import org.pacien.tincapp.R import org.pacien.tincapp.commands.TincApp @@ -29,14 +29,14 @@ import org.pacien.tincapp.commands.TincApp class EncryptDecryptPrivateKeysToolDialogFragment : ConfigurationToolDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?) = makeDialog( - R.layout.configure_tools_dialog_encrypt_decrypt_keys, + ConfigureToolsDialogEncryptDecryptKeysBinding.inflate(dialogLayoutInflater), R.string.configure_tools_private_keys_encryption_title, R.string.configure_tools_private_keys_encryption_action ) { dialog -> encryptDecryptPrivateKeys( - dialog.enc_dec_net_name.text.toString(), - dialog.enc_dec_current_passphrase.text.toString(), - dialog.enc_dec_new_passphrase.text.toString() + dialog.encDecNetName.text.toString(), + dialog.encDecCurrentPassphrase.text.toString(), + dialog.encDecNewPassphrase.text.toString() ) } diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt index 2440e21..62d3890 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/GenerateConfigToolDialogFragment.kt @@ -19,7 +19,7 @@ package org.pacien.tincapp.activities.configure.tools import android.os.Bundle -import kotlinx.android.synthetic.main.configure_tools_dialog_network_generate.view.* +import org.pacien.tincapp.databinding.ConfigureToolsDialogNetworkGenerateBinding import org.pacien.tincapp.R import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.TincApp @@ -32,14 +32,14 @@ import org.pacien.tincapp.utils.makePublic class GenerateConfigToolDialogFragment : ConfigurationToolDialogFragment() { override fun onCreateDialog(savedInstanceState: Bundle?) = makeDialog( - R.layout.configure_tools_dialog_network_generate, + ConfigureToolsDialogNetworkGenerateBinding.inflate(dialogLayoutInflater), R.string.configure_tools_generate_config_title, R.string.configure_tools_generate_config_action ) { dialog -> generateConf( - dialog.new_net_name.text.toString(), - dialog.new_node_name.text.toString(), - dialog.new_passphrase.text.toString() + dialog.newNetName.text.toString(), + dialog.newNodeName.text.toString(), + dialog.newPassphrase.text.toString() ) } diff --git a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt index 4460ddc..a7283bd 100644 --- a/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt +++ b/app/src/main/java/org/pacien/tincapp/activities/configure/tools/JoinNetworkToolDialogFragment.kt @@ -20,10 +20,8 @@ package org.pacien.tincapp.activities.configure.tools import android.content.Intent import android.os.Bundle -import android.view.View import com.google.zxing.integration.android.IntentIntegrator import com.google.zxing.integration.android.IntentResult -import kotlinx.android.synthetic.main.configure_tools_dialog_network_join.view.* import org.pacien.tincapp.R import org.pacien.tincapp.commands.Tinc import org.pacien.tincapp.commands.TincApp @@ -36,13 +34,13 @@ import org.pacien.tincapp.utils.makePublic */ class JoinNetworkToolDialogFragment : ConfigurationToolDialogFragment() { private val scanner by lazy { IntentIntegrator.forSupportFragment(this) } - private var joinDialog: View? = null + private var joinDialog: ConfigureToolsDialogNetworkJoinBinding? = null override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) { IntentIntegrator.parseActivityResult(requestCode, resultCode, data) ?.let(IntentResult::getContents) ?.let(String::trim) - ?.let { joinDialog?.invitation_url?.setText(it) } + ?.let { joinDialog?.invitationUrl?.setText(it) } } override fun onCreateDialog(savedInstanceState: Bundle?) = @@ -54,19 +52,16 @@ class JoinNetworkToolDialogFragment : ConfigurationToolDialogFragment() { R.string.configure_tools_join_network_action ) { dialog -> joinNetwork( - dialog.net_name.text.toString(), - dialog.invitation_url.text.toString(), - dialog.join_passphrase.text.toString() + dialog.netName.text.toString(), + dialog.invitationUrl.text.toString(), + dialog.joinPassphrase.text.toString() ) } } private fun makeJoinDialog() = - parentActivity.inflate { inflater, parent, attachToRoot -> - ConfigureToolsDialogNetworkJoinBinding.inflate(inflater, parent, attachToRoot) - .apply { scanAction = this@JoinNetworkToolDialogFragment::scanCode } - .root - } + ConfigureToolsDialogNetworkJoinBinding.inflate(dialogLayoutInflater) + .apply { scanAction = this@JoinNetworkToolDialogFragment::scanCode } private fun scanCode() { scanner.initiateScan() -- cgit v1.2.3