Skip to content

Commit

Permalink
feat: start Reviewer port
Browse files Browse the repository at this point in the history
the style and features are going to be implemented incrementally
  • Loading branch information
BrayanDSO committed May 22, 2024
1 parent 9e1ddbe commit d1bc2b9
Show file tree
Hide file tree
Showing 7 changed files with 447 additions and 2 deletions.
9 changes: 7 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.ui.dialogs.storageMigrationFailedDialogIsShownOrPending
import com.ichi2.anki.ui.windows.reviewer.ReviewerFragment
import com.ichi2.anki.utils.SECONDS_PER_DAY
import com.ichi2.anki.widgets.DeckAdapter
import com.ichi2.anki.worker.SyncMediaWorker
Expand Down Expand Up @@ -2305,8 +2306,12 @@ open class DeckPicker :
}

private fun openReviewer() {
val reviewer = Intent(this, Reviewer::class.java)
reviewLauncher.launch(reviewer)
val intent = if (sharedPrefs().getBoolean("newReviewer", true)) {
ReviewerFragment.getIntent(this)
} else {
Intent(this, Reviewer::class.java)
}
reviewLauncher.launch(intent)
}

override fun onCreateCustomStudySession() {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
/*
* Copyright (c) 2024 Brayan Oliveira <brayandso.dev@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.ui.windows.reviewer

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.view.MenuItem
import android.view.View
import android.webkit.WebView
import androidx.appcompat.view.menu.MenuBuilder
import androidx.appcompat.widget.ThemeUtils
import androidx.appcompat.widget.Toolbar
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.view.isVisible
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import com.google.android.material.appbar.MaterialToolbar
import com.google.android.material.button.MaterialButton
import com.ichi2.anki.AbstractFlashcardViewer.Companion.RESULT_NO_MORE_CARDS
import com.ichi2.anki.R
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.previewer.CardViewerActivity
import com.ichi2.anki.previewer.CardViewerFragment
import com.ichi2.anki.snackbar.BaseSnackbarBuilderProvider
import com.ichi2.anki.snackbar.SnackbarBuilder
import com.ichi2.anki.snackbar.showSnackbar
import com.ichi2.anki.utils.ext.collectIn
import com.ichi2.anki.utils.ext.collectLatestIn
import com.ichi2.anki.utils.navBarNeedsScrim
import com.ichi2.utils.increaseHorizontalPaddingOfOverflowMenuIcons

class ReviewerFragment :
CardViewerFragment(R.layout.reviewer2),
BaseSnackbarBuilderProvider,
Toolbar.OnMenuItemClickListener {

override val viewModel: ReviewerViewModel by viewModels {
ReviewerViewModel.factory(CardMediaPlayer())
}

override val webView: WebView
get() = requireView().findViewById(R.id.webview)

override val baseSnackbarBuilder: SnackbarBuilder = {
anchorView = this@ReviewerFragment.view?.findViewById(R.id.buttons_area)
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)

setupAnswerButtons(view)

view.findViewById<MaterialToolbar>(R.id.toolbar).apply {
setOnMenuItemClickListener(this@ReviewerFragment)
setNavigationOnClickListener { requireActivity().onBackPressedDispatcher.onBackPressed() }
(menu as? MenuBuilder)?.let {
it.setOptionalIconsVisible(true)
requireContext().increaseHorizontalPaddingOfOverflowMenuIcons(it)
}
}

with(requireActivity()) {
if (!navBarNeedsScrim) {
window.navigationBarColor =
ThemeUtils.getThemeAttrColor(this, R.attr.alternativeBackgroundColor)
}
}

viewModel.isQueueFinishedFlow.collectIn(lifecycleScope) { isQueueFinished ->
if (isQueueFinished) {
requireActivity().run {
setResult(RESULT_NO_MORE_CARDS)
finish()
}
}
}
}

// TODO
override fun onMenuItemClick(item: MenuItem?): Boolean {
showSnackbar("Not implemented yet")
return true
}

private fun setupAnswerButtons(view: View) {
view.findViewById<MaterialButton>(R.id.again_button).setOnClickListener {
viewModel.answerAgain()
}
view.findViewById<MaterialButton>(R.id.hard_button).setOnClickListener {
viewModel.answerHard()
}
view.findViewById<MaterialButton>(R.id.good_button).setOnClickListener {
viewModel.answerGood()
}
view.findViewById<MaterialButton>(R.id.easy_button).setOnClickListener {
viewModel.answerEasy()
}

val showAnswerButton = view.findViewById<MaterialButton>(R.id.show_answer).apply {
setOnClickListener {
viewModel.showAnswer()
}
}
val answerButtonsLayout = view.findViewById<ConstraintLayout>(R.id.answer_buttons)

// TODO add some kind of feedback/animation after tapping show answer or the answer buttons
viewModel.showingAnswer.collectLatestIn(lifecycleScope) { shouldShowAnswer ->
if (shouldShowAnswer) {
showAnswerButton.isVisible = false
answerButtonsLayout.isVisible = true
} else {
showAnswerButton.isVisible = true
answerButtonsLayout.isVisible = false
}
}
}

companion object {
fun getIntent(context: Context): Intent {
return CardViewerActivity.getIntent(context, ReviewerFragment::class)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
/*
* Copyright (c) 2024 Brayan Oliveira <brayandso.dev@gmail.com>
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU General Public License as published by the Free Software
* Foundation; either version 3 of the License, or (at your option) any later
* version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with
* this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.ichi2.anki.ui.windows.reviewer

import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewmodel.initializer
import androidx.lifecycle.viewmodel.viewModelFactory
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.Ease
import com.ichi2.anki.asyncIO
import com.ichi2.anki.cardviewer.CardMediaPlayer
import com.ichi2.anki.launchCatchingIO
import com.ichi2.anki.previewer.CardViewerViewModel
import com.ichi2.anki.reviewer.CardSide
import com.ichi2.libanki.sched.CurrentQueueState
import com.ichi2.libanki.undoableOp
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.Deferred
import kotlinx.coroutines.flow.MutableSharedFlow

class ReviewerViewModel(cardMediaPlayer: CardMediaPlayer) : CardViewerViewModel(cardMediaPlayer) {

private var queueState: Deferred<CurrentQueueState?> = asyncIO {
withCol { sched.currentQueueState() }
}
override var currentCard = asyncIO {
// this assumes that the Reviewer won't be launched if there isn't a queueState
queueState.await()!!.topCard
}
var isQueueFinishedFlow = MutableSharedFlow<Boolean>()

/* *********************************************************************************************
************************ Public methods: meant to be used by the View **************************
********************************************************************************************* */

override fun onPageFinished(isAfterRecreation: Boolean) {
if (isAfterRecreation) {
launchCatchingIO {
// TODO handle "Don't keep activities"
if (showingAnswer.value) showAnswerInternal() else showQuestion()
}
} else {
launchCatchingIO {
updateCurrentCard()
}
}
}

fun showAnswer() {
launchCatchingIO {
showAnswerInternal()
loadAndPlaySounds(CardSide.ANSWER)
}
}

fun answerAgain() = answerCard(Ease.AGAIN)
fun answerHard() = answerCard(Ease.HARD)
fun answerGood() = answerCard(Ease.GOOD)
fun answerEasy() = answerCard(Ease.EASY)

/* *********************************************************************************************
*************************************** Internal methods ***************************************
********************************************************************************************* */

private fun answerCard(ease: Ease) {
launchCatchingIO {
queueState.await()?.let {
undoableOp { sched.answerCard(it, ease.value) }
updateCurrentCard()
}
}
}

private suspend fun loadAndPlaySounds(side: CardSide) {
cardMediaPlayer.loadCardSounds(currentCard.await())
cardMediaPlayer.playAllSoundsForSide(side)
}

private suspend fun updateCurrentCard() {
queueState = asyncIO {
withCol {
sched.currentQueueState()
}
}
queueState.await()?.let {
currentCard = CompletableDeferred(it.topCard)
showQuestion()
loadAndPlaySounds(CardSide.QUESTION)
} ?: isQueueFinishedFlow.emit(true)
}

// TODO
override suspend fun typeAnsFilter(text: String): String {
return text
}

companion object {
fun factory(soundPlayer: CardMediaPlayer): ViewModelProvider.Factory {
return viewModelFactory {
initializer {
ReviewerViewModel(soundPlayer)
}
}
}
}
}

0 comments on commit d1bc2b9

Please sign in to comment.