@@ -14,16 +14,21 @@ import androidx.fragment.app.DialogFragment
14
14
import app.grapheneos.pdfviewer.PdfViewer
15
15
import app.grapheneos.pdfviewer.R
16
16
import app.grapheneos.pdfviewer.databinding.PasswordDialogFragmentBinding
17
+ import app.grapheneos.pdfviewer.viewModel.PasswordStatus
18
+ import com.google.android.material.dialog.MaterialAlertDialogBuilder
17
19
import com.google.android.material.textfield.TextInputEditText
20
+ import com.google.android.material.textfield.TextInputLayout
18
21
19
22
class PasswordPromptFragment : DialogFragment () {
20
23
24
+ private lateinit var passwordLayout : TextInputLayout
21
25
private lateinit var passwordEditText : TextInputEditText
22
26
23
27
override fun onCreateDialog (savedInstanceState : Bundle ? ): Dialog {
24
- val passwordPrompt = AlertDialog . Builder (requireContext())
28
+ val passwordPrompt = MaterialAlertDialogBuilder (requireContext())
25
29
val passwordDialogFragmentBinding =
26
30
PasswordDialogFragmentBinding .inflate(LayoutInflater .from(requireContext()))
31
+ passwordLayout = passwordDialogFragmentBinding.pdfPasswordTextInputLayout
27
32
passwordEditText = passwordDialogFragmentBinding.pdfPasswordEditText
28
33
passwordPrompt.setView(passwordDialogFragmentBinding.root)
29
34
passwordEditText.addTextChangedListener(object : TextWatcher {
@@ -38,15 +43,39 @@ class PasswordPromptFragment : DialogFragment() {
38
43
sendPassword()
39
44
true
40
45
}
41
- passwordPrompt.setPositiveButton(R .string.open) { _, _ -> sendPassword() }
46
+ passwordPrompt.setPositiveButton(R .string.open, null )
42
47
passwordPrompt.setNegativeButton(R .string.cancel, null )
43
48
val dialog = passwordPrompt.create()
49
+ passwordPrompt.setCancelable(false )
50
+ isCancelable = false
44
51
dialog.setCanceledOnTouchOutside(false )
45
52
dialog.window?.setSoftInputMode(WindowManager .LayoutParams .SOFT_INPUT_STATE_VISIBLE )
53
+ (requireActivity() as PdfViewer ).passwordValidationViewModel.status.observe(
54
+ this
55
+ ) {
56
+ when (it) {
57
+ PasswordStatus .Status .MissingPassword -> {
58
+ passwordEditText.editableText.clear()
59
+ passwordDialogFragmentBinding.title.setText(R .string.password_prompt_description)
60
+ }
61
+ PasswordStatus .Status .InvalidPassword -> {
62
+ passwordEditText.editableText.clear()
63
+ passwordDialogFragmentBinding.pdfPasswordTextInputLayout.error =
64
+ " invalid password"
65
+ }
66
+ PasswordStatus .Status .Validated -> {
67
+ // Activity will dismiss the dialog
68
+ }
69
+ else -> {
70
+ throw NullPointerException (" status shouldn't be null" )
71
+ }
72
+ }
73
+ }
46
74
return dialog
47
75
}
48
76
49
77
private fun updatePositiveButton () {
78
+ passwordLayout.error = " "
50
79
val btn = (dialog as AlertDialog ).getButton(DialogInterface .BUTTON_POSITIVE )
51
80
btn.isEnabled = passwordEditText.text?.isNotEmpty() ? : false
52
81
}
@@ -55,7 +84,6 @@ class PasswordPromptFragment : DialogFragment() {
55
84
val password = passwordEditText.text.toString()
56
85
if (! TextUtils .isEmpty(password)) {
57
86
(activity as PdfViewer ).loadPdfWithPassword(password)
58
- dialog?.dismiss()
59
87
}
60
88
}
61
89
@@ -64,4 +92,11 @@ class PasswordPromptFragment : DialogFragment() {
64
92
updatePositiveButton()
65
93
passwordEditText.requestFocus()
66
94
}
95
+
96
+ override fun onResume () {
97
+ super .onResume()
98
+ (dialog as AlertDialog ).getButton(DialogInterface .BUTTON_POSITIVE ).setOnClickListener {
99
+ sendPassword()
100
+ }
101
+ }
67
102
}
0 commit comments