diff --git a/src/routes/docs/quick-starts/android-java/prompt.md b/src/routes/docs/quick-starts/android-java/prompt.md new file mode 100644 index 0000000000..f7da195aef --- /dev/null +++ b/src/routes/docs/quick-starts/android-java/prompt.md @@ -0,0 +1,363 @@ +Goal: Add Appwrite auth to a new Android (Java) app with a working login/register/logout UI. + +Rules + +- Never assume project details. Ask the user for Cloud Region, Project ID, and package name. +- Use Android Studio as the development environment. +- Ensure the user creates a platform in the Appwrite Console with the correct package name. + +1. Create or use existing Android project + - If you already have an Android project open in Android Studio, stay in it and use it. + - Otherwise, open Android Studio and click **New Project**. + - Choose a project template (e.g., **Empty Activity**) and click **Next**. + - Enter the app **name** and **package name**, then click **Finish**. + +2. Create Appwrite project + - Ask the user to go to the [Appwrite Console](https://cloud.appwrite.io/console). + - If this is their first time, they should create an account and create their first project. + - Under **Add a platform**, add an **Android app** with the app's **name** and **package name** (the `applicationId` from the app-level `build.gradle`). + +3. Add the Appwrite SDK (ask user for Project ID) + - Add the following dependency to the app-level **build.gradle** file inside the **dependencies** block: + ```groovy + implementation "io.appwrite:sdk-for-android:8.1.0" + ``` + - Add the OAuth callback activity inside the `` tag in **AndroidManifest.xml** (replace `` with actual project ID): + ```xml + + ... + + ... + + + + + + + + + + + + ``` + +4. Create Appwrite helper class (ask user for Region and Project ID) + - Ask the user for: + - Cloud Region (e.g., fra, nyc) + - Project ID (from Console -> Settings) + - Create file: `AppwriteHelper.java` with the following code (replace ``, ``, and ``): + ```java + package ; + + import android.content.Context; + + import java.util.Map; + + import io.appwrite.Client; + import io.appwrite.ID; + import io.appwrite.coroutines.CoroutineCallback; + import io.appwrite.models.Session; + import io.appwrite.models.User; + import io.appwrite.services.Account; + + public class AppwriteHelper { + private static AppwriteHelper instance; + private Client client; + private Account account; + + private AppwriteHelper(Context context) { + client = new Client(context) + .setEndpoint("https://.cloud.appwrite.io/v1") + .setProject(""); + + account = new Account(client); + } + + public static synchronized AppwriteHelper getInstance(Context context) { + if (instance == null) { + instance = new AppwriteHelper(context.getApplicationContext()); + } + return instance; + } + + public interface AuthCallback { + void onSuccess(T result); + void onError(Exception error); + } + + public void login(String email, String password, final AuthCallback callback) { + account.createEmailPasswordSession( + email, + password, + new CoroutineCallback<>(result -> { + callback.onSuccess(result); + return null; + }, error -> { + callback.onError(error); + return null; + })); + } + + public void register(String email, String password, final AuthCallback>> callback) { + account.create( + ID.unique(), + email, + password, + new CoroutineCallback<>(result -> { + callback.onSuccess(result); + return null; + }, error -> { + callback.onError(error); + return null; + }) + ); + } + + public void logout(final AuthCallback callback) { + account.deleteSession("current", new CoroutineCallback<>(result -> { + callback.onSuccess(result); + return null; + }, error -> { + callback.onError(error); + return null; + })); + } + } + ``` + +5. Create login UI in XML + - Update `activity_main.xml` layout file: + ```xml + + + + + +