Skip to content

Commit

Permalink
feat: add backside only option to the newPreviewer
Browse files Browse the repository at this point in the history
  • Loading branch information
BrayanDSO committed Dec 12, 2023
1 parent a2a9dc1 commit f27a479
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ import java.io.File
class PreviewerActivity : AnkiActivity() {
private lateinit var viewModel: PreviewerViewModel

private var backsideOnlyButton: MenuItem? = null

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.previewer)
Expand Down Expand Up @@ -113,6 +115,13 @@ class PreviewerActivity : AnkiActivity() {
webView.evaluateJavascript(eval, null)
}
}
lifecycleScope.launch {
viewModel.backsideOnly
.flowWithLifecycle(lifecycle)
.collectLatest { isBacksideOnly ->
setBacksideOnlyButtonIcon(isBacksideOnly)
}
}

val cardsCount = selectedCardIds.count()
lifecycleScope.launch {
Expand Down Expand Up @@ -159,14 +168,32 @@ class PreviewerActivity : AnkiActivity() {

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.previewer2, menu)
backsideOnlyButton = menu?.findItem(R.id.action_back_side_only)
return true
}

private fun setBacksideOnlyButtonIcon(isBacksideOnly: Boolean) {
backsideOnlyButton?.apply {
if (isBacksideOnly) {
setIcon(R.drawable.ic_card_answer)
setTitle(R.string.card_side_answer)
} else {
setIcon(R.drawable.ic_card_question)
setTitle(R.string.card_side_both)
}
}
}

override fun onPrepareOptionsMenu(menu: Menu?): Boolean {
setBacksideOnlyButtonIcon(viewModel.backsideOnly.value)
return super.onPrepareOptionsMenu(menu)
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_edit -> TODO() // in the next commit :)
R.id.action_back_side_only -> {
TODO() // in the next commit
viewModel.toggleBacksideOnly()
}
}
return super.onOptionsItemSelected(item)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,24 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
val eval = MutableSharedFlow<String>()
val onError = MutableSharedFlow<String>()
val currentIndex = MutableStateFlow(firstIndex)
val backsideOnly = MutableStateFlow(false)

private var showingAnswer = false

// TODO maybe move the server to a Service and move it out of here?
private val server = PreviewerServer(mediaDir).also { it.start() }
private lateinit var currentCard: Card

fun toggleBacksideOnly() {
Timber.v("toggleBacksideOnly() %b", !backsideOnly.value)
launchCatching {
backsideOnly.emit(!backsideOnly.value)
if (backsideOnly.value && !showingAnswer) {
showAnswer()
}
}
}

fun serverBaseUrl() = server.baseUrl()

/**
Expand All @@ -64,8 +75,9 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
if (!this::currentCard.isInitialized || reload) {
currentCard = withCol { getCard(selectedCardIds[currentIndex.value]) }
}
val answerShouldBeShown = showingAnswer || backsideOnly.value
showQuestion()
if (showingAnswer) {
if (answerShouldBeShown) {
showAnswer()
}
}
Expand Down Expand Up @@ -109,11 +121,14 @@ class PreviewerViewModel(mediaDir: String, private val selectedCardIds: LongArra
currentIndex.emit(index)
currentCard = withCol { getCard(selectedCardIds[index]) }
showQuestion()
if (backsideOnly.value) {
showAnswer()
}
}
}

private suspend fun showAnswerOrDisplayCard(index: Int) {
if (!showingAnswer) {
if (!showingAnswer && !backsideOnly.value) {
showAnswer()
} else {
displayCard(index)
Expand Down

0 comments on commit f27a479

Please sign in to comment.