forked from raj-ship-it/Orate-for-ineuron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeViewModel.kt
77 lines (67 loc) · 2.27 KB
/
HomeViewModel.kt
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
package live.hms.app2.ui.home
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import kotlinx.coroutines.launch
import live.hms.app2.api.Resource
import live.hms.app2.api.RetrofitBuilder
import live.hms.app2.model.TokenResponse
import live.hms.app2.util.*
import okhttp3.Request
class HomeViewModel : ViewModel() {
private val repository = HomeRepository()
val authTokenResponse = MutableLiveData<Resource<TokenResponse>>()
fun sendAuthTokenRequest(url: String) {
try {
val env = url.getTokenEndpointEnvironment()
val subdomain = url.toSubdomain()
when {
REGEX_MEETING_URL_CODE.matches(url) -> {
val groups = REGEX_MEETING_URL_CODE.findAll(url).toList()[0].groupValues
val code = groups[2]
sendAuthTokenRequest(subdomain, code, env)
}
REGEX_MEETING_URL_ROOM_ID.matches(url) -> {
val groups = REGEX_MEETING_URL_ROOM_ID.findAll(url).toList()[0].groupValues
val roomId = groups[2]
val role = groups[3]
sendAuthTokenRequest(subdomain, roomId, role, env)
}
else -> {
authTokenResponse.postValue(Resource.error("Invalid Meeting URL"))
}
}
} catch (ex: Exception) {
authTokenResponse.postValue(Resource.error("Invalid Meeting URL [${ex.message}]"))
}
}
private fun sendAuthTokenRequest(
subdomain: String,
roomId: String,
role: String,
environment: String
) {
val request = RetrofitBuilder.makeTokenWithRoomIdRequest(subdomain, roomId, role, environment)
sendAuthTokenRequest(request)
}
private fun sendAuthTokenRequest(
subdomain: String,
code: String,
environment: String
) {
val request = RetrofitBuilder.makeTokenWithCodeRequest(subdomain, code, environment)
sendAuthTokenRequest(request)
}
private fun sendAuthTokenRequest(request: Request) {
viewModelScope.launch {
authTokenResponse.postValue(Resource.loading())
try {
val response = repository.fetchAuthToken(request)
authTokenResponse.postValue(Resource.success(response))
} catch (e: Exception) {
crashlytics.recordException(e)
authTokenResponse.postValue(Resource.error(e.message))
}
}
}
}