Skip to content

Commit

Permalink
Fix search behaviour and some code style issues.
Browse files Browse the repository at this point in the history
  • Loading branch information
Swordfish90 committed May 8, 2024
1 parent 14ea60a commit fc23c42
Show file tree
Hide file tree
Showing 16 changed files with 178 additions and 207 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,9 @@ private fun HomeRow(
val game = games[index]
LemuroidGameCard(
modifier =
Modifier
.widthIn(0.dp, 144.dp)
.animateItemPlacement(),
Modifier
.widthIn(0.dp, 144.dp)
.animateItemPlacement(),
game = game,
onClick = { onGameClicked(game) },
onLongClick = { onGameLongClick(game) },
Expand All @@ -181,15 +181,15 @@ private fun HomeNotification(
) {
ElevatedCard(
modifier =
Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp),
Modifier
.fillMaxWidth()
.padding(start = 16.dp, end = 16.dp),
) {
Column(
modifier =
Modifier
.fillMaxWidth()
.padding(16.dp),
Modifier
.fillMaxWidth()
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(16.dp),
) {
Text(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import androidx.lifecycle.viewModelScope
import com.swordfish.lemuroid.app.shared.library.PendingOperationsMonitor
import com.swordfish.lemuroid.app.shared.settings.SettingsInteractor
import com.swordfish.lemuroid.app.shared.settings.StorageFrameworkPickerLauncher
import com.swordfish.lemuroid.lib.library.db.RetrogradeDatabase
import com.swordfish.lemuroid.lib.library.db.entity.Game
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import androidx.activity.viewModels
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Scaffold
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
Expand Down Expand Up @@ -117,6 +118,7 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
}
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
private fun MainScreen(navController: NavHostController) {
AppTheme {
Expand Down Expand Up @@ -182,50 +184,55 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
composable(MainRoute.HOME) {
HomeScreen(
modifier = Modifier.padding(padding),
viewModel = viewModel(
factory =
HomeViewModel.Factory(
applicationContext,
retrogradeDb,
viewModel =
viewModel(
factory =
HomeViewModel.Factory(
applicationContext,
retrogradeDb,
),
),
),
onGameClick = onGameClick,
onGameLongClick = onGameLongClick,
)
}
composable(MainRoute.FAVORITES) {
FavoritesScreen(
modifier = Modifier.padding(padding),
viewModel = viewModel(
factory = FavoritesViewModel.Factory(retrogradeDb),
),
viewModel =
viewModel(
factory = FavoritesViewModel.Factory(retrogradeDb),
),
onGameClick = onGameClick,
onGameLongClick = onGameLongClick,
)
}
composable(MainRoute.SEARCH) {
SearchScreen(
modifier = Modifier.padding(padding),
viewModel = viewModel(
factory = SearchViewModel.Factory(retrogradeDb),
),
viewModel =
viewModel(
factory = SearchViewModel.Factory(retrogradeDb),
),
searchQuery = mainUIState.searchQuery,
onGameClick = onGameClick,
onGameLongClick = onGameLongClick,
onGameFavoriteToggle = onGameFavoriteToggle,
onResetSearchQuery = { mainViewModel.changeQueryString("") },
)
}
composable(MainRoute.SYSTEMS) {
MetaSystemsScreen(
modifier = Modifier.padding(padding),
navController = navController,
viewModel = viewModel(
factory =
MetaSystemsViewModel.Factory(
retrogradeDb,
applicationContext,
),
),
viewModel =
viewModel(
factory =
MetaSystemsViewModel.Factory(
retrogradeDb,
applicationContext,
),
),
)
}
composable(MainRoute.SYSTEM_GAMES) { entry ->
Expand All @@ -251,16 +258,16 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
viewModel =
viewModel(
factory =
SettingsViewModel.Factory(
applicationContext,
settingsInteractor,
saveSyncManager,
FlowSharedPreferences(
SharedPreferencesHelper.getLegacySharedPreferences(
applicationContext,
SettingsViewModel.Factory(
applicationContext,
settingsInteractor,
saveSyncManager,
FlowSharedPreferences(
SharedPreferencesHelper.getLegacySharedPreferences(
applicationContext,
),
),
),
),
),
navController = navController,
)
Expand All @@ -271,10 +278,10 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
viewModel =
viewModel(
factory =
AdvancedSettingsViewModel.Factory(
applicationContext,
settingsInteractor,
),
AdvancedSettingsViewModel.Factory(
applicationContext,
settingsInteractor,
),
),
navController = navController,
)
Expand All @@ -294,10 +301,10 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
viewModel =
viewModel(
factory =
CoresSelectionViewModel.Factory(
applicationContext,
coresSelection,
),
CoresSelectionViewModel.Factory(
applicationContext,
coresSelection,
),
),
)
}
Expand All @@ -320,10 +327,10 @@ class MainActivity : RetrogradeComponentActivity(), BusyActivity {
viewModel =
viewModel(
factory =
SaveSyncSettingsViewModel.Factory(
application,
saveSyncManager,
),
SaveSyncSettingsViewModel.Factory(
application,
saveSyncManager,
),
),
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ fun MainNavigationBar(
AnimatedVisibility(
visible = currentRoute?.showBottomNavigation != false,
enter = expandVertically(),
exit = shrinkVertically()
exit = shrinkVertically(),
) {
LemuroidNavigationBar(currentRoute, navController)
}
Expand All @@ -31,7 +31,7 @@ fun MainNavigationBar(
@Composable
private fun LemuroidNavigationBar(
currentRoute: MainRoute?,
navController: NavHostController
navController: NavHostController,
) {
NavigationBar(modifier = Modifier.fillMaxWidth()) {
MainNavigationRoutes.values().forEach { destination ->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ package com.swordfish.lemuroid.app.mobile.feature.main

import android.content.Context
import androidx.compose.animation.AnimatedVisibility
import androidx.compose.animation.Crossfade
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.ArrowBack
import androidx.compose.material.icons.filled.Search
import androidx.compose.material.icons.outlined.CloudSync
import androidx.compose.material.icons.outlined.Info
import androidx.compose.material.icons.outlined.Settings
Expand All @@ -21,13 +27,22 @@ import androidx.compose.material3.LinearProgressIndicator
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.material3.TextFieldDefaults
import androidx.compose.material3.TopAppBar
import androidx.compose.material3.TopAppBarDefaults
import androidx.compose.material3.surfaceColorAtElevation
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.focus.FocusRequester
import androidx.compose.ui.focus.focusRequester
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalFocusManager
import androidx.compose.ui.res.stringResource
import androidx.compose.ui.unit.dp
import androidx.navigation.NavController
import androidx.navigation.NavHostController
import com.swordfish.lemuroid.R
Expand All @@ -42,22 +57,13 @@ fun MainTopBar(
mainUIState: MainViewModel.UiState,
) {
Column {
Surface(tonalElevation = BottomAppBarDefaults.ContainerElevation) {
Crossfade(
targetState = currentRoute,
label = "MainTopBar"
) { route ->
when (route) {
MainRoute.SEARCH -> SearchTopBar(
navController = navController,
onHelpPressed = onHelpPressed,
mainUIState = mainUIState,
onUpdateQueryString = onUpdateQueryString,
)
else -> LemuroidTopAppBar(route, navController, mainUIState, onHelpPressed)
}
}
}
LemuroidTopAppBar(
route = currentRoute,
navController = navController,
mainUIState = mainUIState,
onHelpPressed = onHelpPressed,
onUpdateQueryString = onUpdateQueryString,
)

AnimatedVisibility(mainUIState.operationInProgress) {
LinearProgressIndicator(modifier = Modifier.fillMaxWidth())
Expand All @@ -72,6 +78,7 @@ fun LemuroidTopAppBar(
navController: NavController,
mainUIState: MainViewModel.UiState,
onHelpPressed: () -> Unit,
onUpdateQueryString: (String) -> Unit,
) {
val context = LocalContext.current
val topBarColor =
Expand All @@ -80,15 +87,24 @@ fun LemuroidTopAppBar(
)

TopAppBar(
title = { Text(text = stringResource(route.titleId)) },
title = {
if (route == MainRoute.SEARCH) {
LemuroidSearchView(
mainUIState = mainUIState,
onUpdateQueryString = onUpdateQueryString,
)
} else {
Text(text = stringResource(route.titleId))
}
},
colors =
TopAppBarDefaults.topAppBarColors(
scrolledContainerColor = topBarColor,
containerColor = topBarColor,
),
TopAppBarDefaults.topAppBarColors(
scrolledContainerColor = topBarColor,
containerColor = topBarColor,
),
navigationIcon = {
AnimatedVisibility(
visible = route?.parent != null,
visible = route.parent != null,
enter = fadeIn(),
exit = fadeOut(),
) {
Expand Down Expand Up @@ -154,3 +170,55 @@ fun LemuroidTopBarActions(
}
}
}

@Composable
private fun LemuroidSearchView(
mainUIState: MainViewModel.UiState,
onUpdateQueryString: (String) -> Unit,
) {
val focusRequester = remember { FocusRequester() }
val focusManager = LocalFocusManager.current

LaunchedEffect(Unit) {
focusRequester.requestFocus()
}

Box(
modifier =
Modifier
.fillMaxWidth()
.height(56.dp),
) {
Surface(
modifier =
Modifier
.fillMaxSize()
.padding(top = 8.dp, bottom = 8.dp, end = 8.dp),
shape = RoundedCornerShape(100),
tonalElevation = 16.dp,
) { }

TextField(
value = mainUIState.searchQuery,
modifier =
Modifier
.fillMaxSize()
.focusRequester(focusRequester),
textStyle = MaterialTheme.typography.bodyMedium,
leadingIcon = { Icon(Icons.Default.Search, contentDescription = null) },
onValueChange = { onUpdateQueryString(it) },
singleLine = true,
keyboardActions =
KeyboardActions(
onDone = { focusManager.clearFocus(true) },
),
colors =
TextFieldDefaults.colors(
focusedContainerColor = Color.Transparent,
unfocusedContainerColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
),
)
}
}

0 comments on commit fc23c42

Please sign in to comment.