Commit 1071f098 authored by Tiago Cunha's avatar Tiago Cunha

A little better (?)

parent d0e6bc2f
...@@ -45,6 +45,7 @@ import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionIte ...@@ -45,6 +45,7 @@ import chat.rocket.android.layouthelper.extra_action.upload.AudioUploadActionIte
import chat.rocket.android.layouthelper.extra_action.upload.ImageUploadActionItem; import chat.rocket.android.layouthelper.extra_action.upload.ImageUploadActionItem;
import chat.rocket.android.layouthelper.extra_action.upload.VideoUploadActionItem; import chat.rocket.android.layouthelper.extra_action.upload.VideoUploadActionItem;
import chat.rocket.android.log.RCLog; import chat.rocket.android.log.RCLog;
import chat.rocket.core.interactors.MessageInteractor;
import chat.rocket.core.models.Message; import chat.rocket.core.models.Message;
import chat.rocket.core.models.Room; import chat.rocket.core.models.Room;
import chat.rocket.persistence.realm.repositories.RealmMessageRepository; import chat.rocket.persistence.realm.repositories.RealmMessageRepository;
...@@ -109,11 +110,16 @@ public class RoomFragment extends AbstractChatRoomFragment ...@@ -109,11 +110,16 @@ public class RoomFragment extends AbstractChatRoomFragment
hostname = args.getString(HOSTNAME); hostname = args.getString(HOSTNAME);
roomId = args.getString(ROOM_ID); roomId = args.getString(ROOM_ID);
MessageInteractor messageInteractor = new MessageInteractor(
new RealmMessageRepository(hostname),
new RealmRoomRepository(hostname)
);
presenter = new RoomPresenter( presenter = new RoomPresenter(
roomId, roomId,
new RealmUserRepository(hostname), new RealmUserRepository(hostname),
messageInteractor,
new RealmRoomRepository(hostname), new RealmRoomRepository(hostname),
new RealmMessageRepository(hostname),
new MethodCallHelper(getContext(), hostname), new MethodCallHelper(getContext(), hostname),
ConnectivityManager.getInstance(getContext()) ConnectivityManager.getInstance(getContext())
); );
......
...@@ -4,15 +4,15 @@ import android.support.annotation.NonNull; ...@@ -4,15 +4,15 @@ import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.v4.util.Pair; import android.support.v4.util.Pair;
import java.util.UUID;
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogcatIfError; import chat.rocket.android.helper.LogcatIfError;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.SyncState; import chat.rocket.core.SyncState;
import chat.rocket.core.interactors.MessageInteractor;
import chat.rocket.core.models.Message; import chat.rocket.core.models.Message;
import chat.rocket.core.models.RoomHistoryState; import chat.rocket.core.models.Room;
import chat.rocket.core.repositories.MessageRepository; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.RoomRepository; import chat.rocket.core.repositories.RoomRepository;
import chat.rocket.core.repositories.UserRepository; import chat.rocket.core.repositories.UserRepository;
import chat.rocket.android.service.ConnectivityManagerApi; import chat.rocket.android.service.ConnectivityManagerApi;
...@@ -24,21 +24,22 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -24,21 +24,22 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
implements RoomContract.Presenter { implements RoomContract.Presenter {
private final String roomId; private final String roomId;
private final MessageInteractor messageInteractor;
private final UserRepository userRepository; private final UserRepository userRepository;
private final RoomRepository roomRepository; private final RoomRepository roomRepository;
private final MessageRepository messageRepository;
private final MethodCallHelper methodCallHelper; private final MethodCallHelper methodCallHelper;
private final ConnectivityManagerApi connectivityManagerApi; private final ConnectivityManagerApi connectivityManagerApi;
public RoomPresenter(String roomId, UserRepository userRepository, public RoomPresenter(String roomId,
UserRepository userRepository,
MessageInteractor messageInteractor,
RoomRepository roomRepository, RoomRepository roomRepository,
MessageRepository messageRepository,
MethodCallHelper methodCallHelper, MethodCallHelper methodCallHelper,
ConnectivityManagerApi connectivityManagerApi) { ConnectivityManagerApi connectivityManagerApi) {
this.roomId = roomId; this.roomId = roomId;
this.userRepository = userRepository; this.userRepository = userRepository;
this.messageInteractor = messageInteractor;
this.roomRepository = roomRepository; this.roomRepository = roomRepository;
this.messageRepository = messageRepository;
this.methodCallHelper = methodCallHelper; this.methodCallHelper = methodCallHelper;
this.connectivityManagerApi = connectivityManagerApi; this.connectivityManagerApi = connectivityManagerApi;
} }
...@@ -54,16 +55,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -54,16 +55,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void loadMessages() { public void loadMessages() {
RoomHistoryState roomHistoryState = RoomHistoryState.builder() final Subscription subscription = getSingleRoom()
.setRoomId(roomId) .flatMap(messageInteractor::loadMessages)
.setSyncState(SyncState.NOT_SYNCED)
.setCount(100)
.setReset(true)
.setComplete(false)
.setTimestamp(0)
.build();
final Subscription subscription = roomRepository.setHistoryState(roomHistoryState)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> { .subscribe(success -> {
...@@ -78,16 +71,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -78,16 +71,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void loadMoreMessages() { public void loadMoreMessages() {
final Subscription subscription = roomRepository.getHistoryStateByRoomId(roomId) final Subscription subscription = getSingleRoom()
.filter(roomHistoryState -> { .flatMap(messageInteractor::loadMoreMessages)
int syncState = roomHistoryState.getSyncState();
return !roomHistoryState.isComplete()
&& (syncState == SyncState.SYNCED || syncState == SyncState.FAILED);
})
.first()
.toSingle()
.flatMap(roomHistoryState -> roomRepository
.setHistoryState(roomHistoryState.withSyncState(SyncState.NOT_SYNCED)))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> { .subscribe(success -> {
...@@ -112,23 +97,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -112,23 +97,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void sendMessage(String messageText) { public void sendMessage(String messageText) {
final Subscription subscription = userRepository.getCurrent() final Subscription subscription = getRoomUserPair()
.filter(user -> user != null) .flatMap(pair -> messageInteractor.send(pair.first, pair.second, messageText))
.first()
.toSingle()
.flatMap(user -> {
Message message = Message.builder()
.setId(UUID.randomUUID().toString())
.setSyncState(SyncState.NOT_SYNCED)
.setTimestamp(System.currentTimeMillis())
.setRoomId(roomId)
.setMessage(messageText)
.setGroupable(false)
.setUser(user)
.build();
return messageRepository.save(message);
})
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(success -> { .subscribe(success -> {
...@@ -142,8 +112,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -142,8 +112,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void resendMessage(Message message) { public void resendMessage(Message message) {
final Subscription subscription = messageRepository.resend( final Subscription subscription = messageInteractor.resend(message)
message.withSyncState(SyncState.NOT_SYNCED))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(); .subscribe();
...@@ -153,7 +122,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -153,7 +122,7 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void deleteMessage(Message message) { public void deleteMessage(Message message) {
final Subscription subscription = messageRepository.delete(message) final Subscription subscription = messageInteractor.delete(message)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(); .subscribe();
...@@ -163,17 +132,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -163,17 +132,8 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
@Override @Override
public void onUnreadCount() { public void onUnreadCount() {
final Subscription subscription = Single.zip( final Subscription subscription = getRoomUserPair()
userRepository.getCurrent() .flatMap(roomUserPair -> messageInteractor
.filter(user -> user != null)
.first()
.toSingle(),
roomRepository.getById(roomId)
.first()
.toSingle(),
(user, room) -> new Pair<>(room, user)
)
.flatMap(roomUserPair -> messageRepository
.unreadCountFor(roomUserPair.first, roomUserPair.second)) .unreadCountFor(roomUserPair.first, roomUserPair.second))
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
...@@ -232,11 +192,28 @@ public class RoomPresenter extends BasePresenter<RoomContract.View> ...@@ -232,11 +192,28 @@ public class RoomPresenter extends BasePresenter<RoomContract.View>
private void getMessages() { private void getMessages() {
final Subscription subscription = roomRepository.getById(roomId) final Subscription subscription = roomRepository.getById(roomId)
.first() .first()
.flatMap(messageRepository::getAllFrom) .flatMap(messageInteractor::getAllFrom)
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(messages -> view.showMessages(messages)); .subscribe(messages -> view.showMessages(messages));
addSubscription(subscription); addSubscription(subscription);
} }
private Single<Pair<Room, User>> getRoomUserPair() {
return Single.zip(
getSingleRoom(),
userRepository.getCurrent()
.filter(user -> user != null)
.first()
.toSingle(),
Pair::new
);
}
private Single<Room> getSingleRoom() {
return roomRepository.getById(roomId)
.first()
.toSingle();
}
} }
...@@ -25,9 +25,8 @@ public abstract class ModelListAdapter<T, VM, VH extends ModelViewHolder<VM>> ...@@ -25,9 +25,8 @@ public abstract class ModelListAdapter<T, VM, VH extends ModelViewHolder<VM>>
protected abstract int getRealmModelViewType(VM model); protected abstract int getRealmModelViewType(VM model);
protected abstract
@LayoutRes @LayoutRes
int getLayout(int viewType); protected abstract int getLayout(int viewType);
protected abstract VH onCreateRealmModelViewHolder(int viewType, View itemView); protected abstract VH onCreateRealmModelViewHolder(int viewType, View itemView);
......
...@@ -69,47 +69,32 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe ...@@ -69,47 +69,32 @@ public class RealmMessageRepository extends RealmRepository implements MessageRe
return Single.just(false); return Single.just(false);
} }
// need to improve this for real RealmMessage realmMessage = realm.where(RealmMessage.class)
final JSONObject messageToSend = new JSONObject() .equalTo(RealmMessage.ID, message.getId())
.put(RealmMessage.ID, message.getId()) .findFirst();
.put(RealmMessage.SYNC_STATE, message.getSyncState())
.put(RealmMessage.TIMESTAMP, message.getTimestamp())
.put(RealmMessage.ROOM_ID, message.getRoomId())
.put(RealmMessage.USER, new JSONObject()
.put(RealmUser.ID, message.getUser().getId()))
.put(RealmMessage.MESSAGE, message.getMessage());
realm.beginTransaction();
return realm.createOrUpdateObjectFromJson(RealmMessage.class, messageToSend) if (realmMessage == null) {
.asObservable() realmMessage = new RealmMessage();
.unsubscribeOn(AndroidSchedulers.from(looper)) } else {
.doOnUnsubscribe(() -> close(realm, looper)) realmMessage = realm.copyFromRealm(realmMessage);
.filter(it -> it != null && it.isLoaded() && it.isValid())
.first()
.doOnNext(it -> realm.commitTransaction())
.toSingle()
.map(realmObject -> true);
});
} }
@Override realmMessage.setId(message.getId());
public Single<Boolean> resend(Message message) { realmMessage.setSyncState(message.getSyncState());
return Single.defer(() -> { realmMessage.setTimestamp(message.getTimestamp());
final Realm realm = RealmStore.getRealm(hostname); realmMessage.setRoomId(message.getRoomId());
final Looper looper = Looper.myLooper(); realmMessage.setMessage(message.getMessage());
if (realm == null) { RealmUser realmUser = realmMessage.getUser();
return Single.just(false); if (realmUser == null) {
realmUser = new RealmUser();
} }
final JSONObject messageToSend = new JSONObject() realmUser.setId(message.getUser().getId());
.put(RealmMessage.ID, message.getId())
.put(RealmMessage.SYNC_STATE, message.getSyncState());
realm.beginTransaction(); realm.beginTransaction();
return realm.createOrUpdateObjectFromJson(RealmMessage.class, messageToSend) return realm.copyToRealmOrUpdate(realmMessage)
.asObservable() .asObservable()
.unsubscribeOn(AndroidSchedulers.from(looper)) .unsubscribeOn(AndroidSchedulers.from(looper))
.doOnUnsubscribe(() -> close(realm, looper)) .doOnUnsubscribe(() -> close(realm, looper))
......
package chat.rocket.core.interactors;
import java.util.List;
import java.util.UUID;
import chat.rocket.core.SyncState;
import chat.rocket.core.models.Message;
import chat.rocket.core.models.Room;
import chat.rocket.core.models.RoomHistoryState;
import chat.rocket.core.models.User;
import chat.rocket.core.repositories.MessageRepository;
import chat.rocket.core.repositories.RoomRepository;
import rx.Observable;
import rx.Single;
public class MessageInteractor {
private final MessageRepository messageRepository;
private final RoomRepository roomRepository;
public MessageInteractor(MessageRepository messageRepository, RoomRepository roomRepository) {
this.messageRepository = messageRepository;
this.roomRepository = roomRepository;
}
public Single<Boolean> loadMessages(Room room) {
final RoomHistoryState roomHistoryState = RoomHistoryState.builder()
.setRoomId(room.getRoomId())
.setSyncState(SyncState.NOT_SYNCED)
.setCount(100)
.setReset(true)
.setComplete(false)
.setTimestamp(0)
.build();
return roomRepository.setHistoryState(roomHistoryState);
}
public Single<Boolean> loadMoreMessages(Room room) {
return roomRepository.getHistoryStateByRoomId(room.getRoomId())
.filter(roomHistoryState -> {
int syncState = roomHistoryState.getSyncState();
return !roomHistoryState.isComplete()
&& (syncState == SyncState.SYNCED || syncState == SyncState.FAILED);
})
.first()
.toSingle()
.flatMap(roomHistoryState -> roomRepository
.setHistoryState(roomHistoryState.withSyncState(SyncState.NOT_SYNCED)));
}
public Single<Boolean> send(Room destination, User sender, String messageText) {
final Message message = Message.builder()
.setId(UUID.randomUUID().toString())
.setSyncState(SyncState.NOT_SYNCED)
.setTimestamp(System.currentTimeMillis())
.setRoomId(destination.getRoomId())
.setMessage(messageText)
.setGroupable(false)
.setUser(sender)
.build();
return messageRepository.save(message);
}
public Single<Boolean> resend(Message message) {
return messageRepository.save(
message.withSyncState(SyncState.NOT_SYNCED));
}
public Single<Boolean> delete(Message message) {
return messageRepository.delete(message);
}
public Single<Integer> unreadCountFor(Room room, User user) {
return messageRepository.unreadCountFor(room, user);
}
public Observable<List<Message>> getAllFrom(Room room) {
return messageRepository.getAllFrom(room);
}
}
...@@ -13,8 +13,6 @@ public interface MessageRepository { ...@@ -13,8 +13,6 @@ public interface MessageRepository {
Single<Boolean> save(Message message); Single<Boolean> save(Message message);
Single<Boolean> resend(Message message);
Single<Boolean> delete(Message message); Single<Boolean> delete(Message message);
Observable<List<Message>> getAllFrom(Room room); Observable<List<Message>> getAllFrom(Room room);
......
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