Skip to content

Commit

Permalink
Navigate to next screen from composable (#88)
Browse files Browse the repository at this point in the history
* Navigate to next screen from composable

* Rename method to match other screens naming convention
  • Loading branch information
marinacoelho authored Oct 23, 2024
1 parent 1a91c88 commit 119f993
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ fun NavGraphBuilder.notesGraph(appState: NotesAppState) {

composable(SIGN_IN_SCREEN) {
SignInScreen(
onSignUpClicked = { route -> appState.navigate(route) },
openScreen = { route -> appState.navigate(route) },
openAndPopUp = { route, popUp -> appState.navigateAndPopUp(route, popUp) }
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.hilt.navigation.compose.hiltViewModel
import com.notes.app.R
import com.notes.app.SIGN_UP_SCREEN
import com.notes.app.screens.authentication.AuthenticationButton
import com.notes.app.screens.authentication.launchCredManBottomSheet
import com.notes.app.ui.theme.NotesTheme
Expand All @@ -46,7 +47,7 @@ import com.notes.app.ui.theme.Purple40
@Composable
@OptIn(ExperimentalMaterial3Api::class)
fun SignInScreen(
onSignUpClicked: (String) -> Unit,
openScreen: (String) -> Unit,
openAndPopUp: (String, String) -> Unit,
modifier: Modifier = Modifier,
viewModel: SignInViewModel = hiltViewModel()
Expand Down Expand Up @@ -159,7 +160,7 @@ fun SignInScreen(
.fillMaxWidth()
.padding(8.dp))

TextButton(onClick = { viewModel.onSignUpClick(onSignUpClicked) }) {
TextButton(onClick = { openScreen(SIGN_UP_SCREEN) }) {
Text(text = stringResource(R.string.sign_up_description), fontSize = 16.sp, color = Purple40)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential.Co
import com.notes.app.ERROR_TAG
import com.notes.app.SIGN_IN_SCREEN
import com.notes.app.NOTES_LIST_SCREEN
import com.notes.app.SIGN_UP_SCREEN
import com.notes.app.UNEXPECTED_CREDENTIAL
import com.notes.app.model.service.AccountService
import com.notes.app.screens.NotesAppViewModel
Expand Down Expand Up @@ -55,8 +54,4 @@ class SignInViewModel @Inject constructor(
}
}
}

fun onSignUpClick(openScreen: (String) -> Unit) {
openScreen(SIGN_UP_SCREEN)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ import androidx.compose.ui.res.stringResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.notes.app.ACCOUNT_CENTER_SCREEN
import com.notes.app.NOTE_DEFAULT_ID
import com.notes.app.NOTE_ID
import com.notes.app.NOTE_SCREEN
import com.notes.app.R
import com.notes.app.model.Note
import com.notes.app.model.getTitle
Expand All @@ -54,7 +58,7 @@ fun NotesListScreen(
Scaffold(
floatingActionButton = {
FloatingActionButton(
onClick = { viewModel.onAddClick(openScreen) },
onClick = { openScreen("$NOTE_SCREEN?$NOTE_ID=$NOTE_DEFAULT_ID") },
modifier = modifier.padding(16.dp),
containerColor = Purple40,
shape = RoundedCornerShape(16.dp)
Expand All @@ -71,7 +75,7 @@ fun NotesListScreen(
TopAppBar(
title = { Text(stringResource(R.string.app_name)) },
actions = {
IconButton(onClick = { viewModel.onAccountCenterClick(openScreen) }) {
IconButton(onClick = { openScreen(ACCOUNT_CENTER_SCREEN) }) {
Icon(Icons.Filled.Person, "Account center")
}
}
Expand All @@ -90,7 +94,7 @@ fun NotesListScreen(
items(notes, key = { it.id }) { noteItem ->
NoteItem(
note = noteItem,
onActionClick = { viewModel.onNoteClick(openScreen, noteItem) }
onActionClick = { openScreen("$NOTE_SCREEN?$NOTE_ID=${noteItem.id}") }
)
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
package com.notes.app.screens.notes_list

import com.notes.app.ACCOUNT_CENTER_SCREEN
import com.notes.app.NOTE_DEFAULT_ID
import com.notes.app.NOTE_ID
import com.notes.app.NOTE_SCREEN
import com.notes.app.SPLASH_SCREEN
import com.notes.app.model.Note
import com.notes.app.model.service.AccountService
import com.notes.app.model.service.StorageService
import com.notes.app.screens.NotesAppViewModel
Expand All @@ -26,16 +21,4 @@ class NotesListViewModel @Inject constructor(
}
}
}

fun onAddClick(openScreen: (String) -> Unit) {
openScreen("$NOTE_SCREEN?$NOTE_ID=$NOTE_DEFAULT_ID")
}

fun onNoteClick(openScreen: (String) -> Unit, note: Note) {
openScreen("$NOTE_SCREEN?$NOTE_ID=${note.id}")
}

fun onAccountCenterClick(openScreen: (String) -> Unit) {
openScreen(ACCOUNT_CENTER_SCREEN)
}
}

0 comments on commit 119f993

Please sign in to comment.