-
-
Notifications
You must be signed in to change notification settings - Fork 22
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Refactor: Extract Multiple Actions Logic
This commit refactors the logic for handling multiple actions in the bottom navigation bar by extracting it into a separate composable function called `MultipleActions`. This change improves code organization and readability by isolating the multiple actions functionality into a dedicated component. It also introduces a new state class, `MultipleBarState`, to manage the state of the multiple actions bar, further enhancing modularity and maintainability.
- Loading branch information
jacobrein
committed
Oct 18, 2024
1 parent
720cc9d
commit ad73c29
Showing
2 changed files
with
158 additions
and
145 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
UIViews/src/main/java/com/programmersbox/uiviews/utils/components/MultipleActions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
package com.programmersbox.uiviews.utils.components | ||
|
||
import androidx.compose.animation.AnimatedVisibility | ||
import androidx.compose.animation.fadeIn | ||
import androidx.compose.animation.fadeOut | ||
import androidx.compose.animation.slideInVertically | ||
import androidx.compose.animation.slideOutVertically | ||
import androidx.compose.foundation.layout.BoxScope | ||
import androidx.compose.foundation.layout.WindowInsets | ||
import androidx.compose.foundation.layout.navigationBars | ||
import androidx.compose.foundation.layout.offset | ||
import androidx.compose.foundation.layout.width | ||
import androidx.compose.foundation.layout.windowInsetsPadding | ||
import androidx.compose.material.icons.Icons | ||
import androidx.compose.material.icons.filled.UnfoldLess | ||
import androidx.compose.material.icons.filled.UnfoldMore | ||
import androidx.compose.material3.ExperimentalMaterial3ExpressiveApi | ||
import androidx.compose.material3.FilledIconButton | ||
import androidx.compose.material3.FloatingAppBarDefaults | ||
import androidx.compose.material3.HorizontalFloatingAppBar | ||
import androidx.compose.material3.Icon | ||
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.rememberCoroutineScope | ||
import androidx.compose.runtime.setValue | ||
import androidx.compose.ui.Alignment | ||
import androidx.compose.ui.Modifier | ||
import androidx.compose.ui.unit.dp | ||
import androidx.navigation.NavDestination | ||
import androidx.navigation.NavHostController | ||
import com.programmersbox.uiviews.MiddleMultipleActions | ||
import com.programmersbox.uiviews.MiddleNavigationAction | ||
import com.programmersbox.uiviews.utils.customsettings.ScreenBottomItem | ||
import com.programmersbox.uiviews.utils.customsettings.item | ||
import kotlinx.coroutines.delay | ||
import kotlinx.coroutines.launch | ||
|
||
@Composable | ||
fun rememberMultipleBarState( | ||
hideOnClick: Boolean = true, | ||
) = remember(hideOnClick) { MultipleBarState(hideOnClick) } | ||
|
||
class MultipleBarState( | ||
hideOnClick: Boolean = true, | ||
) { | ||
var expanded by mutableStateOf(false) | ||
private set | ||
var showHorizontalBar by mutableStateOf(false) | ||
private set | ||
|
||
var hideOnClick by mutableStateOf(hideOnClick) | ||
|
||
suspend fun show() { | ||
expanded = false | ||
showHorizontalBar = true | ||
delay(250) | ||
expanded = true | ||
} | ||
|
||
suspend fun hide() { | ||
expanded = false | ||
delay(250) | ||
showHorizontalBar = false | ||
} | ||
} | ||
|
||
@OptIn(ExperimentalMaterial3ExpressiveApi::class) | ||
@Composable | ||
fun BoxScope.MultipleActions( | ||
state: MultipleBarState, | ||
middleNavItem: MiddleNavigationAction, | ||
multipleActions: MiddleMultipleActions, | ||
currentDestination: NavDestination?, | ||
navController: NavHostController, | ||
modifier: Modifier = Modifier, | ||
) { | ||
val scope = rememberCoroutineScope() | ||
if (middleNavItem == MiddleNavigationAction.Multiple) { | ||
AnimatedVisibility( | ||
visible = state.showHorizontalBar, | ||
enter = slideInVertically( | ||
animationSpec = MaterialTheme.motionScheme.fastSpatialSpec() | ||
) { it / 2 } + fadeIn( | ||
animationSpec = MaterialTheme.motionScheme.fastSpatialSpec() | ||
), | ||
exit = slideOutVertically( | ||
animationSpec = MaterialTheme.motionScheme.fastSpatialSpec() | ||
) { it / 2 } + fadeOut( | ||
animationSpec = MaterialTheme.motionScheme.fastSpatialSpec() | ||
), | ||
modifier = modifier | ||
.align(Alignment.BottomCenter) | ||
.windowInsetsPadding(WindowInsets.navigationBars) | ||
.offset(y = -FloatingAppBarDefaults.ScreenOffset), | ||
) { | ||
HorizontalFloatingAppBar( | ||
expanded = state.expanded, | ||
leadingContent = { | ||
multipleActions.startAction.item?.ScreenBottomItem( | ||
currentDestination = currentDestination, | ||
navController = navController, | ||
additionalOnClick = { scope.launch { if (state.hideOnClick) state.hide() } } | ||
) | ||
}, | ||
trailingContent = { | ||
multipleActions.endAction.item?.ScreenBottomItem( | ||
currentDestination = currentDestination, | ||
navController = navController, | ||
additionalOnClick = { scope.launch { if (state.hideOnClick) state.hide() } } | ||
) | ||
}, | ||
) { | ||
FilledIconButton( | ||
modifier = Modifier.width(64.dp), | ||
onClick = { scope.launch { state.hide() } } | ||
) { | ||
Icon( | ||
if (state.expanded) Icons.Default.UnfoldLess else Icons.Filled.UnfoldMore, | ||
contentDescription = "Localized description" | ||
) | ||
} | ||
} | ||
} | ||
} | ||
} |