Skip to content

Commit

Permalink
Merge pull request #60 from leandroBorgesFerreira/TopBar
Browse files Browse the repository at this point in the history
TopBar for notes
  • Loading branch information
leandroBorgesFerreira authored Jul 5, 2023
2 parents cfed95c + 648eb20 commit 8340479
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,16 +58,21 @@ fun NavigationGraph() {
val notesUseCase = NotesUseCase(repository, sharedPreferences)
val chooseNoteViewModel = ChooseNoteViewModel(notesUseCase)

ChooseNoteScreen(chooseNoteViewModel = chooseNoteViewModel) { noteId ->
navController.navigate("${Destinations.NOTE_DETAILS.id}/$noteId")
ChooseNoteScreen(chooseNoteViewModel = chooseNoteViewModel) { noteId, noteTitle ->
navController.navigate(
"${Destinations.NOTE_DETAILS.id}/$noteId/$noteTitle"
)
}
}

composable(
route = "${Destinations.NOTE_DETAILS.id}/{noteId}",
route = "${Destinations.NOTE_DETAILS.id}/{noteId}/{noteTitle}",
arguments = listOf(navArgument("noteId") { type = NavType.StringType })
) { backStackEntry ->
backStackEntry.arguments?.getString("noteId")?.let { id ->
val noteId = backStackEntry.arguments?.getString("noteId")
val noteTitle = backStackEntry.arguments?.getString("noteTitle")

if (noteId != null && noteTitle != null) {
val repository = DocumentRepositoryImpl(
database.documentDao(),
database.storyUnitDao()
Expand All @@ -82,7 +87,17 @@ fun NavigationGraph() {
)
)

NoteDetailsScreen(id.takeIf { it != "null" }, noteDetailsViewModel)
NoteDetailsScreen(

noteId.takeIf { it != "null" },
noteTitle.takeIf { it != "null" },
noteDetailsViewModel,
navigateBack = {
navController.navigateUp()
}
)
} else {
throw IllegalArgumentException("Wrong route!")
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,6 @@ import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
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.draw.clip
Expand Down Expand Up @@ -64,7 +61,7 @@ private fun previewDrawers(): Map<String, StoryUnitDrawer> =
@Composable
fun ChooseNoteScreen(
chooseNoteViewModel: ChooseNoteViewModel,
navigateToNote: (String?) -> Unit
navigateToNote: (String?, String?) -> Unit
) {
chooseNoteViewModel.requestDocuments()

Expand All @@ -73,7 +70,10 @@ fun ChooseNoteScreen(
topBar = {
TopAppBar(
title = {
Text(text = "StoryTeller")
Text(
text = "StoryTeller",
color = MaterialTheme.colorScheme.onPrimary
)
},
actions = {
Icon(
Expand All @@ -91,7 +91,7 @@ fun ChooseNoteScreen(
floatingActionButton = {
FloatingActionButton(
containerColor = MaterialTheme.colorScheme.primary,
onClick = { navigateToNote(null) },
onClick = { navigateToNote(null, null) },
content = {
Icon(
imageVector = Icons.Default.Add,
Expand All @@ -114,7 +114,7 @@ fun ChooseNoteScreen(
@Composable
private fun Content(
chooseNoteViewModel: ChooseNoteViewModel,
navigateToNote: (String) -> Unit,
navigateToNote: (String, String) -> Unit,
paddingValues: PaddingValues,
) {
Box(
Expand All @@ -137,7 +137,10 @@ private fun Content(
}

@Composable
private fun Notes(chooseNoteViewModel: ChooseNoteViewModel, navigateToNote: (String) -> Unit) {
private fun Notes(
chooseNoteViewModel: ChooseNoteViewModel,
navigateToNote: (String, String) -> Unit
) {
when (val documents =
chooseNoteViewModel.documentsState.collectAsStateWithLifecycle().value) {
is ResultData.Complete -> {
Expand Down Expand Up @@ -186,7 +189,10 @@ private fun Notes(chooseNoteViewModel: ChooseNoteViewModel, navigateToNote: (Str

@OptIn(ExperimentalFoundationApi::class)
@Composable
private fun LazyGridNotes(documents: List<DocumentCard>, onDocumentClick: (String) -> Unit) {
private fun LazyGridNotes(
documents: List<DocumentCard>,
onDocumentClick: (String, String) -> Unit
) {
LazyVerticalStaggeredGrid(
modifier = Modifier.padding(6.dp),
columns = StaggeredGridCells.Adaptive(minSize = 150.dp),
Expand All @@ -200,7 +206,10 @@ private fun LazyGridNotes(documents: List<DocumentCard>, onDocumentClick: (Strin
}

@Composable
private fun LazyColumnNotes(documents: List<DocumentCard>, onDocumentClick: (String) -> Unit) {
private fun LazyColumnNotes(
documents: List<DocumentCard>,
onDocumentClick: (String, String) -> Unit
) {
LazyColumn(
modifier = Modifier.padding(6.dp),
verticalArrangement = Arrangement.spacedBy(6.dp),
Expand All @@ -215,15 +224,15 @@ private fun LazyColumnNotes(documents: List<DocumentCard>, onDocumentClick: (Str
@Composable
private fun DocumentItem(
documentCard: DocumentCard,
documentClick: (String) -> Unit,
documentClick: (String, String) -> Unit,
drawers: Map<String, StoryUnitDrawer>,
) {
Card(
modifier = Modifier
.fillMaxWidth()
.padding(bottom = 6.dp)
.clickable {
documentClick(documentCard.documentId)
documentClick(documentCard.documentId, documentCard.title)
},
shape = RoundedCornerShape(12.dp)
) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import androidx.compose.animation.AnimatedContent
import androidx.compose.animation.ExperimentalAnimationApi
import androidx.compose.animation.core.tween
import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.animation.with
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.isSystemInDarkTheme
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.ColumnScope
Expand All @@ -18,11 +18,17 @@ import androidx.compose.foundation.layout.imePadding
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.lazy.LazyListState
import androidx.compose.foundation.lazy.rememberLazyListState
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.foundation.shape.CornerSize
import androidx.compose.foundation.shape.RoundedCornerShape
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.filled.ArrowBack
import androidx.compose.material3.ExperimentalMaterial3Api
import androidx.compose.material3.Icon
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Scaffold
import androidx.compose.material3.Text
import androidx.compose.material3.TopAppBar
import androidx.compose.runtime.Composable
import androidx.compose.runtime.DisposableEffect
import androidx.compose.runtime.LaunchedEffect
Expand All @@ -37,6 +43,7 @@ import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleEventObserver
import androidx.lifecycle.LifecycleOwner
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import androidx.navigation.compose.rememberNavController
import br.com.leandroferreira.app_sample.R
import br.com.leandroferreira.app_sample.screens.note.input.InputScreen
import br.com.leandroferreira.app_sample.theme.BACKGROUND_VARIATION
Expand All @@ -49,7 +56,14 @@ import java.util.UUID

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun NoteDetailsScreen(documentId: String?, noteDetailsViewModel: NoteDetailsViewModel) {
fun NoteDetailsScreen(
documentId: String?,
title: String?,
noteDetailsViewModel: NoteDetailsViewModel,
navigateBack: () -> Unit,
) {
val navController = rememberNavController()

if (documentId != null) {
noteDetailsViewModel.requestDocumentContent(documentId)
} else {
Expand All @@ -59,7 +73,30 @@ fun NoteDetailsScreen(documentId: String?, noteDetailsViewModel: NoteDetailsView
)
}

Scaffold { paddingValues ->
Scaffold(
topBar = {
TopAppBar(
title = {
Text(
text = title?.takeIf { it.isNotBlank() }
?: stringResource(id = R.string.note),
color = MaterialTheme.colorScheme.onPrimary
)
},
navigationIcon = {
Icon(
modifier = Modifier
.clip(CircleShape)
.clickable(onClick = navigateBack)
.padding(10.dp),
imageVector = Icons.Filled.ArrowBack,
contentDescription = stringResource(R.string.back),
tint = MaterialTheme.colorScheme.onPrimary
)
}
)
},
) { paddingValues ->
Column(
modifier = Modifier
.padding(top = paddingValues.calculateTopPadding())
Expand Down Expand Up @@ -128,7 +165,7 @@ fun BottomScreen(noteDetailsViewModel: NoteDetailsViewModel) {
initialOffsetY = { fullHeight -> fullHeight }
) + fadeIn() with slideOutVertically(
animationSpec = tween(durationMillis = 130),
targetOffsetY = { fullHeight -> fullHeight }
targetOffsetY = { fullHeight -> fullHeight }
)
}
) { isEdit ->
Expand Down
1 change: 1 addition & 0 deletions app_sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
<string name="last_created">Create</string>
<string name="name">Name</string>
<string name="add_note">Add note</string>
<string name="back">Back</string>
</resources>

0 comments on commit 8340479

Please sign in to comment.