Commit 8b18a0f2 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Delete RocketChatUserAvatar and RocketChatUserAvatarTest class.

parent 1efd2a14
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
object RocketChatUserAvatar {
/**
* Returns the user avatar URI.
*
* 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
*/
fun getUri(hostname : String, username: String): String {
return "https://" +
hostname.replace("http://", "").replace("https://", "") +
"/avatar/" +
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 {
if (username.isEmpty()) {
return "?"
}
val splitUsername = username.split(".")
if (splitUsername.size > 1) {
return (splitUsername[0].substring(0, 1) + splitUsername[splitUsername.size - 1].substring(0, 1)).toUpperCase()
} else {
if (username.length > 1) {
return username.substring(0, 2).toUpperCase()
} else {
return username.substring(0, 1).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
import chat.rocket.android.helper.RocketChatUserAvatar
import org.junit.Test
class RocketChatUserAvatarTest {
@Test
fun getUsernameInitialsTest() {
assert(RocketChatUserAvatar.getUsernameInitials("") == "?")
assert(RocketChatUserAvatar.getUsernameInitials("?") == "?")
assert(RocketChatUserAvatar.getUsernameInitials("f") == "F")
assert(RocketChatUserAvatar.getUsernameInitials("B") == "B")
assert(RocketChatUserAvatar.getUsernameInitials("fo") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("FO") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("fOo") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("FOO") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("F.O") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("F.o") == "FO")
assert(RocketChatUserAvatar.getUsernameInitials("Foo.bar") == "FB")
assert(RocketChatUserAvatar.getUsernameInitials("Foobar.bar") == "FB")
assert(RocketChatUserAvatar.getUsernameInitials("Foobar.bar.zab") == "FZ")
}
}
\ 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