@@ -5,11 +5,10 @@ import androidx.activity.ComponentActivity
55import androidx.lifecycle.*
66import {{ sdk .namespace | caseDot }}.android.utils.Client.client
77import {{ sdk .namespace | caseDot }}.android.utils.Event
8- import {{ sdk .namespace | caseDot }}.exceptions.AppwriteException
8+ import {{ sdk .namespace | caseDot }}.exceptions.{{ spec .title | caseUcfirst }}Exception
9+ import {{ sdk .namespace | caseDot }}.extensions.toJson
910import {{ sdk .namespace | caseDot }}.services.Account
1011import kotlinx.coroutines.launch
11- import org.json.JSONObject
12-
1312
1413class AccountsViewModel : ViewModel() {
1514
@@ -27,28 +26,25 @@ class AccountsViewModel : ViewModel() {
2726 Account(client)
2827 }
2928
30- fun onLogin(email: Editable , password : Editable) {
29+ fun onLogin(email: Editable, password: Editable) {
3130 viewModelScope.launch {
3231 try {
33- var response = accountService.createSession(email.toString(), password.toString())
34- var json = response.body?.string() ?: ""
35- json = JSONObject(json).toString(8)
36- _response.postValue(Event(json))
37- } catch (e: AppwriteException) {
32+ val session = accountService.createSession(email.toString(), password.toString())
33+ _response.postValue(Event(session.toJson()))
34+ } catch (e: {{ spec .title | caseUcfirst }}Exception) {
3835 _error.postValue(Event(e))
3936 }
4037 }
4138
4239 }
4340
44- fun onSignup(email: Editable , password : Editable, name: Editable) {
41+ fun onSignup(email: Editable, password: Editable, name: Editable) {
4542 viewModelScope.launch {
4643 try {
47- var response = accountService.create(email.toString(), password.toString(), name.toString())
48- var json = response.body?.string() ?: ""
49- json = JSONObject(json).toString(2)
50- _response.postValue(Event(json))
51- } catch (e: AppwriteException) {
44+ val user =
45+ accountService.create(email.toString(), password.toString(), name.toString())
46+ _response.postValue(Event(user.toJson()))
47+ } catch (e: {{ spec .title | caseUcfirst }}Exception) {
5248 _error.postValue(Event(e))
5349 }
5450 }
@@ -58,10 +54,15 @@ class AccountsViewModel : ViewModel() {
5854 fun oAuthLogin(activity: ComponentActivity) {
5955 viewModelScope.launch {
6056 try {
61- accountService.createOAuth2Session(activity, "facebook", "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/success", "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/failure")
57+ accountService.createOAuth2Session(
58+ activity,
59+ "facebook",
60+ "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/success",
61+ "appwrite-callback-6070749e6acd4://demo.appwrite.io/auth/oauth2/failure"
62+ )
6263 } catch (e: Exception) {
6364 _error.postValue(Event(e))
64- } catch (e: AppwriteException ) {
65+ } catch (e: {{ spec . title | caseUcfirst }}Exception ) {
6566 _error.postValue(Event(e))
6667 }
6768 }
@@ -70,11 +71,9 @@ class AccountsViewModel : ViewModel() {
7071 fun getUser() {
7172 viewModelScope.launch {
7273 try {
73- var response = accountService.get()
74- var json = response.body?.string() ?: ""
75- json = JSONObject(json).toString(2)
76- _response.postValue(Event(json))
77- } catch (e: AppwriteException) {
74+ val account = accountService.get()
75+ _response.postValue(Event(account.toJson()))
76+ } catch (e: {{ spec .title | caseUcfirst }}Exception) {
7877 _error.postValue(Event(e))
7978 }
8079 }
@@ -83,14 +82,11 @@ class AccountsViewModel : ViewModel() {
8382 fun logout() {
8483 viewModelScope.launch {
8584 try {
86- var response = accountService.deleteSession("current")
87- var json = response.body?.string()?.ifEmpty { "{}" }
88- json = JSONObject(json).toString(4)
89- _response.postValue(Event(json))
90- } catch (e: AppwriteException) {
85+ val result = accountService.deleteSession("current")
86+ _response.postValue(Event(result.toJson()))
87+ } catch (e: {{ spec .title | caseUcfirst }}Exception) {
9188 _error.postValue(Event(e))
9289 }
9390 }
9491 }
95-
9692}
0 commit comments