-
-
Notifications
You must be signed in to change notification settings - Fork 36
fix: dialog view state preservation #284
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Vatsuki
wants to merge
5
commits into
FossifyOrg:main
Choose a base branch
from
Vatsuki:DialogView-StatePreservation
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+499
−26
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
22113b0
Fix #744 - Dialog view is destroyed (state not preserved) when rotati…
Vatsuki 198bbb4
Merge branch 'FossifyOrg:main' into DialogView-StatePreservation
Vatsuki fb9e246
update CHANGELOG.md
Vatsuki eae0176
Fix for PR : reduced lines lenght + optimized imports
Vatsuki 5404d92
Implementation of the PR feedbacks and previous features
Vatsuki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
147 changes: 147 additions & 0 deletions
147
app/src/main/kotlin/org/fossify/notes/dialogs/ChecklistItemDialogFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,147 @@ | ||
| package org.fossify.notes.dialogs | ||
|
|
||
| import android.app.Dialog | ||
| import android.os.Bundle | ||
| import android.view.View | ||
| import android.view.WindowManager | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.appcompat.widget.AppCompatEditText | ||
| import androidx.fragment.app.DialogFragment | ||
| import org.fossify.notes.R | ||
| import org.fossify.notes.databinding.DialogNewChecklistItemBinding | ||
| import org.fossify.notes.databinding.ItemAddChecklistBinding | ||
| import org.fossify.commons.extensions.getAlertDialogBuilder | ||
| import org.fossify.commons.extensions.getContrastColor | ||
| import org.fossify.commons.extensions.getProperPrimaryColor | ||
| import org.fossify.commons.extensions.showKeyboard | ||
| import org.fossify.commons.R as CommonsR | ||
|
|
||
| class ChecklistItemDialogFragment : DialogFragment() { | ||
|
|
||
| private val activeInputFields = mutableListOf<AppCompatEditText>() | ||
| private var binding: DialogNewChecklistItemBinding? = null | ||
|
|
||
| companion object { | ||
| const val DIALOG_TAG = "ChecklistItemDialogFragment" | ||
| const val REQUEST_KEY = "ChecklistItemRequest" | ||
| const val RESULT_TEXT_KEY = "ResultText" | ||
| const val RESULT_TASK_ID_KEY = "ResultTaskId" | ||
|
|
||
| private const val ARG_TEXT = "ArgText" | ||
| private const val ARG_TASK_ID = "ArgTaskId" | ||
| private const val SAVED_STATE_TEXTS = "SavedStateTexts" | ||
|
Comment on lines
+25
to
+32
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. IDs should use snake case for consistency. |
||
|
|
||
| fun newInstance(taskId: Int = -1, text: String = ""): ChecklistItemDialogFragment { | ||
| val fragment = ChecklistItemDialogFragment() | ||
| val args = Bundle() | ||
| args.putInt(ARG_TASK_ID, taskId) | ||
| args.putString(ARG_TEXT, text) | ||
| fragment.arguments = args | ||
| return fragment | ||
| } | ||
| } | ||
|
|
||
| override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { | ||
| val activity = requireActivity() | ||
| val taskId = arguments?.getInt(ARG_TASK_ID) ?: -1 | ||
|
|
||
| binding = DialogNewChecklistItemBinding.inflate(activity.layoutInflater) | ||
|
|
||
| activeInputFields.clear() | ||
|
|
||
| // Restore rows | ||
| if (savedInstanceState != null) { | ||
| val savedTexts = savedInstanceState.getStringArrayList(SAVED_STATE_TEXTS) | ||
| if (!savedTexts.isNullOrEmpty()) { | ||
| savedTexts.forEach { text -> addNewRow(text) } | ||
| } else { | ||
| addNewRow("") | ||
| } | ||
| } else { | ||
| val initialText = arguments?.getString(ARG_TEXT) ?: "" | ||
| addNewRow(initialText) | ||
| } | ||
|
|
||
| val isNewTaskMode = (taskId == -1) | ||
| if (isNewTaskMode) { | ||
| val contrastColor = activity.getProperPrimaryColor().getContrastColor() | ||
| binding!!.addItem.setColorFilter(contrastColor) | ||
|
|
||
| binding!!.addItem.setOnClickListener { | ||
| addNewRow("") | ||
| } | ||
| } else { | ||
| binding!!.addItem.visibility = View.GONE | ||
| binding!!.settingsAddChecklistTop.visibility = View.GONE | ||
| } | ||
|
|
||
| val titleRes = if (isNewTaskMode) R.string.add_new_checklist_items else R.string.rename_note | ||
|
|
||
| val builder = activity.getAlertDialogBuilder() | ||
| .setTitle(titleRes) | ||
| .setView(binding!!.root) | ||
| .setPositiveButton(CommonsR.string.ok, null) | ||
| .setNegativeButton(CommonsR.string.cancel, null) | ||
|
|
||
| val dialog = builder.create() | ||
| dialog.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE) | ||
|
|
||
| dialog.setOnShowListener { | ||
| if (activeInputFields.isNotEmpty()) { | ||
| dialog.showKeyboard(activeInputFields.last()) | ||
| } | ||
|
|
||
| val positiveButton = (dialog as AlertDialog).getButton(AlertDialog.BUTTON_POSITIVE) | ||
| positiveButton.setOnClickListener { | ||
| val combinedText = activeInputFields | ||
| .map { it.text.toString().trim() } | ||
| .filter { it.isNotEmpty() } | ||
| .joinToString("\n") | ||
|
|
||
| if (combinedText.isNotEmpty()) { | ||
| val resultBundle = Bundle().apply { | ||
| putString(RESULT_TEXT_KEY, combinedText) | ||
| putInt(RESULT_TASK_ID_KEY, taskId) | ||
| } | ||
| parentFragmentManager.setFragmentResult(REQUEST_KEY, resultBundle) | ||
| dialog.dismiss() | ||
| } else { | ||
| dialog.dismiss() | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return dialog | ||
| } | ||
|
|
||
| private fun addNewRow(text: String) { | ||
| val rowBinding = ItemAddChecklistBinding.inflate(layoutInflater) | ||
|
|
||
| // We disable automatic state saving for this view. | ||
| // This prevents Android from confusing the multiple EditTexts (which all share the same ID) | ||
| // and overwriting our manually restored text with the last view's text. | ||
| rowBinding.titleEditText.isSaveEnabled = false | ||
|
|
||
| rowBinding.titleEditText.setText(text) | ||
|
|
||
| if (text.isNotEmpty()) { | ||
| rowBinding.titleEditText.setSelection(text.length) | ||
| } | ||
|
|
||
| val inputField = rowBinding.titleEditText as AppCompatEditText | ||
| activeInputFields.add(inputField) | ||
|
|
||
| binding?.checklistHolder?.addView(rowBinding.root) | ||
| } | ||
|
|
||
| override fun onSaveInstanceState(outState: Bundle) { | ||
| super.onSaveInstanceState(outState) | ||
| val currentTexts = ArrayList(activeInputFields.map { it.text.toString() }) | ||
| outState.putStringArrayList(SAVED_STATE_TEXTS, currentTexts) | ||
| } | ||
|
|
||
| override fun onDestroyView() { | ||
| super.onDestroyView() | ||
| binding = null | ||
| } | ||
| } | ||
78 changes: 78 additions & 0 deletions
78
app/src/main/kotlin/org/fossify/notes/dialogs/EditTaskDialogFragment.kt
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,78 @@ | ||
| package org.fossify.notes.dialogs | ||
|
|
||
| import android.content.DialogInterface | ||
| import android.os.Bundle | ||
| import androidx.appcompat.app.AlertDialog | ||
| import androidx.core.os.bundleOf | ||
| import androidx.fragment.app.DialogFragment | ||
| import org.fossify.commons.extensions.getAlertDialogBuilder | ||
| import org.fossify.commons.extensions.setupDialogStuff | ||
| import org.fossify.commons.extensions.showKeyboard | ||
| import org.fossify.commons.extensions.toast | ||
| import org.fossify.notes.databinding.DialogRenameChecklistItemBinding | ||
| import org.fossify.notes.extensions.maybeRequestIncognito | ||
| import org.fossify.notes.models.Task | ||
|
|
||
| class EditTaskDialogFragment : DialogFragment() { | ||
|
|
||
| companion object { | ||
| const val TAG = "EditTaskDialog" | ||
| const val ARG_TASK_ID = "arg_task_id" | ||
| const val ARG_OLD_TITLE = "arg_old_title" | ||
| const val REQUEST_KEY = "edit_task_request" | ||
| const val RESULT_TITLE = "result_title" | ||
| const val RESULT_TASK_ID = "result_task_id" | ||
| private const val STATE_TEXT = "state_text" | ||
|
|
||
| fun show( | ||
| host: androidx.fragment.app.FragmentManager, | ||
| task: Task | ||
| ) = EditTaskDialogFragment().apply { | ||
| arguments = bundleOf(ARG_OLD_TITLE to task.title, ARG_TASK_ID to task.id) | ||
| }.show(host, TAG) | ||
| } | ||
|
|
||
| private lateinit var binding: DialogRenameChecklistItemBinding | ||
|
|
||
| override fun onCreateDialog(savedInstanceState: Bundle?): AlertDialog { | ||
| val activity = requireActivity() | ||
| binding = DialogRenameChecklistItemBinding.inflate(activity.layoutInflater).also { | ||
| val restored = savedInstanceState?.getString(STATE_TEXT) | ||
| it.checklistItemTitle.setText( | ||
| restored ?: requireArguments().getString(ARG_OLD_TITLE).orEmpty() | ||
| ) | ||
| it.checklistItemTitle.maybeRequestIncognito() | ||
| } | ||
|
|
||
| val builder = activity.getAlertDialogBuilder() | ||
| .setPositiveButton(org.fossify.commons.R.string.ok, null) | ||
| .setNegativeButton(org.fossify.commons.R.string.cancel, null) | ||
|
|
||
| var dialog: AlertDialog? = null | ||
| activity.setupDialogStuff(binding.root, builder) { alert -> | ||
| alert.showKeyboard(binding.checklistItemTitle) | ||
| alert.getButton(DialogInterface.BUTTON_POSITIVE).setOnClickListener { | ||
| val newTitle = binding.checklistItemTitle.text?.toString().orEmpty() | ||
| if (newTitle.isEmpty()) { | ||
| activity.toast(org.fossify.commons.R.string.empty_name) | ||
| } else { | ||
| val taskId = requireArguments().getInt(ARG_TASK_ID) | ||
| parentFragmentManager | ||
| .setFragmentResult( | ||
| REQUEST_KEY, bundleOf(RESULT_TASK_ID to taskId, RESULT_TITLE to newTitle) | ||
| ) | ||
| alert.dismiss() | ||
| } | ||
| } | ||
| dialog = alert | ||
| } | ||
|
|
||
| return dialog!! | ||
| } | ||
|
|
||
| override fun onSaveInstanceState(outState: Bundle) { | ||
| val text = binding.checklistItemTitle.text?.toString().orEmpty() | ||
| outState.putString(STATE_TEXT, text) | ||
| super.onSaveInstanceState(outState) | ||
| } | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can keep using the fully qualified R class name for this as that's the pattern everywhere in this project.