
Streamlines Firebase integration in mobile apps for swift initialization on Android and iOS, enhancing development efficiency. Offers real-time data operations like write, read, update, and delete.
KFirebaseDatabase is available on mavenCentral().
implementation("io.github.the-best-is-best:kfirebase-database:2.0.0")Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseRemoteConfig and add it to your project.import Firebase
FirebaseApp.configure()@OptIn(ExperimentalTime::class)
@Composable
@Preview
fun App() {
MaterialTheme {
val firebaseDatabase = KFirebaseDatabase()
var readValue by remember { mutableStateOf<String?>(null) }
var listenValue by remember { mutableStateOf<String?>(null) }
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
firebaseDatabase.addObserveValueListener("message").collect {
val result = it.getOrNull()
listenValue = result?.toString()
}
}
Scaffold {
Column(
modifier = Modifier
.fillMaxSize()
.safeContentPadding()
.background(MaterialTheme.colorScheme.background)
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
ElevatedButton(
onClick = {
// Write a new value to the database
val newValue =
"Hello, KFirebase! ${Clock.System.now().toEpochMilliseconds()}"
scope.launch {
firebaseDatabase.write("message", mapOf("text" to newValue))
}
},
modifier = Modifier
.fillMaxWidth()
) {
Text("Write to Database")
}
ElevatedButton(
onClick = {
// Write a new value to the database
scope.launch {
val value = firebaseDatabase.read("message")
value.onSuccess {
readValue = it?.toString()
}.onFailure {
readValue = "Error: ${it.message}"
}
}
},
modifier = Modifier
.fillMaxWidth()
) {
Text("read to Database")
}
Text("Read Value: ${readValue ?: "No data"}", modifier = Modifier.fillMaxWidth())
Text(
"Listen Value: ${listenValue ?: "No data"}",
modifier = Modifier.fillMaxWidth()
)
}
}
}
}expect class KFirebaseDatabase() {
suspend fun write(path: String, data: Map<String, Any>): Result<Boolean?>
suspend fun read(path: String): Result<Any?>
suspend fun writeList(path: String, dataList: List<Map<String, Any>>): Result<Boolean>
suspend fun readList(path: String): Result<List<Any?>>
suspend fun delete(path: String): Result<Boolean?>
suspend fun update(path: String, data: Map<String, Any>): Result<Boolean?>
// Listen for real-time changes at the specified path
fun addObserveValueListener(path: String): Flow<Result<Any?>>
}KFirebaseDatabase is available on mavenCentral().
implementation("io.github.the-best-is-best:kfirebase-database:2.0.0")Make sure to add Firebase as a dependency using Swift Package Manager (SPM).
File > Add Packages....https://github.com/firebase/firebase-ios-sdk
FirebaseRemoteConfig and add it to your project.import Firebase
FirebaseApp.configure()@OptIn(ExperimentalTime::class)
@Composable
@Preview
fun App() {
MaterialTheme {
val firebaseDatabase = KFirebaseDatabase()
var readValue by remember { mutableStateOf<String?>(null) }
var listenValue by remember { mutableStateOf<String?>(null) }
val scope = rememberCoroutineScope()
LaunchedEffect(Unit) {
firebaseDatabase.addObserveValueListener("message").collect {
val result = it.getOrNull()
listenValue = result?.toString()
}
}
Scaffold {
Column(
modifier = Modifier
.fillMaxSize()
.safeContentPadding()
.background(MaterialTheme.colorScheme.background)
.padding(16.dp),
verticalArrangement = Arrangement.spacedBy(10.dp)
) {
ElevatedButton(
onClick = {
// Write a new value to the database
val newValue =
"Hello, KFirebase! ${Clock.System.now().toEpochMilliseconds()}"
scope.launch {
firebaseDatabase.write("message", mapOf("text" to newValue))
}
},
modifier = Modifier
.fillMaxWidth()
) {
Text("Write to Database")
}
ElevatedButton(
onClick = {
// Write a new value to the database
scope.launch {
val value = firebaseDatabase.read("message")
value.onSuccess {
readValue = it?.toString()
}.onFailure {
readValue = "Error: ${it.message}"
}
}
},
modifier = Modifier
.fillMaxWidth()
) {
Text("read to Database")
}
Text("Read Value: ${readValue ?: "No data"}", modifier = Modifier.fillMaxWidth())
Text(
"Listen Value: ${listenValue ?: "No data"}",
modifier = Modifier.fillMaxWidth()
)
}
}
}
}expect class KFirebaseDatabase() {
suspend fun write(path: String, data: Map<String, Any>): Result<Boolean?>
suspend fun read(path: String): Result<Any?>
suspend fun writeList(path: String, dataList: List<Map<String, Any>>): Result<Boolean>
suspend fun readList(path: String): Result<List<Any?>>
suspend fun delete(path: String): Result<Boolean?>
suspend fun update(path: String, data: Map<String, Any>): Result<Boolean?>
// Listen for real-time changes at the specified path
fun addObserveValueListener(path: String): Flow<Result<Any?>>
}