Skip to content

Commit

Permalink
CustomStudyDialog to use Alert Dialog
Browse files Browse the repository at this point in the history
  • Loading branch information
neeldoshii committed Feb 8, 2024
1 parent 899a442 commit 80f320c
Showing 1 changed file with 23 additions and 19 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,9 @@ import android.widget.EditText
import android.widget.TextView
import androidx.annotation.StringRes
import androidx.annotation.VisibleForTesting
import androidx.appcompat.app.AlertDialog
import androidx.core.content.edit
import androidx.fragment.app.DialogFragment
import com.afollestad.materialdialogs.MaterialDialog
import com.afollestad.materialdialogs.WhichButton
import com.afollestad.materialdialogs.actions.getActionButton
import com.afollestad.materialdialogs.customview.customView
import com.afollestad.materialdialogs.list.listItems
import com.ichi2.anki.*
import com.ichi2.anki.UIUtils.showThemedToast
import com.ichi2.anki.analytics.AnalyticsDialogFragment
Expand All @@ -53,6 +49,11 @@ import com.ichi2.libanki.Deck
import com.ichi2.libanki.DeckId
import com.ichi2.utils.HashUtil.hashMapInit
import com.ichi2.utils.KotlinCleanup
import com.ichi2.utils.cancelable
import com.ichi2.utils.customView
import com.ichi2.utils.negativeButton
import com.ichi2.utils.positiveButton
import com.ichi2.utils.title
import net.ankiweb.rsdroid.exceptions.BackendDeckIsFilteredException
import org.json.JSONArray
import org.json.JSONObject
Expand Down Expand Up @@ -100,15 +101,16 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
* Build a context menu for custom study
* @param id the id type of the dialog
*/
private fun buildContextMenu(id: Int): MaterialDialog {
private fun buildContextMenu(id: Int): Dialog {
val listIds = getListIds(ContextMenuConfiguration.fromInt(id)).map { it.value }.toIntArray()
val jumpToReviewer = requireArguments().getBoolean("jumpToReviewer")
val items = getValuesFromKeys(keyValueMap, listIds).toList().toTypedArray()

return MaterialDialog(requireActivity())
val dialog = AlertDialog.Builder(requireActivity())
.title(R.string.custom_study)
.cancelable(true)
.listItems(items = getValuesFromKeys(keyValueMap, listIds).toList().map { it as CharSequence }) { _: MaterialDialog, _: Int, charSequence: CharSequence ->
when (ContextMenuOption.fromString(resources, charSequence.toString())) {
.setItems(items) { _, index ->
when (ContextMenuOption.fromInt(listIds[index])) {
DECK_OPTIONS -> {
// User asked to permanently change the deck options
val deckId = requireArguments().getLong("did")
Expand Down Expand Up @@ -144,14 +146,15 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
// User asked for a standard custom study option
val d = CustomStudyDialog(collection, customStudyListener)
.withArguments(
ContextMenuOption.fromString(resources, charSequence.toString()),
ContextMenuOption.fromInt(listIds[index]),
requireArguments().getLong("did"),
jumpToReviewer
)
customStudyListener?.showDialogFragment(d)
}
}
}
return dialog.create()
}

@KotlinCleanup("make this use enum instead of Int")
Expand All @@ -167,7 +170,7 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
* Build an input dialog that is used to get a parameter related to custom study from the user
* @param contextMenuOption the option of the dialog
*/
private fun buildInputDialog(contextMenuOption: ContextMenuOption): MaterialDialog {
private fun buildInputDialog(contextMenuOption: ContextMenuOption): AlertDialog {
/*
TODO: Try to change to a standard input dialog (currently the thing holding us back is having the extra
TODO: hint line for the number of cards available, and having the pre-filled text selected by default)
Expand All @@ -194,9 +197,9 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
// Whether or not to jump straight to the reviewer
val jumpToReviewer = requireArguments().getBoolean("jumpToReviewer")
// Set material dialog parameters
val dialog = MaterialDialog(requireActivity())
.customView(view = v, scrollable = true, noVerticalPadding = false, horizontalPadding = true)
.positiveButton(R.string.dialog_ok) {
val dialog = AlertDialog.Builder(requireActivity()).apply {
customView(view = v, paddingLeft = 64, paddingRight = 64, paddingTop = 32, paddingBottom = 32)
positiveButton(R.string.dialog_ok) {
// Get the value selected by user
val n: Int = try {
editText.text.toString().toInt()
Expand Down Expand Up @@ -274,19 +277,20 @@ class CustomStudyDialog(private val collection: Collection, private val customSt
MORE_OPTIONS -> TODO("This branch has not been covered before")
}
}
.negativeButton(R.string.dialog_cancel) {
customStudyListener?.dismissAllDialogFragments()
}
.negativeButton(R.string.dialog_cancel) {
customStudyListener?.dismissAllDialogFragments()
}
}.create()
editText.addTextChangedListener(object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun afterTextChanged(editable: Editable) {
try {
editText.text.toString().toInt()
dialog.getActionButton(WhichButton.POSITIVE).isEnabled = true
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = true
} catch (e: Exception) {
Timber.w(e)
dialog.getActionButton(WhichButton.POSITIVE).isEnabled = false
dialog.getButton(AlertDialog.BUTTON_POSITIVE).isEnabled = false
}
}
})
Expand Down

0 comments on commit 80f320c

Please sign in to comment.