Skip to content

Commit

Permalink
refactor: add a Flag Enum
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 14, 2023
1 parent b4db9f8 commit 1de854f
Show file tree
Hide file tree
Showing 6 changed files with 97 additions and 102 deletions.
35 changes: 35 additions & 0 deletions AnkiDroid/src/main/java/com/ichi2/anki/Flag.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* 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.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 }
}
}
}
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.code
}
}
}
47 changes: 23 additions & 24 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 @@ -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()
Expand All @@ -264,7 +263,7 @@ open class Reviewer :
if (currentCard == null) {
return
}
mCardMarker!!.displayFlag(flagToDisplay)
mCardMarker!!.displayFlag(Flag.fromCode(flagToDisplay))
}

private fun selectDeckFromExtra() {
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) {
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)
Expand Down
34 changes: 7 additions & 27 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,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) {
Expand All @@ -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
}
}

Expand All @@ -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
}
}
Loading

0 comments on commit 1de854f

Please sign in to comment.