arblant
FoxyVPN-MultiAcc / app / src / main / java / com / vauth / foxyvpn / ui / screens / FoxyBottomBar.kt
FoxyBottomBar.kt256 lines · 9.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
package com.vauth.foxyvpn.ui.screens

import androidx.compose.animation.animateColorAsState
import androidx.compose.animation.core.Spring
import androidx.compose.animation.core.animateDpAsState
import androidx.compose.animation.core.spring
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.navigationBarsPadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.AccountCircle
import androidx.compose.material.icons.filled.Home
import androidx.compose.material.icons.filled.Public
import androidx.compose.material.icons.filled.Settings
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.NavigationBar
import androidx.compose.material3.NavigationBarItem
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.semantics.Role
import androidx.compose.ui.unit.dp
import com.vauth.foxyvpn.ui.theme.NavBarStyle

enum class FoxyDestination(val label: String, val icon: ImageVector) {
    Home("Home", Icons.Filled.Home),
    Locations("Locations", Icons.Filled.Public),
    Accounts("Accounts", Icons.Filled.AccountCircle),
    Settings("Settings", Icons.Filled.Settings),
}

/**
 * Bottom navigation in all six ZedSecure nav-bar styles. Design language
 * studied from ZedSecure; implementation is original.
 */
@Composable
fun FoxyBottomBar(
    style: NavBarStyle,
    current: FoxyDestination,
    onNavigate: (FoxyDestination) -> Unit,
    modifier: Modifier = Modifier,
) {
    when (style) {
        NavBarStyle.FloatingPill -> FloatingPillBar(current, onNavigate, large = false, modifier = modifier)
        NavBarStyle.CompactDock -> FloatingPillBar(current, onNavigate, large = false, compact = true, modifier = modifier)
        NavBarStyle.ExpressivePill -> FloatingPillBar(current, onNavigate, large = true, modifier = modifier)
        NavBarStyle.FullBar -> FullNavBar(current, onNavigate, modifier)
        NavBarStyle.Underline -> UnderlineBar(current, onNavigate, modifier)
        NavBarStyle.Minimal -> MinimalBar(current, onNavigate, modifier)
    }
}

@Composable
private fun FloatingPillBar(
    current: FoxyDestination,
    onNavigate: (FoxyDestination) -> Unit,
    large: Boolean,
    compact: Boolean = false,
    modifier: Modifier = Modifier,
) {
    val cs = MaterialTheme.colorScheme
    Box(
        modifier = modifier
            .fillMaxWidth()
            .navigationBarsPadding()
            .padding(horizontal = 24.dp, vertical = 10.dp),
        contentAlignment = Alignment.Center,
    ) {
        Surface(
            shape = RoundedCornerShape(if (compact) 24.dp else 32.dp),
            color = cs.surfaceContainerHigh,
            tonalElevation = 3.dp,
            shadowElevation = 6.dp,
        ) {
            Row(
                modifier = Modifier.padding(
                    horizontal = if (compact) 8.dp else 12.dp,
                    vertical = if (compact) 6.dp else 8.dp,
                ),
                horizontalArrangement = Arrangement.spacedBy(if (compact) 2.dp else 4.dp),
                verticalAlignment = Alignment.CenterVertically,
            ) {
                FoxyDestination.entries.forEach { dest ->
                    val selected = dest == current
                    val bg by animateColorAsState(
                        if (selected) cs.primary else cs.surfaceContainerHigh,
                        tween(250), label = "pill-bg",
                    )
                    val fg by animateColorAsState(
                        if (selected) cs.onPrimary else cs.onSurfaceVariant,
                        tween(250), label = "pill-fg",
                    )
                    val pad by animateDpAsState(
                        if (selected && large) 20.dp else 12.dp,
                        spring(dampingRatio = 0.6f, stiffness = Spring.StiffnessMedium),
                        label = "pill-pad",
                    )
                    Row(
                        modifier = Modifier
                            .clip(RoundedCornerShape(if (compact) 18.dp else 24.dp))
                            .background(bg)
                            .clickable(
                                interactionSource = remember { MutableInteractionSource() },
                                indication = null,
                                role = Role.Tab,
                                onClick = { onNavigate(dest) },
                            )
                            .padding(horizontal = pad, vertical = if (compact) 8.dp else 10.dp),
                        verticalAlignment = Alignment.CenterVertically,
                    ) {
                        Icon(dest.icon, contentDescription = dest.label, tint = fg, modifier = Modifier.size(22.dp))
                        if (selected && large) {
                            Spacer(Modifier.width(8.dp))
                            Text(dest.label, style = MaterialTheme.typography.labelLarge, color = fg)
                        }
                    }
                }
            }
        }
    }
}

@Composable
private fun FullNavBar(
    current: FoxyDestination,
    onNavigate: (FoxyDestination) -> Unit,
    modifier: Modifier = Modifier,
) {
    NavigationBar(modifier = modifier) {
        FoxyDestination.entries.forEach { dest ->
            NavigationBarItem(
                selected = current == dest,
                onClick = { onNavigate(dest) },
                icon = { Icon(dest.icon, contentDescription = dest.label) },
                label = { Text(dest.label) },
            )
        }
    }
}

@Composable
private fun UnderlineBar(
    current: FoxyDestination,
    onNavigate: (FoxyDestination) -> Unit,
    modifier: Modifier = Modifier,
) {
    val cs = MaterialTheme.colorScheme
    Row(
        modifier = modifier
            .fillMaxWidth()
            .navigationBarsPadding()
            .padding(horizontal = 16.dp, vertical = 6.dp),
        horizontalArrangement = Arrangement.SpaceEvenly,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        FoxyDestination.entries.forEach { dest ->
            val selected = dest == current
            val fg by animateColorAsState(
                if (selected) cs.primary else cs.onSurfaceVariant, tween(200), label = "ul-fg",
            )
            val indicatorWidth by animateDpAsState(
                if (selected) 28.dp else 0.dp,
                spring(dampingRatio = 0.6f, stiffness = Spring.StiffnessMedium),
                label = "ul-w",
            )
            Column(
                horizontalAlignment = Alignment.CenterHorizontally,
                modifier = Modifier
                    .clip(RoundedCornerShape(16.dp))
                    .clickable(
                        interactionSource = remember { MutableInteractionSource() },
                        indication = null,
                        role = Role.Tab,
                        onClick = { onNavigate(dest) },
                    )
                    .padding(horizontal = 14.dp, vertical = 8.dp),
            ) {
                Icon(dest.icon, contentDescription = dest.label, tint = fg, modifier = Modifier.size(24.dp))
                Spacer(Modifier.height(4.dp))
                Box(
                    Modifier
                        .width(indicatorWidth)
                        .height(3.dp)
                        .clip(CircleShape)
                        .background(cs.primary),
                )
            }
        }
    }
}

@Composable
private fun MinimalBar(
    current: FoxyDestination,
    onNavigate: (FoxyDestination) -> Unit,
    modifier: Modifier = Modifier,
) {
    val cs = MaterialTheme.colorScheme
    Row(
        modifier = modifier
            .fillMaxWidth()
            .navigationBarsPadding()
            .padding(horizontal = 32.dp, vertical = 10.dp),
        horizontalArrangement = Arrangement.SpaceBetween,
        verticalAlignment = Alignment.CenterVertically,
    ) {
        FoxyDestination.entries.forEach { dest ->
            val selected = dest == current
            val fg by animateColorAsState(
                if (selected) cs.primary else cs.onSurfaceVariant.copy(alpha = 0.6f),
                tween(200), label = "min-fg",
            )
            val scale by animateDpAsState(
                if (selected) 28.dp else 24.dp,
                spring(dampingRatio = 0.55f, stiffness = Spring.StiffnessMedium),
                label = "min-s",
            )
            Icon(
                dest.icon,
                contentDescription = dest.label,
                tint = fg,
                modifier = Modifier
                    .size(scale)
                    .clip(CircleShape)
                    .clickable(
                        interactionSource = remember { MutableInteractionSource() },
                        indication = null,
                        role = Role.Tab,
                        onClick = { onNavigate(dest) },
                    )
                    .padding(2.dp),
            )
        }
    }
}