diff --git a/app/build.gradle b/app/build.gradle index 3409a19f..49846803 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -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' diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/Note.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/Note.kt new file mode 100644 index 00000000..9ab200d3 --- /dev/null +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/Note.kt @@ -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("", "") +} \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteRepository.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteRepository.kt new file mode 100644 index 00000000..7faa5952 --- /dev/null +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteRepository.kt @@ -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) -> 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) + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteScreen.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteScreen.kt new file mode 100644 index 00000000..6f248468 --- /dev/null +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/demos/firestore_notes/NoteScreen.kt @@ -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()) } + 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}") + } + } + } + } + } + + +} \ No newline at end of file diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt index f90a0b04..0759d069 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/ui/components/MainContainerOfApp.kt @@ -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 @@ -319,7 +320,9 @@ fun MainContainerOfApp( composable(NavDes.barcodeScanner.route()){ ScannerApp() } - + composable(NavDes.firestoreNotes.route()) { + NoteScreen() + } composable(NavDes.ChatDemoUI.route()) { val vm = hiltViewModel() DemoFullChat2( diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavConstants.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavConstants.kt index b906cf4b..c28de8b6 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavConstants.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavConstants.kt @@ -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" diff --git a/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavDes.kt b/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavDes.kt index b611ae90..b2825a2f 100644 --- a/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavDes.kt +++ b/app/src/main/java/com/example/jetpack_compose_all_in_one/utils/navigation/NavDes.kt @@ -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 @@ -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)) @@ -566,7 +569,8 @@ sealed class NavDes(val data: INavigationDrawerItem, val customAppBarStringId: I polls, quiz, NewsApiHeadlineSwipe, - barcodeScanner + barcodeScanner, + firestoreNotes ), DEMO_UI ) )