@@ -52,11 +52,11 @@ You can skip optional steps.
52
52
{% section #step-3 step=3 title="Add the Appwrite SDK" %}
53
53
To add the Appwrite SDK for Android as a dependency, add the following to your app-level **build.gradle** file inside the **dependencies** block.
54
54
55
- ```java
55
+ ```groovy
56
56
implementation "io.appwrite:sdk-for-android:7.0.0"
57
57
```
58
58
59
- In order to allow creating OAuth sessions, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your [AndroidManifest.xml](https://github.com/appwrite/playground-for-flutter /blob/master/android /app/src/main/AndroidManifest.xml).
59
+ In order to allow creating OAuth sessions, the following activity needs to be added inside the `<application>` tag, along side the existing `<activity>` tags in your [AndroidManifest.xml](https://github.com/appwrite/playground-for-android /blob/master/app/src/main/AndroidManifest.xml).
60
60
Be sure to replace the **<PROJECT_ID>** string with your actual Appwrite project ID.
61
61
You can find your Appwrite project ID in you project settings screen in your Appwrite Console.
62
62
@@ -106,23 +106,31 @@ import io.appwrite.models.User;
106
106
import io.appwrite.services.Account;
107
107
108
108
public class AppwriteHelper {
109
- private static Client client;
110
- private static Account account;
109
+ private static AppwriteHelper instance;
110
+ private Client client;
111
+ private Account account;
111
112
112
- public static void init (Context context) {
113
+ private AppwriteHelper (Context context) {
113
114
client = new Client(context)
114
115
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
115
116
.setProject("<PROJECT_ID>");
116
117
117
118
account = new Account(client);
118
119
}
119
120
121
+ public static synchronized AppwriteHelper getInstance(Context context) {
122
+ if (instance == null) {
123
+ instance = new AppwriteHelper(context.getApplicationContext());
124
+ }
125
+ return instance;
126
+ }
127
+
120
128
public interface AuthCallback<T> {
121
129
void onSuccess(T result);
122
130
void onError(Exception error);
123
131
}
124
132
125
- public static void login(String email, String password, final AuthCallback<Session> callback) {
133
+ public void login(String email, String password, final AuthCallback<Session> callback) {
126
134
account.createEmailPasswordSession(email, password, new CoroutineCallback<>(result -> {
127
135
callback.onSuccess(result);
128
136
return null;
@@ -132,7 +140,7 @@ public class AppwriteHelper {
132
140
}));
133
141
}
134
142
135
- public static void register(String email, String password, final AuthCallback<User<Map<String, Object>>> callback) {
143
+ public void register(String email, String password, final AuthCallback<User<Map<String, Object>>> callback) {
136
144
account.create(
137
145
ID.unique(),
138
146
email,
@@ -148,7 +156,7 @@ public class AppwriteHelper {
148
156
);
149
157
}
150
158
151
- public static void logout(final AuthCallback<Object> callback) {
159
+ public void logout(final AuthCallback<Object> callback) {
152
160
account.deleteSession("current", new CoroutineCallback<>(result -> {
153
161
callback.onSuccess(result);
154
162
return null;
@@ -264,13 +272,14 @@ public class MainActivity extends AppCompatActivity {
264
272
private Button buttonRegister;
265
273
private TextView textViewStatus;
266
274
private Button buttonLogout;
275
+ private AppwriteHelper appwrite;
267
276
268
277
@Override
269
278
protected void onCreate(Bundle savedInstanceState) {
270
279
super.onCreate(savedInstanceState);
271
280
272
281
// Initialize Appwrite
273
- AppwriteHelper.init (getApplicationContext());
282
+ appwrite = AppwriteHelper.getInstance (getApplicationContext());
274
283
275
284
setContentView(R.layout.activity_main);
276
285
@@ -297,7 +306,7 @@ public class MainActivity extends AppCompatActivity {
297
306
return;
298
307
}
299
308
300
- AppwriteHelper .login(email, password, new AppwriteHelper.AuthCallback<Session>() {
309
+ appwrite .login(email, password, new AppwriteHelper.AuthCallback<Session>() {
301
310
@Override
302
311
public void onSuccess(Session result) {
303
312
runOnUiThread(() -> {
@@ -325,7 +334,7 @@ public class MainActivity extends AppCompatActivity {
325
334
return;
326
335
}
327
336
328
- AppwriteHelper .register(email, password, new AppwriteHelper.AuthCallback<User<Map<String, Object>>>() {
337
+ appwrite .register(email, password, new AppwriteHelper.AuthCallback<User<Map<String, Object>>>() {
329
338
@Override
330
339
public void onSuccess(User<Map<String, Object>> result) {
331
340
runOnUiThread(() -> {
@@ -344,7 +353,7 @@ public class MainActivity extends AppCompatActivity {
344
353
}
345
354
346
355
private void logout() {
347
- AppwriteHelper .logout(new AppwriteHelper.AuthCallback<Object>() {
356
+ appwrite .logout(new AppwriteHelper.AuthCallback<Object>() {
348
357
@Override
349
358
public void onSuccess(Object result) {
350
359
runOnUiThread(() -> {
0 commit comments