RoomFragment.java 3.31 KB
Newer Older
Yusuke Iwaki's avatar
Yusuke Iwaki committed
1 2 3 4
package chat.rocket.android.fragment.chatroom;

import android.os.Bundle;
import android.support.annotation.Nullable;
5 6
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
7
import chat.rocket.android.R;
8
import chat.rocket.android.helper.LogcatIfError;
9
import chat.rocket.android.layouthelper.chatroom.MessageListAdapter;
10
import chat.rocket.android.model.SyncState;
11
import chat.rocket.android.model.ddp.Message;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
12
import chat.rocket.android.model.ddp.RoomSubscription;
13 14
import chat.rocket.android.model.internal.LoadMessageProcedure;
import chat.rocket.android.realm_helper.RealmHelper;
15
import chat.rocket.android.realm_helper.RealmModelListView;
16 17
import chat.rocket.android.realm_helper.RealmObjectObserver;
import chat.rocket.android.realm_helper.RealmStore;
18 19 20
import io.realm.Realm;
import io.realm.RealmResults;
import io.realm.Sort;
21
import org.json.JSONObject;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
22 23

/**
Yusuke Iwaki's avatar
Yusuke Iwaki committed
24
 * Chat room screen.
Yusuke Iwaki's avatar
Yusuke Iwaki committed
25 26 27
 */
public class RoomFragment extends AbstractChatRoomFragment {

28
  private RealmHelper realmHelper;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
29
  private String roomId;
30
  private RealmObjectObserver<RoomSubscription> roomObserver;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
31

Yusuke Iwaki's avatar
Yusuke Iwaki committed
32 33 34
  /**
   * create fragment with roomId.
   */
35
  public static RoomFragment create(String serverConfigId, String roomId) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
36
    Bundle args = new Bundle();
37
    args.putString("serverConfigId", serverConfigId);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
38 39 40 41 42 43 44 45 46 47 48 49 50
    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();
51
    realmHelper = RealmStore.get(args.getString("serverConfigId"));
Yusuke Iwaki's avatar
Yusuke Iwaki committed
52
    roomId = args.getString("roomId");
53 54 55
    roomObserver = realmHelper
        .createObjectObserver(realm -> realm.where(RoomSubscription.class).equalTo("rid", roomId))
        .setOnUpdateListener(this::onRenderRoom);
56 57

    initialRequest();
Yusuke Iwaki's avatar
Yusuke Iwaki committed
58 59 60 61 62 63 64
  }

  @Override protected int getLayout() {
    return R.layout.fragment_room;
  }

  @Override protected void onSetupView() {
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
    RealmModelListView listView = (RealmModelListView) rootView.findViewById(R.id.listview);

    realmHelper.bindListView(listView,
        realm -> realm.where(Message.class)
            .equalTo("rid", roomId)
            .findAllSorted("ts", Sort.DESCENDING),
        MessageListAdapter::new);

    RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(getContext(),
        LinearLayoutManager.VERTICAL, true);
    listView.setLayoutManager(layoutManager);
  }

  private void onRenderRoom(RoomSubscription roomSubscription) {
    activityToolbar.setTitle(roomSubscription.getName());
  }
Yusuke Iwaki's avatar
Yusuke Iwaki committed
81

82
  private void initialRequest() {
83
    realmHelper.executeTransaction(realm -> {
84 85 86 87 88 89 90
      realm.createOrUpdateObjectFromJson(LoadMessageProcedure.class, new JSONObject()
          .put("roomId", roomId)
          .put("syncstate", SyncState.NOT_SYNCED)
          .put("count", 50)
          .put("reset", true));
      return null;
    }).continueWith(new LogcatIfError());
Yusuke Iwaki's avatar
Yusuke Iwaki committed
91 92
  }

93 94
  private RealmResults<Message> queryItems(Realm realm) {
    return realm.where(Message.class).equalTo("rid", roomId).findAllSorted("ts");
Yusuke Iwaki's avatar
Yusuke Iwaki committed
95 96 97 98 99 100 101 102 103 104 105 106
  }

  @Override public void onResume() {
    super.onResume();
    roomObserver.sub();
  }

  @Override public void onPause() {
    roomObserver.unsub();
    super.onPause();
  }
}