Skip to content

Commit

Permalink
refactor: replace Scaffold with internal component
Browse files Browse the repository at this point in the history
  • Loading branch information
andrekir committed Jan 6, 2025
1 parent 1c863f3 commit 7794c08
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 135 deletions.
61 changes: 19 additions & 42 deletions app/src/main/java/com/geeksville/mesh/ui/DebugFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.PaddingValues
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
Expand All @@ -36,13 +35,9 @@ import androidx.compose.foundation.text.selection.SelectionContainer
import androidx.compose.material.Button
import androidx.compose.material.Card
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Surface
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.outlined.CloudDownload
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -64,13 +59,13 @@ import androidx.compose.ui.text.font.FontWeight
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.core.content.ContextCompat
import androidx.fragment.app.Fragment
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.geeksville.mesh.R
import com.geeksville.mesh.database.entity.MeshLog
import com.geeksville.mesh.model.DebugViewModel
import com.geeksville.mesh.ui.components.BaseScaffold
import com.geeksville.mesh.ui.theme.AppTheme
import dagger.hilt.android.AndroidEntryPoint
import java.text.DateFormat
Expand All @@ -85,36 +80,9 @@ class DebugFragment : Fragment() {
): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setBackgroundColor(ContextCompat.getColor(context, R.color.colorAdvancedBackground))
setContent {
val viewModel: DebugViewModel = hiltViewModel()

AppTheme {
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(id = R.string.debug_panel)) },
navigationIcon = {
IconButton(onClick = { parentFragmentManager.popBackStack() }) {
Icon(
Icons.AutoMirrored.Filled.ArrowBack,
stringResource(id = R.string.navigate_back),
)
}
},
actions = {
Button(onClick = viewModel::deleteAllLogs) {
Text(text = stringResource(R.string.clear))
}
}
)
},
) { innerPadding ->
DebugScreen(
viewModel = viewModel,
contentPadding = innerPadding,
)
}
DebugScreen { parentFragmentManager.popBackStack() }
}
}
}
Expand Down Expand Up @@ -185,7 +153,7 @@ private fun Int.asNodeId(): String {
@Composable
internal fun DebugScreen(
viewModel: DebugViewModel = hiltViewModel(),
contentPadding: PaddingValues,
navigateUp: () -> Unit
) {
val listState = rememberLazyListState()
val logs by viewModel.meshLog.collectAsStateWithLifecycle()
Expand All @@ -199,13 +167,22 @@ internal fun DebugScreen(
}
}

SelectionContainer {
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
contentPadding = contentPadding,
) {
items(logs, key = { it.uuid }) { log -> DebugItem(annotateMeshLog(log)) }
BaseScaffold(
title = stringResource(id = R.string.debug_panel),
navigateUp = navigateUp,
actions = {
Button(onClick = viewModel::deleteAllLogs) {
Text(text = stringResource(R.string.clear))
}
}
) {
SelectionContainer {
LazyColumn(
modifier = Modifier.fillMaxSize(),
state = listState,
) {
items(logs, key = { it.uuid }) { log -> DebugItem(annotateMeshLog(log)) }
}
}
}
}
Expand Down
62 changes: 13 additions & 49 deletions app/src/main/java/com/geeksville/mesh/ui/NavGraph.kt
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,7 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.StringRes
import androidx.compose.foundation.layout.padding
import androidx.compose.material.Icon
import androidx.compose.material.IconButton
import androidx.compose.material.Scaffold
import androidx.compose.material.Text
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.automirrored.filled.Forward
import androidx.compose.material.icons.automirrored.filled.List
import androidx.compose.material.icons.automirrored.filled.Message
Expand Down Expand Up @@ -61,7 +54,6 @@ import androidx.compose.ui.graphics.vector.ImageVector
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.compose.ui.res.stringResource
import androidx.core.content.ContextCompat
import androidx.core.os.bundleOf
import androidx.fragment.app.FragmentManager
import androidx.fragment.app.viewModels
Expand All @@ -77,6 +69,7 @@ import com.geeksville.mesh.R
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.model.MetricsViewModel
import com.geeksville.mesh.model.RadioConfigViewModel
import com.geeksville.mesh.ui.components.BaseScaffold
import com.geeksville.mesh.ui.components.DeviceMetricsScreen
import com.geeksville.mesh.ui.components.EnvironmentMetricsScreen
import com.geeksville.mesh.ui.components.NodeMapScreen
Expand Down Expand Up @@ -143,32 +136,26 @@ class NavGraphFragment : ScreenFragment("NavGraph"), Logging {

return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setBackgroundColor(ContextCompat.getColor(context, R.color.colorAdvancedBackground))
setContent {
val node by model.destNode.collectAsStateWithLifecycle()

AppTheme {
val navController: NavHostController = rememberNavController()
Scaffold(
topBar = {
MeshAppBar(
currentScreen = node?.user?.longName
?: stringResource(R.string.unknown_username),
canNavigateBack = true,
navigateUp = {
if (navController.previousBackStackEntry != null) {
navController.navigateUp()
} else {
parentFragmentManager.popBackStack()
}
},
)
}
) { innerPadding ->
BaseScaffold(
title = node?.user?.longName
?: stringResource(R.string.unknown_username),
canNavigateBack = true,
navigateUp = {
if (navController.previousBackStackEntry != null) {
navController.navigateUp()
} else {
parentFragmentManager.popBackStack()
}
},
) {
NavGraph(
navController = navController,
startDestination = startDestination,
modifier = Modifier.padding(innerPadding),
)
}
}
Expand Down Expand Up @@ -294,29 +281,6 @@ sealed class ResponseState<out T> {
fun isWaiting() = this !is Empty
}

@Composable
private fun MeshAppBar(
currentScreen: String,
canNavigateBack: Boolean,
navigateUp: () -> Unit,
modifier: Modifier = Modifier,
) {
TopAppBar(
title = { Text(currentScreen) },
modifier = modifier,
navigationIcon = {
if (canNavigateBack) {
IconButton(onClick = navigateUp) {
Icon(
imageVector = Icons.AutoMirrored.Filled.ArrowBack,
contentDescription = stringResource(id = R.string.navigate_back),
)
}
}
}
)
}

@Suppress("LongMethod")
@Composable
fun NavGraph(
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/com/geeksville/mesh/ui/NodeDetail.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/

@file:Suppress("TooManyFunctions", "LongMethod")
@file:Suppress("TooManyFunctions")

package com.geeksville.mesh.ui

Expand Down Expand Up @@ -108,7 +108,7 @@ import kotlin.math.ln
fun NodeDetailScreen(
modifier: Modifier = Modifier,
viewModel: MetricsViewModel = hiltViewModel(),
onNavigate: (Any) -> Unit,
onNavigate: (Route) -> Unit,
) {
val state by viewModel.state.collectAsStateWithLifecycle()

Expand All @@ -135,7 +135,7 @@ private fun NodeDetailList(
modifier: Modifier = Modifier,
node: Node,
metricsState: MetricsState,
onNavigate: (Any) -> Unit = {},
onNavigate: (Route) -> Unit = {},
) {
LazyColumn(
modifier = modifier.fillMaxSize(),
Expand Down Expand Up @@ -320,7 +320,7 @@ private fun NodeDetailsContent(
}

@Composable
fun LogNavigationList(state: MetricsState, onNavigate: (Any) -> Unit) {
fun LogNavigationList(state: MetricsState, onNavigate: (Route) -> Unit) {
NavCard(
title = stringResource(R.string.device_metrics_log),
icon = Icons.Default.ChargingStation,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,22 @@ import androidx.compose.material.IconButton
import androidx.compose.material.ListItem
import androidx.compose.material.MaterialTheme
import androidx.compose.material.OutlinedTextField
import androidx.compose.material.Scaffold
import androidx.compose.material.Switch
import androidx.compose.material.Text
import androidx.compose.material.TextButton
import androidx.compose.material.TopAppBar
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Add
import androidx.compose.material.icons.filled.FastForward
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
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.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.focus.onFocusEvent
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.LocalHapticFeedback
Expand All @@ -76,13 +76,13 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.PreviewLightDark
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import androidx.core.content.ContextCompat
import androidx.hilt.navigation.compose.hiltViewModel
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.geeksville.mesh.R
import com.geeksville.mesh.android.Logging
import com.geeksville.mesh.database.entity.QuickChatAction
import com.geeksville.mesh.model.UIViewModel
import com.geeksville.mesh.ui.components.BaseScaffold
import com.geeksville.mesh.ui.components.dragContainer
import com.geeksville.mesh.ui.components.dragDropItemsIndexed
import com.geeksville.mesh.ui.components.rememberDragDropState
Expand All @@ -98,28 +98,9 @@ class QuickChatSettingsFragment : ScreenFragment("Quick Chat Settings"), Logging
): View {
return ComposeView(requireContext()).apply {
setViewCompositionStrategy(ViewCompositionStrategy.DisposeOnViewTreeLifecycleDestroyed)
setBackgroundColor(ContextCompat.getColor(context, R.color.colorAdvancedBackground))
setContent {
AppTheme {
Scaffold(
topBar = {
TopAppBar(
title = { Text(stringResource(id = R.string.quick_chat)) },
navigationIcon = {
IconButton(onClick = { parentFragmentManager.popBackStack() }) {
Icon(
Icons.AutoMirrored.Filled.ArrowBack,
stringResource(id = R.string.navigate_back),
)
}
},
)
},
) { innerPadding ->
QuickChatScreen(
modifier = Modifier.padding(innerPadding)
)
}
QuickChatScreen { parentFragmentManager.popBackStack() }
}
}
}
Expand All @@ -129,7 +110,19 @@ class QuickChatSettingsFragment : ScreenFragment("Quick Chat Settings"), Logging
@Composable
internal fun QuickChatScreen(
viewModel: UIViewModel = hiltViewModel(),
modifier: Modifier = Modifier,
navigateUp: () -> Unit
) {
BaseScaffold(
title = stringResource(id = R.string.quick_chat),
navigateUp = navigateUp,
) {
QuickChatContent(viewModel)
}
}

@Composable
private fun QuickChatContent(
viewModel: UIViewModel = hiltViewModel(),
) {
val actions by viewModel.quickChatActions.collectAsStateWithLifecycle()
var showActionDialog by remember { mutableStateOf<QuickChatAction?>(null) }
Expand All @@ -140,7 +133,7 @@ internal fun QuickChatScreen(
viewModel.updateActionPositions(list)
}

Box(modifier = modifier.fillMaxSize()) {
Box(modifier = Modifier.fillMaxSize()) {
if (showActionDialog != null) {
val action = showActionDialog ?: return
EditQuickChatDialog(
Expand Down Expand Up @@ -212,10 +205,17 @@ private fun EditQuickChatDialog(
onDismiss: () -> Unit,
) {
var actionInput by remember { mutableStateOf(action) }
val newQuickChat = action.uuid == 0L
val newQuickChat = remember { action.uuid == 0L }
val isInstant = actionInput.mode == QuickChatAction.Mode.Instant
val title = if (newQuickChat) R.string.quick_chat_new else R.string.quick_chat_edit

val focusRequester = remember { FocusRequester() }
LaunchedEffect(Unit) {
if (newQuickChat) {
focusRequester.requestFocus()
}
}

AlertDialog(
onDismissRequest = onDismiss,
shape = RoundedCornerShape(16.dp),
Expand Down Expand Up @@ -246,9 +246,11 @@ private fun EditQuickChatDialog(
OutlinedTextFieldWithCounter(
label = stringResource(id = R.string.message),
value = actionInput.message,
maxSize = 235,
maxSize = 200,
getSize = { it.toByteArray().size + 1 },
modifier = Modifier.fillMaxWidth()
modifier = Modifier
.fillMaxWidth()
.focusRequester(focusRequester),
) {
actionInput = actionInput.copy(message = it)
if (newQuickChat) {
Expand Down
Loading

0 comments on commit 7794c08

Please sign in to comment.