Skip to content

Commit 68a9d02

Browse files
committed
Moved menu logic in viewmodel file
1 parent 7defb30 commit 68a9d02

3 files changed

Lines changed: 70 additions & 18 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/DeckPicker.kt

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
7272
import androidx.work.WorkInfo
7373
import androidx.work.WorkManager
7474
import anki.collection.OpChanges
75+
import anki.decks.deckId
7576
import anki.sync.SyncStatusResponse
7677
import com.google.android.material.progressindicator.LinearProgressIndicator
7778
import com.google.android.material.snackbar.BaseTransientBottomBar
@@ -436,25 +437,18 @@ open class DeckPicker :
436437
}
437438
}
438439

439-
private fun showDeckPickerContextMenu(deckId: DeckId) =
440-
launchCatchingTask {
441-
viewModel.selectDeck(deckId).join()
442-
val menu = DeckPickerContextMenu.newInstance(deckId)
443-
CardBrowser.clearLastDeckId()
444-
showDialogFragment(menu)
445-
}
440+
private suspend fun showDeckPickerContextMenu(deckId: DeckId) {
441+
val menu = DeckPickerContextMenu.newInstance(deckId)
442+
CardBrowser.clearLastDeckId()
443+
showDialogFragment(menu)
444+
}
446445

447-
private fun showDeckPickerRightClickContextMenu(
448-
deckId: DeckId,
449-
x: Float,
450-
y: Float,
451-
) = launchCatchingTask {
452-
viewModel.selectDeck(deckId).join()
446+
private suspend fun showDeckPickerRightClickContextMenu(request: DeckPickerViewModel.RightClickMenuRequest) {
453447
DeckPickerMenuContentProvider.show(
454448
deckPicker = this@DeckPicker,
455-
deckId = deckId,
456-
x = x,
457-
y = y,
449+
deckId = request.deckId,
450+
x = request.x,
451+
y = request.y,
458452
)
459453
}
460454

@@ -520,9 +514,9 @@ open class DeckPicker :
520514
viewModel.toggleDeckExpand(deckId)
521515
dismissAllDialogFragments()
522516
},
523-
onDeckContextRequested = ::showDeckPickerContextMenu,
517+
onDeckContextRequested = { deckId -> viewModel.requestContextMenu(deckId) },
524518
onDeckRightClick = { deckId, x, y ->
525-
showDeckPickerRightClickContextMenu(deckId, x, y)
519+
viewModel.requestRightClickContextMenu(deckId, x, y)
526520
Timber.d("Right Click on deck recorded!! %d, %f %f", deckId, x, y)
527521
},
528522
)
@@ -795,6 +789,8 @@ open class DeckPicker :
795789
viewModel.flowOfResizingDividerVisible.launchCollectionInLifecycleScope(::onResizingDividerVisibilityChanged)
796790
viewModel.flowOfDecksReloaded.launchCollectionInLifecycleScope(::onDecksReloaded)
797791
viewModel.flowOfStartupResponse.filterNotNull().launchCollectionInLifecycleScope(::onStartupResponse)
792+
viewModel.flowOfShowContextMenu.launchCollectionInLifecycleScope(::showDeckPickerContextMenu)
793+
viewModel.flowOfShowRightClickContextMenu.launchCollectionInLifecycleScope(::showDeckPickerRightClickContextMenu)
798794
}
799795

800796
private val onReceiveContentListener =

AnkiDroid/src/main/java/com/ichi2/anki/deckpicker/DeckPickerViewModel.kt

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,31 @@ class DeckPickerViewModel :
304304
flowOfDestination.emit(NoteEditorLauncher.AddNote(deckId))
305305
}
306306

307+
val flowOfShowContextMenu = MutableSharedFlow<DeckId>(extraBufferCapacity = 1)
308+
309+
data class RightClickMenuRequest(
310+
val deckId: DeckId,
311+
val x: Float,
312+
val y: Float,
313+
)
314+
315+
val flowOfShowRightClickContextMenu = MutableSharedFlow<RightClickMenuRequest>(extraBufferCapacity = 1)
316+
317+
fun requestContextMenu(deckId: DeckId) =
318+
viewModelScope.launch {
319+
selectDeck(deckId).join()
320+
flowOfShowContextMenu.emit(deckId)
321+
}
322+
323+
fun requestRightClickContextMenu(
324+
deckId: DeckId,
325+
x: Float,
326+
y: Float,
327+
) = viewModelScope.launch {
328+
selectDeck(deckId).join()
329+
flowOfShowRightClickContextMenu.emit(RightClickMenuRequest(deckId, x, y))
330+
}
331+
307332
/**
308333
* Opens the Manage Note Types screen.
309334
*/

AnkiDroid/src/test/java/com/ichi2/anki/deckpicker/DeckPickerViewModelTest.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,35 @@ class DeckPickerViewModelTest : RobolectricTest() {
208208
}
209209
}
210210
}
211+
212+
@Test
213+
fun `request context menu - flow`() {
214+
runTest {
215+
val deckId = addDeck("Deck A")
216+
217+
viewModel.flowOfShowContextMenu.test {
218+
viewModel.requestContextMenu(deckId).join()
219+
220+
assertEquals(deckId, awaitItem())
221+
}
222+
}
223+
}
224+
225+
@Test
226+
fun `request right click context menu - flow`() {
227+
runTest {
228+
val deckId = addDeck("Deck B")
229+
val x = 10f
230+
val y = 20f
231+
232+
viewModel.flowOfShowRightClickContextMenu.test {
233+
viewModel.requestRightClickContextMenu(deckId, x, y).join()
234+
235+
val item = awaitItem()
236+
assertEquals(deckId, item.deckId)
237+
assertEquals(x, item.x)
238+
assertEquals(y, item.y)
239+
}
240+
}
241+
}
211242
}

0 commit comments

Comments
 (0)