Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
32caa99
Add kotlin realtime models
Jul 28, 2021
4b94c1f
Add kotlin foreach async function
Jul 28, 2021
427e981
Add realtime endpoint to kotlin client
Jul 28, 2021
60add68
Add WIP kotlin realtime service
Jul 28, 2021
c8a4074
Add realtime files to android template
Jul 29, 2021
f342383
Move JSON extensions to top level functions
Jul 29, 2021
4d64875
Fix setting default realtime endpoint
Jul 29, 2021
41e3e80
Make realtime subscription closeable for try-with-resources or .use e…
Jul 29, 2021
d0d0cc7
Make service name pattern consistent, make client protected in services
Jul 29, 2021
c6358f1
Add unsubscribe + subscription returns, fix message parsing, channel …
Jul 29, 2021
6c66937
Add unknown error code
Jul 29, 2021
d676994
Fix reentry locking on subscribe to defer socket connection, fix unsu…
abnegate Jul 30, 2021
ef06bc6
Add Kotlin realtime files
abnegate Aug 2, 2021
6014687
Merge remote-tracking branch 'origin/master' into feat-android-realtime
abnegate Aug 17, 2021
135866f
add tests
TorstenDittmann Aug 17, 2021
09fa1f0
fix tets
TorstenDittmann Aug 17, 2021
5e65044
WIP add realtime tests
abnegate Aug 18, 2021
12a9fc8
Fix Android + Kotlin coroutine dispatchers for realtime tests
abnegate Aug 19, 2021
ee2a790
Add Android + Kotlin JSON cast extension to simplify casts to client …
abnegate Aug 19, 2021
ef4fada
Remove web changes
abnegate Aug 19, 2021
38a2f6c
Review fixes
abnegate Aug 23, 2021
eb84bc1
Use spec exception instead of `RealtimeError`
abnegate Aug 24, 2021
c4fe45d
Fix unsubscribe
abnegate Aug 24, 2021
a3af790
Remove realtime from Kotlin
abnegate Aug 24, 2021
a905d20
Fix kotlin removal
abnegate Aug 24, 2021
da4fae3
Remove redundant template
abnegate Aug 26, 2021
2641fa2
WIP adapt for realtime updates
abnegate Aug 27, 2021
c404e46
Package updates
abnegate Aug 30, 2021
735d16b
Add generic event model
abnegate Aug 30, 2021
3162fa5
Add more JSON helpers
abnegate Aug 30, 2021
d618499
Remove redundant error class
abnegate Aug 30, 2021
5f9c92a
Add typed response/payload support
abnegate Aug 30, 2021
8a551f2
Upload test to support realtime payload typing
abnegate Aug 30, 2021
2ca9c25
Fix realtime URL parsing for more than one occurence of `http`
abnegate Aug 31, 2021
e74478c
Merge remote-tracking branch 'origin/master' into feat-android-realtime
abnegate Aug 31, 2021
0857e5c
Set publish version from release tag
abnegate Aug 31, 2021
6f37805
Merge branch 'master' into feat-android-realtime
TorstenDittmann Sep 2, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/SDK/Language/Android.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,14 @@ public function getFiles()
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/Error.kt',
'template' => '/android/library/src/main/java/io/appwrite/models/Error.kt.twig',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/extensions/CollectionExtensions.kt',
'template' => '/android/library/src/main/java/io/appwrite/extensions/CollectionExtensions.kt.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/RealtimeModels.kt',
'template' => '/android/library/src/main/java/io/appwrite/models/RealtimeModels.kt.twig',
'minify' => false,
],
[
Expand All @@ -160,10 +166,16 @@ public function getFiles()
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/services/BaseService.kt',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/services/Service.kt',
'template' => '/android/library/src/main/java/io/appwrite/services/Service.kt.twig',
'minify' => false,
],
[
'scope' => 'default',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/services/Realtime.kt',
'template' => '/android/library/src/main/java/io/appwrite/services/Realtime.kt.twig',
'minify' => false,
],
[
'scope' => 'service',
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/services/{{service.name | caseUcfirst}}.kt',
Expand Down
3 changes: 2 additions & 1 deletion templates/android/.github/workflows/publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,5 @@ jobs:
SIGNING_KEY_ID: ${{ secrets.SIGNING_KEY_ID }}
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
SIGNING_SECRET_KEY_RING_FILE: ${{ secrets.SIGNING_SECRET_KEY_RING_FILE }}
SONATYPE_STAGING_PROFILE_ID: ${{ secrets.SONATYPE_STAGING_PROFILE_ID }}
SONATYPE_STAGING_PROFILE_ID: ${{ secrets.SONATYPE_STAGING_PROFILE_ID }}
SDK_VERSION: ${{ github.event.release.tag_name }}
17 changes: 9 additions & 8 deletions templates/android/library/build.gradle.twig
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ plugins {
ext {
PUBLISH_GROUP_ID = '{{ sdk.namespace | caseDot }}'
PUBLISH_ARTIFACT_ID = '{{ sdk.gitRepoName | caseDash }}'
PUBLISH_VERSION = '{{ sdk.version }}'
PUBLISH_VERSION = System.getenv('SDK_VERSION')
POM_URL = 'https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName }}'
POM_SCM_URL = 'https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName }}'
POM_ISSUE_URL = 'https://github.com/{{ sdk.gitUserName }}/{{ sdk.gitRepoName }}/issues'
Expand Down Expand Up @@ -53,8 +53,8 @@ android {

dependencies {
implementation("org.jetbrains.kotlin:kotlin-stdlib-jdk8:${KotlinCompilerVersion.VERSION}")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3")
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3")
api("org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.1")
api("org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.0")

api(platform("com.squareup.okhttp3:okhttp-bom:4.9.0"))
api("com.squareup.okhttp3:okhttp")
Expand All @@ -65,15 +65,16 @@ dependencies {
implementation("net.gotev:cookie-store:1.3.5")
implementation("androidx.lifecycle:lifecycle-runtime-ktx:2.3.1")
implementation("androidx.lifecycle:lifecycle-common-java8:2.3.1")
implementation("androidx.appcompat:appcompat:1.2.0")
implementation("androidx.fragment:fragment-ktx:1.3.2")
implementation("androidx.activity:activity-ktx:1.2.2")
implementation("androidx.appcompat:appcompat:1.3.1")
implementation("androidx.fragment:fragment-ktx:1.3.6")
implementation("androidx.activity:activity-ktx:1.3.1")
implementation("androidx.browser:browser:1.3.0")

testImplementation 'junit:junit:4.+'
testImplementation "androidx.test.ext:junit-ktx:1.1.2"
testImplementation "androidx.test:core-ktx:1.3.0"
testImplementation "androidx.test.ext:junit-ktx:1.1.3"
testImplementation "androidx.test:core-ktx:1.4.0"
testImplementation "org.robolectric:robolectric:4.5.1"
testApi("org.jetbrains.kotlinx:kotlinx-coroutines-test:1.5.1")
}

apply from: "${rootProject.projectDir}/scripts/publish-module.gradle"
27 changes: 22 additions & 5 deletions templates/android/library/src/main/java/io/appwrite/Client.kt.twig
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ import android.content.pm.PackageManager
import com.google.gson.Gson
import io.appwrite.appwrite.BuildConfig
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
import {{ sdk.namespace | caseDot }}.extensions.JsonExtensions.fromJson
import {{ sdk.namespace | caseDot }}.models.Error
import {{ sdk.namespace | caseDot }}.extensions.fromJson
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
Expand Down Expand Up @@ -36,6 +35,7 @@ import kotlin.coroutines.resume
class Client @JvmOverloads constructor(
context: Context,
var endPoint: String = "https://appwrite.io/v1",
var endPointRealtime: String? = null,
private var selfSigned: Boolean = false
) : CoroutineScope {

Expand Down Expand Up @@ -151,14 +151,31 @@ class Client @JvmOverloads constructor(
}

/**
* Set endpoint
* Set endpoint and realtime endpoint.
*
* @param endpoint
*
* @return this
*/
fun setEndpoint(endPoint: String): Client {
this.endPoint = endPoint

if (this.endPointRealtime == null && endPoint.startsWith("http")) {
this.endPointRealtime = endPoint.replaceFirst("http", "ws")
}

return this
}

/**
* Set realtime endpoint
*
* @param endpoint
*
* @return this
*/
fun setEndpointRealtime(endPoint: String): Client {
this.endPointRealtime = endPoint
return this
}

Expand Down Expand Up @@ -297,9 +314,9 @@ class Client @JvmOverloads constructor(

val contentType: String = response.headers["content-type"] ?: ""
val error = if (contentType.contains("application/json", ignoreCase = true)) {
bodyString.fromJson(Error::class.java)
bodyString.fromJson<{{ spec.title | caseUcfirst }}Exception>()
} else {
Error(bodyString, response.code)
{{ spec.title | caseUcfirst }}Exception(bodyString, response.code)
}

it.cancel(AppwriteException(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package {{ sdk.namespace | caseDot }}.extensions

import kotlinx.coroutines.Dispatchers.IO
import kotlinx.coroutines.async
import kotlinx.coroutines.awaitAll
import kotlinx.coroutines.withContext

suspend fun <T> Collection<T>.forEachAsync(
callback: suspend (T) -> Unit
) = withContext(IO) {
map { async { callback.invoke(it) } }.awaitAll()
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,33 @@ package {{ sdk.namespace | caseDot }}.extensions

import com.google.gson.Gson

object JsonExtensions {
val gson = Gson()

fun Any.toJson(): String =
Gson().toJson(this)
fun Any.toJson(): String =
gson.toJson(this)

fun <T> String.fromJson(clazz: Class<T>): T =
Gson().fromJson(this, clazz)
}
fun <T> String.fromJson(clazz: Class<T>): T =
gson.fromJson(this, clazz)

inline fun <reified T> String.fromJson(): T =
gson.fromJson(this, T::class.java)

fun <T> Any.jsonCast(to: Class<T>): T =
toJson().fromJson(to)

inline fun <reified T> Any.jsonCast(): T =
toJson().fromJson(T::class.java)

fun <T> Any.tryJsonCast(to: Class<T>): T? = try {
toJson().fromJson(to)
} catch (ex: Exception) {
ex.printStackTrace()
null
}

inline fun <reified T> Any.tryJsonCast(): T? = try {
toJson().fromJson(T::class.java)
} catch (ex: Exception) {
ex.printStackTrace()
null
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package {{ sdk.namespace | caseDot }}.models

import java.io.Closeable

data class RealtimeSubscription(
private val close: () -> Unit
) : Closeable {
override fun close() = close.invoke()
}

data class RealtimeCallback(
val payloadClass: Class<*>,
val callback: (RealtimeResponseEvent<*>) -> Unit
)

open class RealtimeResponse(
val type: String,
val data: Any
)

data class RealtimeResponseEvent<T>(
val event: String,
val channels: Collection<String>,
val timestamp: Long,
var payload: T
)

enum class RealtimeCode(val value: Int) {
POLICY_VIOLATION(1008),
UNKNOWN_ERROR(-1)
}
Loading