Search in sources :

Example 1 with ChatMessageRemoteRepository

use of de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository in project TumCampusApp by TCA-Team.

the class ChatNotification method getNewMessages.

private void getNewMessages(ChatRoom chatRoom, ChatMember member, int messageId) throws NoPrivateKey, IOException {
    ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
    localRepository.setDb(TcaDb.getInstance(context));
    ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
    remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(context));
    ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, new CompositeDisposable());
    if (messageId == -1) {
        chatMessageViewModel.getNewMessages(chatRoom.getId(), ChatVerification.Companion.getChatVerification(context, member), this);
    } else {
        // edit
        chatMessageViewModel.getOlderMessages(chatRoom.getId(), messageId, ChatVerification.Companion.getChatVerification(context, member), null);
    }
}
Also used : ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository) CompositeDisposable(io.reactivex.disposables.CompositeDisposable)

Example 2 with ChatMessageRemoteRepository

use of de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository in project TumCampusApp by TCA-Team.

the class ChatActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Toolbar toolbar = findViewById(R.id.main_toolbar);
    setSupportActionBar(toolbar);
    this.getIntentData();
    TcaDb tcaDb = TcaDb.getInstance(this);
    ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
    remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
    ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
    localRepository.setDb(tcaDb);
    chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
    this.bindUIElements();
}
Also used : ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessageViewModel(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository) Toolbar(android.support.v7.widget.Toolbar)

Example 3 with ChatMessageRemoteRepository

use of de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository in project TumCampusApp by TCA-Team.

the class SendMessageService method onHandleWork.

@Override
protected void onHandleWork(@NonNull Intent intent) {
    TcaDb tcaDb = TcaDb.getInstance(this);
    final CompositeDisposable mDisposable = new CompositeDisposable();
    ChatMessageRemoteRepository remoteRepository = ChatMessageRemoteRepository.INSTANCE;
    remoteRepository.setTumCabeClient(TUMCabeClient.getInstance(this));
    ChatMessageLocalRepository localRepository = ChatMessageLocalRepository.INSTANCE;
    localRepository.setDb(tcaDb);
    ChatMessageViewModel chatMessageViewModel = new ChatMessageViewModel(localRepository, remoteRepository, mDisposable);
    chatMessageViewModel.deleteOldEntries();
    // Get all unsent messages from database
    List<ChatMessage> unsentMsg = chatMessageViewModel.getUnsent();
    if (unsentMsg.isEmpty()) {
        return;
    }
    int numberOfAttempts = 0;
    AuthenticationManager am = new AuthenticationManager(this);
    // Try to send the message 5 times
    while (numberOfAttempts < MAX_SEND_TRIES) {
        try {
            for (ChatMessage message : unsentMsg) {
                // Generate signature and store it in the message
                message.setSignature(am.sign(message.getText()));
                // Send the message to the server
                chatMessageViewModel.sendMessage(message.getRoom(), message, this.getApplicationContext());
                Utils.logv("successfully sent message: " + message.getText());
            }
            // Exit the loop
            return;
        } catch (NoPrivateKey noPrivateKey) {
            // Nothing can be done, just exit
            return;
        } catch (Exception e) {
            Utils.log(e);
            numberOfAttempts++;
        }
        // Sleep for five seconds, maybe the server is currently really busy
        try {
            Thread.sleep(5000);
        } catch (InterruptedException e) {
            Utils.log(e);
        }
    }
}
Also used : AuthenticationManager(de.tum.in.tumcampusapp.api.app.AuthenticationManager) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatMessageRemoteRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository) ChatMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage) ChatMessageViewModel(de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel) TcaDb(de.tum.in.tumcampusapp.database.TcaDb) CompositeDisposable(io.reactivex.disposables.CompositeDisposable) ChatMessageLocalRepository(de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)

Aggregations

ChatMessageLocalRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageLocalRepository)3 ChatMessageRemoteRepository (de.tum.in.tumcampusapp.component.ui.chat.repository.ChatMessageRemoteRepository)3 ChatMessageViewModel (de.tum.in.tumcampusapp.component.ui.chat.ChatMessageViewModel)2 TcaDb (de.tum.in.tumcampusapp.database.TcaDb)2 CompositeDisposable (io.reactivex.disposables.CompositeDisposable)2 Toolbar (android.support.v7.widget.Toolbar)1 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)1 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)1 ChatMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMessage)1