Skip to content

Commit

Permalink
Working Note feature.
Browse files Browse the repository at this point in the history
  • Loading branch information
kKrzysciak96 committed Nov 7, 2023
1 parent 1ba06d4 commit 6446045
Show file tree
Hide file tree
Showing 10 changed files with 122 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import com.eltescode.core_ui.ui.SilverColors.Companion.color4c4e50


@OptIn(ExperimentalMaterial3Api::class)


@Composable
fun texFieldColors_1(): TextFieldColors {

Expand All @@ -22,6 +20,21 @@ fun texFieldColors_1(): TextFieldColors {
focusedLabelColor = Color.Black,
focusedLeadingIconColor = Color.Black,
cursorColor = Color.Black
)
}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun texFieldColors_2(): TextFieldColors {

return TextFieldDefaults.textFieldColors(
containerColor = Color.Transparent,
focusedIndicatorColor = Color.Transparent,
unfocusedIndicatorColor = Color.Transparent,
unfocusedSupportingTextColor = Color.Black,
textColor = Color.Black,
focusedLabelColor = Color.Black,
focusedLeadingIconColor = Color.Black,
cursorColor = Color.Black
)
}
3 changes: 3 additions & 0 deletions core_ui/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,7 @@
<string name="note_deleted">Note deleted</string>
<string name="undo">Undo</string>
<string name="your_notes">Your notes</string>
<string name="search_photo_hint">Search photo…</string>
<string name="enter_content_hint">Enter some content…</string>
<string name="enter_title_hint">Enter title…</string>
</resources>
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package com.eltescode.notes_presentation.add_edit_note

import android.content.Context
import androidx.compose.animation.Animatable
import androidx.compose.animation.core.Animatable
import androidx.compose.animation.core.AnimationVector4D
import androidx.compose.animation.core.tween
import androidx.compose.foundation.background
import androidx.compose.foundation.border
Expand Down Expand Up @@ -32,10 +35,12 @@ import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.toArgb
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.unit.dp
import androidx.hilt.navigation.compose.hiltViewModel
import com.eltescode.notes_presentation.add_edit_note.components.TransparentTextField
import com.eltescode.notes_presentation.model.NoteDisplayable
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

Expand All @@ -53,6 +58,8 @@ fun AddEditNoteScreen(
remember { Animatable(Color(if (noteColor != -1) noteColor else viewModel.noteColor.value)) }
val scope = rememberCoroutineScope()

val context = LocalContext.current

LaunchedEffect(key1 = Unit, block = {
viewModel.eventFlow.collectLatest { event ->
when (event) {
Expand All @@ -68,8 +75,26 @@ fun AddEditNoteScreen(
}
})

AddEditNoteScreen(
titleState = titleState,
contentState = contentState,
noteBackgroundAnimatable = noteBackgroundAnimatable,
scope = scope,
context = context
)

}

@Composable
private fun AddEditNoteScreen(
titleState: NoteTextFieldState,
contentState: NoteTextFieldState,
viewModel: AddEditNoteViewModel = hiltViewModel(),
noteBackgroundAnimatable: Animatable<Color, AnimationVector4D>,
scope: CoroutineScope,
context: Context

) {
Box(modifier = Modifier.fillMaxSize(), contentAlignment = Alignment.Center) {

Column(
Expand Down Expand Up @@ -110,29 +135,27 @@ fun AddEditNoteScreen(
}
viewModel.onEvent(AddEditNoteEvent.ChangeColor(colorInt))
}

)
}
}
Spacer(modifier = Modifier.height(16.dp))
TransparentTextField(
text = titleState.text,
hint = titleState.hint,
hint = titleState.hint.asString(context),
onValueChange = { viewModel.onEvent(AddEditNoteEvent.EnteredTitle(it)) },
onFocusChange = { viewModel.onEvent(AddEditNoteEvent.ChangeTitleFocus(it)) },
isHintVisible = titleState.isHintVisible,
singleLine = true,
// textStyle = MaterialTheme.typography.h5
)

)
Spacer(modifier = Modifier.height(16.dp))
TransparentTextField(
text = contentState.text,
hint = contentState.hint,
hint = contentState.hint.asString(context),
onValueChange = { viewModel.onEvent(AddEditNoteEvent.EnteredContent(it)) },
onFocusChange = { viewModel.onEvent(AddEditNoteEvent.ChangeContentFocus(it)) },
isHintVisible = contentState.isHintVisible,
singleLine = false,
// textStyle = MaterialTheme.typography.body1,
modifier = Modifier.fillMaxHeight()
)
}
Expand All @@ -146,5 +169,4 @@ fun AddEditNoteScreen(
Icon(imageVector = Icons.Default.Save, contentDescription = null)
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import androidx.compose.ui.graphics.toArgb
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.eltescode.notes.features.note.presentation.add_edit_note.NoteTextFieldState
import com.eltescode.core_ui.R
import com.eltescode.core_ui.utils.UiText
import com.eltescode.notes_domain.model.Note
import com.eltescode.notes_domain.repository.Result
import com.eltescode.notes_domain.use_cases.NoteUseCases
Expand All @@ -30,14 +31,14 @@ class AddEditNoteViewModel @Inject constructor(

private val _noteTitle = mutableStateOf(
NoteTextFieldState(
hint = "Enter title..."
hint = UiText.StringResource(R.string.enter_title_hint)
)
)
val noteTitle: State<NoteTextFieldState> = _noteTitle

private val _noteContent = mutableStateOf(
NoteTextFieldState(
hint = "Enter some content..."
hint = UiText.StringResource(R.string.enter_content_hint)
)
)
val noteContent: State<NoteTextFieldState> = _noteContent
Expand All @@ -57,9 +58,15 @@ class AddEditNoteViewModel @Inject constructor(
noteUseCases.getNoteUseCase(UUID.fromString(noteId))?.also { note: Note ->
currentNoteId = UUID.fromString(note.noteId)
_noteTitle.value =
noteTitle.value.copy(text = note.title, isHintVisible = false)
noteTitle.value.copy(
text = note.title,
isHintVisible = note.title.isBlank()
)
_noteContent.value =
noteContent.value.copy(text = note.content, isHintVisible = false)
noteContent.value.copy(
text = note.content,
isHintVisible = note.content.isBlank()
)
_noteColor.value = note.color
}
}
Expand Down Expand Up @@ -123,7 +130,7 @@ class AddEditNoteViewModel @Inject constructor(
}

} catch (e: InvalidNoteException) {
_eventFlow.emit(UiEvent.ShowSnackBar("Error: $e"))
_eventFlow.emit(UiEvent.ShowSnackBar(e.message.toString()))
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package com.eltescode.notes.features.note.presentation.add_edit_note
package com.eltescode.notes_presentation.add_edit_note

import com.eltescode.core_ui.utils.UiText


data class NoteTextFieldState(
val text: String = "",
val hint: String = "",
val hint: UiText,
val isHintVisible: Boolean = true
)
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.compose.animation.fadeIn
import androidx.compose.animation.fadeOut
import androidx.compose.animation.slideInVertically
import androidx.compose.animation.slideOutVertically
import androidx.compose.foundation.ExperimentalFoundationApi
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
Expand Down Expand Up @@ -68,6 +69,7 @@ fun NotesScreen(
is UiEvent.OnNextScreen -> {
onNextScreen(event.route)
}

is UiEvent.ShowSnackBar -> {
scope.launch {
snackBarHostState.currentSnackbarData?.dismiss()
Expand All @@ -83,20 +85,22 @@ fun NotesScreen(
}
}
}

else -> Unit
}
}
}

NotesScreen(
state = state,
onEvent = viewModel::onEvent
onEvent = viewModel::onEvent,
)

}

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun NotesScreen(state: NotesState, onEvent: (NotesEvent) -> Unit) {

Box(
modifier = Modifier
.fillMaxSize()
Expand Down Expand Up @@ -134,10 +138,11 @@ fun NotesScreen(state: NotesState, onEvent: (NotesEvent) -> Unit) {
}
Spacer(modifier = Modifier.height(16.dp))
LazyColumn(modifier = Modifier.fillMaxSize(), content = {
items(state.notes) { note ->
items(items = state.notes, key = { item -> item.noteId }) { note ->
NoteItem(
note = note,
modifier = Modifier
.animateItemPlacement()
.fillMaxWidth()
.clickable {
onEvent(
Expand All @@ -150,7 +155,6 @@ fun NotesScreen(state: NotesState, onEvent: (NotesEvent) -> Unit) {
onDeleteClick = {
onEvent(NotesEvent.DeleteNote(note))
})
Spacer(modifier = Modifier.height(16.dp))
}
})
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.eltescode.notes_presentation.notes

import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
import androidx.lifecycle.ViewModel
Expand Down Expand Up @@ -42,13 +41,9 @@ class NotesViewModel @Inject constructor(private val noteUseCases: NoteUseCases)

private var job: Job? = null

private var job2: Job? = null

init {


viewModelScope.launch {
Log.d("SYNC", "${noteUseCases.checkSyncNeedUseCase()}")
if (noteUseCases.checkSyncNeedUseCase()) {
syncData()
getNotes()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ fun NoteItem(
Icon(imageVector = Icons.Default.Delete, contentDescription = null)
}
}
Spacer(modifier = Modifier.height(16.dp))
}
Loading

0 comments on commit 6446045

Please sign in to comment.