Skip to content

Commit

Permalink
refactor: define flags ids statically
Browse files Browse the repository at this point in the history
It is technically possible (although unlikely) to get an ID conflicts when using the ordinals as IDs.

Also, I think that `onMenuItemClick` is easier to follow this way.
  • Loading branch information
BrayanDSO committed Jun 20, 2024
1 parent 3fe04cb commit 97e72c9
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 17 deletions.
24 changes: 15 additions & 9 deletions AnkiDroid/src/main/java/com/ichi2/anki/Flag.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package com.ichi2.anki

import androidx.annotation.ColorRes
import androidx.annotation.DrawableRes
import androidx.annotation.IdRes
import com.ichi2.anki.CollectionManager.TR
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.utils.ext.getStringOrNull
Expand All @@ -26,15 +27,20 @@ import com.ichi2.libanki.Collection
import org.json.JSONObject
import timber.log.Timber

enum class Flag(val code: Int, @DrawableRes val drawableRes: Int, @ColorRes val browserColorRes: Int?) {
NONE(0, R.drawable.ic_flag_transparent, null),
RED(1, R.drawable.ic_flag_red, R.color.flag_red),
ORANGE(2, R.drawable.ic_flag_orange, R.color.flag_orange),
GREEN(3, R.drawable.ic_flag_green, R.color.flag_green),
BLUE(4, R.drawable.ic_flag_blue, R.color.flag_blue),
PINK(5, R.drawable.ic_flag_pink, R.color.flag_pink),
TURQUOISE(6, R.drawable.ic_flag_turquoise, R.color.flag_turquoise),
PURPLE(7, R.drawable.ic_flag_purple, R.color.flag_purple);
enum class Flag(
val code: Int,
@IdRes val id: Int,
@DrawableRes val drawableRes: Int,
@ColorRes val browserColorRes: Int?
) {
NONE(0, R.id.flag_none, R.drawable.ic_flag_transparent, null),
RED(1, R.id.flag_red, R.drawable.ic_flag_red, R.color.flag_red),
ORANGE(2, R.id.flag_orange, R.drawable.ic_flag_orange, R.color.flag_orange),
GREEN(3, R.id.flag_green, R.drawable.ic_flag_green, R.color.flag_green),
BLUE(4, R.id.flag_blue, R.drawable.ic_flag_blue, R.color.flag_blue),
PINK(5, R.id.flag_pink, R.drawable.ic_flag_pink, R.color.flag_pink),
TURQUOISE(6, R.id.flag_turquoise, R.drawable.ic_flag_turquoise, R.color.flag_turquoise),
PURPLE(7, R.id.flag_purple, R.drawable.ic_flag_purple, R.color.flag_purple);

/**
* Retrieves the name associated with the flag. This may be user-defined
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,6 @@ import com.ichi2.annotations.NeedsTest
import com.ichi2.utils.performClickIfEnabled
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch
import timber.log.Timber

class PreviewerFragment :
CardViewerFragment(R.layout.previewer),
Expand Down Expand Up @@ -194,23 +193,25 @@ class PreviewerFragment :
val submenu = menu.findItem(R.id.action_flag).subMenu
lifecycleScope.launch {
for ((flag, name) in Flag.queryDisplayNames()) {
submenu?.add(Menu.NONE, flag.ordinal, Menu.NONE, name)
submenu?.add(Menu.NONE, flag.id, Menu.NONE, name)
?.setIcon(flag.drawableRes)
}
}
}

override fun onMenuItemClick(item: MenuItem): Boolean {
Flag.entries.find { it.ordinal == item.itemId }?.let { flag ->
Timber.i("PreviewerFragment:: onMenuItemClick Flag - ${flag.name} clicked")
viewModel.setFlag(flag)
return true
}

when (item.itemId) {
R.id.action_edit -> editCard()
R.id.action_mark -> viewModel.toggleMark()
R.id.action_back_side_only -> viewModel.toggleBackSideOnly()
R.id.flag_none -> viewModel.setFlag(Flag.NONE)
R.id.flag_red -> viewModel.setFlag(Flag.RED)
R.id.flag_orange -> viewModel.setFlag(Flag.ORANGE)
R.id.flag_green -> viewModel.setFlag(Flag.GREEN)
R.id.flag_blue -> viewModel.setFlag(Flag.BLUE)
R.id.flag_pink -> viewModel.setFlag(Flag.PINK)
R.id.flag_turquoise -> viewModel.setFlag(Flag.TURQUOISE)
R.id.flag_purple -> viewModel.setFlag(Flag.PURPLE)
}
return true
}
Expand Down
26 changes: 26 additions & 0 deletions AnkiDroid/src/main/res/values/ids.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8"?>
<!--
~ Copyright (c) 2024 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/>.
-->
<resources>
<item type="id" name="flag_none"/>
<item type="id" name="flag_red"/>
<item type="id" name="flag_orange"/>
<item type="id" name="flag_green"/>
<item type="id" name="flag_blue"/>
<item type="id" name="flag_pink"/>
<item type="id" name="flag_turquoise"/>
<item type="id" name="flag_purple"/>
</resources>

0 comments on commit 97e72c9

Please sign in to comment.