Search in sources :

Example 1 with ChatRoom

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

the class ChatRoomController method replaceIntoRooms.

/**
 * Saves the given chat rooms into database
 */
public void replaceIntoRooms(Collection<ChatRoom> rooms) {
    if (rooms == null || rooms.isEmpty()) {
        Utils.log("No rooms passed, can't insert anything.");
        return;
    }
    chatRoomDao.markAsNotJoined();
    Utils.log("reset join status of all rooms");
    for (ChatRoom room : rooms) {
        String roomName = room.getActualName();
        String semester = room.getSemester();
        List<Integer> roomIds = chatRoomDao.getGivenLecture(roomName, semester);
        if (roomIds.isEmpty()) {
            ChatRoomDbRow chatRoom = new ChatRoomDbRow(room.getId(), roomName, "", semester, 1, 0, "", room.getMembers(), -1);
            chatRoomDao.replaceRoom(chatRoom);
        } else {
            // in dao
            chatRoomDao.updateRoomToJoined(room.getId(), room.getMembers(), roomName, semester);
        /* TODO(jacqueline8711) load the last messages when joining the chat
                chatMessageViewModel.getNewMessages(room.getId(), verification,
                                                    context instanceof ChatMessageViewModel.DataLoadInterface ?
                                                    (ChatMessageViewModel.DataLoadInterface)context : null);
                Utils.log("Loading some messages for a newly joined chatroom");*/
        }
    }
}
Also used : ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomDbRow(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomDbRow)

Example 2 with ChatRoom

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

the class ChatActivity method onNewIntent.

/**
 * User pressed on the notification and wants to view the room with the new messages
 *
 * @param intent Intent
 */
@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    // Try to get the room from the extras
    final ChatRoom room = new Gson().fromJson(intent.getExtras().getString(Const.CURRENT_CHAT_ROOM), ChatRoom.class);
    // Check, maybe it wasn't there
    if (room != null && room.getId() != currentChatRoom.getId()) {
        // If currently in a room which does not match the one from the notification --> Switch
        currentChatRoom = room;
        if (getSupportActionBar() != null) {
            getSupportActionBar().setSubtitle(currentChatRoom.getName().substring(4));
        }
        chatHistoryAdapter = null;
        getNextHistoryFromServer(true);
    }
}
Also used : ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) Gson(com.google.gson.Gson)

Example 3 with ChatRoom

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

the class ChatRoomController method onRequestCard.

@Override
public void onRequestCard(Context context) {
    // Get all of the users lectures and save them as possible chat rooms
    TUMOnlineRequest<LecturesSearchRowSet> requestHandler = new TUMOnlineRequest<>(TUMOnlineConst.Companion.getLECTURES_PERSONAL(), context, true);
    Optional<LecturesSearchRowSet> lecturesList = requestHandler.fetch();
    if (lecturesList.isPresent()) {
        List<LecturesSearchRow> lectures = lecturesList.get().getLehrveranstaltungen();
        this.createLectureRooms(lectures);
    }
    // Join all new chat rooms
    if (Utils.getSettingBool(context, Const.AUTO_JOIN_NEW_ROOMS, false)) {
        List<String> newRooms = this.getNewUnjoined();
        ChatMember currentChatMember = Utils.getSetting(context, Const.CHAT_MEMBER, ChatMember.class);
        for (String roomId : newRooms) {
            // Join chat room
            try {
                ChatRoom currentChatRoom = new ChatRoom(roomId);
                currentChatRoom = TUMCabeClient.getInstance(context).createRoom(currentChatRoom, ChatVerification.Companion.getChatVerification(context, currentChatMember));
                if (currentChatRoom != null) {
                    this.join(currentChatRoom);
                }
            } catch (IOException e) {
                Utils.log(e, " - error occured while creating the room!");
            } catch (NoPrivateKey noPrivateKey) {
                return;
            }
        }
    }
    // Get all rooms that have unread messages
    List<ChatRoomDbRow> rooms = chatRoomDao.getUnreadRooms();
    if (!rooms.isEmpty()) {
        for (ChatRoomDbRow room : rooms) {
            ChatMessagesCard card = new ChatMessagesCard(context, room);
            card.apply();
        }
    }
}
Also used : ChatMember(de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember) TUMOnlineRequest(de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest) LecturesSearchRowSet(de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRowSet) LecturesSearchRow(de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRow) IOException(java.io.IOException) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomDbRow(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomDbRow)

Example 4 with ChatRoom

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

the class ChatActivity method onClick.

/**
 * When user confirms the leave dialog send the request to the server.
 *
 * @param dialog Dialog handle
 * @param which  The users choice (ignored because this is only called when the user confirms)
 */
@Override
public void onClick(DialogInterface dialog, int which) {
    // Send request to the server to remove the user from this room
    ChatVerification verification;
    try {
        verification = ChatVerification.Companion.getChatVerification(this, currentChatMember);
    } catch (NoPrivateKey noPrivateKey) {
        return;
    }
    TUMCabeClient.getInstance(this).leaveChatRoom(currentChatRoom, verification, new Callback<ChatRoom>() {

        @Override
        public void onResponse(Call<ChatRoom> call, Response<ChatRoom> room) {
            Utils.logv("Success leaving chat room: " + room.body().getName());
            new ChatRoomController(ChatActivity.this).leave(currentChatRoom);
            // Move back to ChatRoomsActivity
            Intent intent = new Intent(ChatActivity.this, ChatRoomsActivity.class);
            startActivity(intent);
        }

        @Override
        public void onFailure(Call<ChatRoom> call, Throwable t) {
            Utils.log(t, "Failure leaving chat room");
        }
    });
}
Also used : NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification) Intent(android.content.Intent) ChatRoomController(de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)

Example 5 with ChatRoom

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

the class ChatRoomsActivity method createOrJoinChatRoom.

/**
 * Creates a given chat room if it does not exist and joins it
 * Works asynchronously.
 */
private void createOrJoinChatRoom(String name) {
    if (this.currentChatMember == null) {
        Utils.showToast(this, getString(R.string.chat_not_setup));
        return;
    }
    Utils.logv("create or join chat room " + name);
    currentChatRoom = new ChatRoom(name);
    ChatVerification verification;
    try {
        verification = ChatVerification.Companion.getChatVerification(this, this.currentChatMember);
    } catch (NoPrivateKey noPrivateKey) {
        this.finish();
        return;
    }
    Callback callback = new Callback<ChatRoom>() {

        @Override
        public void onResponse(@NonNull Call<ChatRoom> call, @NonNull Response<ChatRoom> response) {
            if (!response.isSuccessful()) {
                Utils.logv("Error creating&joining chat room: " + response.message());
                return;
            }
            // The POST request is successful: go to room. API should have auto joined it
            Utils.logv("Success creating&joining chat room: " + response.body());
            currentChatRoom = response.body();
            manager.join(currentChatRoom);
            // When we show joined chat rooms open chat room directly
            if (mCurrentMode == 1) {
                moveToChatActivity();
            } else {
                // Otherwise show a nice information, that we added the room
                final List<ChatRoomAndLastMessage> rooms = manager.getAllByStatus(mCurrentMode);
                runOnUiThread(() -> {
                    chatRoomAdapter.updateRooms(rooms);
                    Utils.showToast(ChatRoomsActivity.this, R.string.joined_chat_room);
                });
            }
        }

        @Override
        public void onFailure(@NonNull Call<ChatRoom> call, @NonNull Throwable t) {
            Utils.log(t, "Failure creating/joining chat room - trying to GET it from the server");
            Utils.showToastOnUIThread(ChatRoomsActivity.this, R.string.activate_key);
        }
    };
    TUMCabeClient.getInstance(this).createRoom(currentChatRoom, verification, callback);
}
Also used : Response(retrofit2.Response) Call(retrofit2.Call) Callback(retrofit2.Callback) NoPrivateKey(de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey) ChatRoom(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom) ChatRoomAndLastMessage(de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage) NonNull(android.support.annotation.NonNull) ChatVerification(de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)

Aggregations

ChatRoom (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoom)6 NoPrivateKey (de.tum.in.tumcampusapp.api.app.exception.NoPrivateKey)4 ChatRoomController (de.tum.in.tumcampusapp.component.ui.chat.ChatRoomController)2 ChatMember (de.tum.in.tumcampusapp.component.ui.chat.model.ChatMember)2 ChatRoomDbRow (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomDbRow)2 ChatVerification (de.tum.in.tumcampusapp.component.ui.chat.model.ChatVerification)2 IOException (java.io.IOException)2 Intent (android.content.Intent)1 NonNull (android.support.annotation.NonNull)1 Gson (com.google.gson.Gson)1 AuthenticationManager (de.tum.in.tumcampusapp.api.app.AuthenticationManager)1 TUMOnlineRequest (de.tum.in.tumcampusapp.api.tumonline.TUMOnlineRequest)1 LecturesSearchRow (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRow)1 LecturesSearchRowSet (de.tum.in.tumcampusapp.component.tumui.lectures.model.LecturesSearchRowSet)1 ChatRoomAndLastMessage (de.tum.in.tumcampusapp.component.ui.chat.model.ChatRoomAndLastMessage)1 Call (retrofit2.Call)1 Callback (retrofit2.Callback)1 Response (retrofit2.Response)1