Skip to content
This repository has been archived by the owner on Jun 7, 2020. It is now read-only.

Commit

Permalink
change MethodCall to RealmModel! and fix get room operation...(not co…
Browse files Browse the repository at this point in the history
…mpletely fixed...)
  • Loading branch information
Yusuke Iwaki committed Sep 11, 2016
1 parent a863de3 commit 0c9353e
Show file tree
Hide file tree
Showing 21 changed files with 571 additions and 562 deletions.
3 changes: 3 additions & 0 deletions app/build.gradle
Expand Up @@ -2,6 +2,7 @@ apply plugin: 'com.android.application'
apply plugin: 'com.neenbedankt.android-apt'
apply plugin: 'com.jakewharton.hugo'
apply plugin: 'io.fabric'
apply plugin: 'realm-android'

def getCurrentCommitMessage() {
def gitlog = 'git log origin/develop...HEAD --decorate'.execute().text
Expand Down Expand Up @@ -69,6 +70,8 @@ dependencies {
compile 'com.parse.bolts:bolts-tasks:1.4.0'

compile 'chat.rocket:android-ddp:0.0.2'
compile 'jp.co.crowdworks:realm-java-helpers:0.0.4'
compile 'jp.co.crowdworks:realm-java-helpers-bolts:0.0.4'

compile 'com.facebook.stetho:stetho:1.3.1'
compile 'com.facebook.stetho:stetho-okhttp3:1.3.1'
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/chat/rocket/android/Constants.java
@@ -1,7 +1,22 @@
package chat.rocket.android;

import android.util.Log;

import bolts.Continuation;
import bolts.Task;

public class Constants {
public static final String LOG_TAG = "Rocket.Chat.Android";

public static final String AUTHORITY = "chat.rocket.android";

public static final Continuation ERROR_LOGGING = new Continuation() {
@Override
public Object then(Task task) throws Exception {
if (task.isFaulted()) {
Log.e(LOG_TAG, "error", task.getError());
}
return null;
}
};
}
18 changes: 18 additions & 0 deletions app/src/main/java/chat/rocket/android/RealmConfig.java
@@ -0,0 +1,18 @@
package chat.rocket.android;

import android.content.Context;

import io.realm.Realm;
import io.realm.RealmConfiguration;

public class RealmConfig {
private static RealmConfiguration sConfig;
public static void setDefault(Context context) {
sConfig = new RealmConfiguration.Builder(context)
.name("default.realm")
.schemaVersion(1)
.deleteRealmIfMigrationNeeded()
.build();
Realm.setDefaultConfiguration(sConfig);
}
}
Expand Up @@ -27,5 +27,6 @@ public void onCreate() {

CrashlyticsCore core = new CrashlyticsCore.Builder().disabled(BuildConfig.DEBUG).build();
Fabric.with(this, new Crashlytics.Builder().core(core).build());
RealmConfig.setDefault(this);
}
}
24 changes: 22 additions & 2 deletions app/src/main/java/chat/rocket/android/activity/MainActivity.java
Expand Up @@ -31,6 +31,9 @@

import com.github.clans.fab.FloatingActionMenu;

import org.json.JSONException;
import org.json.JSONObject;

import chat.rocket.android.Constants;
import chat.rocket.android.R;
import chat.rocket.android.content.RocketChatDatabaseHelper;
Expand All @@ -40,14 +43,16 @@
import chat.rocket.android.fragment.ChatRoomFragment;
import chat.rocket.android.fragment.HomeRoomFragment;
import chat.rocket.android.model.Message;
import chat.rocket.android.model.MethodCall;
import chat.rocket.android.model2.MethodCall;
import chat.rocket.android.model.Room;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState;
import chat.rocket.android.model.User;
import chat.rocket.android.preference.Cache;
import chat.rocket.android.view.Avatar;
import chat.rocket.android.view.CursorRecyclerViewAdapter;
import io.realm.Realm;
import jp.co.crowdworks.realm_java_helpers_bolts.RealmHelperBolts;

public class MainActivity extends AbstractActivity {
private static final String TAG = Constants.LOG_TAG;
Expand All @@ -65,6 +70,7 @@ protected int getContainerId() {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
requestGetRooms();
showHomeRoomFragment();

setupUserInfo();
Expand All @@ -90,6 +96,13 @@ public ServerConfig process(SQLiteDatabase db) {
});
}

private void requestGetRooms() {
try {
MethodCall.create("rooms/get", new JSONObject().put("timestamp", 0));
} catch (JSONException e) {
}
}

private BroadcastReceiver mToggleNavReveiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand Down Expand Up @@ -221,7 +234,7 @@ public void onItemClick(AdapterView<?> parent, View view, int position, long id)
Context context = parent.getContext();
final ServerConfig s = getPrimaryServerConfig();
if(s!=null) {
MethodCall.create("logout",null).putByContentProvider(context);
MethodCall.create("logout",null);
RocketChatDatabaseHelper.writeWithTransaction(context, new RocketChatDatabaseHelper.DBCallbackEx<Object>() {
@Override
public Object process(SQLiteDatabase db) throws Exception {
Expand All @@ -242,6 +255,13 @@ public void handleException(Exception e) {
.remove(Cache.KEY_MY_USER_ID)
.remove(Cache.KEY_MY_USER_NAME)
.commit();
RealmHelperBolts.executeTransactionAsync(new RealmHelperBolts.Transaction() {
@Override
public Object execute(Realm realm) throws Exception {
realm.deleteAll();
return null;
}
});
showEntryActivity();
}
}
Expand Down
Expand Up @@ -93,6 +93,13 @@ public Task<DDPClientCallback.RPC> sendMessage(final String roomId, String msg,
return mDDPClient.rpc("sendMessage", new JSONArray().put(param) ,generateId("message"));
}

public Task<DDPClientCallback.RPC> getRooms(long time) throws JSONException {
JSONArray params = new JSONArray()
.put(new JSONObject().put("$date",time));

return mDDPClient.rpc("rooms/get", params, generateId("rooms-get"));
}

public Task<DDPClientCallback.RPC> loadMessages(final String roomID, long endTs, int num) throws JSONException {
JSONArray params = new JSONArray()
.put(roomID)
Expand Down
Expand Up @@ -22,15 +22,12 @@
import chat.rocket.android.content.observer.AddRoomHandler;
import chat.rocket.android.content.observer.LoginHandler;
import chat.rocket.android.content.observer.MarkRoomAsReadHandler;
import chat.rocket.android.content.observer.MethodCallObserver;
import chat.rocket.android.content.observer.MethodCall2Observer;
import chat.rocket.android.content.observer.RocketChatRoom;
import chat.rocket.android.content.observer.SendNewMessageHandler;
import chat.rocket.android.content.observer.UserStatusObserver;
import chat.rocket.android.content.subscriber.FilteredUsers;
import chat.rocket.android.content.subscriber.LoginServiceConfiguration;
import chat.rocket.android.content.subscriber.RocketChatSubscription;
import chat.rocket.android.content.subscriber.StreamMessage;
import chat.rocket.android.content.subscriber.StreamNotifyRoom;
import chat.rocket.android.content.subscriber.UserData;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SyncState;
Expand All @@ -44,18 +41,16 @@ public class RocketChatWSService extends Service {

private static final Class[] HANDLER_CLASSES = {
LoginHandler.class
, RocketChatSubscription.class
, RocketChatRoom.class
, AddRoomHandler.class
, MethodCallObserver.class
, SendNewMessageHandler.class
, UserData.class
, UserStatusObserver.class
, StreamMessage.class
, StreamNotifyRoom.class
, MarkRoomAsReadHandler.class
, LoginServiceConfiguration.class
, FilteredUsers.class

, MethodCall2Observer.class
};

private RocketChatWSAPI mAPI;
Expand Down

This file was deleted.

Expand Up @@ -12,12 +12,10 @@
import android.provider.BaseColumns;
import android.text.TextUtils;

import java.util.ArrayList;
import java.util.HashMap;

import chat.rocket.android.Constants;
import chat.rocket.android.model.Message;
import chat.rocket.android.model.MethodCall;
import chat.rocket.android.model.Room;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.SqliteUtil;
Expand All @@ -29,12 +27,10 @@ public class RocketChatProvider extends ContentProvider {
public static final String CONTENT_URI_BASE = "content://"+Constants.AUTHORITY;

RocketChatDatabaseHelper mDBHelper;
RocketChatOnMemoryDatabaseHelper mOnMemDBHelper;

@Override
public boolean onCreate() {
mDBHelper = new RocketChatDatabaseHelper(getContext());
mOnMemDBHelper = new RocketChatOnMemoryDatabaseHelper(getContext());
return true;
}

Expand All @@ -48,11 +44,8 @@ public boolean onCreate() {
, Room.TABLE_NAME
, ServerConfig.TABLE_NAME
, User.TABLE_NAME
, MethodCall.TABLE_NAME
, UserRoom.TABLE_NAME
};
private static final ArrayList<String> MODELS_ONMEM = new ArrayList<>();

static
{
for(int i=0;i<MODELS.length;i++){
Expand All @@ -69,7 +62,6 @@ public boolean onCreate() {
URI_MAP.put(i + _NEW, model);
sURIMatcher.addURI(Constants.AUTHORITY, model, i + _NEW);
}
MODELS_ONMEM.add(MethodCall.TABLE_NAME);
}
public static String getQueryId(Uri uri){
return uri.getPathSegments().get(1);
Expand Down Expand Up @@ -97,8 +89,7 @@ public static Uri getUriForQuery(Uri uriBase, long id) {
}

private SQLiteOpenHelper getDatabaseHelperFor(String table){
if(MODELS_ONMEM.contains(table)) return mOnMemDBHelper;
else return mDBHelper;
return mDBHelper;
}

@Override
Expand Down
@@ -0,0 +1,57 @@
package chat.rocket.android.content.observer;

import android.content.Context;
import android.os.Handler;
import android.os.Looper;

import chat.rocket.android.Constants;
import chat.rocket.android.api.ws.RocketChatWSAPI;
import chat.rocket.android.content.Registerable;
import io.realm.RealmObject;
import jp.co.crowdworks.realm_java_helpers.RealmObjectObserver;

public abstract class AbstractRealmObserver<T extends RealmObject> extends RealmObjectObserver<T> implements Registerable {
protected static final String TAG = Constants.LOG_TAG;
protected final Context mContext;
protected final RocketChatWSAPI mAPI;
private final Handler mHandler;

public AbstractRealmObserver(Context context, Looper looper, RocketChatWSAPI api) {
super();
mContext = context;
mHandler = new Handler(looper);
mAPI = api;
}

@Override
public void register() {
mHandler.post(new Runnable() {
@Override
public void run() {
sub();
}
});
}

@Override
public void unregister() {
mHandler.post(new Runnable() {
@Override
public void run() {
unsub();
}
});
}

@Override
protected final void onChange(final T t) {
mHandler.post(new Runnable() {
@Override
public void run() {
onModelChanged(t);
}
});
}

protected abstract void onModelChanged(T t);
}

0 comments on commit 0c9353e

Please sign in to comment.