Commit 4ec4fff2 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update RocketChatUserAvatar.

Since it is a helper, we don't need to instantiate the RocketChatUserAvatar to each use. This commit adds methods to get a drawable with username initials.
parent 4d84c1eb
package chat.rocket.android.helper package chat.rocket.android.helper
import android.content.Context
import android.graphics.Typeface
import android.graphics.drawable.Drawable
import com.amulyakhare.textdrawable.TextDrawable
import java.net.URLEncoder import java.net.URLEncoder
class RocketChatUserAvatar(val hostname: String, val username: String) { object RocketChatUserAvatar {
val imageUri: String /**
/** REMARK * Returns the user avatar URI.
* This is often a SVG image (see Rocket.Chat:server/startup/avatar.js). *
* REMARK: This is often a SVG image (Rocket.Chat:server/startup/avatar.js).
*
* @param hostname The server's hostname.
* @param username The username.
* @return The user avatar URI.
* @see getImageFormat
*/ */
get() { fun getUri(hostname : String, username: String): String {
return "https://" + return "https://" +
hostname.replace("http://", "").replace("https://", "") + hostname.replace("http://", "").replace("https://", "") +
"/avatar/" + "/avatar/" +
URLEncoder.encode(username, "UTF-8") URLEncoder.encode(username, "UTF-8")
} }
/**
* Returns the user avatar image format.
*
* @param uri The user avatar image URI to get the image format.
* @return The user avatar image format. Possible values are: "image/jpeg", "image/png" and "image/svg+xml".
* @see getUri
*/
fun getImageFormat(uri: String): String {
return OkHttpHelper.getContentType(uri)
}
/**
* Returns a drawable with username initials.
*
* @param username The username.
* @param context The context.
* @return A drawable with username initials.
* @see getUsernameInitials
*/
fun getTextDrawable(username: String, context: Context): Drawable {
val round = (4 * context.resources.displayMetrics.density).toInt()
return TextDrawable.builder()
.beginConfig()
.useFont(Typeface.SANS_SERIF)
.endConfig()
.buildRoundRect(getUsernameInitials(username), getUserAvatarBackgroundColor(username), round)
}
/**
* Returns a string with the username initials. For example: username John Doe returns JD initials.
*
* @param username The username.
* @return A string with username initials.
*/
fun getUsernameInitials(username: String): String {
val name = username
.replace("[^A-Za-z0-9]", ".")
.replace("\\.+", ".")
.replace("(^\\.)|(\\.$)", "")
val initials = name.split(".")
if (initials.size > 1) {
return (initials[0].substring(0, 1) + initials[initials.size - 1].substring(0, 1)).toUpperCase()
} else {
return (name.replace("[^A-Za-z0-9]", "").substring(0, 2)).toUpperCase()
}
}
/**
* Returns a background color to be rendered on the user avatar (Rocket.Chat:server/startup/avatar.js).
*
* @param username The username.
* @return A hexadecimal color.
*/
fun getUserAvatarBackgroundColor(username: String): Int {
return COLORS[username.length % COLORS.size]
}
private val COLORS = intArrayOf(
0xFFF44336.toInt(), 0xFFE91E63.toInt(), 0xFF9C27B0.toInt(), 0xFF673AB7.toInt(), 0xFF3F51B5.toInt(),
0xFF2196F3.toInt(), 0xFF03A9F4.toInt(), 0xFF00BCD4.toInt(), 0xFF009688.toInt(), 0xFF4CAF50.toInt(),
0xFF8BC34A.toInt(), 0xFFCDDC39.toInt(), 0xFFFFC107.toInt(), 0xFFFF9800.toInt(), 0xFFFF5722.toInt(),
0xFF795548.toInt(), 0xFF9E9E9E.toInt(), 0xFF607D8B.toInt())
} }
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment