Skip to content

Commit e6ba41d

Browse files
committed
Improvements to code
1 parent 6ad1605 commit e6ba41d

File tree

1 file changed

+21
-12
lines changed

1 file changed

+21
-12
lines changed

src/routes/docs/quick-starts/android-java/+page.markdoc

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,11 @@ You can skip optional steps.
5252
{% section #step-3 step=3 title="Add the Appwrite SDK" %}
5353
To add the Appwrite SDK for Android as a dependency, add the following to your app-level **build.gradle** file inside the **dependencies** block.
5454

55-
```java
55+
```groovy
5656
implementation "io.appwrite:sdk-for-android:7.0.0"
5757
```
5858

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).
6060
Be sure to replace the **<PROJECT_ID>** string with your actual Appwrite project ID.
6161
You can find your Appwrite project ID in you project settings screen in your Appwrite Console.
6262

@@ -106,23 +106,31 @@ import io.appwrite.models.User;
106106
import io.appwrite.services.Account;
107107

108108
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;
111112

112-
public static void init(Context context) {
113+
private AppwriteHelper(Context context) {
113114
client = new Client(context)
114115
.setEndpoint("https://<REGION>.cloud.appwrite.io/v1")
115116
.setProject("<PROJECT_ID>");
116117

117118
account = new Account(client);
118119
}
119120

121+
public static synchronized AppwriteHelper getInstance(Context context) {
122+
if (instance == null) {
123+
instance = new AppwriteHelper(context.getApplicationContext());
124+
}
125+
return instance;
126+
}
127+
120128
public interface AuthCallback<T> {
121129
void onSuccess(T result);
122130
void onError(Exception error);
123131
}
124132

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) {
126134
account.createEmailPasswordSession(email, password, new CoroutineCallback<>(result -> {
127135
callback.onSuccess(result);
128136
return null;
@@ -132,7 +140,7 @@ public class AppwriteHelper {
132140
}));
133141
}
134142

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) {
136144
account.create(
137145
ID.unique(),
138146
email,
@@ -148,7 +156,7 @@ public class AppwriteHelper {
148156
);
149157
}
150158

151-
public static void logout(final AuthCallback<Object> callback) {
159+
public void logout(final AuthCallback<Object> callback) {
152160
account.deleteSession("current", new CoroutineCallback<>(result -> {
153161
callback.onSuccess(result);
154162
return null;
@@ -264,13 +272,14 @@ public class MainActivity extends AppCompatActivity {
264272
private Button buttonRegister;
265273
private TextView textViewStatus;
266274
private Button buttonLogout;
275+
private AppwriteHelper appwrite;
267276

268277
@Override
269278
protected void onCreate(Bundle savedInstanceState) {
270279
super.onCreate(savedInstanceState);
271280

272281
// Initialize Appwrite
273-
AppwriteHelper.init(getApplicationContext());
282+
appwrite = AppwriteHelper.getInstance(getApplicationContext());
274283

275284
setContentView(R.layout.activity_main);
276285

@@ -297,7 +306,7 @@ public class MainActivity extends AppCompatActivity {
297306
return;
298307
}
299308

300-
AppwriteHelper.login(email, password, new AppwriteHelper.AuthCallback<Session>() {
309+
appwrite.login(email, password, new AppwriteHelper.AuthCallback<Session>() {
301310
@Override
302311
public void onSuccess(Session result) {
303312
runOnUiThread(() -> {
@@ -325,7 +334,7 @@ public class MainActivity extends AppCompatActivity {
325334
return;
326335
}
327336

328-
AppwriteHelper.register(email, password, new AppwriteHelper.AuthCallback<User<Map<String, Object>>>() {
337+
appwrite.register(email, password, new AppwriteHelper.AuthCallback<User<Map<String, Object>>>() {
329338
@Override
330339
public void onSuccess(User<Map<String, Object>> result) {
331340
runOnUiThread(() -> {
@@ -344,7 +353,7 @@ public class MainActivity extends AppCompatActivity {
344353
}
345354

346355
private void logout() {
347-
AppwriteHelper.logout(new AppwriteHelper.AuthCallback<Object>() {
356+
appwrite.logout(new AppwriteHelper.AuthCallback<Object>() {
348357
@Override
349358
public void onSuccess(Object result) {
350359
runOnUiThread(() -> {

0 commit comments

Comments
 (0)