improved quick access icon

This commit is contained in:
Mariotaku Lee 2017-03-21 18:43:48 +08:00
parent 6498392b6e
commit f837f8920c
No known key found for this signature in database
GPG Key ID: 15C10F89D7C33535
2 changed files with 98 additions and 79 deletions

View File

@ -28,8 +28,6 @@ import android.graphics.Canvas
import android.os.Bundle
import android.support.v7.app.AlertDialog
import com.bumptech.glide.Glide
import com.bumptech.glide.load.resource.drawable.GlideDrawable
import nl.komponents.kovenant.deferred
import nl.komponents.kovenant.ui.failUi
import nl.komponents.kovenant.ui.successUi
import org.mariotaku.kpreferences.get
@ -46,11 +44,13 @@ import org.mariotaku.twidere.constant.profileImageStyleKey
import org.mariotaku.twidere.extension.applyTheme
import org.mariotaku.twidere.extension.loadProfileImage
import org.mariotaku.twidere.fragment.BaseDialogFragment
import org.mariotaku.twidere.fragment.ProgressDialogFragment
import org.mariotaku.twidere.model.ParcelableUser
import org.mariotaku.twidere.model.ParcelableUserList
import org.mariotaku.twidere.model.UserKey
import org.mariotaku.twidere.util.IntentUtils
import org.mariotaku.twidere.util.glide.DeferredTarget
import java.lang.ref.WeakReference
class CreateQuickAccessShortcutActivity : BaseActivity() {
@ -112,6 +112,24 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
val extras = data.getBundleExtra(EXTRA_EXTRAS)
val accountKey = extras.getParcelable<UserKey>(EXTRA_ACCOUNT_KEY)
val actionType = extras.getString(EXTRA_TYPE)
addUserRelatedShortcut(actionType, accountKey, user)
}
REQUEST_SELECT_USER_LIST -> {
if (resultCode != Activity.RESULT_OK || data == null) {
setResult(Activity.RESULT_CANCELED)
finish()
return
}
val list = data.getParcelableExtra<ParcelableUserList>(EXTRA_USER_LIST)
val extras = data.getBundleExtra(EXTRA_EXTRAS)
val accountKey = extras.getParcelable<UserKey>(EXTRA_ACCOUNT_KEY)
val actionType = extras.getString(EXTRA_TYPE)
addUserListRelatedShortcut(actionType, accountKey, list)
}
}
}
private fun addUserRelatedShortcut(actionType: String?, accountKey: UserKey?, user: ParcelableUser) {
when (actionType) {
"user_timeline" -> {
val launchIntent = IntentUtils.userTimeline(accountKey, user.key,
@ -138,42 +156,37 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
finish()
}
else -> {
val displayName = userColorNameManager.getDisplayName(user, preferences[nameFirstKey])
val deferred = Glide.with(this).loadProfileImage(this, user,
shapeStyle = preferences[profileImageStyleKey], cornerRadiusRatio = 0.1f,
size = getString(R.string.profile_image_size)).into(DeferredTarget())
val weakThis = WeakReference(this)
ProgressDialogFragment.show(supportFragmentManager, TAG_LOAD_ICON_PROGRESS)
deferred.promise.successUi { drawable ->
val activity = weakThis.get() ?: return@successUi
val launchIntent = IntentUtils.userProfile(accountKey, user.key,
user.screen_name, profileUrl = user.extras?.statusnet_profile_url)
val requestBuilder = Glide.with(this).loadProfileImage(this, user, shapeStyle = preferences[profileImageStyleKey],
cornerRadiusRatio = 0.1f, size = getString(R.string.profile_image_size))
val deferred = deferred<GlideDrawable, Exception>()
requestBuilder.into(DeferredTarget(deferred))
deferred.promise.successUi { drawable ->
val icon = Bitmap.createBitmap(drawable.intrinsicWidth,
drawable.intrinsicHeight, Bitmap.Config.ARGB_8888)
val canvas = Canvas(icon)
drawable.setBounds(0, 0, icon.width, icon.height)
drawable.draw(canvas)
setResult(Activity.RESULT_OK, Intent().apply {
activity.setResult(Activity.RESULT_OK, Intent().apply {
putExtra(Intent.EXTRA_SHORTCUT_INTENT, launchIntent)
putExtra(Intent.EXTRA_SHORTCUT_ICON, icon)
putExtra(Intent.EXTRA_SHORTCUT_NAME, userColorNameManager.getDisplayName(user,
preferences[nameFirstKey]))
putExtra(Intent.EXTRA_SHORTCUT_NAME, displayName)
})
finish()
activity.finish()
}.failUi {
setResult(Activity.RESULT_CANCELED)
finish()
val activity = weakThis.get() ?: return@failUi
activity.setResult(Activity.RESULT_CANCELED)
activity.finish()
}
}
}
}
REQUEST_SELECT_USER_LIST -> {
if (resultCode != Activity.RESULT_OK || data == null) {
setResult(Activity.RESULT_CANCELED)
finish()
return
}
val list = data.getParcelableExtra<ParcelableUserList>(EXTRA_USER_LIST)
val extras = data.getBundleExtra(EXTRA_EXTRAS)
val accountKey = extras.getParcelable<UserKey>(EXTRA_ACCOUNT_KEY)
val actionType = extras.getString(EXTRA_TYPE)
private fun addUserListRelatedShortcut(actionType: String?, accountKey: UserKey?, list: ParcelableUserList) {
when (actionType) {
"list_timeline" -> {
val launchIntent = IntentUtils.userListTimeline(accountKey, list.id,
@ -198,8 +211,6 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
}
finish()
}
}
}
private fun onItemSelected(which: Int) {
val actionType = resources.getStringArray(R.array.values_quick_access_shortcut_types)[which]
@ -231,4 +242,8 @@ class CreateQuickAccessShortcutActivity : BaseActivity() {
activity?.finish()
}
}
companion object {
private const val TAG_LOAD_ICON_PROGRESS = "load_icon_progress"
}
}

View File

@ -23,19 +23,23 @@ import android.graphics.drawable.Drawable
import com.bumptech.glide.request.animation.GlideAnimation
import com.bumptech.glide.request.target.SimpleTarget
import nl.komponents.kovenant.Deferred
import nl.komponents.kovenant.Promise
import nl.komponents.kovenant.deferred
/**
* Created by mariotaku on 2017/3/21.
*/
class DeferredTarget<R>(val deferred: Deferred<R, Exception>) : SimpleTarget<R>() {
class DeferredTarget<R>(private val deferredInstance: Deferred<R, Exception> = deferred()) : SimpleTarget<R>() {
val promise: Promise<R, Exception> get() = deferredInstance.promise
override fun onLoadFailed(e: Exception, errorDrawable: Drawable?) {
deferred.reject(e)
deferredInstance.reject(e)
}
override fun onResourceReady(resource: R, glideAnimation: GlideAnimation<in R>) {
deferred.resolve(resource)
deferredInstance.resolve(resource)
}
}