Commit 562c7e0e authored by Yusuke Iwaki's avatar Yusuke Iwaki

add RoomFragment.

parent f4803387
......@@ -2,11 +2,13 @@ package chat.rocket.android.activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.widget.SlidingPaneLayout;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import chat.rocket.android.R;
import chat.rocket.android.fragment.chatroom.HomeFragment;
import chat.rocket.android.fragment.chatroom.RoomFragment;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.layouthelper.chatroom.RoomListManager;
import chat.rocket.android.model.Room;
......@@ -44,11 +46,23 @@ public class MainActivity extends AbstractAuthedActivity {
roomListManager = new RoomListManager(
(LinearLayout) findViewById(R.id.channels_container),
(LinearLayout) findViewById(R.id.direct_messages_container));
roomListManager.setOnItemClickListener(view -> {
showRoomFragment(view.getRoomId());
closeSidebarIfNeeded();
});
ImageView myAvatar = (ImageView) findViewById(R.id.img_my_avatar);
new Avatar("demo.rocket.chat", "John Doe").into(myAvatar);
}
private void closeSidebarIfNeeded() {
// REMARK: Tablet UI doesn't have SlidingPane!
SlidingPaneLayout pane = (SlidingPaneLayout) findViewById(R.id.sliding_pane);
if (pane != null) {
pane.closePane();
}
}
private void setupUserActionToggle() {
final CompoundButton toggleUserAction =
((CompoundButton) findViewById(R.id.toggle_user_action));
......@@ -71,6 +85,10 @@ public class MainActivity extends AbstractAuthedActivity {
}
};
private void showRoomFragment(String roomId) {
showFragment(RoomFragment.create(roomId));
}
@Override protected void onResume() {
super.onResume();
roomsObserver.sub();
......
......@@ -3,6 +3,9 @@ package chat.rocket.android.fragment.chatroom;
import chat.rocket.android.R;
public class HomeFragment extends AbstractChatRoomFragment {
public HomeFragment() {
}
@Override protected int getLayout() {
return R.layout.fragment_home;
}
......@@ -10,4 +13,9 @@ public class HomeFragment extends AbstractChatRoomFragment {
@Override protected void onSetupView() {
activityToolbar.setTitle("Rocket.Chat - Home");
}
@Override public void onResume() {
super.onResume();
activityToolbar.setTitle("Rocket.Chat - Home");
}
}
package chat.rocket.android.fragment.chatroom;
import android.os.Bundle;
import android.support.annotation.Nullable;
import chat.rocket.android.R;
import chat.rocket.android.model.Room;
import io.realm.Realm;
import io.realm.RealmQuery;
import jp.co.crowdworks.realm_java_helpers.RealmObjectObserver;
/**
*/
public class RoomFragment extends AbstractChatRoomFragment {
private String roomId;
public static RoomFragment create(String roomId) {
Bundle args = new Bundle();
args.putString("roomId", roomId);
RoomFragment fragment = new RoomFragment();
fragment.setArguments(args);
return fragment;
}
public RoomFragment() {
}
@Override public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle args = getArguments();
roomId = args.getString("roomId");
}
@Override protected int getLayout() {
return R.layout.fragment_room;
}
@Override protected void onSetupView() {
}
private RealmObjectObserver<Room> roomObserver = new RealmObjectObserver<Room>() {
@Override protected RealmQuery<Room> query(Realm realm) {
return realm.where(Room.class).equalTo("_id", roomId);
}
@Override protected void onChange(Room room) {
onRenderRoom(room);
}
};
private void onRenderRoom(Room room) {
activityToolbar.setTitle(room.getName());
}
@Override public void onResume() {
super.onResume();
roomObserver.sub();
}
@Override public void onPause() {
roomObserver.unsub();
super.onPause();
}
}
package chat.rocket.android.layouthelper.chatroom;
import android.view.View;
import android.view.ViewGroup;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.Room;
......@@ -13,6 +14,15 @@ public class RoomListManager {
private ViewGroup channelsContainer;
private ViewGroup dmContainer;
/**
* Callback interface for List item clicked.
*/
public interface OnItemClickListener {
void onItemClick(RoomListItemView roomListItemView);
}
private OnItemClickListener mListener;
/**
* constructor with two ViewGroups.
*/
......@@ -43,13 +53,20 @@ public class RoomListManager {
}
}
private static void insertOrUpdateItem(ViewGroup parent, Room room) {
/**
* set callback on List item clicked.
*/
public void setOnItemClickListener(OnItemClickListener listener) {
mListener = listener;
}
private void insertOrUpdateItem(ViewGroup parent, Room room) {
final String roomName = room.getName();
int index;
for (index = 0; index < parent.getChildCount(); index++) {
RoomListItemView roomListItemView = (RoomListItemView) parent.getChildAt(index);
final String targetRoomName = roomListItemView.getName();
final String targetRoomName = roomListItemView.getRoomName();
if (roomName.equals(targetRoomName)) {
updateRoomItemView(roomListItemView, room);
return;
......@@ -68,16 +85,28 @@ public class RoomListManager {
}
}
private static void updateRoomItemView(RoomListItemView roomListItemView, Room room) {
private void updateRoomItemView(RoomListItemView roomListItemView, Room room) {
roomListItemView
.setRoom(room.getT(), room.getName())
.setRoomId(room.get_id())
.setRoomName(room.getName())
.setRoomType(room.getT())
.setAlertCount(0); // TODO not implemented yet.
roomListItemView.setOnClickListener(this::onItemClick);
}
private void onItemClick(View view) {
if (view instanceof RoomListItemView) {
if (mListener != null) {
mListener.onItemClick((RoomListItemView) view);
}
}
}
private static void removeItemIfExists(ViewGroup parent, String roomName) {
for (int i = 0; i < parent.getChildCount(); i++) {
RoomListItemView roomListItemView = (RoomListItemView) parent.getChildAt(i);
if (roomName.equals(roomListItemView.getName())) {
if (roomName.equals(roomListItemView.getRoomName())) {
parent.removeViewAt(i);
break;
}
......
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
android:id="@+id/sliding_pane"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
......@@ -33,6 +34,7 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/white"
android:clickable="true"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
></FrameLayout>
......
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="@+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="@+id/btn_compose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="@dimen/margin_16"
/>
</FrameLayout>
\ No newline at end of file
......@@ -2,6 +2,7 @@ package chat.rocket.android.widget.internal;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
......@@ -14,7 +15,8 @@ import java.util.HashMap;
* Room list-item view used in sidebar.
*/
public class RoomListItemView extends LinearLayout {
private String name;
private String roomId;
private String roomName;
private static HashMap<String, Integer> ICON_TABLE = new HashMap<String, Integer>(){
{
......@@ -47,19 +49,31 @@ public class RoomListItemView extends LinearLayout {
private void initialize(Context context) {
setOrientation(HORIZONTAL);
TypedArray array2 = context.getTheme().obtainStyledAttributes(new int[]{
R.attr.selectableItemBackground
});
setBackground(array2.getDrawable(0));
array2.recycle();
View.inflate(context, R.layout.room_list_item, this);
}
public RoomListItemView setRoom(String type, String name) {
public String getRoomId() {
return roomId;
}
public RoomListItemView setRoomId(String roomId) {
this.roomId = roomId;
return this;
}
public RoomListItemView setRoomType(String type) {
if (ICON_TABLE.containsKey(type)) {
TextView icon = (TextView) findViewById(R.id.icon);
icon.setText(ICON_TABLE.get(type));
}
TextView text = (TextView) findViewById(R.id.text);
text.setText(name);
this.name = name;
return this;
}
......@@ -76,7 +90,15 @@ public class RoomListItemView extends LinearLayout {
return this;
}
public String getName() {
return name;
public String getRoomName() {
return roomName;
}
public RoomListItemView setRoomName(String roomName) {
this.roomName = roomName;
TextView text = (TextView) findViewById(R.id.text);
text.setText(roomName);
return this;
}
}
......@@ -5,7 +5,8 @@
<FrameLayout
android:layout_width="32dp"
android:layout_height="?attr/listPreferredItemHeightSmall"
android:layout_marginEnd="16dp">
android:layout_marginEnd="16dp"
>
<chat.rocket.android.widget.FontAwesomeTextView
android:id="@+id/icon"
android:layout_width="wrap_content"
......
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