Skip to content

Commit b3fcbb8

Browse files
Merge pull request #85 from appwrite/dev
Dev
2 parents 4e9e8a9 + 2f6378f commit b3fcbb8

File tree

5 files changed

+139
-12
lines changed

5 files changed

+139
-12
lines changed

README.md

Lines changed: 134 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ repositories {
3838
Next, add the dependency to your project's `build.gradle(.kts)` file:
3939

4040
```groovy
41-
implementation("io.appwrite:sdk-for-android:8.2.0")
41+
implementation("io.appwrite:sdk-for-android:8.2.2")
4242
```
4343

4444
### Maven
@@ -49,7 +49,7 @@ Add this to your project's `pom.xml` file:
4949
<dependency>
5050
<groupId>io.appwrite</groupId>
5151
<artifactId>sdk-for-android</artifactId>
52-
<version>8.2.0</version>
52+
<version>8.2.2</version>
5353
</dependency>
5454
</dependencies>
5555
```
@@ -75,7 +75,7 @@ In order to capture the Appwrite OAuth callback url, the following activity need
7575
<action android:name="android.intent.action.VIEW" />
7676
<category android:name="android.intent.category.DEFAULT" />
7777
<category android:name="android.intent.category.BROWSABLE" />
78-
<data android:scheme="appwrite-callback-[PROJECT_ID]" />
78+
<data android:scheme="appwrite-callback-<PROJECT_ID>" />
7979
</intent-filter>
8080
</activity>
8181
</application>
@@ -91,8 +91,8 @@ import io.appwrite.Client
9191
import io.appwrite.services.Account
9292

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

@@ -123,8 +123,8 @@ import io.appwrite.services.Account
123123
import io.appwrite.ID
124124

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

130130
val account = Account(client)
@@ -136,7 +136,134 @@ val user = account.create(
136136
)
137137
```
138138

139+
### Type Safety with Models
140+
141+
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.
142+
143+
**Kotlin:**
144+
```kotlin
145+
data class Book(
146+
val name: String,
147+
val author: String,
148+
val releaseYear: String? = null,
149+
val category: String? = null,
150+
val genre: List<String>? = null,
151+
val isCheckedOut: Boolean
152+
)
153+
154+
val databases = Databases(client)
155+
156+
try {
157+
val documents = databases.listDocuments(
158+
databaseId = "your-database-id",
159+
collectionId = "your-collection-id",
160+
nestedType = Book::class.java // Pass in your custom model type
161+
)
162+
163+
for (book in documents.documents) {
164+
Log.d("Appwrite", "Book: ${book.name} by ${book.author}") // Now you have full type safety
165+
}
166+
} catch (e: AppwriteException) {
167+
Log.e("Appwrite", e.message ?: "Unknown error")
168+
}
169+
```
170+
171+
**Java:**
172+
```java
173+
public class Book {
174+
private String name;
175+
private String author;
176+
private String releaseYear;
177+
private String category;
178+
private List<String> genre;
179+
private boolean isCheckedOut;
180+
181+
// Constructor
182+
public Book(String name, String author, boolean isCheckedOut) {
183+
this.name = name;
184+
this.author = author;
185+
this.isCheckedOut = isCheckedOut;
186+
}
187+
188+
// Getters and setters
189+
public String getName() { return name; }
190+
public void setName(String name) { this.name = name; }
191+
192+
public String getAuthor() { return author; }
193+
public void setAuthor(String author) { this.author = author; }
194+
195+
public String getReleaseYear() { return releaseYear; }
196+
public void setReleaseYear(String releaseYear) { this.releaseYear = releaseYear; }
197+
198+
public String getCategory() { return category; }
199+
public void setCategory(String category) { this.category = category; }
200+
201+
public List<String> getGenre() { return genre; }
202+
public void setGenre(List<String> genre) { this.genre = genre; }
203+
204+
public boolean isCheckedOut() { return isCheckedOut; }
205+
public void setCheckedOut(boolean checkedOut) { isCheckedOut = checkedOut; }
206+
}
207+
208+
Databases databases = new Databases(client);
209+
210+
try {
211+
DocumentList<Book> documents = databases.listDocuments(
212+
"your-database-id",
213+
"your-collection-id",
214+
Book.class // Pass in your custom model type
215+
);
216+
217+
for (Book book : documents.getDocuments()) {
218+
Log.d("Appwrite", "Book: " + book.getName() + " by " + book.getAuthor()); // Now you have full type safety
219+
}
220+
} catch (AppwriteException e) {
221+
Log.e("Appwrite", e.getMessage() != null ? e.getMessage() : "Unknown error");
222+
}
223+
```
224+
225+
**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).
226+
227+
### Working with Model Methods
228+
229+
All Appwrite models come with built-in methods for data conversion and manipulation:
230+
231+
**`toMap()`** - Converts a model instance to a Map format, useful for debugging or manual data manipulation:
232+
```kotlin
233+
val account = Account(client)
234+
val user = account.get()
235+
val userMap = user.toMap()
236+
Log.d("Appwrite", userMap.toString()) // Prints all user properties as a Map
237+
```
238+
239+
**`from(map:, nestedType:)`** - Creates a model instance from a Map, useful when working with raw data:
240+
```kotlin
241+
val userData: Map<String, Any> = mapOf(
242+
"\$id" to "123",
243+
"name" to "John",
244+
"email" to "[email protected]"
245+
)
246+
val user = User.from(userData, User::class.java)
247+
```
248+
249+
**JSON Serialization** - Models can be easily converted to/from JSON using Gson (which the SDK uses internally):
250+
```kotlin
251+
import com.google.gson.Gson
252+
253+
val account = Account(client)
254+
val user = account.get()
255+
256+
// Convert to JSON
257+
val gson = Gson()
258+
val jsonString = gson.toJson(user)
259+
Log.d("Appwrite", "User JSON: $jsonString")
260+
261+
// Convert from JSON
262+
val userFromJson = gson.fromJson(jsonString, User::class.java)
263+
```
264+
139265
### Error Handling
266+
140267
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.
141268

142269
```kotlin

docs/examples/java/functions/create-execution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ functions.createExecution(
1515
"<PATH>", // path (optional)
1616
ExecutionMethod.GET, // method (optional)
1717
mapOf( "a" to "b" ), // headers (optional)
18-
"", // scheduledAt (optional)
18+
"<SCHEDULED_AT>", // scheduledAt (optional)
1919
new CoroutineCallback<>((result, error) -> {
2020
if (error != null) {
2121
error.printStackTrace();

docs/examples/kotlin/functions/create-execution.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,5 @@ val result = functions.createExecution(
1515
path = "<PATH>", // (optional)
1616
method = ExecutionMethod.GET, // (optional)
1717
headers = mapOf( "a" to "b" ), // (optional)
18-
scheduledAt = "", // (optional)
18+
scheduledAt = "<SCHEDULED_AT>", // (optional)
1919
)

library/src/main/java/io/appwrite/Client.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,7 @@ class Client @JvmOverloads constructor(
8787
"x-sdk-name" to "Android",
8888
"x-sdk-platform" to "client",
8989
"x-sdk-language" to "android",
90-
"x-sdk-version" to "8.2.0",
90+
"x-sdk-version" to "8.2.2",
9191
"x-appwrite-response-format" to "1.7.0"
9292
)
9393
config = mutableMapOf()

scripts/publish-config.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ nexusPublishing {
3030
stagingProfileId = sonatypeStagingProfileId
3131
username = ossrhUsername
3232
password = ossrhPassword
33-
nexusUrl.set(uri("https://s01.oss.sonatype.org/service/local/"))
34-
snapshotRepositoryUrl.set(uri("https://s01.oss.sonatype.org/content/repositories/snapshots/"))
33+
nexusUrl.set(uri("https://ossrh-staging-api.central.sonatype.com/service/local/"))
34+
snapshotRepositoryUrl.set(uri("https://central.sonatype.com/repository/maven-snapshots/"))
3535
}
3636
}
3737
}

0 commit comments

Comments
 (0)