-
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 #203 from myofficework000/FirebaseRealtimeDatabase
Firebase realtime database
- Loading branch information
Showing
5 changed files
with
163 additions
and
0 deletions.
There are no files selected for viewing
7 changes: 7 additions & 0 deletions
7
.../java/com/example/jetpack_compose_all_in_one/third_party_lib/firebase/realtime_db/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,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 | ||
) |
83 changes: 83 additions & 0 deletions
83
...xample/jetpack_compose_all_in_one/third_party_lib/firebase/realtime_db/NoteComposables.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,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()) | ||
} | ||
} |
34 changes: 34 additions & 0 deletions
34
...example/jetpack_compose_all_in_one/third_party_lib/firebase/realtime_db/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,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()) | ||
} | ||
}) | ||
} | ||
|
||
} |
26 changes: 26 additions & 0 deletions
26
.../example/jetpack_compose_all_in_one/third_party_lib/firebase/realtime_db/NoteViewModel.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,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) | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
...e/jetpack_compose_all_in_one/third_party_lib/firebase/realtime_db/NoteViewModelFactory.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,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") | ||
} | ||
} |