Skip to content

Commit

Permalink
Merge remote-tracking branch 'GitLiveApp/master' into fix-on-disconnect
Browse files Browse the repository at this point in the history
  • Loading branch information
Daeda88 committed Apr 21, 2024
2 parents 83f0192 + 8daf907 commit f615bc3
Show file tree
Hide file tree
Showing 14 changed files with 828 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,6 @@ package dev.gitlive.firebase.auth
actual val emulatorHost: String = "10.0.2.2"

actual val context: Any = Unit

@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
actual annotation class IgnoreForAndroidUnitTest
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Copyright (c) 2020 GitLive Ltd. Use of this source code is governed by the Apache 2.0 license.
*/

@file:JvmName("databaseAndroid")
package dev.gitlive.firebase.database

import com.google.android.gms.tasks.Task
Expand All @@ -17,7 +18,6 @@ import dev.gitlive.firebase.internal.EncodedObject
import dev.gitlive.firebase.Firebase
import dev.gitlive.firebase.FirebaseApp
import dev.gitlive.firebase.database.ChildEvent.Type
import dev.gitlive.firebase.database.FirebaseDatabase.Companion.FirebaseDatabase
import dev.gitlive.firebase.internal.android
import dev.gitlive.firebase.internal.decode
import dev.gitlive.firebase.internal.reencodeTransformation
Expand Down Expand Up @@ -52,23 +52,23 @@ suspend fun <T> Task<T>.awaitWhileOnline(database: FirebaseDatabase): T =
.first()

actual val Firebase.database
by lazy { FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance()) }
by lazy { FirebaseDatabase.getInstance(com.google.firebase.database.FirebaseDatabase.getInstance()) }

actual fun Firebase.database(url: String) =
FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance(url))
FirebaseDatabase.getInstance(com.google.firebase.database.FirebaseDatabase.getInstance(url))

actual fun Firebase.database(app: FirebaseApp) =
FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance(app.android))
FirebaseDatabase.getInstance(com.google.firebase.database.FirebaseDatabase.getInstance(app.android))

actual fun Firebase.database(app: FirebaseApp, url: String) =
FirebaseDatabase(com.google.firebase.database.FirebaseDatabase.getInstance(app.android, url))
FirebaseDatabase.getInstance(com.google.firebase.database.FirebaseDatabase.getInstance(app.android, url))

actual class FirebaseDatabase private constructor(val android: com.google.firebase.database.FirebaseDatabase) {
actual class FirebaseDatabase internal constructor(val android: com.google.firebase.database.FirebaseDatabase) {

companion object {
private val instances = WeakHashMap<com.google.firebase.database.FirebaseDatabase, FirebaseDatabase>()

internal fun FirebaseDatabase(
internal fun getInstance(
android: com.google.firebase.database.FirebaseDatabase
) = instances.getOrPut(android) { dev.gitlive.firebase.database.FirebaseDatabase(android) }
}
Expand All @@ -81,8 +81,14 @@ actual class FirebaseDatabase private constructor(val android: com.google.fireba
actual fun reference() =
DatabaseReference(NativeDatabaseReference(android.reference, persistenceEnabled))

actual fun setPersistenceEnabled(enabled: Boolean) =
android.setPersistenceEnabled(enabled).also { persistenceEnabled = enabled }
actual fun setPersistenceEnabled(enabled: Boolean) {
android.setPersistenceEnabled(enabled)
persistenceEnabled = enabled
}

actual fun setPersistenceCacheSizeBytes(cacheSizeInBytes: Long) {
android.setPersistenceCacheSizeBytes(cacheSizeInBytes)
}

actual fun setLoggingEnabled(enabled: Boolean) =
android.setLogLevel(Logger.Level.DEBUG.takeIf { enabled } ?: Logger.Level.NONE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ expect fun Firebase.database(app: FirebaseApp): FirebaseDatabase
expect fun Firebase.database(app: FirebaseApp, url: String): FirebaseDatabase

expect class FirebaseDatabase {

fun reference(path: String): DatabaseReference
fun reference(): DatabaseReference
fun setPersistenceEnabled(enabled: Boolean)
fun setLoggingEnabled(enabled: Boolean)
fun setPersistenceEnabled(enabled: Boolean)
fun setPersistenceCacheSizeBytes(cacheSizeInBytes: Long)
fun useEmulator(host: String, port: Int)

fun goOffline()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,9 @@ import kotlinx.serialization.DeserializationStrategy
import kotlinx.serialization.KSerializer
import platform.Foundation.NSError
import platform.Foundation.allObjects
import platform.darwin.dispatch_queue_t
import kotlin.collections.component1
import kotlin.collections.component2

actual val Firebase.database
by lazy { FirebaseDatabase(FIRDatabase.database()) }
Expand Down Expand Up @@ -66,6 +69,10 @@ actual class FirebaseDatabase internal constructor(val ios: FIRDatabase) {
ios.persistenceEnabled = enabled
}

actual fun setPersistenceCacheSizeBytes(cacheSizeInBytes: Long) {
ios.setPersistenceCacheSizeBytes(cacheSizeInBytes.toULong())
}

actual fun setLoggingEnabled(enabled: Boolean) =
FIRDatabase.setLoggingEnabled(enabled)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,11 @@ actual fun Firebase.database(app: FirebaseApp, url: String) =
rethrow { FirebaseDatabase(getDatabase(app = app.js, url = url)) }

actual class FirebaseDatabase internal constructor(val js: Database) {

actual fun reference(path: String) = rethrow { DatabaseReference(NativeDatabaseReference(ref(js, path), js)) }
actual fun reference() = rethrow { DatabaseReference(NativeDatabaseReference(ref(js), js)) }
actual fun setPersistenceEnabled(enabled: Boolean) {}
actual fun setPersistenceCacheSizeBytes(cacheSizeInBytes: Long) {}
actual fun setLoggingEnabled(enabled: Boolean) = rethrow { enableLogging(enabled) }
actual fun useEmulator(host: String, port: Int) = rethrow { connectDatabaseEmulator(js, host, port) }

Expand Down
Loading

0 comments on commit f615bc3

Please sign in to comment.