diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Flag.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Flag.kt new file mode 100644 index 000000000000..5f6a5c16a115 --- /dev/null +++ b/AnkiDroid/src/main/java/com/ichi2/anki/Flag.kt @@ -0,0 +1,35 @@ +/* + * 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.DrawableRes + +enum class Flag(val code: Int, @DrawableRes val drawableRes: Int) { + NONE(0, R.drawable.ic_flag_transparent), + RED(1, R.drawable.ic_flag_red), + ORANGE(2, R.drawable.ic_flag_orange), + GREEN(3, R.drawable.ic_flag_green), + BLUE(4, R.drawable.ic_flag_blue), + PINK(5, R.drawable.ic_flag_pink), + TURQUOISE(6, R.drawable.ic_flag_turquoise), + PURPLE(7, R.drawable.ic_flag_purple); + + companion object { + fun fromCode(code: Int): Flag { + return entries.first { it.code == code } + } + } +} diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt b/AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt index 597f365b6538..1ad326dcff5c 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.code } } } diff --git a/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt b/AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt index efa13e140a1a..ef6c0da86238 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 @@ -246,14 +245,14 @@ open class Reviewer : mCardMarker!!.displayMark(shouldDisplayMark()) } - protected open fun onFlag(card: Card?, flag: Int) { + protected open fun onFlag(card: Card?, flag: Flag) { if (card == null) { return } launchCatchingTask { - card.setUserFlag(flag) + card.setUserFlag(flag.code) withCol { - setUserFlagForCards(listOf(card.id), flag) + setUserFlagForCards(listOf(card.id), flag.code) } refreshActionBar() onFlagChanged() @@ -264,7 +263,7 @@ open class Reviewer : if (currentCard == null) { return } - mCardMarker!!.displayFlag(flagToDisplay) + mCardMarker!!.displayFlag(Flag.fromCode(flagToDisplay)) } private fun selectDeckFromExtra() { @@ -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) { - if (currentCard!!.userFlag() == flag) { + private fun toggleFlag(flag: Flag) { + if (currentCard!!.userFlag() == flag.code) { 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..5009a81040b5 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,11 @@ 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 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,17 +39,12 @@ 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) { - when (flagStatus) { - FLAG_RED -> setFlagView(R.drawable.ic_flag_red) - FLAG_ORANGE -> setFlagView(R.drawable.ic_flag_orange) - FLAG_GREEN -> setFlagView(R.drawable.ic_flag_green) - FLAG_BLUE -> setFlagView(R.drawable.ic_flag_blue) - FLAG_PINK -> setFlagView(R.drawable.ic_flag_pink) - FLAG_TURQUOISE -> setFlagView(R.drawable.ic_flag_turquoise) - FLAG_PURPLE -> setFlagView(R.drawable.ic_flag_purple) - FLAG_NONE -> flagView.visibility = View.INVISIBLE - else -> flagView.visibility = View.INVISIBLE + fun displayFlag(flag: Flag) { + when (flag) { + Flag.RED, Flag.BLUE, Flag.GREEN, Flag.ORANGE, Flag.PINK, Flag.PURPLE, Flag.TURQUOISE -> { + setFlagView(flag.drawableRes) + } + Flag.NONE -> flagView.visibility = View.INVISIBLE } } @@ -62,15 +53,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..9f9952492ffc 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 @@ -46,7 +37,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_RED) viewer.executeCommand(TOGGLE_FLAG_RED) - assertThat(viewer.lastFlag, equalTo(FLAG_NONE)) + assertThat(viewer.lastFlag, equalTo(Flag.NONE.code)) } @Test @@ -55,7 +46,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(UNSET_FLAG) - assertThat(viewer.lastFlag, equalTo(FLAG_NONE)) + assertThat(viewer.lastFlag, equalTo(Flag.NONE.code)) } @Test @@ -65,7 +56,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(UNSET_FLAG) viewer.executeCommand(UNSET_FLAG) - assertThat(viewer.lastFlag, equalTo(FLAG_NONE)) + assertThat(viewer.lastFlag, equalTo(Flag.NONE.code)) } @Test @@ -75,7 +66,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_RED) viewer.executeCommand(TOGGLE_FLAG_BLUE) - assertThat(viewer.lastFlag, equalTo(FLAG_BLUE)) + assertThat(viewer.lastFlag, equalTo(Flag.BLUE.code)) } @Test @@ -85,7 +76,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_RED) viewer.executeCommand(UNSET_FLAG) - assertThat(viewer.lastFlag, equalTo(FLAG_NONE)) + assertThat(viewer.lastFlag, equalTo(Flag.NONE.code)) } @Test @@ -94,7 +85,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_RED) - assertThat(viewer.lastFlag, equalTo(FLAG_RED)) + assertThat(viewer.lastFlag, equalTo(Flag.RED.code)) } @Test @@ -103,7 +94,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_ORANGE) - assertThat(viewer.lastFlag, equalTo(FLAG_ORANGE)) + assertThat(viewer.lastFlag, equalTo(Flag.ORANGE.code)) } @Test @@ -112,7 +103,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_GREEN) - assertThat(viewer.lastFlag, equalTo(FLAG_GREEN)) + assertThat(viewer.lastFlag, equalTo(Flag.GREEN.code)) } @Test @@ -121,7 +112,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_BLUE) - assertThat(viewer.lastFlag, equalTo(FLAG_BLUE)) + assertThat(viewer.lastFlag, equalTo(Flag.BLUE.code)) } @Test @@ -130,7 +121,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_PINK) - assertThat(viewer.lastFlag, equalTo(FLAG_PINK)) + assertThat(viewer.lastFlag, equalTo(Flag.PINK.code)) } @Test @@ -139,7 +130,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_TURQUOISE) - assertThat(viewer.lastFlag, equalTo(FLAG_TURQUOISE)) + assertThat(viewer.lastFlag, equalTo(Flag.TURQUOISE.code)) } @Test @@ -148,7 +139,7 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(TOGGLE_FLAG_PURPLE) - assertThat(viewer.lastFlag, equalTo(FLAG_PURPLE)) + assertThat(viewer.lastFlag, equalTo(Flag.PURPLE.code)) } @Test @@ -168,14 +159,14 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { viewer.executeCommand(command) viewer.executeCommand(command) - assertThat(command.toString(), viewer.lastFlag, equalTo(FLAG_NONE)) + assertThat(command.toString(), viewer.lastFlag, equalTo(Flag.NONE.code)) } private fun createViewer(): CommandTestCardViewer { - return CommandTestCardViewer(cardWith(FLAG_NONE)) + return CommandTestCardViewer(cardWith(Flag.NONE.code)) } - private fun cardWith(@Suppress("SameParameterValue") @FlagDef flag: Int): Card { + private fun cardWith(@Suppress("SameParameterValue") flag: Int): Card { val c = mock(Card::class.java) val flags = intArrayOf(flag) whenever(c.userFlag()).then { flags[0] } @@ -210,9 +201,9 @@ class AbstractFlashcardViewerCommandTest : RobolectricTest() { override val isControlBlocked: Boolean get() = controlBlocked !== ControlBlock.UNBLOCKED - override fun onFlag(card: Card?, @FlagDef flag: Int) { - lastFlag = flag - currentCard!!.setUserFlag(flag) + override fun onFlag(card: Card?, flag: Flag) { + lastFlag = flag.code + currentCard!!.setUserFlag(flag.code) } } } diff --git a/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt b/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt index 7dbf085d4dd5..12b87a101651 100644 --- a/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt +++ b/AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt @@ -1,41 +1,33 @@ //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 +import org.junit.jupiter.params.provider.EnumSource class FlagToDisplayTest { @ParameterizedTest - @ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE]) - fun `is hidden if flag is on app bar and fullscreen is disabled`(actualFlag: Int) { - assertEquals(FLAG_NONE, FlagToDisplay(actualFlag, isOnAppBar = true, isFullscreen = false).get()) + @EnumSource(value = Flag::class) + fun `is hidden if flag is on app bar and fullscreen is disabled`(actualFlag: Flag) { + assertEquals(Flag.NONE.code, FlagToDisplay(actualFlag.code, isOnAppBar = true, isFullscreen = false).get()) } @ParameterizedTest - @ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE]) - fun `is not hidden if flag is not on app bar and fullscreen is disabled`(actualFlag: Int) { - assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = false, isFullscreen = false).get()) + @EnumSource(value = Flag::class) + fun `is not hidden if flag is not on app bar and fullscreen is disabled`(actualFlag: Flag) { + assertEquals(actualFlag.code, FlagToDisplay(actualFlag.code, isOnAppBar = false, isFullscreen = false).get()) } @ParameterizedTest - @ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE]) - fun `is not hidden if flag is not on app bar and fullscreen is enabled`(actualFlag: Int) { - assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = false, isFullscreen = true).get()) + @EnumSource(value = Flag::class) + fun `is not hidden if flag is not on app bar and fullscreen is enabled`(actualFlag: Flag) { + assertEquals(actualFlag.code, FlagToDisplay(actualFlag.code, isOnAppBar = false, isFullscreen = true).get()) } @ParameterizedTest - @ValueSource(ints = [FLAG_NONE, FLAG_RED, FLAG_ORANGE, FLAG_GREEN, FLAG_BLUE, FLAG_PINK, FLAG_TURQUOISE, FLAG_PURPLE]) - fun `is not hidden if flag is on app bar and fullscreen is enabled`(actualFlag: Int) { - assertEquals(actualFlag, FlagToDisplay(actualFlag, isOnAppBar = true, isFullscreen = true).get()) + @EnumSource(value = Flag::class) + fun `is not hidden if flag is on app bar and fullscreen is enabled`(actualFlag: Flag) { + assertEquals(actualFlag.code, FlagToDisplay(actualFlag.code, isOnAppBar = true, isFullscreen = true).get()) } }