Skip to content
Closed

Dev #84

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
137 changes: 132 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ In order to capture the Appwrite OAuth callback url, the following activity need
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:scheme="appwrite-callback-[PROJECT_ID]" />
<data android:scheme="appwrite-callback-<PROJECT_ID>" />
</intent-filter>
</activity>
</application>
Expand All @@ -91,8 +91,8 @@ import io.appwrite.Client
import io.appwrite.services.Account

val client = Client(context)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setEndpoint("https://<HOSTNAME_OR_IP>/v1") // Your API Endpoint
.setProject("<PROJECT_ID>") // Your project ID
.setSelfSigned(true) // Remove in production
```

Expand Down Expand Up @@ -123,8 +123,8 @@ import io.appwrite.services.Account
import io.appwrite.ID

val client = Client(context)
.setEndpoint("https://[HOSTNAME_OR_IP]/v1") // Your API Endpoint
.setProject("5df5acd0d48c2") // Your project ID
.setEndpoint("https://<HOSTNAME_OR_IP>/v1") // Your API Endpoint
.setProject("<PROJECT_ID>") // Your project ID
.setSelfSigned(true) // Remove in production

val account = Account(client)
Expand All @@ -136,7 +136,134 @@ val user = account.create(
)
```

### Type Safety with Models

The Appwrite Android SDK provides type safety when working with database documents through generic methods. Methods like `listDocuments`, `getDocument`, and others accept a `nestedType` parameter that allows you to specify your custom model type for full type safety.

**Kotlin:**
```kotlin
data class Book(
val name: String,
val author: String,
val releaseYear: String? = null,
val category: String? = null,
val genre: List<String>? = null,
val isCheckedOut: Boolean
)

val databases = Databases(client)

try {
val documents = databases.listDocuments(
databaseId = "your-database-id",
collectionId = "your-collection-id",
nestedType = Book::class.java // Pass in your custom model type
)

for (book in documents.documents) {
Log.d("Appwrite", "Book: ${book.name} by ${book.author}") // Now you have full type safety
}
} catch (e: AppwriteException) {
Log.e("Appwrite", e.message ?: "Unknown error")
}
```

**Java:**
```java
public class Book {
private String name;
private String author;
private String releaseYear;
private String category;
private List<String> genre;
private boolean isCheckedOut;

// Constructor
public Book(String name, String author, boolean isCheckedOut) {
this.name = name;
this.author = author;
this.isCheckedOut = isCheckedOut;
}

// Getters and setters
public String getName() { return name; }
public void setName(String name) { this.name = name; }

public String getAuthor() { return author; }
public void setAuthor(String author) { this.author = author; }

public String getReleaseYear() { return releaseYear; }
public void setReleaseYear(String releaseYear) { this.releaseYear = releaseYear; }

public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }

public List<String> getGenre() { return genre; }
public void setGenre(List<String> genre) { this.genre = genre; }

public boolean isCheckedOut() { return isCheckedOut; }
public void setCheckedOut(boolean checkedOut) { isCheckedOut = checkedOut; }
}

Databases databases = new Databases(client);

try {
DocumentList<Book> documents = databases.listDocuments(
"your-database-id",
"your-collection-id",
Book.class // Pass in your custom model type
);

for (Book book : documents.getDocuments()) {
Log.d("Appwrite", "Book: " + book.getName() + " by " + book.getAuthor()); // Now you have full type safety
}
} catch (AppwriteException e) {
Log.e("Appwrite", e.getMessage() != null ? e.getMessage() : "Unknown error");
}
```

**Tip**: You can use the `appwrite types` command to automatically generate model definitions based on your Appwrite database schema. Learn more about [type generation](https://appwrite.io/docs/products/databases/type-generation).

### Working with Model Methods

All Appwrite models come with built-in methods for data conversion and manipulation:

**`toMap()`** - Converts a model instance to a Map format, useful for debugging or manual data manipulation:
```kotlin
val account = Account(client)
val user = account.get()
val userMap = user.toMap()
Log.d("Appwrite", userMap.toString()) // Prints all user properties as a Map
```

**`from(map:, nestedType:)`** - Creates a model instance from a Map, useful when working with raw data:
```kotlin
val userData: Map<String, Any> = mapOf(
"\$id" to "123",
"name" to "John",
"email" to "[email protected]"
)
val user = User.from(userData, User::class.java)
```

**JSON Serialization** - Models can be easily converted to/from JSON using Gson (which the SDK uses internally):
```kotlin
import com.google.gson.Gson

val account = Account(client)
val user = account.get()

// Convert to JSON
val gson = Gson()
val jsonString = gson.toJson(user)
Log.d("Appwrite", "User JSON: $jsonString")

// Convert from JSON
val userFromJson = gson.fromJson(jsonString, User::class.java)
```

### Error Handling

The Appwrite Android SDK raises an `AppwriteException` object with `message`, `code` and `response` properties. You can handle any errors by catching `AppwriteException` and present the `message` to the user or handle it yourself based on the provided error information. Below is an example.

```kotlin
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/java/functions/create-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ functions.createExecution(
"<PATH>", // path (optional)
ExecutionMethod.GET, // method (optional)
mapOf( "a" to "b" ), // headers (optional)
"", // scheduledAt (optional)
"<SCHEDULED_AT>", // scheduledAt (optional)
new CoroutineCallback<>((result, error) -> {
if (error != null) {
error.printStackTrace();
Expand Down
2 changes: 1 addition & 1 deletion docs/examples/kotlin/functions/create-execution.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@ val result = functions.createExecution(
path = "<PATH>", // (optional)
method = ExecutionMethod.GET, // (optional)
headers = mapOf( "a" to "b" ), // (optional)
scheduledAt = "", // (optional)
scheduledAt = "<SCHEDULED_AT>", // (optional)
)
4 changes: 2 additions & 2 deletions scripts/publish-config.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ nexusPublishing {
stagingProfileId = sonatypeStagingProfileId
username = ossrhUsername
password = ossrhPassword
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
}
}
}