Skip to content

Commit

Permalink
Merge pull request #203 from myofficework000/FirebaseRealtimeDatabase
Browse files Browse the repository at this point in the history
Firebase realtime database
  • Loading branch information
myofficework000 authored Nov 3, 2023
2 parents 88b0a22 + 907514d commit 8e64287
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.firebase.realtime_db

data class Note(
val title: String? = null,
val date: String? = null,
val content: String? = null
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.firebase.realtime_db

import androidx.compose.foundation.layout.Column
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.ExperimentalMaterial3Api
import androidx.compose.material3.Text
import androidx.compose.material3.TextField
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.dp

@Composable
fun NoteScreen(viewModel: NoteViewModel) {
val notes by viewModel.notes.collectAsState()

Column {
NoteList(notes = notes)
UserInput {title, date, content ->
viewModel.sendNote(title, date, content)
}
}

}

@OptIn(ExperimentalMaterial3Api::class)
@Composable
fun UserInput(onNoteSent: (String, String, String) -> Unit) {
var title by remember { mutableStateOf("") }
var date by remember { mutableStateOf("") }
var content by remember { mutableStateOf("") }

Column {
TextField(
value = title,
onValueChange = {title = it},
label = { Text(text = "Title")}
)
TextField(
value = date,
onValueChange = {date = it},
label = { Text(text = "Date")}
)
TextField(
value = content,
onValueChange = {content = it},
label = { Text(text = "Content")}
)
Button(onClick = {
onNoteSent(title, date, content)
}) {
Text(text = "Save")
}
}
}

@Composable
fun NoteList(notes: List<Note>) {
LazyColumn {
items(notes) { note ->
NoteItem(note)
}
}
}

@Composable
fun NoteItem(note: Note) {
Column(
modifier = Modifier
.padding(8.dp)
) {
Text(text = "Title: ${note.title.orEmpty()}")
Text(text = "Date: ${note.date.orEmpty()}")
Text(text = note.title.orEmpty())
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.firebase.realtime_db

import android.util.Log
import com.google.firebase.database.DataSnapshot
import com.google.firebase.database.DatabaseError
import com.google.firebase.database.FirebaseDatabase
import com.google.firebase.database.ValueEventListener
import com.google.firebase.database.ktx.getValue

class NoteRepository {

private val database = FirebaseDatabase.getInstance().getReference("notes")

fun saveNote(note: Note) {
val noteId = database.push().key
noteId?.let {
database.child(it).setValue(note)
}
}

fun getNoteUpdates(action: (List<Note>) -> Unit) {
database.addValueEventListener(object : ValueEventListener {
override fun onDataChange(snapshot: DataSnapshot) {
val note = snapshot.children.mapNotNull { it.getValue<Note>() }
action(note)
}

override fun onCancelled(error: DatabaseError) {
Log.i("MessageRepo", "Failed to read notes", error.toException())
}
})
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.firebase.realtime_db

import androidx.lifecycle.ViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow

class NoteViewModel(private val repository: NoteRepository) : ViewModel() {

private val _notes = MutableStateFlow<List<Note>>(emptyList())
val notes: StateFlow<List<Note>> = _notes

init {
loadNotes()
}

private fun loadNotes() {
repository.getNoteUpdates { note ->
_notes.value = note
}
}

fun sendNote(title: String, date: String, content: String) {
val note = Note(title, date, content)
repository.saveNote(note)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.example.jetpack_compose_all_in_one.third_party_lib.firebase.realtime_db

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider

class NoteViewModelFactory(private val repository: NoteRepository) : ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(NoteViewModel::class.java)) {
return NoteViewModel(repository) as T
}
throw IllegalArgumentException("Unknown viewmodel class")
}
}

0 comments on commit 8e64287

Please sign in to comment.