Refactor overflow menus into a composable

This commit is contained in:
arkon 2022-10-30 11:06:41 -04:00
parent f5451a6881
commit df773ee15c
4 changed files with 139 additions and 142 deletions

View File

@ -1,20 +1,29 @@
package eu.kanade.presentation.components
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.ColumnScope
import androidx.compose.foundation.layout.sizeIn
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.RadioButtonChecked
import androidx.compose.material.icons.outlined.RadioButtonUnchecked
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.DpOffset
import androidx.compose.ui.unit.dp
import androidx.compose.ui.window.PopupProperties
import eu.kanade.tachiyomi.R
import me.saket.cascade.CascadeColumnScope
import me.saket.cascade.CascadeDropdownMenu
import androidx.compose.material3.DropdownMenu as ComposeDropdownMenu
@Composable
@ -61,3 +70,27 @@ fun RadioMenuItem(
},
)
}
@Composable
fun OverflowMenu(
content: @Composable CascadeColumnScope.(() -> Unit) -> Unit,
) {
var moreExpanded by remember { mutableStateOf(false) }
val closeMenu = { moreExpanded = false }
Box {
IconButton(onClick = { moreExpanded = !moreExpanded }) {
Icon(
imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
)
}
CascadeDropdownMenu(
expanded = moreExpanded,
onDismissRequest = closeMenu,
offset = DpOffset(8.dp, (-56).dp),
) {
content(closeMenu)
}
}
}

View File

@ -1,14 +1,12 @@
package eu.kanade.presentation.library.components
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.outlined.FilterList
import androidx.compose.material.icons.outlined.FlipToBack
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.Search
import androidx.compose.material.icons.outlined.SelectAll
import androidx.compose.material3.DropdownMenuItem
@ -19,10 +17,6 @@ import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarScrollBehavior
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalFocusManager
@ -32,7 +26,7 @@ import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.sp
import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.components.DropdownMenu
import eu.kanade.presentation.components.OverflowMenu
import eu.kanade.presentation.components.Pill
import eu.kanade.presentation.components.SearchToolbar
import eu.kanade.presentation.library.LibraryState
@ -137,35 +131,22 @@ fun LibraryRegularToolbar(
IconButton(onClick = onClickFilter) {
Icon(Icons.Outlined.FilterList, contentDescription = stringResource(R.string.action_filter), tint = filterTint)
}
var moreExpanded by remember { mutableStateOf(false) }
Box {
IconButton(onClick = { moreExpanded = !moreExpanded }) {
Icon(
imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
)
}
val onDismissRequest = { moreExpanded = false }
DropdownMenu(
expanded = moreExpanded,
onDismissRequest = onDismissRequest,
) {
OverflowMenu { closeMenu ->
DropdownMenuItem(
text = { Text(text = stringResource(R.string.pref_category_library_update)) },
onClick = {
onClickRefresh()
onDismissRequest()
closeMenu()
},
)
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_open_random_manga)) },
onClick = {
onClickOpenRandomManga()
onDismissRequest()
closeMenu()
},
)
}
}
},
incognitoMode = incognitoMode,
downloadedOnlyMode = downloadedOnlyMode,

View File

@ -8,7 +8,6 @@ import androidx.compose.material.icons.outlined.Close
import androidx.compose.material.icons.outlined.Download
import androidx.compose.material.icons.outlined.FilterList
import androidx.compose.material.icons.outlined.FlipToBack
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.SelectAll
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
@ -20,10 +19,8 @@ import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.alpha
import androidx.compose.ui.res.stringResource
@ -31,6 +28,7 @@ import androidx.compose.ui.text.style.TextOverflow
import androidx.compose.ui.unit.dp
import eu.kanade.presentation.components.AppStateBanners
import eu.kanade.presentation.components.DropdownMenu
import eu.kanade.presentation.components.OverflowMenu
import eu.kanade.presentation.manga.DownloadAction
import eu.kanade.presentation.theme.active
import eu.kanade.tachiyomi.R
@ -157,25 +155,13 @@ fun MangaToolbar(
}
if (onClickEditCategory != null || onClickMigrate != null || onClickShare != null) {
var moreExpanded by remember { mutableStateOf(false) }
Box {
IconButton(onClick = { moreExpanded = !moreExpanded }) {
Icon(
imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
)
}
val onDismissRequest = { moreExpanded = false }
DropdownMenu(
expanded = moreExpanded,
onDismissRequest = onDismissRequest,
) {
OverflowMenu { closeMenu ->
if (onClickEditCategory != null) {
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_edit_categories)) },
onClick = {
onClickEditCategory()
onDismissRequest()
closeMenu()
},
)
}
@ -184,7 +170,7 @@ fun MangaToolbar(
text = { Text(text = stringResource(R.string.action_migrate)) },
onClick = {
onClickMigrate()
onDismissRequest()
closeMenu()
},
)
}
@ -193,14 +179,13 @@ fun MangaToolbar(
text = { Text(text = stringResource(R.string.action_share)) },
onClick = {
onClickShare()
onDismissRequest()
closeMenu()
},
)
}
}
}
}
}
},
colors = TopAppBarDefaults.smallTopAppBarColors(
containerColor = MaterialTheme.colorScheme

View File

@ -14,11 +14,9 @@ import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.PlayArrow
import androidx.compose.material.icons.outlined.MoreVert
import androidx.compose.material.icons.outlined.Pause
import androidx.compose.material3.DropdownMenuItem
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBarDefaults
@ -52,6 +50,7 @@ import androidx.recyclerview.widget.LinearLayoutManager
import eu.kanade.presentation.components.AppBar
import eu.kanade.presentation.components.EmptyScreen
import eu.kanade.presentation.components.ExtendedFloatingActionButton
import eu.kanade.presentation.components.OverflowMenu
import eu.kanade.presentation.components.Pill
import eu.kanade.presentation.components.Scaffold
import eu.kanade.tachiyomi.R
@ -61,7 +60,6 @@ import eu.kanade.tachiyomi.databinding.DownloadListBinding
import eu.kanade.tachiyomi.source.model.Page
import eu.kanade.tachiyomi.ui.base.controller.FullComposeController
import eu.kanade.tachiyomi.util.lang.launchUI
import me.saket.cascade.CascadeDropdownMenu
import rx.Observable
import rx.Subscription
import rx.android.schedulers.AndroidSchedulers
@ -147,18 +145,7 @@ class DownloadController :
navigateUp = router::popCurrentController,
actions = {
if (downloadList.isNotEmpty()) {
var expanded by remember { mutableStateOf(false) }
Box {
IconButton(onClick = { expanded = !expanded }) {
Icon(
imageVector = Icons.Outlined.MoreVert,
contentDescription = stringResource(R.string.abc_action_menu_overflow_description),
)
}
CascadeDropdownMenu(
expanded = expanded,
onDismissRequest = { expanded = false },
) {
OverflowMenu { closeMenu ->
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_reorganize_by)) },
children = {
@ -168,15 +155,21 @@ class DownloadController :
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_newest)) },
onClick = {
reorderQueue({ it.download.chapter.date_upload }, true)
expanded = false
reorderQueue(
{ it.download.chapter.date_upload },
true,
)
closeMenu()
},
)
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_oldest)) },
onClick = {
reorderQueue({ it.download.chapter.date_upload }, false)
expanded = false
reorderQueue(
{ it.download.chapter.date_upload },
false,
)
closeMenu()
},
)
},
@ -187,15 +180,21 @@ class DownloadController :
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_asc)) },
onClick = {
reorderQueue({ it.download.chapter.chapter_number }, false)
expanded = false
reorderQueue(
{ it.download.chapter.chapter_number },
false,
)
closeMenu()
},
)
DropdownMenuItem(
text = { Text(text = stringResource(R.string.action_desc)) },
onClick = {
reorderQueue({ it.download.chapter.chapter_number }, true)
expanded = false
reorderQueue(
{ it.download.chapter.chapter_number },
true,
)
closeMenu()
},
)
},
@ -206,12 +205,11 @@ class DownloadController :
text = { Text(text = stringResource(R.string.action_cancel_all)) },
onClick = {
presenter.clearQueue(context)
expanded = false
closeMenu()
},
)
}
}
}
},
scrollBehavior = scrollBehavior,
)