Build an ideas tracker with Android

6

Create collection

In Appwrite, data is stored as a collection of documents. Create a collection in the Appwrite Console to store our ideas.

Create project screen

Create project screen

Create a new collection with the following attributes:

AttributeTypeRequiredSize
userId
String
Yes
200
title
String
Yes
200
description
String
No
500

Collection permissions screen

Collection permissions screen

Navigate to the Settings tab of your collection, add the role Any and check the Read box. Next, add a Users role and give them access to Create, Update and Delete by checking those boxes.

Add and remove methods

Now that you have a collection to hold ideas, we can read and write to it from our app. Create a new file services/IdeasService.kt and add the following code to it. Replace the values for ideaDatabaseId and ideaCollectionId with the IDs of the database and collection you created in the previous step.

Kotlin
package <YOUR_ROOT_PACKAGE_HERE>.services

import io.appwrite.Client
import io.appwrite.ID
import io.appwrite.Query
import io.appwrite.models.Document
import io.appwrite.services.Databases

class IdeaService(client: Client) {
    companion object {
        private const val ideaDatabaseId = "<YOUR_IDEA_DATABASE_ID_HERE>"
        private const val ideaCollectionId = "<YOUR_IDEA_COLLECTION_ID_HERE>"
    }

    private val databases = Databases(client)

    suspend fun fetch(): List<Document<Map<String, Any>>> {
        return databases.listDocuments(
            ideaDatabaseId,
            ideaCollectionId,
            listOf(Query.orderDesc("\$createdAt"), Query.limit(10))
        ).documents
    }

    suspend fun add(
        userId: String,
        title: String,
        description: String
    ): Document<Map<String, Any>> {
        return databases.createDocument(
            ideaDatabaseId,
            ideaCollectionId,
            ID.unique(),
            mapOf(
                "userId" to userId,
                "title" to title,
                "description" to description
            )
        )
    }

    suspend fun remove(id: String) {
        databases.deleteDocument(
            ideaDatabaseId,
            ideaCollectionId,
            id
        )
    }
}

Update the services/Appwrite.kt file to add a new property for the IdeaService class.

Look for // Add this line 👇 to find where the changes made here.

Kotlin
package io.appwrite.tutorialforandroid.services

import android.content.Context
import io.appwrite.Client

object Appwrite {
    private const val ENDPOINT = "https://cloud.appwrite.io/v1"
    private const val PROJECT_ID = "<PROJECT_ID>"

    private lateinit var client: Client

    // Add this line 👇
    internal lateinit var ideas: IdeaService
    internal lateinit var account: AccountService

    fun init(context: Context) {
        client = Client(context)
            .setEndpoint(ENDPOINT)
            .setProject(PROJECT_ID)

        // Add this line 👇
        ideas = IdeaService(client)
        account = AccountService(client)
    }
}