Skip to content

Commit

Permalink
Merge pull request #1 from psteiger/nonreified
Browse files Browse the repository at this point in the history
Adding non-reified version of API methods.
  • Loading branch information
psteiger authored Feb 2, 2021
2 parents 4fc3633 + 4d7db90 commit 54f074b
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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'
}
```

Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 17 additions & 1 deletion geofire/src/main/java/com/freelapp/geofire/Api.kt
Original file line number Diff line number Diff line change
Expand Up @@ -58,4 +58,20 @@ fun GeoQuery.asFlow(
@ExperimentalCoroutinesApi
inline fun <reified T : Any> GeoQuery.asTypedFlow(
dataRef: String
): Flow<Map<Key, LocationData<T>>> = asTypedFlowImpl(dataRef)
): Flow<Map<Key, LocationData<T>>> = 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]).
* <p>
* 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 <T : Any> GeoQuery.asTypedFlow(
clazz: Class<T>,
dataRef: String
): Flow<Map<Key, LocationData<T>>> = asTypedFlowImpl(clazz, dataRef)
15 changes: 15 additions & 0 deletions geofire/src/main/java/com/freelapp/geofire/flow/GeoQuery.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,3 +64,18 @@ internal inline fun <reified T : Any> GeoQuery.asTypedFlowImpl(
}
}
}

@PublishedApi
@ExperimentalCoroutinesApi
internal fun <T : Any> GeoQuery.asTypedFlowImpl(
clazz: Class<T>,
dataRef: String
): Flow<Map<Key, LocationData<T>>> =
asFlowImpl(dataRef)
.mapLatest { map ->
map.mapValues {
it.value.run {
LocationData(location, data.getTypedValue(clazz))
}
}
}
6 changes: 5 additions & 1 deletion geofire/src/main/java/com/freelapp/geofire/util/Util.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,8 @@ internal fun <E> SendChannel<E>.tryOffer(element: E): Boolean =

@PublishedApi
internal inline fun <reified T : Any> DataSnapshot.getTypedValue(): T? =
getValue(object : GenericTypeIndicator<T>() {})
getValue(object : GenericTypeIndicator<T>() {})

@PublishedApi
internal fun <T : Any> DataSnapshot.getTypedValue(clazz: Class<T>): T? =
getValue(clazz)
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 54f074b

Please sign in to comment.