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
12 changes: 11 additions & 1 deletion demoapp/build.gradle
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
apply plugin: "kotlin-kapt"

android {
compileSdkVersion 29
Expand All @@ -13,7 +14,7 @@ android {

defaultConfig {
applicationId "com.mushare.demoapp"
minSdkVersion 16
minSdkVersion 19
targetSdkVersion 29
versionCode 1
versionName "1.0"
Expand All @@ -30,6 +31,11 @@ android {

}

repositories {
jcenter()
maven { url "https://jitpack.io" } //Make sure to add this in your project for uCrop
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
Expand All @@ -39,6 +45,10 @@ dependencies {
implementation 'androidx.annotation:annotation:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'com.github.dhaval2404:imagepicker:1.7.5'
implementation 'com.github.florent37:inline-activity-result-kotlin:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.11.0'
kapt 'com.github.bumptech.glide:compiler:4.11.0'
testImplementation 'junit:junit:4.13'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
Expand Down
3 changes: 2 additions & 1 deletion demoapp/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
android:theme="@style/AppTheme"
android:requestLegacyExternalStorage="true">
<activity
android:name=".ui.login.LoginActivity"
android:label="@string/app_name">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
package com.mushare.demoapp.ui.login;

import android.app.Activity
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 android.widget.*
import androidx.appcompat.app.AppCompatActivity
import com.bumptech.glide.Glide
import com.github.dhaval2404.imagepicker.ImagePicker
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 com.mushare.plutosdk.*
import java.lang.ref.WeakReference

class ProfileActivity : AppCompatActivity() {
Expand All @@ -21,30 +18,70 @@ class ProfileActivity : AppCompatActivity() {
}

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

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

setContentView(R.layout.activity_profile)

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

Pluto.getInstance()?.myInfo(success = {
nameEditText.get()?.setText(it.name)
})
updateUserInfo()

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

findViewById<Button>(R.id.profile_upload_avatar).setOnClickListener {
ImagePicker.with(this)
.crop()
.compress(64)
.maxResultSize(480, 480)
.start { resultCode, data ->
when (resultCode) {
Activity.RESULT_OK -> {
Pluto.getInstance()?.uploadAvatar(
imageFile = ImagePicker.getFile(data) ?: return@start,
success = {
updateUserInfo {
Toast
.makeText(
this,
"Avatar uploaded",
Toast.LENGTH_SHORT
)
.show()
}
},
error = {
Log.d(TAG, "Error uploading avatar $it")
}
)
}
ImagePicker.RESULT_ERROR -> {
Toast
.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT)
.show()
}
else -> {
Toast
.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT)
.show()
}
}
}
}

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 = {
updateUserInfo {
Toast.makeText(this, "Name updated", Toast.LENGTH_SHORT).show()
})
}
},
error = {
Log.d(TAG, "Error updating username $it")
Expand All @@ -58,4 +95,15 @@ class ProfileActivity : AppCompatActivity() {
})
}
}

private fun updateUserInfo(completion: (() -> Unit)? = null) {
Pluto.getInstance()?.myInfo(success = { user ->
nameEditText.get()?.setText(user.name)
avatarImageView.get()?.let {
Glide.with(this).load(user.avatar).into(it)
}
completion?.let { it() }
})

}
}
27 changes: 24 additions & 3 deletions demoapp/src/main/res/layout/activity_profile.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,22 @@
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<ImageView
android:id="@+id/profile_avatar"
android:layout_width="50dp"
android:layout_height="50dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginStart="20dp"
android:layout_marginTop="20dp"
android:background="@color/colorAccent"/>

<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_marginStart="90dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:hint="Username"/>
Expand All @@ -20,19 +30,30 @@
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/profile_name"
android:layout_marginStart="20dp"
android:layout_marginTop="10dp"
android:layout_marginTop="20dp"
android:layout_marginEnd="20dp"
android:text="Update Username"
/>

<Button
android:id="@+id/profile_refresh_token"
android:id="@+id/profile_upload_avatar"
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="Upload Avatar"
/>

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

<TextView
Expand Down
2 changes: 1 addition & 1 deletion pluto-kotlin-client-sdk/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ apply plugin: 'maven-publish'

buildscript {
ext.versionCode = 11
ext.versionName = '0.2.4'
ext.versionName = '0.3'
}

android {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,13 @@
package com.mushare.plutosdk

import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.util.Base64
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import java.io.ByteArrayOutputStream
import java.io.File

fun Pluto.myInfo(
success: (PlutoUser) -> Unit,
Expand Down Expand Up @@ -110,3 +115,60 @@ fun Pluto.updateName(
handler = handler
)
}

fun Pluto.uploadAvatar(
imageFile: File,
success: () -> Unit,
error: ((PlutoError) -> Unit)? = null,
handler: Pluto.PlutoRequestHandler? = null
) {
val bitmap = BitmapFactory.decodeFile(imageFile.absolutePath)
val outputStream = ByteArrayOutputStream()
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
val base64 = Base64.encodeToString(outputStream.toByteArray(), Base64.DEFAULT)
getAuthorizationHeader(
completion = { header ->
if (header == null) {
handler?.setCall(null)
error?.invoke(PlutoError.notSignin)
return@getAuthorizationHeader
}

val body = UpdateUserInfoPutData(null, base64)
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)
}
},
handler = handler
)
}