Skip to content

Commit 6ada2f3

Browse files
authored
Merge pull request #7 from MuShare/develop/update-user
Update username
2 parents 64e90aa + 16b0e9e commit 6ada2f3

File tree

6 files changed

+185
-29
lines changed

6 files changed

+185
-29
lines changed

demoapp/src/main/java/com/mushare/demoapp/ui/login/LoginActivity.kt

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class LoginActivity : AppCompatActivity() {
3030

3131
Pluto.initialize(
3232
this,
33-
"https://staging.easyjapanese-api-gateway.mushare.cn/pluto-master/",
33+
"https://beta-pluto.kaboocha.com/",
3434
"org.mushare.easyjapanese"
3535
)
3636

@@ -104,9 +104,14 @@ class LoginActivity : AppCompatActivity() {
104104
}
105105
}
106106

107-
if (Pluto.getInstance()?.state == Pluto.State.signin) {
108-
startActivity(Intent(this, ProfileActivity::class.java))
109-
}
107+
Pluto.getInstance()?.state?.observe(this, Observer {
108+
when (it) {
109+
Pluto.State.signin -> {
110+
startActivity(Intent(this, ProfileActivity::class.java))
111+
}
112+
}
113+
})
114+
110115
}
111116

112117
private fun updateUiWithUser(model: LoggedInUserView) {

demoapp/src/main/java/com/mushare/demoapp/ui/login/ProfileActivity.kt

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,57 @@
11
package com.mushare.demoapp.ui.login;
22

33
import android.os.Bundle
4+
import android.util.Log
45
import android.widget.Button
6+
import android.widget.EditText
57
import android.widget.TextView
68
import android.widget.Toast
79
import androidx.appcompat.app.AppCompatActivity
810
import com.mushare.demoapp.R
911
import com.mushare.plutosdk.Pluto
1012
import com.mushare.plutosdk.getToken
1113
import com.mushare.plutosdk.myInfo
14+
import com.mushare.plutosdk.updateName
15+
import java.lang.ref.WeakReference
1216

1317
class ProfileActivity : AppCompatActivity() {
1418

19+
companion object {
20+
private val TAG = ProfileActivity::class.java.simpleName
21+
}
22+
23+
private lateinit var nameEditText: WeakReference<EditText>
24+
1525
override fun onCreate(savedInstanceState: Bundle?) {
1626
super.onCreate(savedInstanceState)
1727

1828
setContentView(R.layout.activity_profile)
1929

30+
nameEditText = WeakReference(findViewById<EditText>(R.id.profile_name))
31+
2032
Pluto.getInstance()?.myInfo(success = {
21-
findViewById<TextView>(R.id.profile_name).text = it.name
33+
nameEditText.get()?.setText(it.name)
2234
})
2335

2436
Pluto.getInstance()?.getToken(completion = {
2537
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
2638
})
2739

40+
findViewById<Button>(R.id.profile_update_name).setOnClickListener {
41+
val name = nameEditText.get()?.text.toString() ?: return@setOnClickListener
42+
Pluto.getInstance()?.updateName(
43+
name = name,
44+
success = {
45+
Pluto.getInstance()?.myInfo(success = {
46+
Toast.makeText(this, "Name updated", Toast.LENGTH_SHORT).show()
47+
})
48+
},
49+
error = {
50+
Log.d(TAG, "Error updating username $it")
51+
}
52+
)
53+
}
54+
2855
findViewById<Button>(R.id.profile_refresh_token).setOnClickListener {
2956
Pluto.getInstance()?.getToken(isForceRefresh = true, completion = {
3057
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"

demoapp/src/main/res/layout/activity_profile.xml

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,23 +4,34 @@
44
android:layout_height="match_parent"
55
xmlns:app="http://schemas.android.com/apk/res-auto">
66

7-
<TextView
7+
<EditText
88
android:id="@+id/profile_name"
99
android:layout_width="match_parent"
1010
android:layout_height="wrap_content"
1111
app:layout_constraintTop_toTopOf="parent"
1212
android:layout_marginStart="20dp"
1313
android:layout_marginTop="20dp"
1414
android:layout_marginEnd="20dp"
15-
android:text="Username"/>
15+
android:hint="Username"/>
1616

1717
<Button
18-
android:id="@+id/profile_refresh_token"
18+
android:id="@+id/profile_update_name"
1919
android:layout_width="match_parent"
2020
android:layout_height="wrap_content"
2121
app:layout_constraintTop_toBottomOf="@id/profile_name"
2222
android:layout_marginStart="20dp"
23-
android:layout_marginTop="20dp"
23+
android:layout_marginTop="10dp"
24+
android:layout_marginEnd="20dp"
25+
android:text="Update Username"
26+
/>
27+
28+
<Button
29+
android:id="@+id/profile_refresh_token"
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
app:layout_constraintTop_toBottomOf="@id/profile_update_name"
33+
android:layout_marginStart="20dp"
34+
android:layout_marginTop="10dp"
2435
android:layout_marginEnd="20dp"
2536
android:text="Refresh Token"/>
2637

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

Lines changed: 37 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,14 @@ import retrofit2.Call
55
import retrofit2.Callback
66
import retrofit2.Response
77

8-
fun Pluto.registerByEmail(address: String, password: String, name: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
8+
fun Pluto.registerByEmail(
9+
address: String,
10+
password: String,
11+
name: String,
12+
success: () -> Unit,
13+
error: ((PlutoError) -> Unit)? = null,
14+
handler: Pluto.PlutoRequestHandler? = null
15+
) {
916
plutoService.registerWithEmail(RegisterWithEmailPostData(address, password, name, appId), getLanguage()).apply {
1017
enqueue(object : Callback<PlutoResponse> {
1118
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
@@ -31,7 +38,12 @@ fun Pluto.registerByEmail(address: String, password: String, name: String, succe
3138
}
3239
}
3340

34-
fun Pluto.resendValidationEmail(address: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
41+
fun Pluto.resendValidationEmail(
42+
address: String,
43+
success: () -> Unit,
44+
error: ((PlutoError) -> Unit)? = null,
45+
handler: Pluto.PlutoRequestHandler? = null
46+
) {
3547
plutoService.resendValidationEmail(EmailPostData(address, appId), getLanguage()).apply {
3648
enqueue(object : Callback<PlutoResponse> {
3749
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
@@ -57,7 +69,13 @@ fun Pluto.resendValidationEmail(address: String, success: () -> Unit, error: ((P
5769
}
5870
}
5971

60-
fun Pluto.loginWithAccount(address: String, password: String, success: (() -> Unit)? = null, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
72+
fun Pluto.loginWithAccount(
73+
address: String,
74+
password: String,
75+
success: (() -> Unit)? = null,
76+
error: ((PlutoError) -> Unit)? = null,
77+
handler: Pluto.PlutoRequestHandler? = null
78+
) {
6179
val deviceId = data.deviceID
6280
if (deviceId == null) {
6381
error?.invoke(PlutoError.badRequest)
@@ -84,7 +102,12 @@ fun Pluto.loginWithAccount(address: String, password: String, success: (() -> Un
84102
}
85103
}
86104

87-
fun Pluto.loginWithGoogle(idToken: String, success: (() -> Unit)? = null, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
105+
fun Pluto.loginWithGoogle(
106+
idToken: String,
107+
success: (() -> Unit)? = null,
108+
error: ((PlutoError) -> Unit)? = null,
109+
handler: Pluto.PlutoRequestHandler? = null
110+
) {
88111
val deviceId = data.deviceID
89112
if (deviceId == null) {
90113
error?.invoke(PlutoError.badRequest)
@@ -111,7 +134,12 @@ fun Pluto.loginWithGoogle(idToken: String, success: (() -> Unit)? = null, error:
111134
}
112135
}
113136

114-
fun Pluto.resetPassword(address: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
137+
fun Pluto.resetPassword(
138+
address: String,
139+
success: () -> Unit,
140+
error: ((PlutoError) -> Unit)? = null,
141+
handler: Pluto.PlutoRequestHandler? = null
142+
) {
115143
plutoService.resetPassword(EmailPostData(address, appId), getLanguage()).apply {
116144
enqueue(object : Callback<PlutoResponse> {
117145
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
@@ -142,7 +170,10 @@ fun Pluto.logout() {
142170
state.postValue(Pluto.State.notSignin)
143171
}
144172

145-
private fun Pluto.handleLogin(response: PlutoResponseWithBody<LoginResponse>, success: (() -> Unit)?, error: ((PlutoError) -> Unit)?) {
173+
private fun Pluto.handleLogin(
174+
response: PlutoResponseWithBody<LoginResponse>,
175+
success: (() -> Unit)?, error: ((PlutoError) -> Unit)?
176+
) {
146177
if (response.statusOK()) {
147178
val body = response.getBody()
148179
data.updateRefreshToken(body.refreshToken)

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

Lines changed: 84 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,38 @@ package com.mushare.plutosdk
33
import retrofit2.Call
44
import retrofit2.Callback
55
import retrofit2.Response
6-
fun Pluto.myInfo(success: (PlutoUser) -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
6+
7+
fun Pluto.myInfo(
8+
success: (PlutoUser) -> Unit,
9+
error: ((PlutoError) -> Unit)? = null,
10+
handler: Pluto.PlutoRequestHandler? = null
11+
) {
712
data.user?.let {
813
success(it)
914
return
1015
}
11-
getAuthorizationHeader({ header ->
12-
if (header != null) {
13-
plutoService.getAccountInfo(header).apply {
16+
getAuthorizationHeader(
17+
completion = { header ->
18+
if (header == null) {
19+
handler?.setCall(null)
20+
error?.invoke(PlutoError.notSignin)
21+
return@getAuthorizationHeader
22+
}
23+
24+
plutoService.getUserInfo(header).apply {
1425
enqueue(object : Callback<PlutoResponseWithBody<PlutoUser>> {
15-
override fun onFailure(call: Call<PlutoResponseWithBody<PlutoUser>>, t: Throwable) {
26+
override fun onFailure(
27+
call: Call<PlutoResponseWithBody<PlutoUser>>,
28+
t: Throwable
29+
) {
1630
t.printStackTrace()
1731
error?.invoke(PlutoError.badRequest)
1832
}
1933

20-
override fun onResponse(call: Call<PlutoResponseWithBody<PlutoUser>>, response: Response<PlutoResponseWithBody<PlutoUser>>) {
34+
override fun onResponse(
35+
call: Call<PlutoResponseWithBody<PlutoUser>>,
36+
response: Response<PlutoResponseWithBody<PlutoUser>>
37+
) {
2138
val plutoResponse = response.body()
2239
if (plutoResponse != null) {
2340
if (plutoResponse.statusOK()) {
@@ -26,16 +43,70 @@ fun Pluto.myInfo(success: (PlutoUser) -> Unit, error: ((PlutoError) -> Unit)? =
2643
error?.invoke(plutoResponse.errorCode())
2744
}
2845
} else {
29-
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
46+
error?.invoke(
47+
parseErrorCodeFromErrorBody(
48+
response.errorBody(),
49+
gson
50+
)
51+
)
52+
}
53+
}
54+
})
55+
}.also {
56+
handler?.setCall(it)
57+
}
58+
},
59+
handler = handler
60+
)
61+
}
62+
63+
fun Pluto.updateName(
64+
name: String,
65+
success: () -> Unit,
66+
error: ((PlutoError) -> Unit)? = null,
67+
handler: Pluto.PlutoRequestHandler? = null
68+
) {
69+
getAuthorizationHeader(
70+
completion = { header ->
71+
if (header == null) {
72+
handler?.setCall(null)
73+
error?.invoke(PlutoError.notSignin)
74+
return@getAuthorizationHeader
75+
}
76+
77+
val body = UpdateUserInfoPutData(name, null)
78+
plutoService.updateUserInfo(body, header).apply {
79+
enqueue(object : Callback<PlutoResponse> {
80+
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
81+
t.printStackTrace()
82+
error?.invoke(PlutoError.badRequest)
83+
}
84+
85+
override fun onResponse(
86+
call: Call<PlutoResponse>,
87+
response: Response<PlutoResponse>
88+
) {
89+
val plutoResponse = response.body()
90+
if (plutoResponse != null) {
91+
if (plutoResponse.statusOK()) {
92+
success()
93+
} else {
94+
error?.invoke(plutoResponse.errorCode())
95+
}
96+
} else {
97+
error?.invoke(
98+
parseErrorCodeFromErrorBody(
99+
response.errorBody(),
100+
gson
101+
)
102+
)
30103
}
31104
}
32105
})
33106
}.also {
34107
handler?.setCall(it)
35108
}
36-
} else {
37-
handler?.setCall(null)
38-
error?.invoke(PlutoError.notSignin)
39-
}
40-
}, handler)
41-
}
109+
},
110+
handler = handler
111+
)
112+
}

pluto-kotlin-client-sdk/src/main/java/com/mushare/plutosdk/PlutoService.kt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,15 @@ interface PlutoService {
3939
): Call<PlutoResponseWithBody<LoginResponse>>
4040

4141
@GET("v1/user/info")
42-
fun getAccountInfo(
42+
fun getUserInfo(
4343
@HeaderMap authorizationHeader: Map<String, String>
4444
): Call<PlutoResponseWithBody<PlutoUser>>
45+
46+
@PUT("v1/user/info")
47+
fun updateUserInfo(
48+
@Body body: UpdateUserInfoPutData,
49+
@HeaderMap authorizationHeader: Map<String, String>
50+
): Call<PlutoResponse>
4551
}
4652

4753
class RefreshAuthPostData(
@@ -72,4 +78,9 @@ class LoginWithGooglePostData(
7278
@field:SerializedName("id_token") var idToken: String,
7379
@field:SerializedName("device_id") var deviceId: String,
7480
@field:SerializedName("app_id") var appId: String
81+
)
82+
83+
class UpdateUserInfoPutData(
84+
@field:SerializedName("name") var name: String?,
85+
@field:SerializedName("avatar") var avatar: String?
7586
)

0 commit comments

Comments
 (0)