-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #231 from myofficework000/firestore_notes
add firestore notes implementation
- Loading branch information
Showing
7 changed files
with
135 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/Note.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.firestore_notes | ||
|
||
data class Note (val title: String = "", val content: String = "") { | ||
constructor() : this("", "") | ||
} |
29 changes: 29 additions & 0 deletions
29
.../main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteRepository.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.firestore_notes | ||
|
||
import com.google.firebase.firestore.FirebaseFirestore | ||
|
||
class NoteRepository { | ||
private val db = FirebaseFirestore.getInstance() | ||
private val notesCollection = db.collection("notes") | ||
|
||
fun getNotes(callback: (List<Note>) -> Unit) { | ||
notesCollection.get() | ||
.addOnSuccessListener { result -> | ||
val notes = result.toObjects(Note::class.java) | ||
callback(notes) | ||
} | ||
.addOnFailureListener { exception -> | ||
exception.printStackTrace() | ||
} | ||
} | ||
|
||
fun addNote(note: Note, callback: (Boolean) -> Unit) { | ||
notesCollection.add(note) | ||
.addOnSuccessListener { | ||
callback(true) | ||
} | ||
.addOnFailureListener { | ||
callback(false) | ||
} | ||
} | ||
} |
88 changes: 88 additions & 0 deletions
88
app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package com.example.jetpack_compose_all_in_one.demos.firestore_notes | ||
|
||
import androidx.compose.foundation.layout.Column | ||
import androidx.compose.foundation.layout.fillMaxWidth | ||
import androidx.compose.foundation.layout.padding | ||
import androidx.compose.foundation.lazy.LazyColumn | ||
import androidx.compose.foundation.lazy.items | ||
import androidx.compose.material3.Button | ||
import androidx.compose.material3.Card | ||
import androidx.compose.material3.Text | ||
import androidx.compose.material3.TextField | ||
import androidx.compose.runtime.Composable | ||
import androidx.compose.runtime.LaunchedEffect | ||
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.unit.dp | ||
|
||
@Composable | ||
fun NoteScreen(noteRepository: NoteRepository = NoteRepository()) { | ||
var notes by remember { mutableStateOf(listOf<Note>()) } | ||
var newNoteTitle by remember { mutableStateOf("") } | ||
var newNoteContent by remember { mutableStateOf("") } | ||
|
||
LaunchedEffect(key1 = "load_notes") { | ||
noteRepository.getNotes { | ||
notes = it | ||
} | ||
} | ||
|
||
Column { | ||
TextField( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
value = newNoteTitle, | ||
onValueChange = { newNoteTitle = it }, | ||
label = { Text(text = "Title") } | ||
) | ||
|
||
TextField( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp), | ||
value = newNoteContent, | ||
onValueChange = { newNoteContent = it }, | ||
label = { Text(text = "Content") } | ||
) | ||
|
||
Button( | ||
modifier = Modifier.align(Alignment.CenterHorizontally), | ||
onClick = { | ||
noteRepository.addNote(Note(newNoteTitle, newNoteContent)) { success -> | ||
if (success) { | ||
newNoteTitle = "" | ||
newNoteContent = "" | ||
noteRepository.getNotes { | ||
notes = it | ||
} | ||
} | ||
} | ||
}) { | ||
Text(text = "Add Note") | ||
} | ||
|
||
LazyColumn() { | ||
items(notes) { note -> | ||
Card( | ||
modifier = Modifier | ||
.fillMaxWidth() | ||
.padding(16.dp) | ||
) { | ||
Column( | ||
modifier = Modifier.padding(16.dp) | ||
) { | ||
Text(text = "Title: ${note.title}") | ||
Text(text = "Content: ${note.content}") | ||
} | ||
} | ||
} | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters