Skip to content

Commit 3441fa9

Browse files
Merge pull request #579 from appwrite/feat-improve-java-interop
2 parents 5c26022 + 83a998e commit 3441fa9

File tree

17 files changed

+154
-230
lines changed

17 files changed

+154
-230
lines changed

src/SDK/Language/Android.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,6 +190,11 @@ public function getFiles(): array
190190
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/cookies/Extensions.kt',
191191
'template' => '/android/library/src/main/java/io/appwrite/cookies/Extensions.kt.twig',
192192
],
193+
[
194+
'scope' => 'default',
195+
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/coroutines/Callback.kt',
196+
'template' => '/android/library/src/main/java/io/appwrite/coroutines/Callback.kt.twig',
197+
],
193198
[
194199
'scope' => 'default',
195200
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/cookies/stores/InMemoryCookieStore.kt',

src/SDK/Language/Kotlin.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,11 @@ public function getFiles(): array
366366
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/Query.kt',
367367
'template' => '/kotlin/src/main/kotlin/io/appwrite/Query.kt.twig',
368368
],
369+
[
370+
'scope' => 'default',
371+
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/coroutines/Callback.kt',
372+
'template' => '/android/library/src/main/java/io/appwrite/coroutines/Callback.kt.twig',
373+
],
369374
[
370375
'scope' => 'default',
371376
'destination' => '/src/main/kotlin/{{ sdk.namespace | caseSlash }}/exceptions/{{spec.title | caseUcfirst}}Exception.kt',
Lines changed: 28 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,44 @@
1-
import androidx.appcompat.app.AppCompatActivity
2-
import android.os.Bundle
3-
import kotlinx.coroutines.GlobalScope
4-
import kotlinx.coroutines.launch
5-
import {{ sdk.namespace | caseDot }}.Client
1+
{% import 'kotlin/base/utils.twig' as utils %}
2+
import {{ sdk.namespace | caseDot }}.Client;
3+
import {{ sdk.namespace | caseDot }}.coroutines.CoroutineCallback;
64
{% if method.parameters.all | filter((param) => param.type == 'file') | length > 0 %}
7-
import {{ sdk.namespace | caseDot }}.models.InputFile
5+
import {{ sdk.namespace | caseDot }}.models.InputFile;
86
{% endif %}
9-
import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}
7+
import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }};
108

11-
public class MainActivity extends AppCompatActivity {
12-
13-
@Override
14-
protected void onCreate(Bundle savedInstanceState) {
15-
super.onCreate(savedInstanceState);
16-
setContentView(R.layout.activity_main);
17-
18-
Client client = new Client(getApplicationContext())
9+
Client client = new Client(context)
1910
{% if method.auth|length > 0 %}
20-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
11+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
2112
{% for node in method.auth %}
2213
{% for key,header in node|keys %}
23-
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
24-
{% endfor %}
25-
{% endfor %}
26-
{% endif %}
14+
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}"){% if loop.last %};{% endif %} // {{node[header].description}}
15+
{% endfor %}{% endfor %}{% endif %}
2716

28-
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
17+
{{ service.name | caseUcfirst }} {{ service.name | caseCamel }} = new {{ service.name | caseUcfirst }}(client);
2918

30-
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new Continuation<Object>() {
31-
@NotNull
32-
@Override
33-
public CoroutineContext getContext() {
34-
return EmptyCoroutineContext.INSTANCE;
35-
}
19+
{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}new CoroutineCallback<>((result, error) -> {
20+
if (error != null)
21+
error.printStackTrace();
22+
return;
23+
}
3624

37-
@Override
38-
public void resumeWith(@NotNull Object o) {
39-
String json = "";
40-
try {
41-
if (o instanceof Result.Failure) {
42-
Result.Failure failure = (Result.Failure) o;
43-
throw failure.exception;
44-
}{% if method.type != "webAuth" %} else {
45-
Response response = (Response) o;
46-
json = response.body().string();
47-
}{% endif %}
48-
49-
}
50-
} catch (Throwable th) {
51-
Log.e("ERROR", th.toString());
52-
}
53-
}
54-
});{% endif %}
25+
Log.d("Appwrite", result.toString());
26+
}));{% endif %}
5527

56-
{% if method.type == "webAuth" %}
57-
this,
58-
{% endif %}
5928
{% for parameter in method.parameters.all %}
6029
{% if parameter.required %}
61-
{{ parameter | paramExample }}{% if not loop.last %}, {% endif %}
30+
{{ parameter | paramExample }}{% if not loop.last %},{% endif %}
6231

6332
{% endif %}
6433
{% if loop.last %}
65-
new Continuation<Object>() {
66-
@NotNull
67-
@Override
68-
public CoroutineContext getContext() {
69-
return EmptyCoroutineContext.INSTANCE;
70-
}
71-
72-
@Override
73-
public void resumeWith(@NotNull Object o) {
74-
String json = "";
75-
try {
76-
if (o instanceof Result.Failure) {
77-
Result.Failure failure = (Result.Failure) o;
78-
throw failure.exception;
79-
}{% if method.type != "webAuth" %} else {
80-
Response response = (Response) o;
81-
json = response.body().string();
82-
}{% endif %}
83-
84-
} catch (Throwable th) {
85-
Log.e("ERROR", th.toString());
86-
}
87-
}
88-
}
89-
);
34+
new CoroutineCallback<>((result, error) -> {
35+
if (error != null) {
36+
error.printStackTrace();
37+
return;
38+
}
39+
40+
Log.d("Appwrite", result.toString());
41+
})
42+
);
9043
{% endif %}
91-
{% endfor %}
92-
}
93-
}
44+
{% endfor %}
Lines changed: 9 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,27 @@
1-
import androidx.appcompat.app.AppCompatActivity
2-
import android.os.Bundle
3-
import kotlinx.coroutines.GlobalScope
4-
import kotlinx.coroutines.launch
51
import {{ sdk.namespace | caseDot }}.Client
62
{% if method.parameters.all | filter((param) => param.type == 'file') | length > 0 %}
73
import {{ sdk.namespace | caseDot }}.models.InputFile
84
{% endif %}
95
import {{ sdk.namespace | caseDot }}.services.{{ service.name | caseUcfirst }}
106

11-
class MainActivity : AppCompatActivity() {
12-
override fun onCreate(savedInstanceState: Bundle?) {
13-
super.onCreate(savedInstanceState)
14-
setContentView(R.layout.activity_main)
15-
16-
val client = Client(applicationContext)
7+
val client = Client(context)
178
{% if method.auth|length > 0 %}
18-
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
9+
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
1910
{% for node in method.auth %}
2011
{% for key,header in node|keys %}
21-
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}") // {{node[header].description}}
22-
{% endfor %}
23-
{% endfor %}
24-
{% endif %}
12+
.set{{header | caseUcfirst}}("{{node[header]['x-appwrite']['demo']}}") // {{node[header].description}}
13+
{% endfor %}{% endfor %}{% endif %}
2514

26-
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
15+
val {{ service.name | caseCamel }} = {{ service.name | caseUcfirst }}(client)
2716

28-
GlobalScope.launch {
29-
{% if method.type == 'webAuth' %} {% elseif method.type == 'location' %} val result = {% else %} val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
17+
{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %}val result = {% else %}val response = {% endif %}{{ service.name | caseCamel }}.{{ method.name | caseCamel }}({% if method.parameters.all | length == 0 %}){% endif %}
3018

31-
{% if method.type == "webAuth" %}
32-
activity = this@MainActivity,
33-
{% endif %}
3419
{% for parameter in method.parameters.all %}
3520
{% if parameter.required %}
36-
{{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %}
21+
{{parameter.name}} = {{ parameter | paramExample }}{% if not loop.last %},{% endif %}
3722

3823
{% endif %}
3924
{% if loop.last %}
40-
)
25+
)
4126
{% endif %}
42-
{% endfor %}
43-
{% if method.type == 'webAuth' %}{% elseif method.type == 'location' %} println(result); // Resource URL{% else %} val json = response.body?.string(){% endif %}
44-
45-
}
46-
}
47-
}
27+
{% endfor %}
Lines changed: 12 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,13 @@
11
package io.appwrite.example_java;
22

3-
import androidx.appcompat.app.AppCompatActivity;
43
import android.os.Bundle;
54
import android.util.Log;
6-
import org.jetbrains.annotations.NotNull;
5+
6+
import androidx.appcompat.app.AppCompatActivity;
7+
78
import {{ sdk.namespace | caseDot }}.Client;
8-
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception;
9-
import {{ sdk.namespace | caseDot }}.extensions.JsonExtensionsKt;
10-
import {{ sdk.namespace | caseDot }}.models.Session;
9+
import {{ sdk.namespace | caseDot }}.coroutines.CoroutineCallback;
1110
import {{ sdk.namespace | caseDot }}.services.Account;
12-
import kotlin.Result;
13-
import kotlin.coroutines.Continuation;
14-
import kotlin.coroutines.CoroutineContext;
15-
import kotlin.coroutines.EmptyCoroutineContext;
1611

1712
public class MainActivity extends AppCompatActivity {
1813

@@ -27,31 +22,13 @@ public class MainActivity extends AppCompatActivity {
2722

2823
Account account = new Account(client);
2924

30-
try {
31-
account.createSession("[email protected]","password", new Continuation<Session>() {
32-
@NotNull
33-
@Override
34-
public CoroutineContext getContext() {
35-
return EmptyCoroutineContext.INSTANCE;
36-
}
37-
38-
@Override
39-
public void resumeWith(@NotNull Object o) {
40-
try {
41-
if (o instanceof Result.Failure) {
42-
Result.Failure failure = (Result.Failure) o;
43-
throw failure.exception;
44-
} else {
45-
Session session = (Session) o;
46-
Log.d("RESPONSE", JsonExtensionsKt.toJson(session));
47-
}
48-
} catch (Throwable th) {
49-
Log.e("ERROR", th.toString());
50-
}
51-
}
52-
});
53-
} catch (AppwriteException e) {
54-
e.printStackTrace();
55-
}
25+
account.createEmailSession("[email protected]", "password", new CoroutineCallback<>((session, error) -> {
26+
if (error != null) {
27+
Log.e("Appwrite", error.getMessage());
28+
return;
29+
}
30+
31+
Log.d("Appwrite", session.toMap().toString());
32+
}));
5633
}
5734
}

templates/android/example/src/main/java/io/appwrite/android/ui/accounts/AccountsViewModel.kt.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class AccountsViewModel : ViewModel() {
2929
fun onLogin(email: Editable, password: Editable) {
3030
viewModelScope.launch {
3131
try {
32-
val session = accountService.createSession(email.toString(), password.toString())
32+
val session = accountService.createEmailSession(email.toString(), password.toString())
3333
_response.postValue(Event(session.toJson()))
3434
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
3535
_error.postValue(Event(e))
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package {{ sdk.namespace | caseDot }}.coroutines
2+
3+
import kotlinx.coroutines.Dispatchers
4+
import kotlin.coroutines.Continuation
5+
import kotlin.coroutines.CoroutineContext
6+
7+
interface Callback<T> {
8+
fun onComplete(result: T?, error: Throwable?)
9+
}
10+
11+
class CoroutineCallback<T> @JvmOverloads constructor(
12+
private val callback: Callback<T>,
13+
override val context: CoroutineContext = Dispatchers.Default
14+
) : Continuation<T> {
15+
override fun resumeWith(result: Result<T>) {
16+
callback.onComplete(result.getOrNull(), result.exceptionOrNull())
17+
}
18+
}

templates/android/library/src/main/java/io/appwrite/services/ServiceTemplate.kt.twig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ class {{ service.name | caseUcfirst }} : Service {
4545

4646
*/
4747
@JvmOverloads
48-
@Throws({{ spec.title | caseUcfirst }}Exception::class)
4948
suspend fun {{ method.name | caseCamel }}({% if method.type == "webAuth" %}{{ '\n\t\t' }}activity: ComponentActivity{% if method.parameters.all | length > 0 %}, {% endif %}{% endif %}{{ _self.method_parameters(method.parameters, method.consumes) }}{% if method.parameters.all|length > 0 %}{{ '\n\t' }}{% endif %}){% if method.type != "webAuth" %}: {{ _self.resultType(sdk.namespace, method) }}{% endif %} {
5049
val path = "{{ method.path }}"{% for parameter in method.parameters.path %}.replace("{{ '{' ~ parameter.name | caseCamel ~ '}' }}", {{ parameter.name | caseCamel }}){% endfor %}
5150

templates/kotlin/base/requests/api.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
path,
55
headers,
66
params,
7-
responseType = {{ utils.resultType(sdk.namespace, method) }}::class.java,
7+
responseType = {{ utils.resultTypeKt(sdk.namespace, method) }}::class.java,
88
{% if method.responseModel %}
99
converter,
1010
{% endif %}

templates/kotlin/base/requests/file.twig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
path,
1111
headers,
1212
params,
13-
responseType = {{ utils.resultType(sdk.namespace, method) }}::class.java,
13+
responseType = {{ utils.resultTypeKt(sdk.namespace, method) }}::class.java,
1414
{% if method.responseModel %}
1515
converter,
1616
{% endif %}

0 commit comments

Comments
 (0)