Commit 1877e61d authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement Avatar

parent a51fb268
......@@ -51,6 +51,7 @@ repositories {
maven { url 'https://github.com/YusukeIwaki/realm-java-helpers/raw/master/repo' }
maven { url 'https://github.com/uPhyca/stetho-realm/raw/master/maven-repo' }
maven { url 'https://github.com/RocketChat/Android-DDP/raw/master/repository' }
maven { url 'http://dl.bintray.com/amulyakhare/maven' }
}
dependencies {
......@@ -61,6 +62,7 @@ dependencies {
compile 'jp.co.crowdworks:realm-java-helpers-bolts:0.0.7'
compile 'com.squareup.okhttp3:okhttp:3.4.1'
compile 'com.squareup.picasso:picasso:2.5.2'
compile 'com.facebook.stetho:stetho:1.4.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.4.1'
......@@ -70,4 +72,6 @@ dependencies {
compile 'com.jakewharton.timber:timber:4.3.1'
compile 'com.jakewharton.rxbinding:rxbinding:0.4.0'
compile 'com.amulyakhare:com.amulyakhare.textdrawable:1.0.1'
}
......@@ -2,7 +2,9 @@ package chat.rocket.android.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.widget.ImageView;
import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.model.ServerConfig;
import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;
......@@ -23,5 +25,8 @@ public class MainActivity extends AbstractAuthedActivity {
return null;
}).continueWith(new LogcatIfError());
}
ImageView myAvatar = (ImageView) findViewById(R.id.img_my_avatar);
new Avatar("demo.rocket.chat", "John Doe").into(myAvatar);
}
}
package chat.rocket.android.helper;
import android.graphics.Typeface;
import android.widget.ImageView;
import com.amulyakhare.textdrawable.TextDrawable;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import timber.log.Timber;
/**
* Helper for rendering user avatar image.
*/
public class Avatar {
private final String hostname;
private final String username;
private static final int[] COLORS = new int[]{
0xFFF44336, 0xFFE91E63, 0xFF9C27B0, 0xFF673AB7, 0xFF3F51B5, 0xFF2196F3,
0xFF03A9F4, 0xFF00BCD4, 0xFF009688, 0xFF4CAF50, 0xFF8BC34A, 0xFFCDDC39,
0xFFFFC107, 0xFFFF9800, 0xFFFF5722, 0xFF795548, 0xFF9E9E9E, 0xFF607D8B
};
public Avatar(String hostname, String username) {
this.hostname = hostname;
this.username = username;
}
private static int getColorForUser(String username) {
return COLORS[username.length() % COLORS.length];
}
private static String getInitialsForUser(String username) {
String name = username
.replaceAll("[^A-Za-z0-9]", ".")
.replaceAll("\\.+", ".")
.replaceAll("(^\\.)|(\\.$)", "");
String[] initials = name.split("\\.");
if (initials.length >= 2) {
return (firstChar(initials[0]) + firstChar(initials[initials.length - 1])).toUpperCase();
} else {
String name2 = name.replaceAll("[^A-Za-z0-9]", "");
return (name2.length() < 2) ? name2 : name2.substring(0, 2).toUpperCase();
}
}
private static String firstChar(String str) {
return TextUtils.isEmpty(str) ? "" : str.substring(0, 1);
}
private String getImageUrl() {
//from Rocket.Chat:packages/rocketchat-ui/lib/avatar.coffee
//REMARK! this is often SVG image! (see: Rocket.Chat:server/startup/avatar.coffee)
try {
return "https://" + hostname + "/avatar/" + URLEncoder.encode(username, "UTF-8") + ".jpg";
} catch (UnsupportedEncodingException exception) {
Timber.e(exception, "failed to get URL for user: %s", username);
return null;
}
}
/**
* render avatar into imageView.
*/
public void into(final ImageView imageView) {
Picasso.with(imageView.getContext())
.load(getImageUrl())
.into(imageView, new Callback() {
@Override public void onSuccess() {
}
@Override public void onError() {
textInto(imageView);
}
});
}
private void textInto(final ImageView imageView) {
int round = (int) (4 * imageView.getContext().getResources().getDisplayMetrics().density);
imageView.setImageDrawable(TextDrawable.builder()
.beginConfig()
.useFont(Typeface.SANS_SERIF)
.endConfig()
.buildRoundRect(getInitialsForUser(username), getColorForUser(username), round));
}
}
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/avatar_image_size_large"
android:layout_height="@dimen/avatar_image_size_large"
>
<FrameLayout
android:id="@+id/avatar_color"
android:layout_width="@dimen/avatar_image_size_large"
android:layout_height="@dimen/avatar_image_size_large"
android:layout_gravity="center"
>
<TextView
android:id="@+id/avatar_initials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="@dimen/avatar_text_size_large"
/>
</FrameLayout>
<ImageView
android:id="@+id/avatar_img"
android:layout_width="@dimen/avatar_image_size_large"
android:layout_height="@dimen/avatar_image_size_large"
android:scaleType="centerInside"
android:src="@drawable/ic_default_avatar"
/>
</FrameLayout>
\ No newline at end of file
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="@dimen/avatar_image_size_normal"
android:layout_height="@dimen/avatar_image_size_normal"
>
<FrameLayout
android:id="@+id/avatar_color"
android:layout_width="@dimen/avatar_image_size_normal"
android:layout_height="@dimen/avatar_image_size_normal"
android:layout_gravity="center"
>
<TextView
android:id="@+id/avatar_initials"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="@dimen/avatar_text_size_normal"
/>
</FrameLayout>
<ImageView
android:id="@+id/avatar_img"
android:layout_width="@dimen/avatar_image_size_normal"
android:layout_height="@dimen/avatar_image_size_normal"
android:scaleType="centerInside"
android:src="@drawable/ic_default_avatar"
/>
</FrameLayout>
\ No newline at end of file
......@@ -37,7 +37,11 @@
android:layout_height="wrap_content"
/>
<include layout="@layout/avatar_container_large"/>
<ImageView
android:id="@+id/img_my_avatar"
android:layout_width="48dp"
android:layout_height="48dp"
/>
<FrameLayout
android:layout_width="0px"
......
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