File indexing completed on 2024-12-22 04:41:42

0001 /*
0002  * SPDX-FileCopyrightText: 2019 Erik Duisters <e.duisters1@gmail.com>
0003  *
0004  * SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0005  */
0006 
0007 package org.kde.kdeconnect.UserInterface;
0008 
0009 import android.app.Dialog;
0010 import android.os.Bundle;
0011 
0012 import androidx.annotation.NonNull;
0013 import androidx.annotation.Nullable;
0014 import androidx.annotation.StringRes;
0015 
0016 import com.google.android.material.textfield.TextInputEditText;
0017 
0018 import org.kde.kdeconnect_tp.R;
0019 import org.kde.kdeconnect_tp.databinding.EditTextAlertDialogViewBinding;
0020 
0021 public class EditTextAlertDialogFragment extends AlertDialogFragment {
0022     private static final String KEY_HINT_RES_ID = "HintResId";
0023     private static final String KEY_TEXT = "Text";
0024 
0025     private EditTextAlertDialogViewBinding binding;
0026     TextInputEditText editText;
0027 
0028     private @StringRes int hintResId;
0029     private String text;
0030 
0031     @NonNull
0032     @Override
0033     public Dialog onCreateDialog(@Nullable Bundle savedInstanceState) {
0034         Dialog dialog = super.onCreateDialog(savedInstanceState);
0035 
0036         Bundle args = getArguments();
0037         if (args != null) {
0038             hintResId = args.getInt(KEY_HINT_RES_ID);
0039             text = args.getString(KEY_TEXT, "");
0040         }
0041 
0042         dialog.setOnShowListener(dialogInterface -> {
0043             dialog.setOnShowListener(null);
0044 
0045             binding = EditTextAlertDialogViewBinding.bind(dialog.getWindow().getDecorView());
0046             editText = binding.textInputEditText;
0047 
0048             binding.textInputLayout.setHint(getString(hintResId));
0049             editText.setText(text);
0050         });
0051 
0052         return dialog;
0053     }
0054 
0055     public static class Builder extends AlertDialogFragment.AbstractBuilder<Builder, EditTextAlertDialogFragment> {
0056         public Builder() {
0057             super();
0058 
0059             super.setView(R.layout.edit_text_alert_dialog_view);
0060         }
0061 
0062         @Override
0063         public Builder getThis() {
0064             return this;
0065         }
0066 
0067         @Override
0068         public Builder setView(int customViewResId) {
0069             throw new RuntimeException("You cannot set a custom view on an EditTextAlertDialogFragment");
0070         }
0071 
0072         public Builder setHint(@StringRes int hintResId) {
0073             args.putInt(KEY_HINT_RES_ID, hintResId);
0074             return getThis();
0075         }
0076 
0077         public Builder setText(@NonNull String text) {
0078             args.putString(KEY_TEXT, text);
0079             return getThis();
0080         }
0081 
0082         @Override
0083         protected EditTextAlertDialogFragment createFragment() {
0084             return new EditTextAlertDialogFragment();
0085         }
0086     }
0087 }