Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions app/src/main/java/dev/scuttle/inventory/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package dev.scuttle.inventory
import android.os.Bundle
import androidx.activity.ComponentActivity
import androidx.activity.compose.setContent
import androidx.activity.enableEdgeToEdge
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.DrawerValue
Expand Down Expand Up @@ -44,6 +45,7 @@ import kotlinx.coroutines.launch
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
enableEdgeToEdge()
setContent {
val themeViewModel: ThemeViewModel = hiltViewModel()
val mode by themeViewModel.mode.collectAsState()
Expand Down Expand Up @@ -161,6 +163,7 @@ private fun InventoryNavHost(
onOpenLocation = { hhId, locId ->
navController.navigate(Routes.location(hhId, locId))
},
onOpenHouseholds = { navController.navigate(Routes.HOUSEHOLDS) },
)
}

Expand Down
11 changes: 8 additions & 3 deletions app/src/main/java/dev/scuttle/inventory/ui/app/AppDrawer.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@ package dev.scuttle.inventory.ui.app

import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.WindowInsets
import androidx.compose.foundation.layout.fillMaxHeight
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.safeDrawing
import androidx.compose.foundation.layout.windowInsetsPadding
import androidx.compose.foundation.layout.width
import androidx.compose.foundation.rememberScrollState
import androidx.compose.foundation.verticalScroll
Expand Down Expand Up @@ -46,8 +49,11 @@ fun AppDrawer(
tonalElevation = DrawerDefaults.ModalDrawerElevation,
shape = DrawerDefaults.shape,
) {
Column(modifier = Modifier.fillMaxHeight()) {
Spacer(Modifier.height(16.dp))
Column(modifier = Modifier
.fillMaxHeight()
.windowInsetsPadding(WindowInsets.safeDrawing)
) {
Spacer(Modifier.height(8.dp))

NavigationDrawerItem(
icon = { Icon(Icons.Default.Home, contentDescription = null) },
Expand Down Expand Up @@ -110,7 +116,6 @@ fun AppDrawer(
modifier = Modifier.padding(horizontal = 12.dp),
)

Spacer(Modifier.height(8.dp))
}
}
}
70 changes: 66 additions & 4 deletions app/src/main/java/dev/scuttle/inventory/ui/auth/AuthScreen.kt
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
package dev.scuttle.inventory.ui.auth

import androidx.compose.foundation.BorderStroke
import androidx.compose.foundation.layout.Arrangement
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.padding
Expand All @@ -10,22 +12,33 @@ import androidx.compose.foundation.text.KeyboardActions
import androidx.compose.foundation.text.KeyboardOptions
import androidx.compose.material3.Button
import androidx.compose.material3.CircularProgressIndicator
import androidx.compose.material3.HorizontalDivider
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.OutlinedTextField
import androidx.compose.material3.Text
import androidx.compose.material3.TextButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.platform.LocalSoftwareKeyboardController
import androidx.compose.ui.text.input.ImeAction
import androidx.compose.ui.text.input.KeyboardCapitalization
import androidx.compose.ui.text.input.KeyboardType
import androidx.compose.ui.text.input.PasswordVisualTransformation
import androidx.compose.ui.unit.dp
import androidx.credentials.CredentialManager
import androidx.credentials.GetCredentialRequest
import androidx.credentials.exceptions.GetCredentialCancellationException
import androidx.hilt.navigation.compose.hiltViewModel
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
import dev.scuttle.inventory.BuildConfig
import kotlinx.coroutines.launch

@Composable
fun AuthScreen(
Expand All @@ -35,6 +48,35 @@ fun AuthScreen(
val state by viewModel.state.collectAsState()
val isRegister = state.mode == AuthMode.REGISTER
val keyboardController = LocalSoftwareKeyboardController.current
val context = LocalContext.current
val scope = rememberCoroutineScope()

fun launchGoogleSignIn() {
if (BuildConfig.GOOGLE_CLIENT_ID.isBlank()) {
viewModel.onGoogleError("Google sign-in is not configured yet (no client ID).")
return
}
scope.launch {
runCatching {
val credentialManager = CredentialManager.create(context)
val googleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setServerClientId(BuildConfig.GOOGLE_CLIENT_ID)
.setAutoSelectEnabled(false)
.build()
val request = GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build()
val result = credentialManager.getCredential(context = context, request = request)
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(result.credential.data)
viewModel.loginWithGoogle(googleIdTokenCredential.idToken)
}.onFailure { e ->
if (e !is GetCredentialCancellationException) {
viewModel.onGoogleError(e.message ?: "Google sign-in failed.")
}
}
}
}

Column(
modifier = modifier
Expand Down Expand Up @@ -94,7 +136,7 @@ fun AuthScreen(

Button(
onClick = { keyboardController?.hide(); viewModel.submit() },
enabled = !state.loading && state.email.isNotBlank() && state.password.isNotBlank(),
enabled = !state.loading && !state.googleLoading && state.email.isNotBlank() && state.password.isNotBlank(),
modifier = Modifier.fillMaxWidth(),
) {
if (state.loading) {
Expand All @@ -104,11 +146,31 @@ fun AuthScreen(
}
}

Row(
verticalAlignment = Alignment.CenterVertically,
horizontalArrangement = Arrangement.spacedBy(8.dp),
modifier = Modifier.fillMaxWidth(),
) {
HorizontalDivider(modifier = Modifier.weight(1f))
Text(text = "or", style = MaterialTheme.typography.bodySmall, color = MaterialTheme.colorScheme.outline)
HorizontalDivider(modifier = Modifier.weight(1f))
}

OutlinedButton(
onClick = { keyboardController?.hide(); launchGoogleSignIn() },
enabled = !state.loading && !state.googleLoading,
border = BorderStroke(1.dp, MaterialTheme.colorScheme.outline),
modifier = Modifier.fillMaxWidth(),
) {
if (state.googleLoading) {
CircularProgressIndicator(modifier = Modifier.size(20.dp))
} else {
Text(text = "Continue with Google")
}
}

TextButton(onClick = viewModel::toggleMode, modifier = Modifier.fillMaxWidth()) {
Text(text = if (isRegister) "Have an account? Sign in" else "New here? Create an account")
}

// TODO: native Google Sign-In (Credential Manager) — needs a Google client ID
// configured; wire its ID token into AuthViewModel/AuthRepository.loginWithGoogle.
}
}
16 changes: 16 additions & 0 deletions app/src/main/java/dev/scuttle/inventory/ui/auth/AuthViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ data class AuthUiState(
val email: String = "",
val password: String = "",
val loading: Boolean = false,
val googleLoading: Boolean = false,
val error: String? = null,
val authenticated: Boolean = false,
)
Expand Down Expand Up @@ -48,6 +49,21 @@ class AuthViewModel @Inject constructor(
}
}

fun onGoogleError(message: String) = _state.update { it.copy(googleLoading = false, error = message) }

fun loginWithGoogle(idToken: String) {
viewModelScope.launch {
_state.update { it.copy(googleLoading = true, error = null) }
val result = runCatching { repository.loginWithGoogle(idToken) }
_state.update { state ->
result.fold(
onSuccess = { state.copy(googleLoading = false, authenticated = true) },
onFailure = { error -> state.copy(googleLoading = false, error = error.message ?: "Google sign-in failed.") },
)
}
}
}

fun submit() {
val current = _state.value
viewModelScope.launch {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,15 @@ import androidx.compose.material.icons.filled.Menu
import androidx.compose.material.icons.filled.Refresh
import androidx.compose.material.icons.filled.Star
import androidx.compose.material.icons.filled.Warning
import androidx.compose.material3.AlertDialog
import androidx.compose.material3.Button
import androidx.compose.material3.Card
import androidx.compose.material3.CardDefaults
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.OutlinedButton
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
Expand All @@ -44,11 +47,26 @@ fun DashboardScreen(
modifier: Modifier = Modifier,
onOpenDrawer: () -> Unit = {},
onOpenLocation: (householdId: Long, locationId: Long) -> Unit = { _, _ -> },
onOpenHouseholds: () -> Unit = {},
viewModel: DashboardViewModel = hiltViewModel(),
) {
val state by viewModel.state.collectAsState()
val primaryColor = MaterialTheme.colorScheme.primary

if (state.hasNoHouseholds && !state.loading) {
AlertDialog(
onDismissRequest = {},
title = { Text("Welcome!") },
text = { Text("You're not part of any household yet. Create a new one or join an existing one with an invite code.") },
confirmButton = {
Button(onClick = onOpenHouseholds) { Text("Create household") }
},
dismissButton = {
OutlinedButton(onClick = onOpenHouseholds) { Text("Join with invite") }
},
)
}

Scaffold(
modifier = modifier,
topBar = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ data class LocationStats(val location: LocationDto, val householdId: Long, val p

data class DashboardUiState(
val loading: Boolean = false,
val hasNoHouseholds: Boolean = false,
val totalLocations: Int = 0,
val totalShelves: Int = 0,
val totalProducts: Int = 0,
Expand Down Expand Up @@ -77,6 +78,7 @@ class DashboardViewModel @Inject constructor(
_state.update {
it.copy(
loading = false,
hasNoHouseholds = households.isEmpty(),
totalLocations = locationStats.size,
totalShelves = totalShelves,
totalProducts = totalProducts,
Expand Down
Loading