Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class LoginActivity : AppCompatActivity() {

Pluto.initialize(
this,
"https://staging.easyjapanese-api-gateway.mushare.cn/pluto-master/",
"https://beta-pluto.kaboocha.com/",
"org.mushare.easyjapanese"
)

Expand Down Expand Up @@ -104,9 +104,14 @@ class LoginActivity : AppCompatActivity() {
}
}

if (Pluto.getInstance()?.state == Pluto.State.signin) {
startActivity(Intent(this, ProfileActivity::class.java))
}
Pluto.getInstance()?.state?.observe(this, Observer {
when (it) {
Pluto.State.signin -> {
startActivity(Intent(this, ProfileActivity::class.java))
}
}
})

}

private fun updateUiWithUser(model: LoggedInUserView) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,57 @@
package com.mushare.demoapp.ui.login;

import android.os.Bundle
import android.util.Log
import android.widget.Button
import android.widget.EditText
import android.widget.TextView
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.mushare.demoapp.R
import com.mushare.plutosdk.Pluto
import com.mushare.plutosdk.getToken
import com.mushare.plutosdk.myInfo
import com.mushare.plutosdk.updateName
import java.lang.ref.WeakReference

class ProfileActivity : AppCompatActivity() {

companion object {
private val TAG = ProfileActivity::class.java.simpleName
}

private lateinit var nameEditText: WeakReference<EditText>

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

setContentView(R.layout.activity_profile)

nameEditText = WeakReference(findViewById<EditText>(R.id.profile_name))

Pluto.getInstance()?.myInfo(success = {
findViewById<TextView>(R.id.profile_name).text = it.name
nameEditText.get()?.setText(it.name)
})

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

findViewById<Button>(R.id.profile_update_name).setOnClickListener {
val name = nameEditText.get()?.text.toString() ?: return@setOnClickListener
Pluto.getInstance()?.updateName(
name = name,
success = {
Pluto.getInstance()?.myInfo(success = {
Toast.makeText(this, "Name updated", Toast.LENGTH_SHORT).show()
})
},
error = {
Log.d(TAG, "Error updating username $it")
}
)
}

findViewById<Button>(R.id.profile_refresh_token).setOnClickListener {
Pluto.getInstance()?.getToken(isForceRefresh = true, completion = {
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
Expand Down
19 changes: 15 additions & 4 deletions demoapp/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,34 @@
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<TextView
<EditText
android:id="@+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Username"/>
android:hint="Username"/>

<Button
android:id="@+id/profile_refresh_token"
android:id="@+id/profile_update_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/profile_name"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:text="Update Username"
/>

<Button
android:id="@+id/profile_refresh_token"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/profile_update_name"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
android:text="Refresh Token"/>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,14 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

fun Pluto.registerByEmail(address: String, password: String, name: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
fun Pluto.registerByEmail(
address: String,
password: String,
name: String,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
plutoService.registerWithEmail(RegisterWithEmailPostData(address, password, name, appId), getLanguage()).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
Expand All @@ -31,7 +38,12 @@ fun Pluto.registerByEmail(address: String, password: String, name: String, succe
}
}

fun Pluto.resendValidationEmail(address: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
fun Pluto.resendValidationEmail(
address: String,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
plutoService.resendValidationEmail(EmailPostData(address, appId), getLanguage()).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
Expand All @@ -57,7 +69,13 @@ fun Pluto.resendValidationEmail(address: String, success: () -> Unit, error: ((P
}
}

fun Pluto.loginWithAccount(address: String, password: String, success: (() -> Unit)? = null, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
fun Pluto.loginWithAccount(
address: String,
password: String,
success: (() -> Unit)? = null,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
val deviceId = data.deviceID
if (deviceId == null) {
error?.invoke(PlutoError.badRequest)
Expand All @@ -84,7 +102,12 @@ fun Pluto.loginWithAccount(address: String, password: String, success: (() -> Un
}
}

fun Pluto.loginWithGoogle(idToken: String, success: (() -> Unit)? = null, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
fun Pluto.loginWithGoogle(
idToken: String,
success: (() -> Unit)? = null,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
val deviceId = data.deviceID
if (deviceId == null) {
error?.invoke(PlutoError.badRequest)
Expand All @@ -111,7 +134,12 @@ fun Pluto.loginWithGoogle(idToken: String, success: (() -> Unit)? = null, error:
}
}

fun Pluto.resetPassword(address: String, success: () -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {
fun Pluto.resetPassword(
address: String,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
plutoService.resetPassword(EmailPostData(address, appId), getLanguage()).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
Expand Down Expand Up @@ -142,7 +170,10 @@ fun Pluto.logout() {
state.postValue(Pluto.State.notSignin)
}

private fun Pluto.handleLogin(response: PlutoResponseWithBody<LoginResponse>, success: (() -> Unit)?, error: ((PlutoError) -> Unit)?) {
private fun Pluto.handleLogin(
response: PlutoResponseWithBody<LoginResponse>,
success: (() -> Unit)?, error: ((PlutoError) -> Unit)?
) {
if (response.statusOK()) {
val body = response.getBody()
data.updateRefreshToken(body.refreshToken)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,38 @@ package com.mushare.plutosdk
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
fun Pluto.myInfo(success: (PlutoUser) -> Unit, error: ((PlutoError) -> Unit)? = null, handler: Pluto.PlutoRequestHandler? = null) {

fun Pluto.myInfo(
success: (PlutoUser) -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
data.user?.let {
success(it)
return
}
getAuthorizationHeader({ header ->
if (header != null) {
plutoService.getAccountInfo(header).apply {
getAuthorizationHeader(
completion = { header ->
if (header == null) {
handler?.setCall(null)
error?.invoke(PlutoError.notSignin)
return@getAuthorizationHeader
}

plutoService.getUserInfo(header).apply {
enqueue(object : Callback<PlutoResponseWithBody<PlutoUser>> {
override fun onFailure(call: Call<PlutoResponseWithBody<PlutoUser>>, t: Throwable) {
override fun onFailure(
call: Call<PlutoResponseWithBody<PlutoUser>>,
t: Throwable
) {
t.printStackTrace()
error?.invoke(PlutoError.badRequest)
}

override fun onResponse(call: Call<PlutoResponseWithBody<PlutoUser>>, response: Response<PlutoResponseWithBody<PlutoUser>>) {
override fun onResponse(
call: Call<PlutoResponseWithBody<PlutoUser>>,
response: Response<PlutoResponseWithBody<PlutoUser>>
) {
val plutoResponse = response.body()
if (plutoResponse != null) {
if (plutoResponse.statusOK()) {
Expand All @@ -26,16 +43,70 @@ fun Pluto.myInfo(success: (PlutoUser) -> Unit, error: ((PlutoError) -> Unit)? =
error?.invoke(plutoResponse.errorCode())
}
} else {
error?.invoke(parseErrorCodeFromErrorBody(response.errorBody(), gson))
error?.invoke(
parseErrorCodeFromErrorBody(
response.errorBody(),
gson
)
)
}
}
})
}.also {
handler?.setCall(it)
}
},
handler = handler
)
}

fun Pluto.updateName(
name: String,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
getAuthorizationHeader(
completion = { header ->
if (header == null) {
handler?.setCall(null)
error?.invoke(PlutoError.notSignin)
return@getAuthorizationHeader
}

val body = UpdateUserInfoPutData(name, null)
plutoService.updateUserInfo(body, header).apply {
enqueue(object : Callback<PlutoResponse> {
override fun onFailure(call: Call<PlutoResponse>, t: Throwable) {
t.printStackTrace()
error?.invoke(PlutoError.badRequest)
}

override fun onResponse(
call: Call<PlutoResponse>,
response: Response<PlutoResponse>
) {
val plutoResponse = response.body()
if (plutoResponse != null) {
if (plutoResponse.statusOK()) {
success()
} else {
error?.invoke(plutoResponse.errorCode())
}
} else {
error?.invoke(
parseErrorCodeFromErrorBody(
response.errorBody(),
gson
)
)
}
}
})
}.also {
handler?.setCall(it)
}
} else {
handler?.setCall(null)
error?.invoke(PlutoError.notSignin)
}
}, handler)
}
},
handler = handler
)
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,15 @@ interface PlutoService {
): Call<PlutoResponseWithBody<LoginResponse>>

@GET("v1/user/info")
fun getAccountInfo(
fun getUserInfo(
@HeaderMap authorizationHeader: Map<String, String>
): Call<PlutoResponseWithBody<PlutoUser>>

@PUT("v1/user/info")
fun updateUserInfo(
@Body body: UpdateUserInfoPutData,
@HeaderMap authorizationHeader: Map<String, String>
): Call<PlutoResponse>
}

class RefreshAuthPostData(
Expand Down Expand Up @@ -72,4 +78,9 @@ class LoginWithGooglePostData(
@field:SerializedName("id_token") var idToken: String,
@field:SerializedName("device_id") var deviceId: String,
@field:SerializedName("app_id") var appId: String
)

class UpdateUserInfoPutData(
@field:SerializedName("name") var name: String?,
@field:SerializedName("avatar") var avatar: String?
)