Search in sources :

Example 1 with Message

use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.

the class MessageOptionsDialogFragment method setUpDialog.

private void setUpDialog(final BottomSheetDialog bottomSheetDialog, String messageId) {
    RocketChatCache cache = new RocketChatCache(bottomSheetDialog.getContext());
    String hostname = cache.getSelectedServerHostname();
    EditMessageInteractor editMessageInteractor = getEditMessageInteractor(hostname);
    MessageRepository messageRepository = new RealmMessageRepository(hostname);
    Disposable disposable = messageRepository.getById(messageId).flatMap(it -> {
        if (!it.isPresent()) {
            return Single.just(Pair.<Message, Boolean>create(null, false));
        }
        Message message = it.get();
        return Single.zip(Single.just(message), editMessageInteractor.isAllowed(message), Pair::create);
    }).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(pair -> {
        if (pair.second) {
            bottomSheetDialog.findViewById(R.id.message_options_info).setVisibility(View.GONE);
            View editView = bottomSheetDialog.findViewById(R.id.message_options_edit_action);
            editView.setVisibility(View.VISIBLE);
            editView.setOnClickListener(view -> internalListener.onEdit(pair.first));
        } else {
            ((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_permissions_info);
        }
    }, throwable -> {
        ((TextView) bottomSheetDialog.findViewById(R.id.message_options_info)).setText(R.string.message_options_no_message_info);
        Logger.report(throwable);
    });
    compositeDisposable.add(disposable);
}
Also used : CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) MessageRepository(chat.rocket.core.repositories.MessageRepository) BottomSheetDialog(android.support.design.widget.BottomSheetDialog) RealmPublicSettingRepository(chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository) Bundle(android.os.Bundle) UserRepository(chat.rocket.core.repositories.UserRepository) RocketChatCache(chat.rocket.android.RocketChatCache) RoomRepository(chat.rocket.core.repositories.RoomRepository) Dialog(android.app.Dialog) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmRoomRoleRepository(chat.rocket.persistence.realm.repositories.RealmRoomRoleRepository) PermissionRepository(chat.rocket.core.repositories.PermissionRepository) PublicSettingRepository(chat.rocket.core.repositories.PublicSettingRepository) PermissionInteractor(chat.rocket.core.interactors.PermissionInteractor) View(android.view.View) RoomRoleRepository(chat.rocket.core.repositories.RoomRoleRepository) R(chat.rocket.android.R) RealmRoomRepository(chat.rocket.persistence.realm.repositories.RealmRoomRepository) BottomSheetDialogFragment(android.support.design.widget.BottomSheetDialogFragment) DialogInterface(android.content.DialogInterface) Logger(chat.rocket.android.helper.Logger) EditMessageInteractor(chat.rocket.core.interactors.EditMessageInteractor) Message(chat.rocket.core.models.Message) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) Disposable(io.reactivex.disposables.Disposable) TextView(android.widget.TextView) Pair(android.support.v4.util.Pair) RealmUserRepository(chat.rocket.persistence.realm.repositories.RealmUserRepository) BackgroundLooper(chat.rocket.android.BackgroundLooper) RealmPermissionRepository(chat.rocket.persistence.realm.repositories.RealmPermissionRepository) Message(chat.rocket.core.models.Message) EditMessageInteractor(chat.rocket.core.interactors.EditMessageInteractor) RocketChatCache(chat.rocket.android.RocketChatCache) MessageRepository(chat.rocket.core.repositories.MessageRepository) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) RealmMessageRepository(chat.rocket.persistence.realm.repositories.RealmMessageRepository) TextView(android.widget.TextView) View(android.view.View) TextView(android.widget.TextView)

Example 2 with Message

use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.

the class RealmMessageRepository method delete.

@Override
public Single<Boolean> delete(Message message) {
    return Single.defer(() -> {
        final Realm realm = RealmStore.getRealm(hostname);
        final Looper looper = Looper.myLooper();
        if (realm == null || looper == null) {
            return Single.just(false);
        }
        realm.beginTransaction();
        return RxJavaInterop.toV2Flowable(realm.where(RealmMessage.class).equalTo(RealmMessage.ID, message.getId()).findAll().<RealmResults<RealmMessage>>asObservable()).filter(realmObject -> realmObject.isLoaded() && realmObject.isValid()).firstElement().toSingle().flatMap(realmMessages -> Single.just(realmMessages.deleteAllFromRealm())).doOnEvent((success, throwable) -> {
            if (success) {
                realm.commitTransaction();
            } else {
                realm.cancelTransaction();
            }
            close(realm, looper);
        });
    });
}
Also used : User(chat.rocket.core.models.User) MessageRepository(chat.rocket.core.repositories.MessageRepository) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) ArrayList(java.util.ArrayList) Message(chat.rocket.core.models.Message) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) Realm(io.realm.Realm) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) RealmResults(io.realm.RealmResults)

Example 3 with Message

use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.

the class RealmMessageRepository method save.

@Override
public Single<Boolean> save(Message message) {
    return Single.defer(() -> {
        final Realm realm = RealmStore.getRealm(hostname);
        final Looper looper = Looper.myLooper();
        if (realm == null || looper == null) {
            return Single.just(false);
        }
        RealmMessage realmMessage = realm.where(RealmMessage.class).equalTo(RealmMessage.ID, message.getId()).findFirst();
        if (realmMessage == null) {
            realmMessage = new RealmMessage();
        } else {
            realmMessage = realm.copyFromRealm(realmMessage);
        }
        realmMessage.setId(message.getId());
        realmMessage.setSyncState(message.getSyncState());
        realmMessage.setTimestamp(message.getTimestamp());
        realmMessage.setRoomId(message.getRoomId());
        realmMessage.setMessage(message.getMessage());
        realmMessage.setEditedAt(message.getEditedAt());
        RealmUser realmUser = realmMessage.getUser();
        if (realmUser == null) {
            realmUser = realm.where(RealmUser.class).equalTo(RealmUser.ID, message.getUser().getId()).findFirst();
        }
        realmMessage.setUser(realmUser);
        realm.beginTransaction();
        return RxJavaInterop.toV2Flowable(realm.copyToRealmOrUpdate(realmMessage).asObservable()).filter(it -> it.isLoaded() && it.isValid()).firstElement().doOnSuccess(it -> realm.commitTransaction()).doOnError(throwable -> realm.cancelTransaction()).doOnEvent((realmObject, throwable) -> close(realm, looper)).toSingle().map(realmObject -> true);
    });
}
Also used : User(chat.rocket.core.models.User) MessageRepository(chat.rocket.core.repositories.MessageRepository) Realm(io.realm.Realm) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Optional(com.fernandocejas.arrow.optional.Optional) RealmResults(io.realm.RealmResults) RxJavaInterop(hu.akarnokd.rxjava.interop.RxJavaInterop) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage) ArrayList(java.util.ArrayList) Message(chat.rocket.core.models.Message) List(java.util.List) Flowable(io.reactivex.Flowable) Pair(android.support.v4.util.Pair) Sort(io.realm.Sort) Room(chat.rocket.core.models.Room) Looper(android.os.Looper) RealmStore(chat.rocket.persistence.realm.RealmStore) Looper(android.os.Looper) RealmUser(chat.rocket.persistence.realm.models.ddp.RealmUser) Realm(io.realm.Realm) RealmMessage(chat.rocket.persistence.realm.models.ddp.RealmMessage)

Example 4 with Message

use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.

the class RoomPresenter method resendMessage.

@Override
public void resendMessage(Message message) {
    final Disposable subscription = getCurrentUser().flatMap(user -> messageInteractor.resend(message, user)).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe();
    addSubscription(subscription);
}
Also used : Disposable(io.reactivex.disposables.Disposable) User(chat.rocket.core.models.User) Settings(chat.rocket.core.models.Settings) Optional(com.fernandocejas.arrow.optional.Optional) UserRepository(chat.rocket.core.repositories.UserRepository) RoomRepository(chat.rocket.core.repositories.RoomRepository) Logger(chat.rocket.android.helper.Logger) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) LogIfError(chat.rocket.android.helper.LogIfError) MessageInteractor(chat.rocket.core.interactors.MessageInteractor) Message(chat.rocket.core.models.Message) MethodCallHelper(chat.rocket.android.api.MethodCallHelper) Disposable(io.reactivex.disposables.Disposable) ConnectivityManagerApi(chat.rocket.android.service.ConnectivityManagerApi) Pair(android.support.v4.util.Pair) Room(chat.rocket.core.models.Room) SyncState(chat.rocket.core.SyncState) Nullable(android.support.annotation.Nullable) BackgroundLooper(chat.rocket.android.BackgroundLooper) BasePresenter(chat.rocket.android.shared.BasePresenter) AbsoluteUrlHelper(chat.rocket.android.helper.AbsoluteUrlHelper)

Example 5 with Message

use of chat.rocket.core.models.Message in project Rocket.Chat.Android by RocketChat.

the class RoomPresenter method updateMessage.

@Override
public void updateMessage(Message message, String content) {
    final Disposable subscription = getCurrentUser().flatMap(user -> messageInteractor.update(message, user, content)).subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())).observeOn(AndroidSchedulers.mainThread()).subscribe(success -> {
        if (success) {
            view.onMessageSendSuccessfully();
        }
    }, Logger::report);
    addSubscription(subscription);
}
Also used : Disposable(io.reactivex.disposables.Disposable) User(chat.rocket.core.models.User) Settings(chat.rocket.core.models.Settings) Optional(com.fernandocejas.arrow.optional.Optional) UserRepository(chat.rocket.core.repositories.UserRepository) RoomRepository(chat.rocket.core.repositories.RoomRepository) Logger(chat.rocket.android.helper.Logger) NonNull(android.support.annotation.NonNull) Single(io.reactivex.Single) AndroidSchedulers(io.reactivex.android.schedulers.AndroidSchedulers) LogIfError(chat.rocket.android.helper.LogIfError) MessageInteractor(chat.rocket.core.interactors.MessageInteractor) Message(chat.rocket.core.models.Message) MethodCallHelper(chat.rocket.android.api.MethodCallHelper) Disposable(io.reactivex.disposables.Disposable) ConnectivityManagerApi(chat.rocket.android.service.ConnectivityManagerApi) Pair(android.support.v4.util.Pair) Room(chat.rocket.core.models.Room) SyncState(chat.rocket.core.SyncState) Nullable(android.support.annotation.Nullable) BackgroundLooper(chat.rocket.android.BackgroundLooper) BasePresenter(chat.rocket.android.shared.BasePresenter) AbsoluteUrlHelper(chat.rocket.android.helper.AbsoluteUrlHelper) Logger(chat.rocket.android.helper.Logger)

Aggregations

Pair (android.support.v4.util.Pair)5 Message (chat.rocket.core.models.Message)5 Single (io.reactivex.Single)5 AndroidSchedulers (io.reactivex.android.schedulers.AndroidSchedulers)5 Room (chat.rocket.core.models.Room)4 User (chat.rocket.core.models.User)4 Optional (com.fernandocejas.arrow.optional.Optional)4 NonNull (android.support.annotation.NonNull)3 BackgroundLooper (chat.rocket.android.BackgroundLooper)3 Logger (chat.rocket.android.helper.Logger)3 MessageRepository (chat.rocket.core.repositories.MessageRepository)3 RoomRepository (chat.rocket.core.repositories.RoomRepository)3 UserRepository (chat.rocket.core.repositories.UserRepository)3 Looper (android.os.Looper)2 Nullable (android.support.annotation.Nullable)2 MethodCallHelper (chat.rocket.android.api.MethodCallHelper)2 AbsoluteUrlHelper (chat.rocket.android.helper.AbsoluteUrlHelper)2 LogIfError (chat.rocket.android.helper.LogIfError)2 ConnectivityManagerApi (chat.rocket.android.service.ConnectivityManagerApi)2 BasePresenter (chat.rocket.android.shared.BasePresenter)2