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");*/
}
}
}
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);
}
}
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();
}
}
}
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");
}
});
}
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);
}
Aggregations