-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathProfile.kt
79 lines (67 loc) · 2.92 KB
/
Profile.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
78
79
package com.converse.dev
import android.content.Context
import android.util.Log
import com.android.volley.Request
import com.android.volley.toolbox.JsonObjectRequest
import com.android.volley.toolbox.Volley
import com.beust.klaxon.Klaxon
import kotlinx.coroutines.suspendCancellableCoroutine
import org.web3j.crypto.Keys
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
suspend fun getProfile(appContext: Context, account: String, address: String): Profile? {
var profileState = getProfilesStore(appContext, account)?.state
var lowercasedAddress = address.lowercase()
var formattedAddress = Keys.toChecksumAddress(address)
profileState?.profiles?.get(address)?.let { return it }
profileState?.profiles?.get(formattedAddress)?.let { return it }
profileState?.profiles?.get(lowercasedAddress)?.let { return it }
// If profile is nil, let's refresh it
try {
refreshProfileFromBackend(appContext, account, formattedAddress)
} catch (e: Exception) {
// Handle exception if needed
}
profileState = getProfilesStore(appContext, account)?.state
return profileState?.profiles?.get(formattedAddress)
}
suspend fun refreshProfileFromBackend(appContext: Context, account: String, address: String) {
val mmkv = getMmkv(appContext)
var apiURI = mmkv?.decodeString("api-uri")
if (!apiURI.isNullOrEmpty()) {
val profileURI = "$apiURI/api/profile?address=$address"
val response = suspendCancellableCoroutine<ByteArray> { continuation ->
val request = JsonObjectRequest(
Request.Method.GET, profileURI, null,
{ response ->
continuation.resume(response.toString().toByteArray())
},
{ error ->
// Log the error details
val networkResponse = error.networkResponse
if (networkResponse != null) {
val statusCode = networkResponse.statusCode
val data = networkResponse.data
val errorMessage = String(data)
Log.e("Volley Error", "Status code: $statusCode, Error message: $errorMessage")
} else {
Log.e("Volley Error", "Error: ${error.message}")
}
continuation.resumeWithException(error)
}
)
Volley.newRequestQueue(appContext).add(request)
continuation.invokeOnCancellation {
request.cancel()
}
}
try {
val socials = Klaxon().parse<ProfileSocials>(response.toString(Charsets.UTF_8))
socials?.let {
saveProfileSocials(appContext, account, address, socials)
}
} catch (e: Exception) {
Log.d("GetProfilesState", "Could not parse returned profile value from backend")
}
}
}