Skip to content

Commit

Permalink
feat(new reviewer): type answer (native)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 22, 2024
1 parent a76cfe7 commit a9ce7c7
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,16 @@ import android.text.style.UnderlineSpan
import android.view.Menu
import android.view.MenuItem
import android.view.View
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.webkit.WebView
import android.widget.FrameLayout
import androidx.activity.result.contract.ActivityResultContracts
import androidx.annotation.StringRes
import androidx.appcompat.view.menu.SubMenuBuilder
import androidx.appcompat.widget.ActionMenuView
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.getSystemService
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.core.view.WindowInsetsControllerCompat
Expand All @@ -40,6 +43,8 @@ import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.google.android.material.card.MaterialCardView
import com.google.android.material.textfield.TextInputEditText
import com.google.android.material.textview.MaterialTextView
import com.ichi2.anki.AbstractFlashcardViewer.Companion.RESULT_NO_MORE_CARDS
import com.ichi2.anki.CollectionManager
Expand Down Expand Up @@ -92,6 +97,7 @@ import com.ichi2.anki.utils.ext.collectLatestIn
import com.ichi2.anki.utils.ext.menu
import com.ichi2.anki.utils.ext.removeSubMenu
import com.ichi2.anki.utils.ext.sharedPrefs
import com.ichi2.anki.utils.ext.window
import com.ichi2.libanki.sched.Counts
import kotlinx.coroutines.launch

Expand All @@ -107,7 +113,13 @@ class ReviewerFragment :
get() = requireView().findViewById(R.id.webview)

override val baseSnackbarBuilder: SnackbarBuilder = {
anchorView = this@ReviewerFragment.view?.findViewById(R.id.buttons_area)
val typeAnswerContainer = this@ReviewerFragment.view?.findViewById<View>(R.id.type_answer_container)
anchorView =
if (typeAnswerContainer?.isVisible == true) {
typeAnswerContainer
} else {
this@ReviewerFragment.view?.findViewById(R.id.buttons_area)
}
}

override fun onStop() {
Expand Down Expand Up @@ -236,10 +248,48 @@ class ReviewerFragment :
easyButton.setAnswerButtonNextTime(R.string.ease_button_easy, times?.easy)
}

// TODO keep text after configuration changes
val typeAnswerContainer = view.findViewById<MaterialCardView>(R.id.type_answer_container)
val typeAnswerEditText = view.findViewById<TextInputEditText>(R.id.type_answer_edit_text)
typeAnswerEditText.setOnEditorActionListener { editTextView, actionId, _ ->
if (actionId == EditorInfo.IME_ACTION_DONE) {
viewModel.onShowAnswer(editTextView.text.toString())
return@setOnEditorActionListener true
}
false
}
typeAnswerEditText.setOnFocusChangeListener { editTextView, hasFocus ->
val insetsController = WindowInsetsControllerCompat(window, editTextView)
if (hasFocus) {
insetsController.show(WindowInsetsCompat.Type.ime())
} else {
insetsController.hide(WindowInsetsCompat.Type.ime())
}
}
val autoFocusTypeAnswer = sharedPrefs().getBoolean(getString(R.string.type_in_answer_focus_key), true)
viewModel.typeAnswerFlow.collectIn(lifecycleScope) { typeInAnswer ->
typeAnswerEditText.text = null
if (typeInAnswer == null) {
typeAnswerContainer.isVisible = false
return@collectIn
}
typeAnswerContainer.isVisible = true
typeAnswerEditText.apply {
if (imeHintLocales != typeInAnswer.imeHintLocales) {
imeHintLocales = typeInAnswer.imeHintLocales
context?.getSystemService<InputMethodManager>()?.restartInput(this)
}
if (autoFocusTypeAnswer) {
requestFocus()
}
}
}

val showAnswerButton =
view.findViewById<MaterialButton>(R.id.show_answer).apply {
setOnClickListener {
viewModel.onShowAnswer()
val typedAnswer = typeAnswerEditText?.text?.toString()
viewModel.onShowAnswer(typedAnswer = typedAnswer)
}
}
val answerButtonsLayout = view.findViewById<ConstraintLayout>(R.id.answer_buttons)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import com.ichi2.anki.pages.CardInfoDestination
import com.ichi2.anki.pages.DeckOptionsDestination
import com.ichi2.anki.preferences.getShowIntervalOnButtons
import com.ichi2.anki.previewer.CardViewerViewModel
import com.ichi2.anki.previewer.TypeAnswer
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.anki.servicelayer.NoteService
Expand Down Expand Up @@ -78,6 +79,7 @@ class ReviewerViewModel(
val undoLabelFlow = MutableStateFlow<String?>(null)
val redoLabelFlow = MutableStateFlow<String?>(null)
val countsFlow = MutableStateFlow(Counts() to Counts.Queue.NEW)
val typeAnswerFlow = MutableStateFlow<TypeAnswer?>(null)

override val server = AnkiServer(this).also { it.start() }
private val stateMutationKey = TimeManager.time.intTimeMS().toString()
Expand Down Expand Up @@ -141,13 +143,13 @@ class ReviewerViewModel(
}
}

fun onShowAnswer() {
fun onShowAnswer(typedAnswer: String? = null) {
launchCatchingIO {
while (!statesMutated) {
delay(50)
}
updateNextTimes()
showAnswer()
showAnswer(typedAnswer)
loadAndPlaySounds(CardSide.ANSWER)
if (!autoAdvance.shouldWaitForAudio()) {
autoAdvance.onShowAnswer()
Expand Down Expand Up @@ -417,11 +419,19 @@ class ReviewerViewModel(
countsFlow.emit(state.counts to state.countsIndex)
}

// TODO
override suspend fun typeAnsFilter(
text: String,
typedAnswer: String?,
): String = text
): String {
val typeAnswer = TypeAnswer.getInstance(currentCard.await(), text)
return if (showingAnswer.value) {
typeAnswerFlow.emit(null)
typeAnswer?.answerFilter(typedAnswer ?: "") ?: text
} else {
typeAnswerFlow.emit(typeAnswer)
TypeAnswer.removeTypeAnswerTags(text)
}
}

private suspend fun updateUndoAndRedoLabels() {
undoLabelFlow.emit(withCol { undoLabel() })
Expand Down
40 changes: 38 additions & 2 deletions AnkiDroid/src/main/res/layout/reviewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
android:layout_marginHorizontal="8dp"
android:layout_marginBottom="4dp"
app:layout_constraintTop_toBottomOf="@id/appbar"
app:layout_constraintBottom_toTopOf="@id/buttons_area"
app:layout_constraintBottom_toTopOf="@id/type_answer_container"
style="@style/CardView.ViewerStyle">

<WebView
Expand All @@ -85,6 +85,42 @@

</com.google.android.material.card.MaterialCardView>

<com.google.android.material.card.MaterialCardView
android:id="@+id/type_answer_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="@id/webview_container"
app:layout_constraintEnd_toEndOf="@id/webview_container"
app:layout_constraintTop_toBottomOf="@id/webview_container"
app:layout_constraintBottom_toTopOf="@id/buttons_area"
style="@style/CardView.ViewerStyle"
android:layout_marginTop="4dp"
android:layout_marginBottom="8dp"
android:visibility="gone"
tools:visibility="visible">

<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="@string/type_answer_hint"
app:endIconMode="clear_text"
app:boxBackgroundMode="filled"
app:boxStrokeWidth="0dp"
>

<com.google.android.material.textfield.TextInputEditText
android:id="@+id/type_answer_edit_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:imeOptions="actionDone"
android:inputType="text|textNoSuggestions"
/>
</com.google.android.material.textfield.TextInputLayout>

</com.google.android.material.card.MaterialCardView>



<FrameLayout
android:id="@+id/buttons_area"
android:layout_width="0dp"
Expand All @@ -93,7 +129,7 @@
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="@id/webview_container"
app:layout_constraintStart_toStartOf="@id/webview_container"
app:layout_constraintTop_toBottomOf="@id/webview_container"
app:layout_constraintTop_toBottomOf="@id/type_answer_container"
>

<com.google.android.material.button.MaterialButton
Expand Down

0 comments on commit a9ce7c7

Please sign in to comment.