Commit 5e11a6bd authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement logic to show the status of current user in Sidebar.

parent a994849c
...@@ -59,10 +59,17 @@ public class MainActivity extends AbstractAuthedActivity { ...@@ -59,10 +59,17 @@ public class MainActivity extends AbstractAuthedActivity {
@Override protected void onServerConfigIdUpdated() { @Override protected void onServerConfigIdUpdated() {
super.onServerConfigIdUpdated(); super.onServerConfigIdUpdated();
updateSidebarMainFragment();
showServerConfigActivityIfNeeded();
}
private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId)) .replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(serverConfigId))
.commit(); .commit();
}
private void showServerConfigActivityIfNeeded() {
if (serverConfigId == null) { if (serverConfigId == null) {
return; return;
} }
......
...@@ -20,7 +20,6 @@ import org.json.JSONObject; ...@@ -20,7 +20,6 @@ import org.json.JSONObject;
/** /**
* Utility class for creating/handling MethodCall or RPC. * Utility class for creating/handling MethodCall or RPC.
*
* TODO: separate method into several manager classes (SubscriptionManager, MessageManager, ...). * TODO: separate method into several manager classes (SubscriptionManager, MessageManager, ...).
*/ */
public class MethodCallHelper { public class MethodCallHelper {
......
...@@ -3,16 +3,22 @@ package chat.rocket.android.fragment.sidebar; ...@@ -3,16 +3,22 @@ package chat.rocket.android.fragment.sidebar;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout; import android.widget.LinearLayout;
import android.widget.TextView;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.fragment.AbstractFragment; import chat.rocket.android.fragment.AbstractFragment;
import chat.rocket.android.helper.TextUtils; import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.RoomListManager; import chat.rocket.android.layouthelper.chatroom.RoomListManager;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.RoomSubscription; import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.model.ddp.User;
import chat.rocket.android.realm_helper.RealmHelper; import chat.rocket.android.realm_helper.RealmHelper;
import chat.rocket.android.realm_helper.RealmListObserver; import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore; import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.renderer.UserRenderer;
import com.jakewharton.rxbinding.view.RxView; import com.jakewharton.rxbinding.view.RxView;
import com.jakewharton.rxbinding.widget.RxCompoundButton; import com.jakewharton.rxbinding.widget.RxCompoundButton;
...@@ -20,11 +26,16 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -20,11 +26,16 @@ public class SidebarMainFragment extends AbstractFragment {
private String serverConfigId; private String serverConfigId;
private RoomListManager roomListManager; private RoomListManager roomListManager;
private String hostname;
private RealmListObserver<RoomSubscription> roomsObserver; private RealmListObserver<RoomSubscription> roomsObserver;
private RealmObjectObserver<User> currentUserObserver;
public SidebarMainFragment() { public SidebarMainFragment() {
} }
/**
* create SidebarMainFragment with serverConfigId.
*/
public static SidebarMainFragment create(String serverConfigId) { public static SidebarMainFragment create(String serverConfigId) {
Bundle args = new Bundle(); Bundle args = new Bundle();
args.putString("serverConfigId", serverConfigId); args.putString("serverConfigId", serverConfigId);
...@@ -40,11 +51,21 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -40,11 +51,21 @@ public class SidebarMainFragment extends AbstractFragment {
Bundle args = getArguments(); Bundle args = getArguments();
serverConfigId = args == null ? null : args.getString("serverConfigId"); serverConfigId = args == null ? null : args.getString("serverConfigId");
if (!TextUtils.isEmpty(serverConfigId)) { if (!TextUtils.isEmpty(serverConfigId)) {
ServerConfig config = RealmStore.getDefault().executeTransactionForRead(realm ->
realm.where(ServerConfig.class).equalTo("serverConfigId", serverConfigId).findFirst());
if (config != null) {
hostname = config.getHostname();
}
RealmHelper realmHelper = RealmStore.get(serverConfigId); RealmHelper realmHelper = RealmStore.get(serverConfigId);
if (realmHelper != null) { if (realmHelper != null) {
roomsObserver = realmHelper roomsObserver = realmHelper
.createListObserver(realm -> realm.where(RoomSubscription.class).findAll()) .createListObserver(realm -> realm.where(RoomSubscription.class).findAll())
.setOnUpdateListener(list -> roomListManager.setRooms(list)); .setOnUpdateListener(list -> roomListManager.setRooms(list));
currentUserObserver = realmHelper
.createObjectObserver(realm -> realm.where(User.class).isNotEmpty("emails"))
.setOnUpdateListener(this::updateCurrentUser);
} }
} }
} }
...@@ -86,15 +107,26 @@ public class SidebarMainFragment extends AbstractFragment { ...@@ -86,15 +107,26 @@ public class SidebarMainFragment extends AbstractFragment {
.subscribe(RxView.visibility(rootView.findViewById(R.id.user_action_outer_container))); .subscribe(RxView.visibility(rootView.findViewById(R.id.user_action_outer_container)));
} }
private void updateCurrentUser(User user) {
if (user != null && !TextUtils.isEmpty(hostname)) {
new UserRenderer(getContext(), user)
.avatarInto((ImageView) rootView.findViewById(R.id.current_user_avatar), hostname)
.usernameInto((TextView) rootView.findViewById(R.id.current_user_name))
.statusColorInto((ImageView) rootView.findViewById(R.id.current_user_status));
}
}
@Override public void onResume() { @Override public void onResume() {
super.onResume(); super.onResume();
if (roomsObserver != null) { if (roomsObserver != null) {
roomsObserver.sub(); roomsObserver.sub();
currentUserObserver.sub();
} }
} }
@Override public void onPause() { @Override public void onPause() {
if (roomsObserver != null) { if (roomsObserver != null) {
currentUserObserver.unsub();
roomsObserver.unsub(); roomsObserver.unsub();
} }
super.onPause(); super.onPause();
......
...@@ -10,6 +10,11 @@ import io.realm.annotations.PrimaryKey; ...@@ -10,6 +10,11 @@ import io.realm.annotations.PrimaryKey;
@SuppressWarnings({"PMD.ShortClassName", "PMD.ShortVariable", @SuppressWarnings({"PMD.ShortClassName", "PMD.ShortVariable",
"PMD.MethodNamingConventions", "PMD.VariableNamingConventions"}) "PMD.MethodNamingConventions", "PMD.VariableNamingConventions"})
public class User extends RealmObject { public class User extends RealmObject {
public static final String STATUS_ONLINE = "online";
public static final String STATUS_BUSY = "busy";
public static final String STATUS_AWAY = "away";
public static final String STATUS_OFFLINE = "offline";
@PrimaryKey private String _id; @PrimaryKey private String _id;
private String username; private String username;
private String status; private String status;
......
package chat.rocket.android.renderer;
import android.content.Context;
import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.model.ddp.User;
/**
* Renderer for User model.
*/
public class UserRenderer extends AbstractRenderer<User> {
public UserRenderer(Context context, User user) {
super(context, user);
}
/**
* show Avatar image.
*/
public UserRenderer avatarInto(ImageView imageView, String hostname) {
if (!shouldHandle(imageView)) {
return this;
}
new Avatar(hostname, object.getUsername()).into(imageView);
return this;
}
/**
* show Username in textView.
*/
public UserRenderer usernameInto(TextView textView) {
if (!shouldHandle(textView)) {
return this;
}
textView.setText(object.getUsername());
return this;
}
/**
* show user's status color into imageView.
*/
public UserRenderer statusColorInto(ImageView imageView) {
if (!shouldHandle(imageView)) {
return this;
}
String status = object.getStatus();
if (User.STATUS_ONLINE.equals(status)) {
imageView.setImageResource(R.drawable.userstatus_online);
} else if (User.STATUS_AWAY.equals(status)) {
imageView.setImageResource(R.drawable.userstatus_away);
} else if (User.STATUS_BUSY.equals(status)) {
imageView.setImageResource(R.drawable.userstatus_busy);
} else if (User.STATUS_OFFLINE.equals(status)) {
imageView.setImageResource(R.drawable.userstatus_offline);
} else {
// unknown status is rendered as "offline" status.
imageView.setImageResource(R.drawable.userstatus_offline);
}
return this;
}
}
...@@ -21,7 +21,7 @@ ...@@ -21,7 +21,7 @@
> >
<ImageView <ImageView
android:id="@+id/img_userstatus" android:id="@+id/current_user_status"
android:layout_width="8dp" android:layout_width="8dp"
android:layout_height="8dp" android:layout_height="8dp"
android:src="@drawable/userstatus_online" android:src="@drawable/userstatus_online"
...@@ -33,7 +33,7 @@ ...@@ -33,7 +33,7 @@
/> />
<ImageView <ImageView
android:id="@+id/img_my_avatar" android:id="@+id/current_user_avatar"
android:layout_width="48dp" android:layout_width="48dp"
android:layout_height="48dp" android:layout_height="48dp"
/> />
...@@ -47,7 +47,7 @@ ...@@ -47,7 +47,7 @@
> >
<TextView <TextView
android:id="@+id/txt_account_info" android:id="@+id/current_user_name"
android:layout_width="match_parent" android:layout_width="match_parent"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="center_vertical" android:layout_gravity="center_vertical"
......
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