Skip to content

Commit

Permalink
refactor(cardviewer): optimize deck access
Browse files Browse the repository at this point in the history
This reduces the number of times getConfigForCurrentCard is called
  • Loading branch information
david-allison committed Nov 16, 2021
1 parent 0bafa3e commit f945b6b
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 16 deletions.
27 changes: 11 additions & 16 deletions AnkiDroid/src/main/java/com/ichi2/anki/AbstractFlashcardViewer.java
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@
import com.ichi2.anki.cardviewer.GestureProcessor;
import com.ichi2.anki.cardviewer.MissingImageHandler;
import com.ichi2.anki.cardviewer.OnRenderProcessGoneDelegate;
import com.ichi2.anki.cardviewer.SoundPlayer;
import com.ichi2.anki.cardviewer.TTS;
import com.ichi2.anki.cardviewer.TypeAnswer;
import com.ichi2.anki.cardviewer.ViewerCommand;
Expand Down Expand Up @@ -121,7 +122,6 @@
import com.ichi2.libanki.Card;
import com.ichi2.libanki.Collection;
import com.ichi2.libanki.Consts;
import com.ichi2.libanki.DeckConfig;
import com.ichi2.libanki.Note;
import com.ichi2.libanki.Sound;
import com.ichi2.libanki.Utils;
Expand Down Expand Up @@ -264,6 +264,7 @@ public abstract class AbstractFlashcardViewer extends NavigationDrawerActivity i

/** set via {@link #setCurrentCard(Card)} */
protected Card mCurrentCard;
private SoundPlayer.CardSoundConfig mCardSoundConfig; // set when mCurrentCard is
private int mCurrentEase;

private int mInitialFlipCardHeight;
Expand Down Expand Up @@ -574,6 +575,11 @@ public void onPostExecute(Computation<?> result) {

protected void setCurrentCard(Card card) {
mCurrentCard = card;
if (card == null) {
mCardSoundConfig = null;
} else {
mCardSoundConfig = SoundPlayer.CardSoundConfig.create(getCol(), card);
}
}


Expand Down Expand Up @@ -1780,7 +1786,7 @@ private void updateCard(final CardHtml content) {
mSoundPlayer.resetSounds();
mAnswerSoundsAdded = false;
mSoundPlayer.addSounds(mBaseUrl, content.getSoundTags(Side.FRONT), SoundSide.QUESTION);
if (mAutomaticAnswer.isEnabled() && !mAnswerSoundsAdded && getConfigForCurrentCard().optBoolean("autoplay", false)) {
if (mAutomaticAnswer.isEnabled() && !mAnswerSoundsAdded && mCardSoundConfig.getAutoplay()) {
addAnswerSounds(() -> content.getSoundTags(Side.BACK));
}
}
Expand Down Expand Up @@ -1810,9 +1816,9 @@ private void updateCard(final CardHtml content) {
* pressing the keyboard shortcut R on the desktop
*/
protected void playSounds(boolean doAudioReplay) {
boolean replayQuestion = getConfigForCurrentCard().optBoolean("replayq", true);
boolean replayQuestion = mCardSoundConfig.getReplayQuestion();

if (getConfigForCurrentCard().optBoolean("autoplay", false) || doAudioReplay) {
if (mCardSoundConfig.getAutoplay() || doAudioReplay) {
// Use TTS if TTS preference enabled and no other sound source
boolean useTTS = mTTS.isEnabled() &&
!(sDisplayAnswer && mSoundPlayer.hasAnswer()) && !(!sDisplayAnswer && mSoundPlayer.hasQuestion());
Expand Down Expand Up @@ -1889,17 +1895,6 @@ protected void showSelectTtsDialogue() {
}
}


/**
* Returns the configuration for the current {@link Card}.
*
* @return The configuration for the current {@link Card}
*/
private DeckConfig getConfigForCurrentCard() {
return getCol().getDecks().confForDid(CardUtils.getDeckIdForCard(mCurrentCard));
}


public void fillFlashcard() {
Timber.d("fillFlashcard()");
Timber.d("base url = %s", mBaseUrl);
Expand All @@ -1918,7 +1913,7 @@ public void fillFlashcard() {

private void loadContentIntoCard(WebView card, String content) {
if (card != null) {
card.getSettings().setMediaPlaybackRequiresUserGesture(!getConfigForCurrentCard().optBoolean("autoplay"));
card.getSettings().setMediaPlaybackRequiresUserGesture(!mCardSoundConfig.getAutoplay());
card.loadDataWithBaseURL(mViewerUrl, content, "text/html", "utf-8", null);
}
}
Expand Down
61 changes: 61 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/SoundPlayer.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
/*
* Copyright (c) 2021 David Allison <[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.cardviewer

import com.ichi2.anki.CardUtils
import com.ichi2.libanki.Card
import com.ichi2.libanki.Collection
import com.ichi2.libanki.SoundOrVideoTag
import com.ichi2.libanki.TTSTag

/**
* Work in Progress
*
* Handles the two ways an Anki card defines sound:
* * Regular Sound (file-based, mp3 etc..): [SoundOrVideoTag]
* * Text to Speech [TTSTag]
*
* https://docs.ankiweb.net/templates/fields.html?highlight=tts#text-to-speech
* No manual reference for [sound:], but this handles Sound or Video with a reference to the file
* in the media folder.
*
* AnkiDroid also introduced a "tts" setting, which existed before Anki Desktop TTS.
* This only allowed TTS if a setting was enabled,
*
* This class combines the above concerns behind an "adapter" interface in order to simplify complexity.
*
* I hope that we can then test and reduce the complexity of this class.
*/
class SoundPlayer {

/** The options for playing sound for a given card */
class CardSoundConfig(val replayQuestion: Boolean, val autoplay: Boolean) {

companion object {
@JvmStatic
fun create(col: Collection, card: Card): CardSoundConfig {
val deckConfig = col.decks.confForDid(CardUtils.getDeckIdForCard(card))

val autoPlay = deckConfig.optBoolean("autoplay", false)

val replayQuestion: Boolean = deckConfig.optBoolean("replayq", true)

return CardSoundConfig(replayQuestion, autoPlay)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,15 @@ private static class CommandTestCardViewer extends Reviewer {
public CommandTestCardViewer(Card currentCard) {
setCurrentCard(currentCard);
}


@Override
protected void setCurrentCard(Card card) {
this.mCurrentCard = card;
// we don't have getCol() here and we don't need the additional sound processing.
}


@Override
protected void setTitle() {
//Intentionally blank
Expand Down

0 comments on commit f945b6b

Please sign in to comment.