2019-11-23 21:16:03 +01:00
|
|
|
package org.mercury_im.messenger.data.repository;
|
|
|
|
|
2020-06-06 18:45:20 +02:00
|
|
|
import org.mercury_im.messenger.core.data.repository.DirectChatRepository;
|
2020-06-09 20:49:13 +02:00
|
|
|
import org.mercury_im.messenger.core.util.Optional;
|
2019-12-01 19:56:13 +01:00
|
|
|
import org.mercury_im.messenger.data.mapping.DirectChatMapping;
|
|
|
|
import org.mercury_im.messenger.data.model.DirectChatModel;
|
2020-07-13 20:50:44 +02:00
|
|
|
import org.mercury_im.messenger.data.model.PeerModel;
|
2019-12-07 02:04:31 +01:00
|
|
|
import org.mercury_im.messenger.data.repository.dao.DirectChatDao;
|
2019-11-23 21:16:03 +01:00
|
|
|
import org.mercury_im.messenger.entity.chat.DirectChat;
|
|
|
|
import org.mercury_im.messenger.entity.contact.Peer;
|
|
|
|
|
2019-12-01 21:16:01 +01:00
|
|
|
import java.util.ArrayList;
|
2019-11-23 21:16:03 +01:00
|
|
|
import java.util.List;
|
2019-12-21 16:30:14 +01:00
|
|
|
import java.util.UUID;
|
2019-11-23 21:16:03 +01:00
|
|
|
|
2019-12-01 19:56:13 +01:00
|
|
|
import javax.inject.Inject;
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
import io.reactivex.Completable;
|
|
|
|
import io.reactivex.Maybe;
|
|
|
|
import io.reactivex.Observable;
|
|
|
|
import io.reactivex.Single;
|
|
|
|
import io.requery.Persistable;
|
2019-12-01 21:16:01 +01:00
|
|
|
import io.requery.query.ResultDelegate;
|
2019-11-23 21:16:03 +01:00
|
|
|
import io.requery.reactivex.ReactiveEntityStore;
|
|
|
|
|
2020-06-09 20:49:13 +02:00
|
|
|
public class RxDirectChatRepository
|
2019-11-23 21:16:03 +01:00
|
|
|
extends RequeryRepository
|
|
|
|
implements DirectChatRepository {
|
|
|
|
|
2019-12-06 20:25:33 +01:00
|
|
|
private final DirectChatMapping directChatMapping;
|
2019-11-23 21:16:03 +01:00
|
|
|
|
2019-12-07 02:04:31 +01:00
|
|
|
private final DirectChatDao dao;
|
|
|
|
|
2019-12-01 19:56:13 +01:00
|
|
|
@Inject
|
2020-06-09 20:49:13 +02:00
|
|
|
public RxDirectChatRepository(
|
2019-11-23 21:16:03 +01:00
|
|
|
ReactiveEntityStore<Persistable> data,
|
2019-12-06 20:25:33 +01:00
|
|
|
DirectChatMapping directChatMapping) {
|
2020-06-09 20:49:13 +02:00
|
|
|
super(data);
|
2019-12-06 20:25:33 +01:00
|
|
|
this.directChatMapping = directChatMapping;
|
2019-12-07 02:04:31 +01:00
|
|
|
this.dao = new DirectChatDao(data);
|
|
|
|
}
|
2019-12-01 19:56:13 +01:00
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
@Override
|
|
|
|
public Single<DirectChat> insertDirectChat(DirectChat chat) {
|
2019-12-07 02:04:31 +01:00
|
|
|
return Single.just(chat)
|
|
|
|
// map entity to model
|
|
|
|
.map(directChatMapping::toModel)
|
|
|
|
.flatMap(dao::insert)
|
|
|
|
// map back to entity
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(model -> directChatMapping.toEntity(model, chat));
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 16:30:14 +01:00
|
|
|
public Observable<Optional<DirectChat>> observeDirectChat(UUID chatId) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.get(chatId).observableResult()
|
2019-12-26 08:46:55 +01:00
|
|
|
.map(result -> new Optional<>(result.firstOrNull()))
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(directChatMapping::toEntity);
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-21 16:30:14 +01:00
|
|
|
public Maybe<DirectChat> getDirectChat(UUID chatId) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.get(chatId).maybe()
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(directChatMapping::toEntity);
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
2019-12-02 02:30:15 +01:00
|
|
|
@Override
|
2019-12-07 02:04:31 +01:00
|
|
|
public Single<DirectChat> getOrCreateChatWithPeer(Peer peer) {
|
|
|
|
return getDirectChatByPeer(peer)
|
2020-05-16 15:53:54 +02:00
|
|
|
.switchIfEmpty(Single.just(new DirectChat())
|
2019-12-21 16:30:14 +01:00
|
|
|
.map(chat -> {
|
|
|
|
chat.setAccount(peer.getAccount());
|
|
|
|
chat.setPeer(peer);
|
|
|
|
return chat;
|
|
|
|
})
|
2020-06-09 20:49:13 +02:00
|
|
|
.flatMap(this::insertDirectChat));
|
2019-12-02 02:30:15 +01:00
|
|
|
}
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
@Override
|
2019-12-01 21:16:01 +01:00
|
|
|
public Observable<Optional<DirectChat>> observeDirectChatByPeer(Peer peer) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.getByPeer(peer.getId()).observableResult()
|
2019-12-26 08:46:55 +01:00
|
|
|
.map(result -> new Optional<>(result.firstOrNull()))
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(directChatMapping::toEntity);
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-01 21:16:01 +01:00
|
|
|
public Maybe<DirectChat> getDirectChatByPeer(Peer peer) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.getByPeer(peer.getId()).maybe()
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(directChatMapping::toEntity);
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public Observable<List<DirectChat>> observeAllDirectChats() {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.getAll().observableResult()
|
2019-12-01 21:16:01 +01:00
|
|
|
.map(ResultDelegate::toList)
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(this::chatModelsToEntities);
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 03:18:14 +01:00
|
|
|
private List<DirectChat> chatModelsToEntities(List<DirectChatModel> models) {
|
|
|
|
List<DirectChat> entities = new ArrayList<>(models.size());
|
|
|
|
for (DirectChatModel model : models) {
|
|
|
|
entities.add(directChatMapping.toEntity(model));
|
|
|
|
}
|
|
|
|
return entities;
|
|
|
|
}
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
@Override
|
|
|
|
public Single<DirectChat> updateDirectChat(DirectChat chat) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.get(chat.getId()).maybe().toSingle()
|
2019-12-07 02:04:31 +01:00
|
|
|
.map(model -> directChatMapping.toModel(chat, model))
|
|
|
|
.flatMap(data()::update)
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(model -> directChatMapping.toEntity(model, chat));
|
2019-12-07 02:04:31 +01:00
|
|
|
}
|
|
|
|
|
2019-11-23 21:16:03 +01:00
|
|
|
@Override
|
|
|
|
public Single<DirectChat> upsertDirectChat(DirectChat chat) {
|
2019-12-08 18:27:34 +01:00
|
|
|
return dao.get(chat.getId()).maybe()
|
|
|
|
.switchIfEmpty(dao.insert(directChatMapping.toModel(chat)))
|
2019-12-07 02:04:31 +01:00
|
|
|
.map(directChatModel -> directChatMapping.toModel(chat, directChatModel))
|
|
|
|
.flatMap(data()::update)
|
2020-06-09 20:49:13 +02:00
|
|
|
.map(model -> directChatMapping.toEntity(model, chat));
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2019-12-22 03:18:14 +01:00
|
|
|
public Completable deleteDirectChat(UUID chatId) {
|
|
|
|
return dao.delete(chatId)
|
2020-06-09 20:49:13 +02:00
|
|
|
.ignoreElement();
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|
2020-07-13 20:50:44 +02:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public Observable<List<DirectChat>> findChatsByQuery(String query) {
|
|
|
|
return data().select(DirectChatModel.class)
|
|
|
|
.join(PeerModel.class)
|
|
|
|
.on(DirectChatModel.PEER_ID.eq(PeerModel.ID))
|
|
|
|
.where(PeerModel.NAME.like("%" + query + "%")
|
|
|
|
.or(PeerModel.ADDRESS.like("%" + query + "%")))
|
|
|
|
.get().observableResult()
|
|
|
|
.map(ResultDelegate::toList)
|
|
|
|
.map(this::chatModelsToEntities);
|
|
|
|
}
|
2019-11-23 21:16:03 +01:00
|
|
|
}
|