Skip to content

Commit d6d6196

Browse files
committed
Update examples for response models
1 parent 9bd3936 commit d6d6196

File tree

2 files changed

+30
-37
lines changed

2 files changed

+30
-37
lines changed

templates/android/example-java/src/main/java/io/appwrite/example_java/MainActivity.java.twig

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@ import androidx.appcompat.app.AppCompatActivity;
44
import android.os.Bundle;
55
import android.util.Log;
66
import org.jetbrains.annotations.NotNull;
7-
import org.json.JSONObject;
87
import {{ sdk.namespace | caseDot }}.Client;
9-
import {{ sdk.namespace | caseDot }}.exceptions.AppwriteException;
8+
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception;
9+
import {{ sdk.namespace | caseDot }}.extensions.JsonExtensionsKt;
10+
import {{ sdk.namespace | caseDot }}.models.Session;
1011
import {{ sdk.namespace | caseDot }}.services.Account;
1112
import kotlin.Result;
1213
import kotlin.coroutines.Continuation;
1314
import kotlin.coroutines.CoroutineContext;
1415
import kotlin.coroutines.EmptyCoroutineContext;
15-
import okhttp3.Response;
1616

1717
public class MainActivity extends AppCompatActivity {
1818

@@ -28,7 +28,7 @@ public class MainActivity extends AppCompatActivity {
2828
Account account = new Account(client);
2929

3030
try {
31-
account.createSession("[email protected]","password", new Continuation<Response>() {
31+
account.createSession("[email protected]","password", new Continuation<Session>() {
3232
@NotNull
3333
@Override
3434
public CoroutineContext getContext() {
@@ -37,16 +37,13 @@ public class MainActivity extends AppCompatActivity {
3737

3838
@Override
3939
public void resumeWith(@NotNull Object o) {
40-
String json = "";
4140
try {
4241
if (o instanceof Result.Failure) {
4342
Result.Failure failure = (Result.Failure) o;
4443
throw failure.exception;
4544
} else {
46-
Response response = (Response) o;
47-
json = response.body().string();
48-
json = new JSONObject(json).toString(8);
49-
Log.d("RESPONSE", json);
45+
Session session = (Session) o;
46+
Log.d("RESPONSE", JsonExtensionsKt.toJson(session));
5047
}
5148
} catch (Throwable th) {
5249
Log.e("ERROR", th.toString());

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

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,10 @@ import androidx.activity.ComponentActivity
55
import androidx.lifecycle.*
66
import {{ sdk.namespace | caseDot }}.android.utils.Client.client
77
import {{ sdk.namespace | caseDot }}.android.utils.Event
8-
import {{ sdk.namespace | caseDot }}.exceptions.AppwriteException
8+
import {{ sdk.namespace | caseDot }}.exceptions.{{ spec.title | caseUcfirst }}Exception
9+
import {{ sdk.namespace | caseDot }}.extensions.toJson
910
import {{ sdk.namespace | caseDot }}.services.Account
1011
import kotlinx.coroutines.launch
11-
import org.json.JSONObject
12-
1312

1413
class AccountsViewModel : ViewModel() {
1514

@@ -27,28 +26,25 @@ class AccountsViewModel : ViewModel() {
2726
Account(client)
2827
}
2928

30-
fun onLogin(email: Editable , password : Editable) {
29+
fun onLogin(email: Editable, password: Editable) {
3130
viewModelScope.launch {
3231
try {
33-
var response = accountService.createSession(email.toString(), password.toString())
34-
var json = response.body?.string() ?: ""
35-
json = JSONObject(json).toString(8)
36-
_response.postValue(Event(json))
37-
} catch (e: AppwriteException) {
32+
val session = accountService.createSession(email.toString(), password.toString())
33+
_response.postValue(Event(session.toJson()))
34+
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
3835
_error.postValue(Event(e))
3936
}
4037
}
4138

4239
}
4340

44-
fun onSignup(email: Editable , password : Editable, name: Editable) {
41+
fun onSignup(email: Editable, password: Editable, name: Editable) {
4542
viewModelScope.launch {
4643
try {
47-
var response = accountService.create(email.toString(), password.toString(), name.toString())
48-
var json = response.body?.string() ?: ""
49-
json = JSONObject(json).toString(2)
50-
_response.postValue(Event(json))
51-
} catch (e: AppwriteException) {
44+
val user =
45+
accountService.create(email.toString(), password.toString(), name.toString())
46+
_response.postValue(Event(user.toJson()))
47+
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
5248
_error.postValue(Event(e))
5349
}
5450
}
@@ -58,10 +54,15 @@ class AccountsViewModel : ViewModel() {
5854
fun oAuthLogin(activity: ComponentActivity) {
5955
viewModelScope.launch {
6056
try {
61-
accountService.createOAuth2Session(activity, "facebook", "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/success", "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/failure")
57+
accountService.createOAuth2Session(
58+
activity,
59+
"facebook",
60+
"appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/success",
61+
"appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/failure"
62+
)
6263
} catch (e: Exception) {
6364
_error.postValue(Event(e))
64-
} catch (e: AppwriteException) {
65+
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
6566
_error.postValue(Event(e))
6667
}
6768
}
@@ -70,11 +71,9 @@ class AccountsViewModel : ViewModel() {
7071
fun getUser() {
7172
viewModelScope.launch {
7273
try {
73-
var response = accountService.get()
74-
var json = response.body?.string() ?: ""
75-
json = JSONObject(json).toString(2)
76-
_response.postValue(Event(json))
77-
} catch (e: AppwriteException) {
74+
val account = accountService.get()
75+
_response.postValue(Event(account.toJson()))
76+
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
7877
_error.postValue(Event(e))
7978
}
8079
}
@@ -83,14 +82,11 @@ class AccountsViewModel : ViewModel() {
8382
fun logout() {
8483
viewModelScope.launch {
8584
try {
86-
var response = accountService.deleteSession("current")
87-
var json = response.body?.string()?.ifEmpty { "{}" }
88-
json = JSONObject(json).toString(4)
89-
_response.postValue(Event(json))
90-
} catch (e: AppwriteException) {
85+
val result = accountService.deleteSession("current")
86+
_response.postValue(Event(result.toJson()))
87+
} catch (e: {{ spec.title | caseUcfirst }}Exception) {
9188
_error.postValue(Event(e))
9289
}
9390
}
9491
}
95-
9692
}

0 commit comments

Comments
 (0)