11package com.mushare.demoapp.ui.login ;
22
33import android.app.Activity
4- import android.graphics.Bitmap
5- import android.graphics.BitmapFactory
64import android.os.Bundle
7- import android.util.Base64
85import android.util.Log
96import android.widget.*
107import androidx.appcompat.app.AppCompatActivity
118import com.bumptech.glide.Glide
129import com.github.dhaval2404.imagepicker.ImagePicker
1310import 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.*
1912import java.lang.ref.WeakReference
2013
2114class 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}
0 commit comments