2019-06-03 16:23:37 +00:00
|
|
|
package code.name.monkey.retromusic.activities
|
|
|
|
|
|
|
|
import android.app.Activity
|
2019-12-01 15:27:01 +00:00
|
|
|
import android.content.Intent
|
2019-06-03 16:23:37 +00:00
|
|
|
import android.content.res.ColorStateList
|
|
|
|
import android.graphics.Bitmap
|
|
|
|
import android.net.Uri
|
|
|
|
import android.os.Bundle
|
2019-06-04 04:00:11 +00:00
|
|
|
import android.text.TextUtils
|
2019-06-03 16:23:37 +00:00
|
|
|
import android.view.MenuItem
|
|
|
|
import android.widget.Toast
|
2019-12-01 15:27:01 +00:00
|
|
|
import code.name.monkey.appthemehelper.util.ColorUtil
|
|
|
|
import code.name.monkey.appthemehelper.util.MaterialValueHelper
|
2019-06-03 16:23:37 +00:00
|
|
|
import code.name.monkey.retromusic.Constants.USER_BANNER
|
|
|
|
import code.name.monkey.retromusic.Constants.USER_PROFILE
|
|
|
|
import code.name.monkey.retromusic.R
|
|
|
|
import code.name.monkey.retromusic.activities.base.AbsBaseActivity
|
2020-05-20 22:53:09 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.accentColor
|
2020-05-20 20:28:38 +00:00
|
|
|
import code.name.monkey.retromusic.extensions.applyToolbar
|
2020-05-20 22:53:09 +00:00
|
|
|
import code.name.monkey.retromusic.glide.ProfileBannerGlideRequest
|
|
|
|
import code.name.monkey.retromusic.glide.UserProfileGlideRequest
|
|
|
|
import code.name.monkey.retromusic.util.ImageUtil
|
2020-06-06 18:57:28 +00:00
|
|
|
import code.name.monkey.retromusic.util.PreferenceUtil
|
2020-05-20 22:53:09 +00:00
|
|
|
import com.bumptech.glide.Glide
|
|
|
|
import com.bumptech.glide.load.engine.DiskCacheStrategy
|
|
|
|
import com.bumptech.glide.request.RequestListener
|
|
|
|
import com.bumptech.glide.request.target.Target
|
|
|
|
import com.github.dhaval2404.imagepicker.ImagePicker
|
2020-05-20 23:07:44 +00:00
|
|
|
import com.github.dhaval2404.imagepicker.constant.ImageProvider
|
2020-02-25 06:44:46 +00:00
|
|
|
import kotlinx.android.synthetic.main.activity_user_info.*
|
2019-12-26 19:53:11 +00:00
|
|
|
import kotlinx.coroutines.CoroutineScope
|
|
|
|
import kotlinx.coroutines.Dispatchers
|
|
|
|
import kotlinx.coroutines.launch
|
|
|
|
import kotlinx.coroutines.withContext
|
2020-05-20 22:53:09 +00:00
|
|
|
import java.io.BufferedOutputStream
|
2019-12-01 15:27:01 +00:00
|
|
|
import java.io.File
|
|
|
|
import java.io.FileOutputStream
|
|
|
|
import java.io.IOException
|
2019-06-03 16:23:37 +00:00
|
|
|
|
|
|
|
class UserInfoActivity : AbsBaseActivity() {
|
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
override fun onCreate(savedInstanceState: Bundle?) {
|
|
|
|
super.onCreate(savedInstanceState)
|
|
|
|
setContentView(R.layout.activity_user_info)
|
|
|
|
setStatusbarColorAuto()
|
2019-12-05 18:18:54 +00:00
|
|
|
setNavigationbarColorAuto()
|
2019-12-01 15:27:01 +00:00
|
|
|
setTaskDescriptionColorAuto()
|
|
|
|
setLightNavigationBar(true)
|
2020-05-20 20:28:38 +00:00
|
|
|
applyToolbar(toolbar)
|
2020-05-20 22:53:09 +00:00
|
|
|
|
2020-08-13 08:24:36 +00:00
|
|
|
nameContainer.accentColor()
|
2020-06-06 18:57:28 +00:00
|
|
|
name.setText(PreferenceUtil.userName)
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
userImage.setOnClickListener {
|
2020-05-20 22:53:09 +00:00
|
|
|
pickNewPhoto()
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2020-05-20 22:53:09 +00:00
|
|
|
|
2020-10-04 13:31:40 +00:00
|
|
|
bannerImage.setOnClickListener {
|
2020-05-20 22:53:09 +00:00
|
|
|
selectBannerImage()
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2020-05-20 22:53:09 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
next.setOnClickListener {
|
|
|
|
val nameString = name.text.toString().trim { it <= ' ' }
|
|
|
|
if (TextUtils.isEmpty(nameString)) {
|
2020-06-30 17:45:47 +00:00
|
|
|
Toast.makeText(this, "Umm you're name can't be empty!", Toast.LENGTH_SHORT).show()
|
2019-12-01 15:27:01 +00:00
|
|
|
return@setOnClickListener
|
|
|
|
}
|
2020-06-06 18:57:28 +00:00
|
|
|
PreferenceUtil.userName = nameString
|
2019-12-01 15:27:01 +00:00
|
|
|
setResult(Activity.RESULT_OK)
|
|
|
|
finish()
|
|
|
|
}
|
2020-05-20 22:53:09 +00:00
|
|
|
|
|
|
|
val textColor =
|
|
|
|
MaterialValueHelper.getPrimaryTextColor(this, ColorUtil.isColorLight(accentColor()))
|
|
|
|
next.backgroundTintList = ColorStateList.valueOf(accentColor())
|
|
|
|
next.iconTint = ColorStateList.valueOf(textColor)
|
|
|
|
next.setTextColor(textColor)
|
|
|
|
loadProfile()
|
|
|
|
}
|
|
|
|
|
|
|
|
private fun loadProfile() {
|
|
|
|
bannerImage?.let {
|
|
|
|
ProfileBannerGlideRequest.Builder.from(
|
|
|
|
Glide.with(this),
|
|
|
|
ProfileBannerGlideRequest.getBannerModel()
|
|
|
|
).build().into(it)
|
|
|
|
}
|
|
|
|
UserProfileGlideRequest.Builder.from(
|
|
|
|
Glide.with(this),
|
|
|
|
UserProfileGlideRequest.getUserModel()
|
|
|
|
).build().into(userImage)
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
|
|
|
if (item.itemId == android.R.id.home) {
|
|
|
|
onBackPressed()
|
|
|
|
}
|
|
|
|
return super.onOptionsItemSelected(item)
|
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
private fun selectBannerImage() {
|
2020-05-20 22:53:09 +00:00
|
|
|
ImagePicker.with(this)
|
2020-05-20 23:07:44 +00:00
|
|
|
.compress(1440)
|
|
|
|
.provider(ImageProvider.GALLERY)
|
2020-05-20 22:53:09 +00:00
|
|
|
.crop(16f, 9f)
|
|
|
|
.start(PICK_BANNER_REQUEST)
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
private fun pickNewPhoto() {
|
2020-05-20 22:53:09 +00:00
|
|
|
ImagePicker.with(this)
|
2020-05-20 23:07:44 +00:00
|
|
|
.provider(ImageProvider.GALLERY)
|
2020-05-20 22:53:09 +00:00
|
|
|
.cropSquare()
|
2020-05-20 23:07:44 +00:00
|
|
|
.compress(1440)
|
2020-05-20 22:53:09 +00:00
|
|
|
.start(PICK_IMAGE_REQUEST)
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2020-05-20 22:53:09 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
public override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
|
|
|
|
super.onActivityResult(requestCode, resultCode, data)
|
2020-05-20 22:53:09 +00:00
|
|
|
if (resultCode == Activity.RESULT_OK && requestCode == PICK_IMAGE_REQUEST) {
|
|
|
|
val fileUri = data?.data
|
|
|
|
fileUri?.let { setAndSaveUserImage(it) }
|
|
|
|
} else if (resultCode == Activity.RESULT_OK && requestCode == PICK_BANNER_REQUEST) {
|
|
|
|
val fileUri = data?.data
|
|
|
|
fileUri?.let { setAndSaveBannerImage(it) }
|
|
|
|
} else if (resultCode == ImagePicker.RESULT_ERROR) {
|
|
|
|
Toast.makeText(this, ImagePicker.getError(data), Toast.LENGTH_SHORT).show()
|
|
|
|
} else {
|
|
|
|
Toast.makeText(this, "Task Cancelled", Toast.LENGTH_SHORT).show()
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2020-05-20 22:53:09 +00:00
|
|
|
private fun setAndSaveBannerImage(fileUri: Uri) {
|
|
|
|
Glide.with(this)
|
|
|
|
.load(fileUri)
|
|
|
|
.asBitmap()
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.listener(object : RequestListener<Any, Bitmap> {
|
|
|
|
override fun onException(
|
|
|
|
e: java.lang.Exception?,
|
|
|
|
model: Any?,
|
|
|
|
target: Target<Bitmap>?,
|
|
|
|
isFirstResource: Boolean
|
|
|
|
): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResourceReady(
|
|
|
|
resource: Bitmap?,
|
|
|
|
model: Any?,
|
|
|
|
target: Target<Bitmap>?,
|
|
|
|
isFromMemoryCache: Boolean,
|
|
|
|
isFirstResource: Boolean
|
|
|
|
): Boolean {
|
2020-05-20 23:23:40 +00:00
|
|
|
resource?.let { saveImage(it, USER_BANNER) }
|
2020-05-20 22:53:09 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into(bannerImage)
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2020-05-20 23:23:40 +00:00
|
|
|
private fun saveImage(bitmap: Bitmap, fileName: String) {
|
2020-08-21 14:19:15 +00:00
|
|
|
CoroutineScope(Dispatchers.IO).launch {
|
2020-05-20 22:53:09 +00:00
|
|
|
val appDir = applicationContext.filesDir
|
2020-05-20 23:23:40 +00:00
|
|
|
val file = File(appDir, fileName)
|
2020-05-20 22:53:09 +00:00
|
|
|
var successful = false
|
|
|
|
try {
|
|
|
|
val os = BufferedOutputStream(FileOutputStream(file))
|
|
|
|
successful = ImageUtil.resizeBitmap(bitmap, 2048)
|
|
|
|
.compress(Bitmap.CompressFormat.WEBP, 100, os)
|
2020-05-20 23:23:40 +00:00
|
|
|
withContext(Dispatchers.IO) { os.close() }
|
2020-05-20 22:53:09 +00:00
|
|
|
} catch (e: IOException) {
|
|
|
|
e.printStackTrace()
|
|
|
|
}
|
|
|
|
if (successful) {
|
|
|
|
withContext(Dispatchers.Main) {
|
2020-05-20 23:23:40 +00:00
|
|
|
Toast.makeText(this@UserInfoActivity, "Updated", Toast.LENGTH_SHORT).show()
|
2020-05-20 22:53:09 +00:00
|
|
|
}
|
2019-12-26 19:53:11 +00:00
|
|
|
}
|
|
|
|
}
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2020-05-20 22:53:09 +00:00
|
|
|
private fun setAndSaveUserImage(fileUri: Uri) {
|
|
|
|
Glide.with(this)
|
|
|
|
.load(fileUri)
|
|
|
|
.asBitmap()
|
|
|
|
.diskCacheStrategy(DiskCacheStrategy.NONE)
|
|
|
|
.listener(object : RequestListener<Any, Bitmap> {
|
|
|
|
override fun onException(
|
|
|
|
e: java.lang.Exception?,
|
|
|
|
model: Any?,
|
|
|
|
target: Target<Bitmap>?,
|
|
|
|
isFirstResource: Boolean
|
|
|
|
): Boolean {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
override fun onResourceReady(
|
|
|
|
resource: Bitmap?,
|
|
|
|
model: Any?,
|
|
|
|
target: Target<Bitmap>?,
|
|
|
|
isFromMemoryCache: Boolean,
|
|
|
|
isFirstResource: Boolean
|
|
|
|
): Boolean {
|
2020-05-20 23:23:40 +00:00
|
|
|
resource?.let { saveImage(it, USER_PROFILE) }
|
2020-05-20 22:53:09 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.into(userImage)
|
2019-12-01 15:27:01 +00:00
|
|
|
}
|
2019-07-10 19:07:21 +00:00
|
|
|
|
2019-06-03 16:23:37 +00:00
|
|
|
|
2019-12-01 15:27:01 +00:00
|
|
|
companion object {
|
|
|
|
private const val PICK_IMAGE_REQUEST = 9002
|
|
|
|
private const val PICK_BANNER_REQUEST = 9004
|
|
|
|
}
|
2020-05-20 22:53:09 +00:00
|
|
|
}
|