Skip to content

Commit

Permalink
Additional refactors and design changes
Browse files Browse the repository at this point in the history
Removed AbstractFlashcardViewer.readCardText and inlined TTS.readCardText.

Extracted AbstractFlashcardViewer.getDeckIdForCard to CardUtils.

Extracted AbstractCardViewer.getTextForTts to TTS.

Comment documentation on TTS.getOrdUsingCardtype.

Improved readability of AbstractFlashcardViewer.showSelectTtsDiagloue.
  • Loading branch information
Tyler-Programs committed Oct 13, 2021
1 parent fabdfaf commit 69b77aa
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2175,10 +2175,10 @@ protected void playSounds(boolean doAudioReplay) {
// If the question is displayed or if the question should be replayed, read the question
if (mTtsInitialized) {
if (!sDisplayAnswer || doAudioReplay && replayQuestion) {
readCardText(mCurrentCard, SoundSide.QUESTION);
mTTS.readCardText(this, mCurrentCard, SoundSide.QUESTION);
}
if (sDisplayAnswer) {
readCardText(mCurrentCard, SoundSide.ANSWER);
mTTS.readCardText(this, mCurrentCard, SoundSide.ANSWER);
}
} else {
mReplayOnTtsInit = true;
Expand Down Expand Up @@ -2209,57 +2209,23 @@ private Sound.OnErrorListener getSoundErrorListener() {
}


/**
* Reads the text (using TTS) for the given side of a card.
*
* @param card The card to play TTS for
* @param cardSide The side of the current card to play TTS for
*/
private void readCardText(final Card card, final SoundSide cardSide) {
mTTS.readCardText(this, card, cardSide);
}

/**
* Shows the dialogue for selecting TTS for the current card and cardside.
*/
protected void showSelectTtsDialogue() {
if (mTtsInitialized) {
if (!sDisplayAnswer) {
mTTS.selectTts(this, mCurrentCard, SoundSide.QUESTION);
} else {
mTTS.selectTts(this, mCurrentCard, SoundSide.ANSWER);
}
mTTS.selectTts(this, mCurrentCard, sDisplayAnswer ? SoundSide.ANSWER : SoundSide.QUESTION);
}
}


public static String getTextForTts(Context context, String text) {
String clozeReplacement = context.getString(R.string.reviewer_tts_cloze_spoken_replacement);
String clozeReplaced = text.replace(TemplateFilters.CLOZE_DELETION_REPLACEMENT, clozeReplacement);
return Utils.stripHTML(clozeReplaced);
}


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


/**
* Returns the deck ID of the given {@link Card}.
*
* @param card The {@link Card} to get the deck ID
* @return The deck ID of the {@link Card}
*/
public static long getDeckIdForCard(final Card card) {
// Try to get the configuration by the original deck ID (available in case of a cram deck),
// else use the direct deck ID (in case of a 'normal' deck.
return card.getODid() == 0 ? card.getDid() : card.getODid();
return getCol().getDecks().confForDid(CardUtils.getDeckIdForCard(mCurrentCard));
}


Expand Down
12 changes: 12 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/CardUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,16 @@ public static boolean isIn(long[] array, long val) {
}
return false;
}

/**
* Returns the deck ID of the given {@link Card}.
*
* @param card The {@link Card} to get the deck ID
* @return The deck ID of the {@link Card}
*/
public static long getDeckIdForCard(final Card card) {
// Try to get the configuration by the original deck ID (available in case of a cram deck),
// else use the direct deck ID (in case of a 'normal' deck.
return card.getODid() == 0 ? card.getDid() : card.getODid();
}
}
19 changes: 17 additions & 2 deletions AnkiDroid/src/main/java/com/ichi2/anki/cardviewer/TTS.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,14 @@

import com.afollestad.materialdialogs.MaterialDialog;
import com.ichi2.anki.AbstractFlashcardViewer;
import com.ichi2.anki.CardUtils;
import com.ichi2.anki.MetaDB;
import com.ichi2.anki.R;
import com.ichi2.anki.ReadText;
import com.ichi2.libanki.Card;
import com.ichi2.libanki.Sound;
import com.ichi2.libanki.Utils;
import com.ichi2.libanki.template.TemplateFilters;

import java.util.ArrayList;

Expand All @@ -22,6 +25,12 @@ public class TTS {
private ReadText mTTS;

/**
* Returns the card ordinal for TTS language storage.
*
* The ordinal of a Cloze card denotes the cloze deletion, causing the TTS
* language to be requested and stored on every new highest cloze deletion when
* used normally.
*
* @param card The card to check the type of before determining the ordinal.
* @return The card ordinal. If it's a Cloze card, returns 0.
*/
Expand Down Expand Up @@ -50,7 +59,7 @@ public void readCardText(Context context, final Card card, final Sound.SoundSide
return;
}
String clozeReplacement = context.getString(R.string.reviewer_tts_cloze_spoken_replacement);
mTTS.readCardSide(cardSide, cardSideContent, AbstractFlashcardViewer.getDeckIdForCard(card), getOrdUsingCardType(card), clozeReplacement);
mTTS.readCardSide(cardSide, cardSideContent, CardUtils.getDeckIdForCard(card), getOrdUsingCardType(card), clozeReplacement);
}

/**
Expand All @@ -62,6 +71,12 @@ public void readCardText(Context context, final Card card, final Sound.SoundSide
public void selectTts(Context context, Card card, Sound.SoundSide qa) {
String mTextToRead = qa == Sound.SoundSide.QUESTION ? card.q(true) : card.getPureAnswer();
// get the text from the card
mTTS.selectTts(AbstractFlashcardViewer.getTextForTts(context, mTextToRead), AbstractFlashcardViewer.getDeckIdForCard(card), getOrdUsingCardType(card), qa);
mTTS.selectTts(getTextForTts(context, mTextToRead), CardUtils.getDeckIdForCard(card), getOrdUsingCardType(card), qa);
}

public String getTextForTts(Context context, String text) {
String clozeReplacement = context.getString(R.string.reviewer_tts_cloze_spoken_replacement);
String clozeReplaced = text.replace(TemplateFilters.CLOZE_DELETION_REPLACEMENT, clozeReplacement);
return Utils.stripHTML(clozeReplaced);
}
}

0 comments on commit 69b77aa

Please sign in to comment.