Twidere-App-Android-Twitter.../twidere/src/main/kotlin/org/mariotaku/twidere/extension/GlideExtensions.kt

165 lines
7.3 KiB
Kotlin
Raw Normal View History

2017-03-02 02:08:54 +01:00
/*
* Twidere - Twitter client for Android
*
* Copyright (C) 2012-2017 Mariotaku Lee <mariotaku.lee@gmail.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package org.mariotaku.twidere.extension
import android.content.Context
import com.bumptech.glide.DrawableRequestBuilder
2017-03-02 07:59:19 +01:00
import com.bumptech.glide.DrawableTypeRequest
2017-03-02 02:08:54 +01:00
import com.bumptech.glide.RequestManager
2017-03-02 07:59:19 +01:00
import com.bumptech.glide.load.engine.DiskCacheStrategy
2017-03-02 02:08:54 +01:00
import jp.wasabeef.glide.transformations.CropCircleTransformation
2017-03-02 03:42:40 +01:00
import org.mariotaku.twidere.R
2017-03-02 09:26:12 +01:00
import org.mariotaku.twidere.annotation.ImageShapeStyle
import org.mariotaku.twidere.extension.model.getBestProfileBanner
2017-03-02 07:59:19 +01:00
import org.mariotaku.twidere.extension.model.user
import org.mariotaku.twidere.model.*
2017-03-02 03:42:40 +01:00
import org.mariotaku.twidere.util.Utils
import org.mariotaku.twidere.util.glide.RoundedRectTransformation
2017-03-02 07:59:19 +01:00
import org.mariotaku.twidere.view.ShapedImageView
2017-03-02 02:08:54 +01:00
2017-03-02 09:26:12 +01:00
fun RequestManager.loadProfileImage(
context: Context,
url: String?,
@ImageShapeStyle style: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f,
2017-03-06 08:12:18 +01:00
size: String? = null
2017-03-02 09:26:12 +01:00
): DrawableRequestBuilder<String?> {
return configureLoadProfileImage(context, style, cornerRadius, cornerRadiusRatio) {
2017-03-06 09:36:25 +01:00
if (url == null || size == null) {
return@configureLoadProfileImage load(url)
} else {
return@configureLoadProfileImage load(Utils.getTwitterProfileImageOfSize(url, size))
}
}
2017-03-02 02:08:54 +01:00
}
2017-03-02 07:59:19 +01:00
fun RequestManager.loadProfileImage(context: Context, resourceId: Int,
@ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f): DrawableRequestBuilder<Int> {
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) { load(resourceId) }
2017-03-02 02:08:54 +01:00
}
2017-03-02 07:59:19 +01:00
fun RequestManager.loadProfileImage(context: Context, account: AccountDetails,
@ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f,
2017-03-06 08:12:18 +01:00
size: String? = null): DrawableRequestBuilder<String?> {
return loadProfileImage(context, account.user, shapeStyle, cornerRadius, cornerRadiusRatio, size)
2017-03-02 03:42:40 +01:00
}
2017-03-02 07:59:19 +01:00
fun RequestManager.loadProfileImage(context: Context, user: ParcelableUser,
@ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f,
2017-03-06 08:12:18 +01:00
size: String? = null): DrawableRequestBuilder<String?> {
2017-03-02 07:59:19 +01:00
if (user.extras != null && user.extras.profile_image_url_fallback == null) {
// No fallback image, use compatible logic
return loadProfileImage(context, user.profile_image_url, shapeStyle, cornerRadius,
cornerRadiusRatio, size)
2017-03-06 08:12:18 +01:00
}
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) {
2017-03-06 08:12:18 +01:00
if (size != null) {
return@configureLoadProfileImage load(Utils.getTwitterProfileImageOfSize(user.profile_image_url, size))
} else {
return@configureLoadProfileImage load(user.profile_image_url)
}
2017-03-02 02:08:54 +01:00
}
2017-03-02 07:59:19 +01:00
}
fun RequestManager.loadProfileImage(context: Context, userList: ParcelableUserList,
@ImageShapeStyle shapeStyle: Int = ImageShapeStyle.SHAPE_CIRCLE,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f): DrawableRequestBuilder<String?> {
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) {
load(userList.user_profile_image_url)
}
2017-03-02 03:42:40 +01:00
}
2017-03-02 07:59:19 +01:00
fun RequestManager.loadProfileImage(context: Context, group: ParcelableGroup,
@ImageShapeStyle shapeStyle: Int = ImageShapeStyle.SHAPE_CIRCLE,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f): DrawableRequestBuilder<String?> {
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) {
load(group.homepage_logo)
}
2017-03-02 07:59:19 +01:00
}
2017-03-02 03:42:40 +01:00
2017-03-02 07:59:19 +01:00
fun RequestManager.loadProfileImage(context: Context, status: ParcelableStatus,
@ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f,
2017-03-06 08:12:18 +01:00
size: String? = null): DrawableRequestBuilder<String?> {
2017-03-02 03:42:40 +01:00
if (status.extras != null && status.extras.user_profile_image_url_fallback == null) {
// No fallback image, use compatible logic
return loadProfileImage(context, status.user_profile_image_url, shapeStyle, cornerRadius,
cornerRadiusRatio, size)
}
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) {
load(status.user_profile_image_url)
2017-03-02 03:42:40 +01:00
}
2017-03-02 07:59:19 +01:00
}
2017-03-06 08:12:18 +01:00
fun RequestManager.loadProfileImage(context: Context, conversation: ParcelableMessageConversation,
@ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f,
2017-03-06 08:12:18 +01:00
size: String? = null): DrawableRequestBuilder<*> {
2017-03-02 07:59:19 +01:00
if (conversation.conversation_type == ParcelableMessageConversation.ConversationType.ONE_TO_ONE) {
val user = conversation.user
if (user != null) {
return loadProfileImage(context, user, shapeStyle, cornerRadius, cornerRadiusRatio, size)
2017-03-02 07:59:19 +01:00
} else {
// TODO: show default conversation icon
return loadProfileImage(context, R.drawable.ic_profile_image_default_group, shapeStyle)
2017-03-02 07:59:19 +01:00
}
} else {
return loadProfileImage(context, conversation.conversation_avatar, shapeStyle, cornerRadius,
cornerRadiusRatio, size).placeholder(R.drawable.ic_profile_image_default_group)
2017-03-02 07:59:19 +01:00
}
}
2017-03-02 09:26:12 +01:00
fun RequestManager.loadOriginalProfileImage(context: Context, user: ParcelableUser,
@ImageShapeStyle shapeStyle: Int, cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f
): DrawableRequestBuilder<String> {
val original = user.extras.profile_image_url_original?.takeUnless(String::isEmpty)
2017-03-02 09:26:12 +01:00
?: Utils.getOriginalTwitterProfileImage(user.profile_image_url)
return configureLoadProfileImage(context, shapeStyle, cornerRadius, cornerRadiusRatio) {
load(original)
}
2017-03-02 09:26:12 +01:00
}
fun RequestManager.loadProfileBanner(context: Context, user: ParcelableUser, width: Int): DrawableTypeRequest<String?> {
return load(user.getBestProfileBanner(width))
}
internal inline fun <T> configureLoadProfileImage(context: Context, @ImageShapeStyle shapeStyle: Int,
cornerRadius: Float = 0f, cornerRadiusRatio: Float = 0f, create: () -> DrawableTypeRequest<T>
): DrawableRequestBuilder<T> {
2017-03-02 07:59:19 +01:00
val builder = create()
builder.diskCacheStrategy(DiskCacheStrategy.RESULT)
builder.dontAnimate()
if (!ShapedImageView.OUTLINE_DRAW) {
when (shapeStyle) {
2017-03-02 09:26:12 +01:00
ImageShapeStyle.SHAPE_CIRCLE -> {
2017-03-02 07:59:19 +01:00
builder.bitmapTransform(CropCircleTransformation(context))
}
2017-03-02 09:26:12 +01:00
ImageShapeStyle.SHAPE_RECTANGLE -> {
builder.bitmapTransform(RoundedRectTransformation(context, cornerRadius,
cornerRadiusRatio))
2017-03-02 07:59:19 +01:00
}
}
}
return builder
2017-03-02 03:42:40 +01:00
}