-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathPlaygroundViewModel.kt
More file actions
362 lines (329 loc) · 11.2 KB
/
PlaygroundViewModel.kt
File metadata and controls
362 lines (329 loc) · 11.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
package io.appwrite.playgroundforandroid
import android.content.Context
import androidx.activity.ComponentActivity
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import io.appwrite.*
import io.appwrite.exceptions.AppwriteException
import io.appwrite.extensions.toJson
import io.appwrite.models.*
import io.appwrite.services.*
import io.appwrite.services.Locale
import kotlinx.coroutines.launch
import java.io.File
import java.io.FileDescriptor
import java.io.FileInputStream
import java.io.FileOutputStream
import java.lang.System.currentTimeMillis
import io.appwrite.models.Account as AppwriteAccount
import io.appwrite.models.File as AppwriteFile
class PlaygroundViewModel : ViewModel() {
private val endpoint = "YOUR_ENDPOINT"
private val projectId = "YOUR_PROJECT_ID"
private val collectionId = "YOUR_COLLECTION_ID" // Single required 'username' string attribute
private val functionId = "YOUR_FUNCTION_ID"
private val bucketId = "YOUR_BUCKET_ID"
private var databaseId = "YOUR_DATABASE_ID"
private var documentId = ""
private var executionId = ""
private var fileId = ""
private lateinit var client: Client
private lateinit var account: Account
private lateinit var avatars: Avatars
private lateinit var databases: Databases
private lateinit var storage: Storage
private lateinit var functions: Functions
private lateinit var locale: Locale
private lateinit var teams: Teams
private lateinit var realtime: Realtime
private val _items = MutableLiveData<String>()
val items: LiveData<String> = _items
fun createClient(context: Context) {
client = Client(context)
.setEndpoint(endpoint)
.setProject(projectId)
.setSelfSigned(true)
account = Account(client)
avatars = Avatars(client)
databases = Databases(client)
storage = Storage(client)
functions = Functions(client)
locale = Locale(client)
teams = Teams(client)
realtime = Realtime(client)
}
private val _user = MutableLiveData<AppwriteAccount<Any>?>(null)
val user: LiveData<AppwriteAccount<Any>?> = _user
private val _dialogText = MutableLiveData<String?>(null)
val dialogText: LiveData<String?> = _dialogText
var emailId = currentTimeMillis()
fun createAccount() {
viewModelScope.launch {
try {
val user: AppwriteAccount<Any> = account.create(
userId = ID.unique(),
email = "$emailId@appwrite.io",
password = "password"
)
val json = user.toJson()
_dialogText.postValue(json)
_user.postValue(user)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun createSession() {
viewModelScope.launch {
try {
val session: Session = account.createEmailSession(
email = "$emailId@appwrite.io",
password = "password"
)
val json = session.toJson()
_dialogText.postValue(json)
getAccount()
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun getSession() {
viewModelScope.launch {
try {
val session: Session = account.getSession("current")
val json = session.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun getAccount() {
viewModelScope.launch {
try {
val user: AppwriteAccount<Any> = account.get()
val json = user.toJson()
_dialogText.postValue(json)
_user.postValue(user)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun createOAuth2Session(activity: ComponentActivity, provider: String) {
viewModelScope.launch {
try {
account.createOAuth2Session(activity, provider)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun updateAccountEmail() {
viewModelScope.launch {
try {
emailId = currentTimeMillis()
val user: AppwriteAccount<Any> = account.updateEmail(
email = "$emailId@email.com",
password = "password"
)
val json = user.toJson()
_user.postValue(user)
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun updateAccountPrefs() {
viewModelScope.launch {
try {
val user: AppwriteAccount<Any> = account.updatePrefs(
mapOf(
"key" to "value"
)
)
val json = user.toJson()
_user.postValue(user)
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun updateStatus() {
viewModelScope.launch {
try {
val user: AppwriteAccount<Any> = account.updateStatus()
val json = user.toJson()
_user.postValue(user)
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun deleteSession() {
viewModelScope.launch {
try {
account.deleteSession("current")
_user.postValue(null)
_dialogText.postValue("Logged out!")
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun createDocument() {
viewModelScope.launch {
try {
val document: Document<Any> = databases.createDocument(
databaseId,
collectionId,
documentId = ID.unique(),
data = mapOf(
"username" to "Android"
),
permissions = listOf(
Permission.read(Role.users()),
Permission.update(Role.users()),
Permission.delete(Role.users()),
),
)
val json = document.toJson()
documentId = document.id
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun listDocuments() {
viewModelScope.launch {
try {
val documentList: DocumentList<Any> = databases.listDocuments(
databaseId,
collectionId,
queries = listOf(
Query.equal("username", "Android")
)
)
val json = documentList.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun deleteDocument() {
viewModelScope.launch {
try {
databases.deleteDocument(
databaseId,
collectionId,
documentId
)
_dialogText.postValue("Deleted document!")
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun createExecution() {
viewModelScope.launch {
try {
val execution: Execution = functions.createExecution(
functionId,
"{}"
)
val json = execution.toJson()
executionId = execution.id
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun listExecutions() {
viewModelScope.launch {
try {
val executionList: ExecutionList = functions.listExecutions(functionId)
val json = executionList.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun getExecution() {
viewModelScope.launch {
try {
val execution: Execution = functions.getExecution(
functionId,
executionId
)
val json = execution.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun uploadFile(input: FileDescriptor, file: File) {
viewModelScope.launch {
try {
val inputStream = FileInputStream(input)
val outputStream = FileOutputStream(file)
inputStream.copyTo(outputStream)
val storageFile: AppwriteFile = storage.createFile(
bucketId,
fileId = ID.unique(),
file = InputFile.fromFile(file),
permissions = listOf(
Permission.read(Role.users()),
Permission.update(Role.users()),
Permission.delete(Role.users()),
),
)
fileId = storageFile.id
val json = storageFile.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun listFiles() {
viewModelScope.launch {
try {
val fileList: FileList = storage.listFiles(bucketId)
val json = fileList.toJson()
_dialogText.postValue(json)
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun deleteFile() {
viewModelScope.launch {
try {
storage.deleteFile(
bucketId = bucketId,
fileId = fileId
)
_dialogText.postValue("File deleted!")
} catch (e: AppwriteException) {
_dialogText.postValue(e.message)
}
}
}
fun subscribeToRealtime() {
val channel = "databases.${databaseId}.collections.${collectionId}.documents"
realtime.subscribe(channel) {
_items.postValue(it.toJson())
}
}
}