Skip to content

Commit

Permalink
refactor: move flag related stuff to Flags.kt
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 14, 2023
1 parent b4db9f8 commit 59c5ffc
Show file tree
Hide file tree
Showing 6 changed files with 85 additions and 58 deletions.
4 changes: 1 addition & 3 deletions AnkiDroid/src/main/java/com/ichi2/anki/FlagToDisplay.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@
****************************************************************************************/
package com.ichi2.anki

import com.ichi2.anki.reviewer.CardMarker

class FlagToDisplay(
private val actualFlag: Int,
private val isOnAppBar: Boolean,
Expand All @@ -27,7 +25,7 @@ class FlagToDisplay(
return when {
!isOnAppBar -> actualFlag
isFullscreen -> actualFlag
else -> CardMarker.FLAG_NONE
else -> FLAG_NONE
}
}
}
54 changes: 54 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/Flags.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
/*
* Copyright (c) 2023 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

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")
}
}
37 changes: 18 additions & 19 deletions AnkiDroid/src/main/java/com/ichi2/anki/Reviewer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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 -> {
Expand All @@ -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)
Expand Down
27 changes: 10 additions & 17 deletions AnkiDroid/src/main/java/com/ichi2/anki/reviewer/CardMarker.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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)
Expand All @@ -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
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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] }
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 0 additions & 8 deletions AnkiDroid/src/test/java/com/ichi2/anki/FlagToDisplayTest.kt
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 59c5ffc

Please sign in to comment.