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
17 changes: 10 additions & 7 deletions demoapp/src/main/java/com/mushare/demoapp/data/LoginDataSource.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ class LoginDataSource {
//pluto?.registerByEmail(username, password, "Test", {
//pluto?.resendValidationEmail(username, {
pluto?.loginWithAccount(username, password, {
pluto.myInfo({
val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), it.name)
onComplete(Result.Success(fakeUser))
}, {
onComplete(Result.Error(IOException("Error getting account info $it")))
Log.e("getInfo", "failed $it")
})
pluto.myInfo(
success = {
val fakeUser = LoggedInUser(java.util.UUID.randomUUID().toString(), it.name)
onComplete(Result.Success(fakeUser))
},
error = {
onComplete(Result.Error(IOException("Error getting account info $it")))
Log.e("getInfo", "failed $it")
}
)
}, {
onComplete(Result.Error(IOException("Error logging in $it")))
Log.e("login", "failed $it")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,17 @@ 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)
Pluto.getInstance()?.myInfo(
isForceRefresh = false,
success = { user ->
nameEditText.get()?.setText(user.name)
userIdEditText.get()?.setText(user.userId)
avatarImageView.get()?.let {
Glide.with(this).load(user.avatar).into(it)
}
completion?.let { it() }
}
completion?.let { it() }
})
)

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,25 @@ import retrofit2.Response
import java.io.ByteArrayOutputStream
import java.io.File

val Pluto.currentUser: PlutoUser?
get() {
if (state != Pluto.State.signin) {
return null
}
return data.user
}

fun Pluto.myInfo(
isForceRefresh: Boolean = false,
success: (PlutoUser) -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
val currentUser = data.user
if (!isForceRefresh && currentUser != null) {
success(currentUser)
return
}
getAuthorizationHeader(
completion = { header ->
if (header == null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,9 @@ class Pluto private constructor() {

internal val data by lazy { PlutoModel(context) }

val state: MutableLiveData<State> by lazy { MutableLiveData(State.loading) }
val state: MutableLiveData<State> by lazy {
MutableLiveData(if (data.isTokenNull) State.notSignin else State.signin)
}

internal val gson: Gson by lazy { GsonBuilder().serializeNulls().create() }
internal val plutoService: PlutoService by lazy {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,9 @@ internal class PlutoModel(context: Context) {
infoJSONString = gson.toJson(value)
}

val isTokenNull: Boolean
get() = accessToken == null || refreshToken == null

fun updateAccessToken(jwt: String): Boolean {
val body = JwtUtils.decodeBody(jwt) ?: return false
return try {
Expand Down