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
15 changes: 12 additions & 3 deletions .idea/codeStyles/Project.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class ProfileActivity : AppCompatActivity() {
}

private lateinit var nameEditText: WeakReference<EditText>
private lateinit var userIdEditText: WeakReference<EditText>
private lateinit var avatarImageView: WeakReference<ImageView>

override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -26,11 +27,12 @@ class ProfileActivity : AppCompatActivity() {
setContentView(R.layout.activity_profile)

nameEditText = WeakReference(findViewById(R.id.profile_name))
userIdEditText = WeakReference(findViewById(R.id.profile_user_id))
avatarImageView = WeakReference(findViewById(R.id.profile_avatar))

updateUserInfo()

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

Expand Down Expand Up @@ -84,13 +86,28 @@ class ProfileActivity : AppCompatActivity() {
}
},
error = {
Log.d(TAG, "Error updating username $it")
Toast.makeText(this, "Error updating username $it", Toast.LENGTH_SHORT).show()
}
)
}

findViewById<Button>(R.id.profile_update_user_id).setOnClickListener {
val userId = userIdEditText.get()?.text.toString() ?: return@setOnClickListener
Pluto.getInstance()?.updateUserId(
userId = userId,
success = {
updateUserInfo {
Toast.makeText(this, "User ID updated", Toast.LENGTH_SHORT).show()
}
},
error = {
Toast.makeText(this, "Error updating userId $it", Toast.LENGTH_SHORT).show()
}
)
}

findViewById<Button>(R.id.profile_refresh_token).setOnClickListener {
Pluto.getInstance()?.getToken(isForceRefresh = true, completion = {
Pluto.getInstance()?.getAccessToken(isForceRefresh = true, completion = {
findViewById<TextView>(R.id.profile_access_token).text = it ?: "Refresh failed"
})
}
Expand All @@ -99,6 +116,7 @@ class ProfileActivity : AppCompatActivity() {
private fun updateUserInfo(completion: (() -> Unit)? = null) {
Pluto.getInstance()?.myInfo(success = { user ->
nameEditText.get()?.setText(user.name)
userIdEditText.get()?.setText(user.userId)
avatarImageView.get()?.let {
Glide.with(this).load(user.avatar).into(it)
}
Expand Down
4 changes: 2 additions & 2 deletions demoapp/src/main/res/layout/activity_login.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
android:layout_marginStart="24dp"
android:layout_marginTop="96dp"
android:layout_marginEnd="24dp"

android:hint="@string/prompt_email"
android:inputType="textEmailAddress"
android:selectAllOnFocus="true"
Expand Down Expand Up @@ -56,9 +55,10 @@
android:text="@string/action_sign_in"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.496"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/password"
app:layout_constraintVertical_bias="0.2" />
app:layout_constraintVertical_bias="0.185" />

<ProgressBar
android:id="@+id/loading"
Expand Down
46 changes: 35 additions & 11 deletions demoapp/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">
Expand All @@ -16,30 +17,53 @@

<EditText
android:id="@+id/profile_name"
android:layout_width="match_parent"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginStart="90dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/profile_avatar"
app:layout_constraintLeft_toLeftOf="@id/profile_avatar"
app:layout_constraintRight_toLeftOf="@id/profile_update_name"
android:hint="Username"/>

<Button
android:id="@+id/profile_update_name"
android:layout_width="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
app:layout_constraintTop_toTopOf="@id/profile_name"
app:layout_constraintBottom_toBottomOf="@id/profile_name"
app:layout_constraintRight_toRightOf="parent"
android:text="Update"
/>

<EditText
android:id="@+id/profile_user_id"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/profile_name"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Update Username"
android:layout_marginRight="20dp"
app:layout_constraintTop_toBottomOf="@id/profile_name"
app:layout_constraintLeft_toLeftOf="@id/profile_name"
app:layout_constraintRight_toLeftOf="@id/profile_update_user_id"
android:hint="User ID"/>

<Button
android:id="@+id/profile_update_user_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
app:layout_constraintTop_toTopOf="@id/profile_user_id"
app:layout_constraintBottom_toBottomOf="@id/profile_user_id"
app:layout_constraintRight_toRightOf="parent"
android:text="Update"
/>

<Button
android:id="@+id/profile_upload_avatar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/profile_update_name"
app:layout_constraintTop_toBottomOf="@id/profile_user_id"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="20dp"
Expand Down
4 changes: 2 additions & 2 deletions demoapp/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<resources>
<string name="app_name">DemoApp</string>
<!-- Strings related to login -->
<string name="prompt_email">Email</string>
<string name="prompt_email">Email or User ID</string>
<string name="prompt_password">Password</string>
<string name="action_sign_in">Sign in or register</string>
<string name="action_sign_in">Sign in</string>
<string name="action_sign_in_short">Sign in</string>
<string name="welcome">"Welcome !"</string>
<string name="invalid_username">Not a valid username</string>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,28 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

fun Pluto.getToken(isForceRefresh: Boolean = false, completion: (String?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
val jwt = data.jwt
fun Pluto.getAccessToken(
isForceRefresh: Boolean = false,
completion: (String?) -> Unit,
handler: Pluto.PlutoRequestHandler? = null
) {
val accessToken = data.accessToken
val expire = data.expire
if (isForceRefresh || jwt == null || expire == null || expire - System.currentTimeMillis() / 1000 < 5 * 60) {
if (isForceRefresh || accessToken == null || expire == null || expire - System.currentTimeMillis() / 1000 < 5 * 60) {
refreshToken({
if (it == null) {
data.clear()
}
completion(it)
}, handler)
}
completion(data.jwt)
completion(data.accessToken)
}

private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
private fun Pluto.refreshToken(
completion: (String?) -> Unit,
handler: Pluto.PlutoRequestHandler? = null
) {
val refreshToken = data.refreshToken
if (refreshToken == null) {
completion(null)
Expand All @@ -34,13 +41,16 @@ private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.Plu
completion(null)
}

override fun onResponse(call: Call<PlutoResponseWithBody<RefreshAuthResponse>>, response: Response<PlutoResponseWithBody<RefreshAuthResponse>>) {
override fun onResponse(
call: Call<PlutoResponseWithBody<RefreshAuthResponse>>,
response: Response<PlutoResponseWithBody<RefreshAuthResponse>>
) {
val plutoResponse = response.body()
if (plutoResponse != null && plutoResponse.statusOK()) {
val accessToken = plutoResponse.getBody().accessToken
val refreshToken = plutoResponse.getBody().refreshToken
if (data.updateJwt(accessToken)) {
data.updateRefreshToken(refreshToken)
if (data.updateAccessToken(accessToken)) {
data.refreshToken = refreshToken
completion(accessToken)
} else {
completion(null)
Expand All @@ -55,8 +65,11 @@ private fun Pluto.refreshToken(completion: (String?) -> Unit, handler: Pluto.Plu
}
}

fun Pluto.getAuthorizationHeader(completion: (Map<String, String>?) -> Unit, handler: Pluto.PlutoRequestHandler? = null) {
getToken(
fun Pluto.getAuthorizationHeader(
completion: (Map<String, String>?) -> Unit,
handler: Pluto.PlutoRequestHandler? = null
) {
getAccessToken(
completion = { token ->
if (token == null) {
completion(null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -194,8 +194,8 @@ private fun Pluto.handleLogin(
) {
if (response.statusOK()) {
val body = response.getBody()
data.updateRefreshToken(body.refreshToken)
if (!data.updateJwt(body.accessToken)) {
data.refreshToken = body.refreshToken
if (!data.updateAccessToken(body.accessToken)) {
error?.invoke(PlutoError.parseError)
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ fun Pluto.myInfo(
val plutoResponse = response.body()
if (plutoResponse != null) {
if (plutoResponse.statusOK()) {
data.user = plutoResponse.getBody().also(success)
data.user = plutoResponse.getBody()
success(plutoResponse.getBody())
} else {
error?.invoke(plutoResponse.errorCode())
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class Pluto private constructor() {
}

init {
getToken(completion = {
getAccessToken(completion = {
state.postValue(
if (it == null) {
State.notSignin
Expand Down
Loading