Skip to content

Commit

Permalink
refactor BottomBar
Browse files Browse the repository at this point in the history
  • Loading branch information
kid1412621 committed Apr 30, 2024
1 parent e6e1ee7 commit 83401b6
Showing 1 changed file with 180 additions and 125 deletions.
305 changes: 180 additions & 125 deletions app/src/main/kotlin/me/nanova/subspace/ui/page/HomePage.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import androidx.compose.material.icons.rounded.ArrowDropUp
import androidx.compose.material.icons.rounded.FilterList
import androidx.compose.material.icons.rounded.Search
import androidx.compose.material.icons.rounded.SortByAlpha
import androidx.compose.material.icons.rounded.Speed
import androidx.compose.material.icons.rounded.SwapVert
import androidx.compose.material3.BottomAppBar
import androidx.compose.material3.BottomAppBarDefaults
Expand All @@ -30,6 +31,7 @@ import androidx.compose.material3.FloatingActionButtonDefaults
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.MenuDefaults
import androidx.compose.material3.ModalBottomSheet
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Surface
Expand Down Expand Up @@ -62,6 +64,7 @@ import me.nanova.subspace.R
import me.nanova.subspace.ui.Routes
import me.nanova.subspace.ui.component.AccountMenu
import me.nanova.subspace.ui.component.TorrentList
import me.nanova.subspace.ui.vm.HomeUiState
import me.nanova.subspace.ui.vm.HomeViewModel

@OptIn(ExperimentalMaterial3Api::class)
Expand All @@ -75,32 +78,8 @@ fun HomePage(
val accounts by homeViewModel.accounts.collectAsState(initial = emptyList())

val drawerState = rememberDrawerState(initialValue = DrawerValue.Closed)
val sheetState = rememberModalBottomSheetState()
var showFilterSheet by remember { mutableStateOf(false) }
var showSortMenu by remember { mutableStateOf(false) }

val scope = rememberCoroutineScope()


if (showFilterSheet) {
ModalBottomSheet(
onDismissRequest = {
showFilterSheet = false
},
sheetState = sheetState
) {
Button(onClick = {
scope.launch { sheetState.hide() }.invokeOnCompletion {
if (!sheetState.isVisible) {
showFilterSheet = false
}
}
}) {
Text("Hide bottom sheet")
}
}
}

DismissibleNavigationDrawer(
drawerState = drawerState,
drawerContent = {
Expand Down Expand Up @@ -132,107 +111,7 @@ fun HomePage(
)
},
bottomBar = {
if (currentAccount != null) {
if (showSortMenu) {
DropdownMenu(
expanded = true,
onDismissRequest = { showSortMenu = false }
) {
DropdownMenuItem(
text = { Text(text = "Name") },
leadingIcon = {
Icon(
Icons.Rounded.SortByAlpha,
contentDescription = "Sort by name"
)
},
trailingIcon = {
if (uiState.filter.reverse) {
Icon(
Icons.Rounded.ArrowDropDown,
contentDescription = "Descending"
)
} else {
Icon(
Icons.Rounded.ArrowDropUp,
contentDescription = "Ascending"
)
}
},
onClick = {
homeViewModel.updateSort(
uiState.filter.copy(
sort = "name",
reverse = if (uiState.filter.sort == "name") !uiState.filter.reverse else uiState.filter.reverse
)
)
showSortMenu = false
})
DropdownMenuItem(
text = { Text(text = "Added On") },
leadingIcon = {
Icon(
Icons.Rounded.AccessTime,
contentDescription = "Sort by add time"
)
},
trailingIcon = {
if (uiState.filter.reverse) {
Icon(
Icons.Rounded.ArrowDropDown,
contentDescription = "Descending"
)
} else {
Icon(
Icons.Rounded.ArrowDropUp,
contentDescription = "Ascending"
)
}
},
onClick = {
homeViewModel.updateSort(
uiState.filter.copy(
sort = "added_on",
reverse = if (uiState.filter.sort == "added_on") !uiState.filter.reverse else uiState.filter.reverse
)
)
showSortMenu = false
})
DropdownMenuItem(
text = { Text(text = "Speed") },
onClick = { /*TODO*/ })
}
}

BottomAppBar(
actions = {
IconButton(onClick = { /* do something */ }, enabled = false) {
Icon(Icons.Rounded.Search, contentDescription = "search")
}
IconButton(onClick = { showFilterSheet = true }) {
Icon(
Icons.Rounded.FilterList,
contentDescription = "filter"
)
}
IconButton(onClick = { showSortMenu = true }) {
Icon(
Icons.Rounded.SwapVert,
contentDescription = "sort"
)
}
},
floatingActionButton = {
FloatingActionButton(
onClick = { },
containerColor = BottomAppBarDefaults.bottomAppBarFabColor,
elevation = FloatingActionButtonDefaults.bottomAppBarFabElevation()
) {
Icon(Icons.Rounded.Add, "Add torrent")
}
}
)
}
BottomBar(currentAccount != null, uiState, homeViewModel)
},
) { innerPadding ->
Surface(
Expand All @@ -256,6 +135,182 @@ fun HomePage(
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun BottomBar(
show: Boolean = false,
uiState: HomeUiState,
homeViewModel: HomeViewModel,
) {
var showSortMenu by remember { mutableStateOf(false) }
var showFilterSheet by remember { mutableStateOf(false) }
val sheetState = rememberModalBottomSheetState()
val scope = rememberCoroutineScope()

if (show) {
if (showFilterSheet) {
ModalBottomSheet(
onDismissRequest = {
showFilterSheet = false
},
sheetState = sheetState
) {
Button(onClick = {
scope.launch { sheetState.hide() }
.invokeOnCompletion {
if (!sheetState.isVisible) {
showFilterSheet = false
}
}
}) {
Text("Hide bottom sheet")
}
}
}

if (showSortMenu) {
DropdownMenu(
expanded = true,
onDismissRequest = { showSortMenu = false }
) {
DropdownMenuItem(
text = { Text(text = "Name") },
colors = if (uiState.filter.sort == "name")
MenuDefaults.itemColors(MaterialTheme.colorScheme.primary)
else MenuDefaults.itemColors(),
leadingIcon = {
Icon(
Icons.Rounded.SortByAlpha,
contentDescription = "Sort by name"
)
},
trailingIcon = {
if (uiState.filter.sort == "name") {
if (uiState.filter.reverse) {
Icon(
Icons.Rounded.ArrowDropDown,
contentDescription = "Descending"
)
} else {
Icon(
Icons.Rounded.ArrowDropUp,
contentDescription = "Ascending"
)
}
}
},
onClick = {
homeViewModel.updateSort(
uiState.filter.copy(
sort = "name",
reverse = if (uiState.filter.sort == "name") !uiState.filter.reverse else uiState.filter.reverse
)
)
showSortMenu = false
})
DropdownMenuItem(
text = { Text(text = "Added On") },
colors = if (uiState.filter.sort == "added_on")
MenuDefaults.itemColors(MaterialTheme.colorScheme.primary)
else MenuDefaults.itemColors(),
leadingIcon = {
Icon(
Icons.Rounded.AccessTime,
contentDescription = "Sort by add time"
)
},
trailingIcon = {
if (uiState.filter.sort == "added_on") {
if (uiState.filter.reverse) {
Icon(
Icons.Rounded.ArrowDropDown,
contentDescription = "Descending"
)
} else {
Icon(
Icons.Rounded.ArrowDropUp,
contentDescription = "Ascending"
)
}
}
},
onClick = {
homeViewModel.updateSort(
uiState.filter.copy(
sort = "added_on",
reverse = if (uiState.filter.sort == "added_on") !uiState.filter.reverse else uiState.filter.reverse
)
)
showSortMenu = false
})
DropdownMenuItem(
text = { Text(text = "Download Speed") },
colors = if (uiState.filter.sort == "dlspeed")
MenuDefaults.itemColors(MaterialTheme.colorScheme.primary)
else MenuDefaults.itemColors(),
leadingIcon = {
Icon(
Icons.Rounded.Speed,
contentDescription = "Sort by download speed"
)
},
trailingIcon = {
if (uiState.filter.sort == "dlspeed") {
if (uiState.filter.reverse) {
Icon(
Icons.Rounded.ArrowDropDown,
contentDescription = "Descending"
)
} else {
Icon(
Icons.Rounded.ArrowDropUp,
contentDescription = "Ascending"
)
}
}
},
onClick = {
homeViewModel.updateSort(
uiState.filter.copy(
sort = "dlspeed",
reverse = if (uiState.filter.sort == "dlspeed") !uiState.filter.reverse else uiState.filter.reverse
)
)
showSortMenu = false
})
}
}

BottomAppBar(
actions = {
IconButton(onClick = { /* do something */ }, enabled = false) {
Icon(Icons.Rounded.Search, contentDescription = "search")
}
IconButton(onClick = { showFilterSheet = true }) {
Icon(
Icons.Rounded.FilterList,
contentDescription = "filter"
)
}
IconButton(onClick = { showSortMenu = true }) {
Icon(
Icons.Rounded.SwapVert,
contentDescription = "sort"
)
}
},
floatingActionButton = {
FloatingActionButton(
onClick = { },
containerColor = BottomAppBarDefaults.bottomAppBarFabColor,
elevation = FloatingActionButtonDefaults.bottomAppBarFabElevation()
) {
Icon(Icons.Rounded.Add, "Add torrent")
}
}
)
}
}

@Composable
private fun BlankAccount(
Expand Down

0 comments on commit 83401b6

Please sign in to comment.