diff --git a/README.md b/README.md index 1508b2d..7959707 100644 --- a/README.md +++ b/README.md @@ -30,7 +30,7 @@ On app-level `build.gradle`, add dependency: ```groovy dependencies { - implementation 'com.github.psteiger:geofire-ktx:0.3' + implementation 'com.github.psteiger:geofire-ktx:0.4' } ``` diff --git a/build.gradle b/build.gradle index 3f40d01..8cc795a 100644 --- a/build.gradle +++ b/build.gradle @@ -10,7 +10,7 @@ buildscript { jcenter() } dependencies { - classpath "com.android.tools.build:gradle:7.0.0-alpha02" + classpath 'com.android.tools.build:gradle:7.0.0-alpha05' classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version" // NOTE: Do not place your application dependencies here; they belong diff --git a/geofire/src/main/java/com/freelapp/geofire/Api.kt b/geofire/src/main/java/com/freelapp/geofire/Api.kt index 45bd6d6..f52aa76 100644 --- a/geofire/src/main/java/com/freelapp/geofire/Api.kt +++ b/geofire/src/main/java/com/freelapp/geofire/Api.kt @@ -58,4 +58,20 @@ fun GeoQuery.asFlow( @ExperimentalCoroutinesApi inline fun GeoQuery.asTypedFlow( dataRef: String -): Flow>> = asTypedFlowImpl(dataRef) \ No newline at end of file +): Flow>> = asTypedFlowImpl(dataRef) + +/** + * Transforms a [GeoQuery] into a cold [Flow] of maps between [GeoQuery] keys and corresponding + * [LocationData] objects, which contain the [GeoLocation] and an object of type [T] (the object + * obtained by converting the [DataSnapshot] stored in "[dataRef]/$key" to an object of type [T]). + *

+ * If conversion of the [DataSnapshot] to an object of type [T] fails, [LocationData]'s data will be + * null. + * + * @return a flow of a mapping between the key and the [LocationData]. + */ +@ExperimentalCoroutinesApi +fun GeoQuery.asTypedFlow( + clazz: Class, + dataRef: String +): Flow>> = asTypedFlowImpl(clazz, dataRef) \ No newline at end of file diff --git a/geofire/src/main/java/com/freelapp/geofire/flow/GeoQuery.kt b/geofire/src/main/java/com/freelapp/geofire/flow/GeoQuery.kt index 30aea49..0b19da6 100644 --- a/geofire/src/main/java/com/freelapp/geofire/flow/GeoQuery.kt +++ b/geofire/src/main/java/com/freelapp/geofire/flow/GeoQuery.kt @@ -64,3 +64,18 @@ internal inline fun GeoQuery.asTypedFlowImpl( } } } + +@PublishedApi +@ExperimentalCoroutinesApi +internal fun GeoQuery.asTypedFlowImpl( + clazz: Class, + dataRef: String +): Flow>> = + asFlowImpl(dataRef) + .mapLatest { map -> + map.mapValues { + it.value.run { + LocationData(location, data.getTypedValue(clazz)) + } + } + } \ No newline at end of file diff --git a/geofire/src/main/java/com/freelapp/geofire/util/Util.kt b/geofire/src/main/java/com/freelapp/geofire/util/Util.kt index 033df09..9fd6bcb 100644 --- a/geofire/src/main/java/com/freelapp/geofire/util/Util.kt +++ b/geofire/src/main/java/com/freelapp/geofire/util/Util.kt @@ -11,4 +11,8 @@ internal fun SendChannel.tryOffer(element: E): Boolean = @PublishedApi internal inline fun DataSnapshot.getTypedValue(): T? = - getValue(object : GenericTypeIndicator() {}) \ No newline at end of file + getValue(object : GenericTypeIndicator() {}) + +@PublishedApi +internal fun DataSnapshot.getTypedValue(clazz: Class): T? = + getValue(clazz) \ No newline at end of file diff --git a/gradle/wrapper/gradle-wrapper.properties b/gradle/wrapper/gradle-wrapper.properties index 78b05ee..7ece61a 100644 --- a/gradle/wrapper/gradle-wrapper.properties +++ b/gradle/wrapper/gradle-wrapper.properties @@ -1,6 +1,6 @@ #Wed Dec 09 17:51:17 BRT 2020 distributionBase=GRADLE_USER_HOME -distributionUrl=https\://services.gradle.org/distributions/gradle-6.7.1-bin.zip +distributionUrl=https://services.gradle.org/distributions/gradle-6.8-bin.zip distributionPath=wrapper/dists zipStorePath=wrapper/dists zipStoreBase=GRADLE_USER_HOME