|
| 1 | +package org.fossify.notes.dialogs |
| 2 | + |
| 3 | +import android.content.DialogInterface |
| 4 | +import android.os.Bundle |
| 5 | +import android.view.KeyEvent |
| 6 | +import android.view.View |
| 7 | +import android.view.inputmethod.EditorInfo |
| 8 | +import androidx.appcompat.app.AlertDialog |
| 9 | +import androidx.appcompat.widget.AppCompatEditText |
| 10 | +import androidx.core.os.bundleOf |
| 11 | +import androidx.fragment.app.DialogFragment |
| 12 | +import org.fossify.commons.extensions.beVisibleIf |
| 13 | +import org.fossify.commons.extensions.getAlertDialogBuilder |
| 14 | +import org.fossify.commons.extensions.getContrastColor |
| 15 | +import org.fossify.commons.extensions.getProperPrimaryColor |
| 16 | +import org.fossify.commons.extensions.setupDialogStuff |
| 17 | +import org.fossify.commons.extensions.showKeyboard |
| 18 | +import org.fossify.commons.extensions.toast |
| 19 | +import org.fossify.commons.helpers.SORT_BY_CUSTOM |
| 20 | +import org.fossify.notes.R |
| 21 | +import org.fossify.notes.databinding.DialogNewChecklistItemBinding |
| 22 | +import org.fossify.notes.databinding.ItemAddChecklistBinding |
| 23 | +import org.fossify.notes.extensions.config |
| 24 | +import org.fossify.notes.extensions.maybeRequestIncognito |
| 25 | + |
| 26 | +class NewChecklistItemDialogFragment : DialogFragment() { |
| 27 | + |
| 28 | + private val activeInputFields = mutableListOf<AppCompatEditText>() |
| 29 | + private var binding: DialogNewChecklistItemBinding? = null |
| 30 | + |
| 31 | + // Track the index of the currently focused row |
| 32 | + private var lastFocusedIndex = -1 |
| 33 | + |
| 34 | + companion object { |
| 35 | + const val TAG = "NewChecklistItemDialogFragment" |
| 36 | + const val REQUEST_KEY = "new_checklist_item_request" |
| 37 | + const val RESULT_TEXT = "result_text" |
| 38 | + const val RESULT_ADD_TOP = "result_add_top" |
| 39 | + |
| 40 | + private const val ARG_NOTE_ID = "arg_note_id" |
| 41 | + private const val STATE_TEXTS = "state_texts" |
| 42 | + |
| 43 | + private const val STATE_FOCUSED_INDEX = "state_focused_index" |
| 44 | + |
| 45 | + |
| 46 | + fun show( |
| 47 | + host: androidx.fragment.app.FragmentManager, |
| 48 | + noteId: Long |
| 49 | + ) = NewChecklistItemDialogFragment().apply { |
| 50 | + arguments = bundleOf(ARG_NOTE_ID to noteId) |
| 51 | + }.show(host, TAG) |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog { |
| 56 | + val activity = requireActivity() |
| 57 | + binding = DialogNewChecklistItemBinding.inflate(activity.layoutInflater) |
| 58 | + activeInputFields.clear() |
| 59 | + |
| 60 | + // Restore state or add initial row |
| 61 | + if (savedInstanceState != null) { |
| 62 | + val savedTexts = savedInstanceState.getStringArrayList(STATE_TEXTS) |
| 63 | + if (!savedTexts.isNullOrEmpty()) { |
| 64 | + savedTexts.forEach { text -> addNewRow(text) } |
| 65 | + } else { |
| 66 | + addNewRow("") |
| 67 | + } |
| 68 | + // Restore the focus index |
| 69 | + lastFocusedIndex = savedInstanceState.getInt(STATE_FOCUSED_INDEX, -1) |
| 70 | + } else { |
| 71 | + addNewRow("") |
| 72 | + } |
| 73 | + |
| 74 | + // Setup UI |
| 75 | + val noteId = requireArguments().getLong(ARG_NOTE_ID) |
| 76 | + val contrastColor = activity.getProperPrimaryColor().getContrastColor() |
| 77 | + binding!!.addItem.setColorFilter(contrastColor) |
| 78 | + |
| 79 | + // Insert after the currently focused row |
| 80 | + binding!!.addItem.setOnClickListener { |
| 81 | + val insertIndex = if (lastFocusedIndex != -1 && lastFocusedIndex < activeInputFields.size) { |
| 82 | + lastFocusedIndex + 1 |
| 83 | + } else { |
| 84 | + null // Append to end if nothing is focused |
| 85 | + } |
| 86 | + addNewRow("", focus = true, position = insertIndex) |
| 87 | + } |
| 88 | + |
| 89 | + val config = activity.config |
| 90 | + binding!!.settingsAddChecklistTop.beVisibleIf(config.getSorting(noteId) == SORT_BY_CUSTOM) |
| 91 | + binding!!.settingsAddChecklistTop.isChecked = config.addNewChecklistItemsTop |
| 92 | + |
| 93 | + val builder = activity.getAlertDialogBuilder() |
| 94 | + .setTitle(R.string.add_new_checklist_items) |
| 95 | + .setPositiveButton(org.fossify.commons.R.string.ok, null) |
| 96 | + .setNegativeButton(org.fossify.commons.R.string.cancel, null) |
| 97 | + |
| 98 | + var dialog: AlertDialog? = null |
| 99 | + activity.setupDialogStuff(binding!!.root, builder) { alert -> |
| 100 | + |
| 101 | + // Apply Focus : if we have a valid restored index, use it |
| 102 | + if (lastFocusedIndex != -1 && lastFocusedIndex < activeInputFields.size) { |
| 103 | + alert.showKeyboard(activeInputFields[lastFocusedIndex]) |
| 104 | + } else if (activeInputFields.isNotEmpty()) { |
| 105 | + // Default to the last |
| 106 | + alert.showKeyboard(activeInputFields.last()) |
| 107 | + } |
| 108 | + |
| 109 | + alert.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { |
| 110 | + // Collect all texts |
| 111 | + val combinedText = activeInputFields |
| 112 | + .map { it.text.toString().trim() } |
| 113 | + .filter { it.isNotEmpty() } |
| 114 | + .joinToString("\n") |
| 115 | + |
| 116 | + if (combinedText.isEmpty()) { |
| 117 | + activity.toast(org.fossify.commons.R.string.empty_name) |
| 118 | + } else { |
| 119 | + config.addNewChecklistItemsTop = binding!!.settingsAddChecklistTop.isChecked |
| 120 | + |
| 121 | + // Return result |
| 122 | + parentFragmentManager.setFragmentResult( |
| 123 | + REQUEST_KEY, |
| 124 | + bundleOf( |
| 125 | + RESULT_TEXT to combinedText, |
| 126 | + RESULT_ADD_TOP to binding!!.settingsAddChecklistTop.isChecked |
| 127 | + ) |
| 128 | + ) |
| 129 | + alert.dismiss() |
| 130 | + } |
| 131 | + } |
| 132 | + dialog = alert |
| 133 | + } |
| 134 | + |
| 135 | + return dialog!! |
| 136 | + } |
| 137 | + |
| 138 | + private fun addNewRow(text: String, focus: Boolean = false, position: Int? = null) { |
| 139 | + val rowBinding = ItemAddChecklistBinding.inflate(layoutInflater) |
| 140 | + |
| 141 | + // Disable state saving for individual views to avoid rotation conflict |
| 142 | + rowBinding.titleEditText.isSaveEnabled = false |
| 143 | + rowBinding.titleEditText.setText(text) |
| 144 | + rowBinding.titleEditText.maybeRequestIncognito() |
| 145 | + |
| 146 | + if (text.isNotEmpty()) { |
| 147 | + rowBinding.titleEditText.setSelection(text.length) |
| 148 | + } |
| 149 | + |
| 150 | + // Track focus changes in real time |
| 151 | + rowBinding.titleEditText.setOnFocusChangeListener { view, hasFocus -> |
| 152 | + if (hasFocus) { |
| 153 | + // When this view gets focus, remember its index |
| 154 | + lastFocusedIndex = activeInputFields.indexOf(view) |
| 155 | + } |
| 156 | + } |
| 157 | + |
| 158 | + // Add "Enter" key listener to create new rows automatically |
| 159 | + rowBinding.titleEditText.setOnEditorActionListener { v, actionId, event -> |
| 160 | + if (actionId == EditorInfo.IME_ACTION_NEXT || |
| 161 | + actionId == EditorInfo.IME_ACTION_DONE || |
| 162 | + event?.keyCode == KeyEvent.KEYCODE_ENTER) { |
| 163 | + |
| 164 | + val currentIndex = activeInputFields.indexOf(v) |
| 165 | + addNewRow("", focus = true, position = currentIndex + 1) |
| 166 | + true |
| 167 | + } else { |
| 168 | + false |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + val inputField = rowBinding.titleEditText as AppCompatEditText |
| 173 | + |
| 174 | + // Insert into list and view hierarchy at correct position |
| 175 | + if (position != null && position < activeInputFields.size) { |
| 176 | + activeInputFields.add(position, inputField) |
| 177 | + binding?.checklistHolder?.addView(rowBinding.root, position) |
| 178 | + } else { |
| 179 | + activeInputFields.add(inputField) |
| 180 | + binding?.checklistHolder?.addView(rowBinding.root) |
| 181 | + } |
| 182 | + |
| 183 | + |
| 184 | + if (focus) { |
| 185 | + binding?.dialogHolder?.post { |
| 186 | + // Only scroll to bottom if appending to the end |
| 187 | + if (position == null) { |
| 188 | + binding?.dialogHolder?.fullScroll(View.FOCUS_DOWN) |
| 189 | + } |
| 190 | + |
| 191 | + inputField.requestFocus() |
| 192 | + requireActivity().showKeyboard(inputField) |
| 193 | + } |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + override fun onSaveInstanceState(outState: Bundle) { |
| 198 | + super.onSaveInstanceState(outState) |
| 199 | + val currentTexts = ArrayList(activeInputFields.map { it.text.toString() }) |
| 200 | + outState.putStringArrayList(STATE_TEXTS, currentTexts) |
| 201 | + |
| 202 | + // Save the index tracked via the listener |
| 203 | + outState.putInt(STATE_FOCUSED_INDEX, lastFocusedIndex) |
| 204 | + } |
| 205 | + |
| 206 | + override fun onDestroyView() { |
| 207 | + super.onDestroyView() |
| 208 | + binding = null |
| 209 | + activeInputFields.clear() |
| 210 | + } |
| 211 | +} |
0 commit comments