Skip to content

Commit ca6022f

Browse files
Merge pull request #299 from appwrite/feat-android-response-models
Feat android response models
2 parents 14212db + e78778a commit ca6022f

File tree

18 files changed

+331
-144
lines changed

18 files changed

+331
-144
lines changed

src/SDK/Language/Android.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,12 @@ public function getFiles()
140140
'template' => '/android/library/src/main/java/io/appwrite/extensions/CollectionExtensions.kt.twig',
141141
'minify' => false,
142142
],
143+
[
144+
'scope' => 'default',
145+
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/json/PreciseNumberAdapter.kt',
146+
'template' => '/android/library/src/main/java/io/appwrite/json/PreciseNumberAdapter.kt.twig',
147+
'minify' => false,
148+
],
143149
[
144150
'scope' => 'default',
145151
'destination' => '/library/src/main/java/{{ sdk.namespace | caseSlash }}/models/RealtimeModels.kt',
@@ -359,6 +365,12 @@ public function getFiles()
359365
'template' => '/android/example-java/.gitignore',
360366
'minify' => false,
361367
],
368+
[
369+
'scope' => 'definition',
370+
'destination' => 'library/src/main/java/io/appwrite/models/{{ definition.name | caseUcfirst }}.kt',
371+
'template' => '/android/library/src/main/java/io/appwrite/models/Model.kt.twig',
372+
'minify' => false,
373+
],
362374
];
363375
}
364376
}

src/SDK/Language/Kotlin.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ public function getTypeName($type)
9797
{
9898
switch ($type) {
9999
case self::TYPE_INTEGER:
100-
return 'Int';
100+
return 'Long';
101101
case self::TYPE_NUMBER:
102102
return 'Double';
103103
case self::TYPE_STRING:

templates/android/.github/workflows/publish.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,10 @@ jobs:
1212
steps:
1313
- name: Check out code
1414
uses: actions/checkout@v2
15-
- name: Set up JDK 1.8
15+
- name: Set up JDK 11
1616
uses: actions/setup-java@v1
1717
with:
18-
java-version: 1.8
18+
java-version: 11
1919
# Base64 decodes and pipes the GPG key content into the secret file
2020
- name: Prepare environment
2121
env:
@@ -51,4 +51,4 @@ jobs:
5151
SIGNING_PASSWORD: ${{ secrets.SIGNING_PASSWORD }}
5252
SIGNING_SECRET_KEY_RING_FILE: ${{ secrets.SIGNING_SECRET_KEY_RING_FILE }}
5353
SONATYPE_STAGING_PROFILE_ID: ${{ secrets.SONATYPE_STAGING_PROFILE_ID }}
54-
SDK_VERSION: ${{ github.event.release.tag_name }}
54+
SDK_VERSION: ${{ github.event.release.tag_name }}

templates/android/build.gradle.twig

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,15 @@ apply plugin: 'io.github.gradle-nexus.publish-plugin'
22

33
// Top-level build file where you can add configuration options common to all sub-projects/modules.
44
buildscript {
5-
ext.kotlin_version = "1.4.31"
6-
version '{{ sdk.version }}'
5+
ext.kotlin_version = "1.5.31"
6+
version "{{ sdk.version }}"
77
repositories {
88
maven { url "https://plugins.gradle.org/m2/" }
99
google()
1010
mavenCentral()
1111
}
1212
dependencies {
13-
classpath "com.android.tools.build:gradle:4.2.0"
13+
classpath "com.android.tools.build:gradle:7.0.3"
1414
classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
1515
classpath 'io.github.gradle-nexus:publish-plugin:1.1.0'
1616

templates/android/example-java/build.gradle.twig

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,12 @@ plugins {
33
}
44

55
android {
6-
compileSdkVersion 30
7-
buildToolsVersion "30.0.3"
6+
compileSdkVersion 31
87

98
defaultConfig {
109
applicationId "{{ sdk.namespace | caseDot }}.example_java"
1110
minSdkVersion 23
12-
targetSdkVersion 30
11+
targetSdkVersion 31
1312
versionCode 1
1413
versionName "1.0"
1514

@@ -29,8 +28,8 @@ android {
2928
}
3029

3130
dependencies {
32-
3331
implementation project(path: ':library')
32+
3433
implementation 'androidx.appcompat:appcompat:1.3.0'
3534
implementation 'com.google.android.material:material:1.3.0'
3635
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'

templates/android/example-java/src/main/AndroidManifest.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.AppwriteAndroidSDK">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".MainActivity" android:exported="true">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515

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/build.gradle.twig

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,12 @@ plugins {
44
}
55

66
android {
7-
compileSdkVersion 30
8-
buildToolsVersion "30.0.3"
7+
compileSdkVersion 31
98

109
defaultConfig {
1110
applicationId "{{ sdk.namespace | caseDot }}.android"
1211
minSdkVersion 21
13-
targetSdkVersion 30
12+
targetSdkVersion 31
1413
versionCode 1
1514
versionName "1.0"
1615

@@ -37,23 +36,23 @@ android {
3736
}
3837

3938
dependencies {
40-
4139
implementation project(path: ':library')
40+
4241
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
43-
implementation 'androidx.core:core-ktx:1.5.0'
44-
implementation 'androidx.appcompat:appcompat:1.3.0'
45-
implementation 'com.google.android.material:material:1.3.0'
46-
implementation 'androidx.constraintlayout:constraintlayout:2.0.4'
42+
implementation 'androidx.core:core-ktx:1.6.0'
43+
implementation 'androidx.appcompat:appcompat:1.3.1'
44+
implementation 'com.google.android.material:material:1.4.0'
45+
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
4746
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
48-
implementation "androidx.fragment:fragment-ktx:1.3.2"
47+
implementation "androidx.fragment:fragment-ktx:1.3.6"
4948
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
5049
implementation 'androidx.lifecycle:lifecycle-livedata-ktx:2.3.1'
5150
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.3.1'
5251
implementation 'androidx.navigation:navigation-fragment-ktx:2.3.5'
5352
implementation 'androidx.navigation:navigation-ui-ktx:2.3.5'
54-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.4.3"
55-
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.4.3"
53+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-core:1.5.2"
54+
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.5.2"
5655
testImplementation 'junit:junit:4.+'
57-
androidTestImplementation 'androidx.test.ext:junit:1.1.2'
58-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0'
56+
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
57+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
5958
}

templates/android/example/src/main/AndroidManifest.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,14 @@
99
android:roundIcon="@mipmap/ic_launcher_round"
1010
android:supportsRtl="true"
1111
android:theme="@style/Theme.AppwriteAndroidSDK">
12-
<activity android:name=".MainActivity">
12+
<activity android:name=".MainActivity" android:exported="true">
1313
<intent-filter>
1414
<action android:name="android.intent.action.MAIN" />
1515
<category android:name="android.intent.category.LAUNCHER" />
1616
</intent-filter>
1717
</activity>
1818

19-
<activity android:name="io.appwrite.views.CallbackActivity" >
19+
<activity android:name="io.appwrite.views.CallbackActivity" android:exported="true">
2020
<intent-filter android:label="android_web_auth">
2121
<action android:name="android.intent.action.VIEW" />
2222
<category android:name="android.intent.category.DEFAULT" />

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)