Skip to content

Commit

Permalink
Merge pull request #231 from myofficework000/firestore_notes
Browse files Browse the repository at this point in the history
add firestore notes implementation
  • Loading branch information
myofficework000 authored Jan 23, 2024
2 parents 783391a + c08a900 commit 6af709f
Show file tree
Hide file tree
Showing 7 changed files with 135 additions and 2 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ dependencies {
implementation platform('androidx.compose:compose-bom:2023.03.00')
implementation 'androidx.compose.ui:ui-graphics'
implementation 'androidx.benchmark:benchmark-macro:1.2.0'
implementation 'com.google.firebase:firebase-firestore:24.10.1'
// implementation 'com.google.android.ads:mediation-test-suite:3.0.0'


Expand Down
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("", "")
}
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)
}
}
}
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}")
}
}
}
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import com.example.jetpack_compose_all_in_one.application_components.content_pro
import com.example.jetpack_compose_all_in_one.application_components.content_provider.demo_images.ShowImages
import com.example.jetpack_compose_all_in_one.demos.barcode_scanner.ScannerApp
import com.example.jetpack_compose_all_in_one.demos.currency_converter.presentation.view.CurrencyFromToScreen
import com.example.jetpack_compose_all_in_one.demos.firestore_notes.NoteScreen
import com.example.jetpack_compose_all_in_one.demos.github_api.presentation.view.GithubUserListScreen
import com.example.jetpack_compose_all_in_one.demos.history_of_day.HistoryOfTheDayUI
import com.example.jetpack_compose_all_in_one.demos.news_app.view.NewsScreen
Expand Down Expand Up @@ -319,7 +320,9 @@ fun MainContainerOfApp(
composable(NavDes.barcodeScanner.route()){
ScannerApp()
}

composable(NavDes.firestoreNotes.route()) {
NoteScreen()
}
composable(NavDes.ChatDemoUI.route()) {
val vm = hiltViewModel<ChatViewModel>()
DemoFullChat2(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,9 @@ object NavConstants {
const val QRCODE_SCANNER = "qrcode_scanner"
const val QRCODE_SCANNER_ABOUT = "QR Code Scanner"

const val FIRESTORE_NOTES = "firestore_notes"
const val FIRESTORE_NOTES_ABOUT = "Firestore Notes"

const val TMDB = "tmdb"
const val TMDB_ABOUT = "TMDB API"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.DOMA
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.DOWNLOAD
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.DOWNLOAD_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FEATURES
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FIRESTORE_NOTES
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FIRESTORE_NOTES_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FLOW_DEMO
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FLOW_DEMO_ABOUT
import com.example.jetpack_compose_all_in_one.utils.navigation.NavConstants.FOREGROUND_SERVICE
Expand Down Expand Up @@ -446,6 +448,7 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I

object youTube : NavDes(NavigationDrawerData(YoutubeUI, YoutubeUI_ABOUT))
object barcodeScanner: NavDes(NavigationDrawerData(BarcodeScannerUI, BarcodeScannerUI_ABOUT))
object firestoreNotes: NavDes(NavigationDrawerData(FIRESTORE_NOTES, FIRESTORE_NOTES_ABOUT))
object news : NavDes(NavigationDrawerData(NewsUI, NewsUI_ABOUT))

object polls : NavDes(NavigationDrawerData(PollUI, PollUI_About))
Expand Down Expand Up @@ -566,7 +569,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I
polls,
quiz,
NewsApiHeadlineSwipe,
barcodeScanner
barcodeScanner,
firestoreNotes
), DEMO_UI
)
)
Expand Down

0 comments on commit 6af709f

Please sign in to comment.