From 59c5ffcbd97ebac1f3b121fbf7c8082e6a364a86 Mon Sep 17 00:00:00 2001 From: Brayan Oliveira <69634269+brayandso@users.noreply.github.com> Date: Wed, 13 Dec 2023 20:23:30 -0300 Subject: [PATCH] refactor: move flag related stuff to Flags.kt --- .../main/java/com/ichi2/anki/FlagToDisplay.kt | 4 +- .../src/main/java/com/ichi2/anki/Flags.kt | 54 +++++++++++++++++++ .../src/main/java/com/ichi2/anki/Reviewer.kt | 37 +++++++------ .../com/ichi2/anki/reviewer/CardMarker.kt | 27 ++++------ .../AbstractFlashcardViewerCommandTest.kt | 13 +---- .../java/com/ichi2/anki/FlagToDisplayTest.kt | 8 --- 6 files changed, 85 insertions(+), 58 deletions(-) create mode 100644 AnkiDroid/src/main/java/com/ichi2/anki/Flags.kt diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt b/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt index 597f365b6538..8626489bca24 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt @@ -15,8 +15,6 @@ ****************************************************************************************/ package com.ichi2.anki -import com.ichi2.anki.reviewer.CardMarker - class FlagToDisplay( private val actualFlag: Int, private val isOnAppBar: Boolean, @@ -27,7 +25,7 @@ class FlagToDisplay( return when { !isOnAppBar -> actualFlag isFullscreen -> actualFlag - else -> CardMarker.FLAG_NONE + else -> FLAG_NONE } } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Flags.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Flags.kt new file mode 100644 index 000000000000..f0b79a128eb2 --- /dev/null +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Flags.kt @@ -0,0 +1,54 @@ +/* + * Copyright (c) 2023 Brayan Oliveira + * + * 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 . + */ +package com.ichi2.anki + +import androidx.annotation.IntDef + +const val FLAG_NONE = 0 +const val FLAG_RED = 1 +const val FLAG_ORANGE = 2 +const val FLAG_GREEN = 3 +const val FLAG_BLUE = 4 +const val FLAG_PINK = 5 +const val FLAG_TURQUOISE = 6 +const val FLAG_PURPLE = 7 + +@Retention(AnnotationRetention.SOURCE) +@IntDef( + FLAG_NONE, + FLAG_RED, + FLAG_ORANGE, + FLAG_GREEN, + FLAG_BLUE, + FLAG_PINK, + FLAG_TURQUOISE, + FLAG_PURPLE +) +annotation class Flag + +fun getFlagDrawable(@Flag flag: Int): Int { + return when (flag) { + FLAG_RED -> R.drawable.ic_flag_red + FLAG_ORANGE -> R.drawable.ic_flag_orange + FLAG_GREEN -> R.drawable.ic_flag_green + FLAG_BLUE -> R.drawable.ic_flag_blue + FLAG_PINK -> R.drawable.ic_flag_pink + FLAG_TURQUOISE -> R.drawable.ic_flag_turquoise + FLAG_PURPLE -> R.drawable.ic_flag_purple + FLAG_NONE -> R.drawable.ic_flag_transparent + else -> TODO("Unsupported flag") + } +} diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt index efa13e140a1a..b933b80ebaca 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt @@ -62,7 +62,6 @@ import com.ichi2.anki.preferences.sharedPrefs import com.ichi2.anki.reviewer.* import com.ichi2.anki.reviewer.AnswerButtons.Companion.getBackgroundColors import com.ichi2.anki.reviewer.AnswerButtons.Companion.getTextColors -import com.ichi2.anki.reviewer.CardMarker.FlagDef import com.ichi2.anki.reviewer.FullScreenMode.Companion.fromPreference import com.ichi2.anki.reviewer.FullScreenMode.Companion.isFullScreenReview import com.ichi2.anki.servicelayer.NoteService.isMarked @@ -451,35 +450,35 @@ open class Reviewer : } R.id.action_flag_zero -> { Timber.i("Reviewer:: No flag") - onFlag(currentCard, CardMarker.FLAG_NONE) + onFlag(currentCard, FLAG_NONE) } R.id.action_flag_one -> { Timber.i("Reviewer:: Flag one") - onFlag(currentCard, CardMarker.FLAG_RED) + onFlag(currentCard, FLAG_RED) } R.id.action_flag_two -> { Timber.i("Reviewer:: Flag two") - onFlag(currentCard, CardMarker.FLAG_ORANGE) + onFlag(currentCard, FLAG_ORANGE) } R.id.action_flag_three -> { Timber.i("Reviewer:: Flag three") - onFlag(currentCard, CardMarker.FLAG_GREEN) + onFlag(currentCard, FLAG_GREEN) } R.id.action_flag_four -> { Timber.i("Reviewer:: Flag four") - onFlag(currentCard, CardMarker.FLAG_BLUE) + onFlag(currentCard, FLAG_BLUE) } R.id.action_flag_five -> { Timber.i("Reviewer:: Flag five") - onFlag(currentCard, CardMarker.FLAG_PINK) + onFlag(currentCard, FLAG_PINK) } R.id.action_flag_six -> { Timber.i("Reviewer:: Flag six") - onFlag(currentCard, CardMarker.FLAG_TURQUOISE) + onFlag(currentCard, FLAG_TURQUOISE) } R.id.action_flag_seven -> { Timber.i("Reviewer:: Flag seven") - onFlag(currentCard, CardMarker.FLAG_PURPLE) + onFlag(currentCard, FLAG_PURPLE) } R.id.action_card_info -> { Timber.i("Card Viewer:: Card Info") @@ -1161,35 +1160,35 @@ open class Reviewer : } when (which) { ViewerCommand.TOGGLE_FLAG_RED -> { - toggleFlag(CardMarker.FLAG_RED) + toggleFlag(FLAG_RED) return true } ViewerCommand.TOGGLE_FLAG_ORANGE -> { - toggleFlag(CardMarker.FLAG_ORANGE) + toggleFlag(FLAG_ORANGE) return true } ViewerCommand.TOGGLE_FLAG_GREEN -> { - toggleFlag(CardMarker.FLAG_GREEN) + toggleFlag(FLAG_GREEN) return true } ViewerCommand.TOGGLE_FLAG_BLUE -> { - toggleFlag(CardMarker.FLAG_BLUE) + toggleFlag(FLAG_BLUE) return true } ViewerCommand.TOGGLE_FLAG_PINK -> { - toggleFlag(CardMarker.FLAG_PINK) + toggleFlag(FLAG_PINK) return true } ViewerCommand.TOGGLE_FLAG_TURQUOISE -> { - toggleFlag(CardMarker.FLAG_TURQUOISE) + toggleFlag(FLAG_TURQUOISE) return true } ViewerCommand.TOGGLE_FLAG_PURPLE -> { - toggleFlag(CardMarker.FLAG_PURPLE) + toggleFlag(FLAG_PURPLE) return true } ViewerCommand.UNSET_FLAG -> { - onFlag(currentCard, CardMarker.FLAG_NONE) + onFlag(currentCard, FLAG_NONE) return true } ViewerCommand.MARK -> { @@ -1216,10 +1215,10 @@ open class Reviewer : } } - private fun toggleFlag(@FlagDef flag: Int) { + private fun toggleFlag(@Flag flag: Int) { if (currentCard!!.userFlag() == flag) { Timber.i("Toggle flag: unsetting flag") - onFlag(currentCard, CardMarker.FLAG_NONE) + onFlag(currentCard, FLAG_NONE) } else { Timber.i("Toggle flag: Setting flag to %d", flag) onFlag(currentCard, flag) diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/CardMarker.kt b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/CardMarker.kt index d4cfb4392c92..3261bbd0c3c3 100644 --- a/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/CardMarker.kt +++ b/AnkiDroid/src/main/java/com/ichi2/anki/reviewer/CardMarker.kt @@ -19,15 +19,19 @@ package com.ichi2.anki.reviewer import android.view.View import android.widget.ImageView import androidx.annotation.DrawableRes -import androidx.annotation.IntDef +import com.ichi2.anki.FLAG_BLUE +import com.ichi2.anki.FLAG_GREEN +import com.ichi2.anki.FLAG_NONE +import com.ichi2.anki.FLAG_ORANGE +import com.ichi2.anki.FLAG_PINK +import com.ichi2.anki.FLAG_PURPLE +import com.ichi2.anki.FLAG_RED +import com.ichi2.anki.FLAG_TURQUOISE +import com.ichi2.anki.Flag import com.ichi2.anki.R /** Handles the star and flag marker for the card viewer */ class CardMarker(private val markView: ImageView, private val flagView: ImageView) { - @kotlin.annotation.Retention(AnnotationRetention.SOURCE) - @IntDef(FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE) - annotation class FlagDef - /** Sets the mark icon on a card (the star) */ fun displayMark(markStatus: Boolean) { if (markStatus) { @@ -43,7 +47,7 @@ class CardMarker(private val markView: ImageView, private val flagView: ImageVie get() = markView.visibility == View.VISIBLE /** Sets the flag icon on the card */ - fun displayFlag(@FlagDef flagStatus: Int) { + fun displayFlag(@Flag flagStatus: Int) { when (flagStatus) { FLAG_RED -> setFlagView(R.drawable.ic_flag_red) FLAG_ORANGE -> setFlagView(R.drawable.ic_flag_orange) @@ -62,15 +66,4 @@ class CardMarker(private val markView: ImageView, private val flagView: ImageVie flagView.setImageResource(drawableId) flagView.visibility = View.VISIBLE } - - companion object { - const val FLAG_NONE = 0 - const val FLAG_RED = 1 - const val FLAG_ORANGE = 2 - const val FLAG_GREEN = 3 - const val FLAG_BLUE = 4 - const val FLAG_PINK = 5 - const val FLAG_TURQUOISE = 6 - const val FLAG_PURPLE = 7 - } } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/AbstractFlashcardViewerCommandTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/AbstractFlashcardViewerCommandTest.kt index febb46e01d83..977d25c0e517 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/AbstractFlashcardViewerCommandTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/AbstractFlashcardViewerCommandTest.kt @@ -18,15 +18,6 @@ package com.ichi2.anki import androidx.test.ext.junit.runners.AndroidJUnit4 import com.ichi2.anki.cardviewer.ViewerCommand import com.ichi2.anki.cardviewer.ViewerCommand.* -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_BLUE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_GREEN -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_NONE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_ORANGE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PINK -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PURPLE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_RED -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_TURQUOISE -import com.ichi2.anki.reviewer.CardMarker.FlagDef import com.ichi2.anki.reviewer.ReviewerUi.ControlBlock import com.ichi2.libanki.Card import org.hamcrest.MatcherAssert.assertThat @@ -175,7 +166,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { return CommandTestCardViewer(cardWith(FLAG_NONE)) } - private fun cardWith(@Suppress("SameParameterValue") @FlagDef flag: Int): Card { + private fun cardWith(@Suppress("SameParameterValue") @Flag flag: Int): Card { val c = mock(Card::class.java) val flags = intArrayOf(flag) whenever(c.userFlag()).then { flags[0] } @@ -210,7 +201,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { override val isControlBlocked: Boolean get() = controlBlocked !== ControlBlock.UNBLOCKED - override fun onFlag(card: Card?, @FlagDef flag: Int) { + override fun onFlag(card: Card?, @Flag flag: Int) { lastFlag = flag currentCard!!.setUserFlag(flag) } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt index 7dbf085d4dd5..78280e93e3c7 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt @@ -1,14 +1,6 @@ //noinspection MissingCopyrightHeader #8659 package com.ichi2.anki -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_BLUE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_GREEN -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_NONE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_ORANGE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PINK -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_PURPLE -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_RED -import com.ichi2.anki.reviewer.CardMarker.Companion.FLAG_TURQUOISE import org.junit.jupiter.api.Assertions.assertEquals import org.junit.jupiter.params.ParameterizedTest import org.junit.jupiter.params.provider.ValueSource