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