forked from ankidroid/Anki-Android
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
the style and features are going to be implemented incrementally
- Loading branch information
Showing
7 changed files
with
446 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
137 changes: 137 additions & 0 deletions
137
AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerFragment.kt
This file contains 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,137 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* 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) | ||
} | ||
} | ||
} |
119 changes: 119 additions & 0 deletions
119
AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerViewModel.kt
This file contains 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,119 @@ | ||
/* | ||
* Copyright (c) 2024 Brayan Oliveira <[email protected]> | ||
* | ||
* 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) | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.