Skip to content

Commit df7a55f

Browse files
committed
Upload avatar
1 parent 9d7b106 commit df7a55f

File tree

2 files changed

+107
-27
lines changed

2 files changed

+107
-27
lines changed
Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,14 @@
11
package com.mushare.demoapp.ui.login;
22

33
import android.app.Activity
4-
import android.graphics.Bitmap
5-
import android.graphics.BitmapFactory
64
import android.os.Bundle
7-
import android.util.Base64
85
import android.util.Log
96
import android.widget.*
107
import androidx.appcompat.app.AppCompatActivity
118
import com.bumptech.glide.Glide
129
import com.github.dhaval2404.imagepicker.ImagePicker
1310
import com.mushare.demoapp.R
14-
import com.mushare.plutosdk.Pluto
15-
import com.mushare.plutosdk.getToken
16-
import com.mushare.plutosdk.myInfo
17-
import com.mushare.plutosdk.updateName
18-
import java.io.ByteArrayOutputStream
11+
import com.mushare.plutosdk.*
1912
import java.lang.ref.WeakReference
2013

2114
class ProfileActivity : AppCompatActivity() {
@@ -35,12 +28,7 @@ class ProfileActivity : AppCompatActivity() {
3528
nameEditText = WeakReference(findViewById(R.id.profile_name))
3629
avatarImageView = WeakReference(findViewById(R.id.profile_avatar))
3730

38-
Pluto.getInstance()?.myInfo(success = { user ->
39-
nameEditText.get()?.setText(user.name)
40-
avatarImageView.get()?.let {
41-
Glide.with(this).load(user.avatar).into(it)
42-
}
43-
})
31+
updateUserInfo()
4432

4533
Pluto.getInstance()?.getToken(completion = {
4634
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
@@ -52,17 +40,36 @@ class ProfileActivity : AppCompatActivity() {
5240
.compress(64)
5341
.maxResultSize(480, 480)
5442
.start { resultCode, data ->
55-
if (resultCode == Activity.RESULT_OK) {
56-
val filePath = ImagePicker.getFilePath(data) ?: return@start
57-
val bitmap = BitmapFactory.decodeFile(filePath)
58-
val outputStream = ByteArrayOutputStream()
59-
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
60-
val base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT)
61-
Log.d(TAG, "base64: $base64")
62-
} else if (resultCode == ImagePicker.RESULT_ERROR) {
63-
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
64-
} else {
65-
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
43+
when (resultCode) {
44+
Activity.RESULT_OK -> {
45+
Pluto.getInstance()?.uploadAvatar(
46+
imageFile = ImagePicker.getFile(data) ?: return@start,
47+
success = {
48+
updateUserInfo {
49+
Toast
50+
.makeText(
51+
this,
52+
"Avatar uploaded",
53+
Toast.LENGTH_SHORT
54+
)
55+
.show()
56+
}
57+
},
58+
error = {
59+
Log.d(TAG, "Error uploading avatar $it")
60+
}
61+
)
62+
}
63+
ImagePicker.RESULT_ERROR -> {
64+
Toast
65+
.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT)
66+
.show()
67+
}
68+
else -> {
69+
Toast
70+
.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT)
71+
.show()
72+
}
6673
}
6774
}
6875
}
@@ -72,9 +79,9 @@ class ProfileActivity : AppCompatActivity() {
7279
Pluto.getInstance()?.updateName(
7380
name = name,
7481
success = {
75-
Pluto.getInstance()?.myInfo(success = {
82+
updateUserInfo {
7683
Toast.makeText(this, "Name updated", Toast.LENGTH_SHORT).show()
77-
})
84+
}
7885
},
7986
error = {
8087
Log.d(TAG, "Error updating username $it")
@@ -88,4 +95,15 @@ class ProfileActivity : AppCompatActivity() {
8895
})
8996
}
9097
}
98+
99+
private fun updateUserInfo(completion: (() -> Unit)? = null) {
100+
Pluto.getInstance()?.myInfo(success = { user ->
101+
nameEditText.get()?.setText(user.name)
102+
avatarImageView.get()?.let {
103+
Glide.with(this).load(user.avatar).into(it)
104+
}
105+
completion?.let { it() }
106+
})
107+
108+
}
91109
}

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/Pluto+User.kt

Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,13 @@
11
package com.mushare.plutosdk
22

3+
import android.graphics.Bitmap
4+
import android.graphics.BitmapFactory
5+
import android.util.Base64
36
import retrofit2.Call
47
import retrofit2.Callback
58
import retrofit2.Response
9+
import java.io.ByteArrayOutputStream
10+
import java.io.File
611

712
fun Pluto.myInfo(
813
success: (PlutoUser) -> Unit,
@@ -110,3 +115,60 @@ fun Pluto.updateName(
110115
handler = handler
111116
)
112117
}
118+
119+
fun Pluto.uploadAvatar(
120+
imageFile: File,
121+
success: () -> Unit,
122+
error: ((PlutoError) -> Unit)? = null,
123+
handler: Pluto.PlutoRequestHandler? = null
124+
) {
125+
val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
126+
val outputStream = ByteArrayOutputStream()
127+
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
128+
val base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT)
129+
getAuthorizationHeader(
130+
completion = { header ->
131+
if (header == null) {
132+
handler?.setCall(null)
133+
error?.invoke(PlutoError.notSignin)
134+
return@getAuthorizationHeader
135+
}
136+
137+
val body = UpdateUserInfoPutData(null, base64)
138+
plutoService.updateUserInfo(body, header).apply {
139+
enqueue(object : Callback<PlutoResponse> {
140+
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
141+
t.printStackTrace()
142+
error?.invoke(PlutoError.badRequest)
143+
}
144+
145+
override fun onResponse(
146+
call: Call<PlutoResponse>,
147+
response: Response<PlutoResponse>
148+
) {
149+
val plutoResponse = response.body()
150+
if (plutoResponse != null) {
151+
if (plutoResponse.statusOK()) {
152+
success()
153+
} else {
154+
error?.invoke(plutoResponse.errorCode())
155+
}
156+
} else {
157+
error?.invoke(
158+
parseErrorCodeFromErrorBody(
159+
response.errorBody(),
160+
gson
161+
)
162+
)
163+
}
164+
}
165+
})
166+
}.also {
167+
handler?.setCall(it)
168+
}
169+
},
170+
handler = handler
171+
)
172+
}
173+
174+

0 commit comments

Comments
 (0)