121 lines
3.7 KiB
Java
121 lines
3.7 KiB
Java
package org.mercury_im.messenger.persistence.room.repository;
|
|
|
|
import android.util.Log;
|
|
|
|
import org.jxmpp.jid.EntityBareJid;
|
|
import org.mercury_im.messenger.persistence.model.EntityModel;
|
|
import org.mercury_im.messenger.persistence.pojo.Chat;
|
|
import org.mercury_im.messenger.persistence.repository.ChatRepository;
|
|
import org.mercury_im.messenger.persistence.repository.RosterRepository;
|
|
import org.mercury_im.messenger.persistence.room.dao.ChatDao;
|
|
import org.mercury_im.messenger.persistence.room.model.RoomChatModel;
|
|
|
|
import java.util.List;
|
|
|
|
import javax.inject.Inject;
|
|
|
|
import io.reactivex.Completable;
|
|
import io.reactivex.Maybe;
|
|
import io.reactivex.Observable;
|
|
import io.reactivex.Single;
|
|
|
|
import static org.mercury_im.messenger.persistence.room.AppDatabase.TAG;
|
|
|
|
public class IChatRepository implements ChatRepository<RoomChatModel> {
|
|
|
|
private final ChatDao chatDao;
|
|
private final RosterRepository rosterRepository;
|
|
|
|
@Inject
|
|
public IChatRepository(ChatDao chatDao, RosterRepository rosterRepository) {
|
|
this.chatDao = chatDao;
|
|
this.rosterRepository = rosterRepository;
|
|
}
|
|
|
|
@Override
|
|
public RoomChatModel newChatModel() {
|
|
return new RoomChatModel();
|
|
}
|
|
|
|
@Override
|
|
public Observable<RoomChatModel> getOrCreateChatWith(long accountId, EntityBareJid jid) {
|
|
return Observable.fromCallable(() -> {
|
|
EntityModel entity = (EntityModel) rosterRepository.getOrCreateEntityForAccountAndJid(accountId, jid).blockingGet();
|
|
RoomChatModel chat = maybeGetChatWithEntity(entity).blockingGet();
|
|
if (chat == null) {
|
|
chat = newChatModel();
|
|
chat.setPeerEntityId(entity.getId());
|
|
chat.setId(insertChat(chat).blockingGet());
|
|
}
|
|
return chat;
|
|
}).concatWith(getChatWith(accountId, jid).skip(1));
|
|
}
|
|
|
|
@Override
|
|
public Observable<RoomChatModel> getChatWith(long accountId, EntityBareJid jid) {
|
|
return chatDao.getChatWithJid(accountId, jid);
|
|
}
|
|
|
|
@Override
|
|
public Observable<RoomChatModel> getChatWithEntity(long entityId) {
|
|
return chatDao.getChatWithEntity(entityId);
|
|
}
|
|
|
|
@Override
|
|
public Completable updateChat(RoomChatModel chat) {
|
|
return chatDao.update(chat);
|
|
}
|
|
|
|
@Override
|
|
public Observable<RoomChatModel> getChatWithContact(long contactId) {
|
|
return chatDao.getChatWithContact(contactId);
|
|
}
|
|
|
|
@Override
|
|
public Observable<List<RoomChatModel>> getAllChats() {
|
|
return chatDao.getAllChats();
|
|
}
|
|
|
|
@Override
|
|
public Observable<List<RoomChatModel>> getAllChatsOfAccount(long accountId) {
|
|
return chatDao.getAllChatsOfAccount(accountId);
|
|
}
|
|
|
|
@Override
|
|
public Maybe<RoomChatModel> maybeGetChatWith(long accountId, EntityBareJid jid) {
|
|
return chatDao.maybeGetChatWithJid(accountId, jid);
|
|
}
|
|
|
|
@Override
|
|
public Maybe<RoomChatModel> maybeGetChatWithEntity(long entityId) {
|
|
return chatDao.maybeGetChatWithEntity(entityId);
|
|
}
|
|
|
|
@Override
|
|
public Maybe<RoomChatModel> maybeGetChatWithContact(long contactId) {
|
|
return chatDao.maybeGetChatWithContact(contactId);
|
|
}
|
|
|
|
@Override
|
|
public Single<Long> insertChat(RoomChatModel chat) {
|
|
return chatDao.insert(chat)
|
|
.map(chatId -> {
|
|
chat.setId(chatId);
|
|
return chatId;
|
|
})
|
|
.doOnSubscribe(ignore -> Log.v(TAG, "Insert " + chat))
|
|
.doAfterSuccess(chatId -> Log.v(TAG, "Assign ID " + chatId + " to " + chat));
|
|
}
|
|
|
|
@Override
|
|
public Completable closeChat(RoomChatModel chat) {
|
|
return chatDao.delete(chat);
|
|
}
|
|
|
|
@Override
|
|
public Observable<List<Chat>> getDisplayableChats() {
|
|
return chatDao.getChatPojos();
|
|
}
|
|
|
|
}
|