Skip to content

Commit 57c47e1

Browse files
committed
refactor: avoid adding unnecessary flow collectors
in case the menu items don't exist it also makes the method more consistent and restricts the scope of the variables
1 parent 7c9dfd7 commit 57c47e1

1 file changed

Lines changed: 35 additions & 30 deletions

File tree

AnkiDroid/src/main/java/com/ichi2/anki/ui/windows/reviewer/ReviewerMenu.kt

Lines changed: 35 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,40 +39,45 @@ fun ReviewerMenuView.setup(
3939
return
4040
}
4141

42-
viewModel.flagFlow
43-
.flowWithLifecycle(lifecycle)
44-
.collectLatestIn(lifecycle.coroutineScope) { flagCode ->
45-
findItem(ViewerAction.FLAG_MENU.menuId)?.setPaddedIcon(context, flagCode.drawableRes)
46-
}
42+
findItem(ViewerAction.FLAG_MENU.menuId)?.let { flagItem ->
43+
viewModel.flagFlow
44+
.flowWithLifecycle(lifecycle)
45+
.collectLatestIn(lifecycle.coroutineScope) { flagCode ->
46+
flagItem.setPaddedIcon(context, flagCode.drawableRes)
47+
}
48+
}
4749

48-
val markItem = findItem(ViewerAction.MARK.menuId)
49-
viewModel.isMarkedFlow
50-
.flowWithLifecycle(lifecycle)
51-
.collectLatestIn(lifecycle.coroutineScope) { isMarked ->
52-
if (isMarked) {
53-
markItem?.setPaddedIcon(context, R.drawable.ic_star)
54-
markItem?.setTitle(R.string.menu_unmark_note)
55-
} else {
56-
markItem?.setPaddedIcon(context, R.drawable.ic_star_border_white)
57-
markItem?.setTitle(R.string.menu_mark_note)
50+
findItem(ViewerAction.MARK.menuId)?.let { markItem ->
51+
viewModel.isMarkedFlow
52+
.flowWithLifecycle(lifecycle)
53+
.collectLatestIn(lifecycle.coroutineScope) { isMarked ->
54+
if (isMarked) {
55+
markItem.setPaddedIcon(context, R.drawable.ic_star)
56+
markItem.setTitle(R.string.menu_unmark_note)
57+
} else {
58+
markItem.setPaddedIcon(context, R.drawable.ic_star_border_white)
59+
markItem.setTitle(R.string.menu_mark_note)
60+
}
5861
}
59-
}
62+
}
6063

61-
val undoItem = findItem(ViewerAction.UNDO.menuId)
62-
viewModel.undoLabelFlow
63-
.flowWithLifecycle(lifecycle)
64-
.collectLatestIn(lifecycle.coroutineScope) { label ->
65-
undoItem?.title = label ?: CollectionManager.TR.undoUndo()
66-
undoItem?.isEnabled = label != null
67-
}
64+
findItem(ViewerAction.UNDO.menuId)?.let { undoItem ->
65+
viewModel.undoLabelFlow
66+
.flowWithLifecycle(lifecycle)
67+
.collectLatestIn(lifecycle.coroutineScope) { label ->
68+
undoItem.title = label ?: CollectionManager.TR.undoUndo()
69+
undoItem.isEnabled = label != null
70+
}
71+
}
6872

69-
val redoItem = findItem(ViewerAction.REDO.menuId)
70-
viewModel.redoLabelFlow
71-
.flowWithLifecycle(lifecycle)
72-
.collectLatestIn(lifecycle.coroutineScope) { label ->
73-
redoItem?.title = label ?: CollectionManager.TR.undoRedo()
74-
redoItem?.isEnabled = label != null
75-
}
73+
findItem(ViewerAction.REDO.menuId)?.let { redoItem ->
74+
viewModel.redoLabelFlow
75+
.flowWithLifecycle(lifecycle)
76+
.collectLatestIn(lifecycle.coroutineScope) { label ->
77+
redoItem.title = label ?: CollectionManager.TR.undoRedo()
78+
redoItem.isEnabled = label != null
79+
}
80+
}
7681

7782
findItem(ViewerAction.SUSPEND_MENU.menuId)?.let { suspendItem ->
7883
val suspendFlow = viewModel.canSuspendNoteFlow.flowWithLifecycle(lifecycle)

0 commit comments

Comments
 (0)