Skip to content

Commit

Permalink
feat: add "Flag" option to the new previewer
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 15, 2023
1 parent b2b13a3 commit 66c6eb8
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ import com.google.android.material.button.MaterialButton
import com.google.android.material.slider.Slider
import com.google.android.material.textview.MaterialTextView
import com.ichi2.anki.CollectionHelper
import com.ichi2.anki.Flag
import com.ichi2.anki.NoteEditor
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
Expand All @@ -62,6 +63,9 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
private val markOption: MenuItem
get() = menu.findItem(R.id.action_mark)

private val flagOption: MenuItem
get() = menu.findItem(R.id.action_flag)

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand Down Expand Up @@ -159,6 +163,13 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
}
}
}
lifecycleScope.launch {
viewModel.flagCode
.flowWithLifecycle(lifecycle)
.collectLatest { flagCode ->
flagOption.setIcon(Flag.fromCode(flagCode).drawableRes)
}
}

if (cardsCount == 1) {
slider.visibility = View.GONE
Expand Down Expand Up @@ -203,6 +214,14 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
R.id.action_edit -> editCard()
R.id.action_mark -> viewModel.toggleMark()
R.id.action_back_side_only -> viewModel.toggleBacksideOnly()
R.id.action_flag_zero -> viewModel.setFlag(Flag.NONE)
R.id.action_flag_one -> viewModel.setFlag(Flag.RED)
R.id.action_flag_two -> viewModel.setFlag(Flag.ORANGE)
R.id.action_flag_three -> viewModel.setFlag(Flag.GREEN)
R.id.action_flag_four -> viewModel.setFlag(Flag.BLUE)
R.id.action_flag_five -> viewModel.setFlag(Flag.PINK)
R.id.action_flag_six -> viewModel.setFlag(Flag.TURQUOISE)
R.id.action_flag_seven -> viewModel.setFlag(Flag.PURPLE)
}
return true
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import androidx.lifecycle.viewmodel.viewModelFactory
import com.google.android.material.color.MaterialColors.getColor
import com.ichi2.anki.AnkiDroidApp
import com.ichi2.anki.CollectionManager.withCol
import com.ichi2.anki.Flag
import com.ichi2.anki.LanguageUtils
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.libanki.Card
Expand All @@ -48,6 +49,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
val currentIndex = MutableStateFlow(firstIndex)
val backsideOnly = MutableStateFlow(false)
val isMarked = MutableStateFlow(false)
val flagCode: MutableStateFlow<Int> = MutableStateFlow(Flag.NONE.code)

private var showingAnswer = false

Expand Down Expand Up @@ -81,6 +83,15 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
}
}

fun setFlag(flag: Flag) {
launchCatching {
withCol {
setUserFlagForCards(listOf(currentCard.id), flag.code)
}
flagCode.emit(flag.code)
}
}

fun serverBaseUrl() = server.baseUrl()

fun cardId() = currentCard.id
Expand All @@ -104,6 +115,10 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
}
}

private suspend fun updateFlagIcon() {
flagCode.emit(currentCard.userFlag())
}

private suspend fun updateMarkIcon() {
isMarked.emit(currentCard.note().hasTag(MARKED_TAG))
}
Expand All @@ -118,6 +133,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra

eval.emit("_showQuestion(${Json.encodeToString(question)}, ${Json.encodeToString(answer)}, '$bodyClass');")

updateFlagIcon()
updateMarkIcon()
}

Expand Down
39 changes: 39 additions & 0 deletions AnkiDroid/src/main/res/menu/previewer2.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,45 @@
android:icon="@drawable/ic_card_question"
android:title="@string/card_side_both"
app:showAsAction="always"/>
<item
android:id="@+id/action_flag"
android:title="@string/menu_flag_card"
android:icon="@drawable/ic_flag_transparent"
app:showAsAction="always" >
<menu>
<item
android:id="@+id/action_flag_zero"
android:title="@string/menu_flag_card_zero"/>
<item
android:id="@+id/action_flag_one"
android:title="@string/menu_flag_card_one"
android:icon="@drawable/ic_flag_red"/>
<item
android:id="@+id/action_flag_two"
android:title="@string/menu_flag_card_two"
android:icon="@drawable/ic_flag_orange"/>
<item
android:id="@+id/action_flag_three"
android:title="@string/menu_flag_card_three"
android:icon="@drawable/ic_flag_green"/>
<item
android:id="@+id/action_flag_four"
android:title="@string/menu_flag_card_four"
android:icon="@drawable/ic_flag_blue"/>
<item
android:id="@+id/action_flag_five"
android:title="@string/menu_flag_card_five"
android:icon="@drawable/ic_flag_pink"/>
<item
android:id="@+id/action_flag_six"
android:title="@string/menu_flag_card_six"
android:icon="@drawable/ic_flag_turquoise"/>
<item
android:id="@+id/action_flag_seven"
android:title="@string/menu_flag_card_seven"
android:icon="@drawable/ic_flag_purple"/>
</menu>
</item>
<item
android:id="@+id/action_mark"
android:icon="@drawable/ic_star_border_white"
Expand Down

0 comments on commit 66c6eb8

Please sign in to comment.