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 13, 2023
1 parent d2e39eb commit be782ee
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,18 @@ 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_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.NoteEditor
import com.ichi2.anki.R
import com.ichi2.anki.SingleFragmentActivity
import com.ichi2.anki.getFlagDrawable
import com.ichi2.anki.previewer.PreviewerViewModel.Companion.stdHtml
import com.ichi2.themes.Themes
import kotlinx.coroutines.flow.collectLatest
Expand All @@ -62,6 +71,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 +171,13 @@ class PreviewerFragment : Fragment(), Toolbar.OnMenuItemClickListener {
}
}
}
lifecycleScope.launch {
viewModel.cardFlag
.flowWithLifecycle(lifecycle)
.collectLatest { flag ->
flagOption.setIcon(getFlagDrawable(flag))
}
}

if (cardsCount == 1) {
slider.visibility = View.GONE
Expand Down Expand Up @@ -203,6 +222,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,8 +24,9 @@ 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_NONE
import com.ichi2.anki.Flag
import com.ichi2.anki.LanguageUtils
import com.ichi2.anki.R
import com.ichi2.anki.servicelayer.MARKED_TAG
import com.ichi2.libanki.Card
import com.ichi2.libanki.addPlayButtons
Expand All @@ -49,6 +50,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
val currentIndex = MutableStateFlow(firstIndex)
val backsideOnly = MutableStateFlow(false)
val isMarked = MutableStateFlow(false)
val cardFlag = MutableStateFlow(FLAG_NONE)

private var showingAnswer = false

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

fun setFlag(@Flag flag: Int) {
launchCatching {
withCol {
setUserFlagForCards(listOf(currentCard.id), flag)
}
cardFlag.emit(flag)
}
}

fun serverBaseUrl() = server.baseUrl()

fun cardId() = currentCard.id
Expand All @@ -106,7 +117,7 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
}

private suspend fun updateFlagIcon() {
eval.emit("_drawFlag(${currentCard.userFlag()});")
cardFlag.emit(currentCard.userFlag())
}

private suspend fun 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 be782ee

Please sign in to comment.