Skip to content

Commit 502c02b

Browse files
committed
use: client.ping(), simpler config model, update dependencies.
1 parent d210113 commit 502c02b

File tree

5 files changed

+55
-77
lines changed

5 files changed

+55
-77
lines changed

README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ Alternatively, open the repository URL in `Android Studio` to clone it directly.
2525
## 🛠️ Development Guide
2626

2727
1. **Configure Appwrite**
28-
Navigate to `data/repository/AppwriteRepository.kt` and update the values to match your Appwrite
29-
project credentials.
28+
Navigate to `constants/AppwriteConfig.kt` and update the values to match your Appwrite project
29+
credentials.
3030

3131
2. **Customize as Needed**
3232
Modify the starter kit to suit your app's requirements. Adjust UI, features, or backend
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package io.appwrite.starterkit.constants
2+
3+
/**
4+
* Appwrite integration constants.
5+
*
6+
* This object holds values related to the Appwrite server setup,
7+
* including version, project details, and API endpoint.
8+
*/
9+
object AppwriteConfig {
10+
/**
11+
* Appwrite Server version.
12+
*/
13+
const val APPWRITE_VERSION = "1.6.0"
14+
15+
/**
16+
* Appwrite project id.
17+
*/
18+
const val APPWRITE_PROJECT_ID = "my-project-id"
19+
20+
/**
21+
* Appwrite project name.
22+
*/
23+
const val APPWRITE_PROJECT_NAME = "My project"
24+
25+
/**
26+
* Appwrite server endpoint url.
27+
*/
28+
const val APPWRITE_PUBLIC_ENDPOINT = "https://cloud.appwrite.io/v1"
29+
}

app/src/main/java/io/appwrite/starterkit/data/repository/AppwriteRepository.kt

+16-64
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,13 @@ package io.appwrite.starterkit.data.repository
22

33
import android.content.Context
44
import io.appwrite.Client
5+
import io.appwrite.exceptions.AppwriteException
56
import io.appwrite.services.Account
67
import io.appwrite.services.Databases
8+
import io.appwrite.starterkit.constants.AppwriteConfig
79
import io.appwrite.starterkit.data.models.Log
810
import kotlinx.coroutines.Dispatchers
911
import kotlinx.coroutines.withContext
10-
import java.io.BufferedReader
11-
import java.net.HttpURLConnection
12-
import java.net.URL
1312
import java.text.SimpleDateFormat
1413
import java.util.Date
1514
import java.util.Locale
@@ -26,8 +25,8 @@ class AppwriteRepository private constructor(context: Context) {
2625

2726
// Appwrite Client and Services
2827
private val client = Client(context.applicationContext)
29-
.setProject(APPWRITE_PROJECT_ID)
30-
.setEndpoint(APPWRITE_PUBLIC_ENDPOINT)
28+
.setProject(AppwriteConfig.APPWRITE_PROJECT_ID)
29+
.setEndpoint(AppwriteConfig.APPWRITE_PUBLIC_ENDPOINT)
3130

3231
private val account: Account = Account(client)
3332
private val databases: Databases = Databases(client)
@@ -38,44 +37,27 @@ class AppwriteRepository private constructor(context: Context) {
3837
*
3938
* @return [Log] A log object containing details of the request and response.
4039
*/
41-
suspend fun fetchPingLog(): Log = withContext(Dispatchers.IO) {
42-
val url = URL("${client.endpoint}$PING_PATH")
43-
val connection = (url.openConnection() as HttpURLConnection).apply {
44-
requestMethod = "GET"
40+
suspend fun fetchPingLog(): Log {
41+
val date = getCurrentDate()
4542

46-
readTimeout = DEFAULT_TIMEOUT
47-
connectTimeout = DEFAULT_TIMEOUT
48-
49-
setRequestProperty("Content-Type", "application/json")
50-
setRequestProperty("x-appwrite-response-format", APPWRITE_VERSION)
51-
setRequestProperty("x-appwrite-project", APPWRITE_PROJECT_ID)
52-
}
53-
54-
try {
55-
val statusCode = connection.responseCode
56-
val response = if (statusCode == HttpURLConnection.HTTP_OK) {
57-
connection.inputStream.bufferedReader().use(BufferedReader::readText)
58-
} else {
59-
"Request failed with status code $statusCode"
60-
}
43+
return try {
44+
val response = withContext(Dispatchers.IO) { client.ping() }
6145

6246
Log(
63-
date = getCurrentDate(),
64-
status = statusCode.toString(),
47+
date = date,
48+
status = "200",
6549
method = "GET",
66-
path = PING_PATH,
50+
path = "/ping",
6751
response = response
6852
)
69-
} catch (e: Exception) {
53+
} catch (exception: AppwriteException) {
7054
Log(
71-
date = getCurrentDate(),
72-
status = "Error",
55+
date = date,
7356
method = "GET",
74-
path = PING_PATH,
75-
response = "Error occurred: ${e.message}"
57+
path = "/ping",
58+
status = "${exception.code}",
59+
response = "Error occurred: ${exception.message}"
7660
)
77-
} finally {
78-
connection.disconnect()
7961
}
8062
}
8163

@@ -93,36 +75,6 @@ class AppwriteRepository private constructor(context: Context) {
9375
@Volatile
9476
private var INSTANCE: AppwriteRepository? = null
9577

96-
/**
97-
* Appwrite Server version.
98-
*/
99-
const val APPWRITE_VERSION = "1.6.0"
100-
101-
/**
102-
* Appwrite project id
103-
*/
104-
const val APPWRITE_PROJECT_ID = "my-project-id"
105-
106-
/**
107-
* Appwrite project name
108-
*/
109-
const val APPWRITE_PROJECT_NAME = "My project"
110-
111-
/**
112-
* Appwrite server endpoint url.
113-
*/
114-
const val APPWRITE_PUBLIC_ENDPOINT = "https://cloud.appwrite.io/v1"
115-
116-
/**
117-
* The path to use for ping.
118-
*/
119-
const val PING_PATH = "/ping"
120-
121-
/**
122-
* Default network timeout.
123-
*/
124-
const val DEFAULT_TIMEOUT = 5_000
125-
12678
/**
12779
* Singleton factory method to get the instance of AppwriteRepository.
12880
* Ensures thread safety

app/src/main/java/io/appwrite/starterkit/viewmodels/AppwriteViewModel.kt

+5-8
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ package io.appwrite.starterkit.viewmodels
22

33
import android.app.Application
44
import androidx.lifecycle.AndroidViewModel
5+
import io.appwrite.starterkit.constants.AppwriteConfig
56
import io.appwrite.starterkit.data.models.Log
67
import io.appwrite.starterkit.data.models.ProjectInfo
78
import io.appwrite.starterkit.data.models.Status
89
import io.appwrite.starterkit.data.repository.AppwriteRepository
9-
import io.appwrite.starterkit.data.repository.AppwriteRepository.Companion.APPWRITE_PROJECT_ID
10-
import io.appwrite.starterkit.data.repository.AppwriteRepository.Companion.APPWRITE_PROJECT_NAME
11-
import io.appwrite.starterkit.data.repository.AppwriteRepository.Companion.APPWRITE_PUBLIC_ENDPOINT
12-
import io.appwrite.starterkit.data.repository.AppwriteRepository.Companion.APPWRITE_VERSION
1310
import kotlinx.coroutines.delay
1411
import kotlinx.coroutines.flow.MutableStateFlow
1512
import kotlinx.coroutines.flow.StateFlow
@@ -36,10 +33,10 @@ class AppwriteViewModel(application: Application) : AndroidViewModel(application
3633
*/
3734
fun getProjectInfo(): ProjectInfo {
3835
return ProjectInfo(
39-
version = APPWRITE_VERSION,
40-
projectId = APPWRITE_PROJECT_ID,
41-
endpoint = APPWRITE_PUBLIC_ENDPOINT,
42-
projectName = APPWRITE_PROJECT_NAME
36+
version = AppwriteConfig.APPWRITE_VERSION,
37+
projectId = AppwriteConfig.APPWRITE_PROJECT_ID,
38+
endpoint = AppwriteConfig.APPWRITE_PUBLIC_ENDPOINT,
39+
projectName = AppwriteConfig.APPWRITE_PROJECT_NAME
4340
)
4441
}
4542

gradle/libs.versions.toml

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ kotlin = "2.0.0"
44
coreKtx = "1.15.0"
55
splashScreen = "1.2.0-alpha02"
66
lifecycleRuntimeKtx = "2.8.7"
7-
activityCompose = "1.9.3"
8-
composeBom = "2024.12.01"
9-
appwrite = "6.1.0"
7+
activityCompose = "1.10.0"
8+
composeBom = "2025.01.01"
9+
appwrite = "7.0.0"
1010

1111
[libraries]
1212
appwrite = { group = "io.appwrite", name = "sdk-for-android", version.ref = "appwrite" }

0 commit comments

Comments
 (0)