Prepare chat controller for menu operations

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2018-05-16 10:30:05 +02:00
parent 6c5860ac97
commit 919e349ac9
4 changed files with 129 additions and 45 deletions

View File

@ -92,6 +92,7 @@ import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.TimeUnit;
import javax.inject.Inject;
@ -125,15 +126,16 @@ public class ChatController extends BaseController implements MessagesListAdapte
private List<Disposable> disposableList = new ArrayList<>();
private String conversationName;
private String roomToken;
private UserEntity currentUser;
private UserEntity conversationUser;
private String roomPassword;
private String credentials;
private String baseUrl;
private Call currentCall;
private boolean inChat = false;
private boolean historyRead = false;
private int globalLastKnownFutureMessageId = -1;
private int globalLastKnownPastMessageId = -1;
private MessagesListAdapter<ChatMessage> adapter;
private Menu globalMenu;
private Autocomplete mentionAutocomplete;
private LinearLayoutManager layoutManager;
@ -149,9 +151,16 @@ public class ChatController extends BaseController implements MessagesListAdapte
super(args);
setHasOptionsMenu(true);
this.conversationName = args.getString(BundleKeys.KEY_CONVERSATION_NAME);
this.currentUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
if (args.containsKey(BundleKeys.KEY_USER_ENTITY)) {
this.conversationUser = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_USER_ENTITY));
}
this.roomToken = args.getString(BundleKeys.KEY_ROOM_TOKEN);
this.roomPassword = args.getString(BundleKeys.KEY_ROOM_PASSWORD, "");
if (args.containsKey(BundleKeys.KEY_ACTIVE_CONVERSATION)) {
this.currentCall = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ACTIVE_CONVERSATION));
}
this.baseUrl = args.getString(BundleKeys.KEY_MODIFIED_BASE_URL, "");
}
@Override
@ -182,7 +191,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
holdersConfig.setOutcoming(MagicOutcomingTextMessageViewHolder.class,
R.layout.item_custom_outcoming_text_message);
adapter = new MessagesListAdapter<>(currentUser.getUserId(), holdersConfig, new ImageLoader() {
adapter = new MessagesListAdapter<>(conversationUser.getUserId(), holdersConfig, new ImageLoader() {
@Override
public void loadImage(ImageView imageView, String url) {
GlideApp.with(NextcloudTalkApplication.getSharedApplication().getApplicationContext())
@ -241,8 +250,40 @@ public class ChatController extends BaseController implements MessagesListAdapte
});
if (adapterWasNull) {
UserEntity currentUser = userUtils.getCurrentUser();
if (conversationUser != null && !currentUser.equals(conversationUser)) {
userUtils.createOrUpdateUser(null,
null, null, null,
null, true, null, currentUser.getId(), null, null)
.subscribe(new Observer<UserEntity>() {
@Override
public void onSubscribe(Disposable d) {
}
@Override
public void onNext(UserEntity userEntity) {
joinRoomWithPassword();
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
} else {
if (conversationUser == null) {
conversationUser = new UserEntity();
conversationUser.setDisplayName(currentUser.getDisplayName());
}
joinRoomWithPassword();
}
}
}
private void setupMentionAutocomplete() {
@ -317,8 +358,18 @@ public class ChatController extends BaseController implements MessagesListAdapte
password = roomPassword;
}
ncApi.joinRoom(ApiUtils.getCredentials(currentUser.getUserId(), currentUser.getToken()), ApiUtils
.getUrlForRoomParticipants(currentUser.getBaseUrl(), roomToken), password)
if (TextUtils.isEmpty(baseUrl)) {
baseUrl = conversationUser.getBaseUrl();
}
if (TextUtils.isEmpty(conversationUser.getUserId())) {
credentials = null;
} else {
credentials = ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken());
}
if (currentCall == null) {
ncApi.joinRoom(credentials, ApiUtils.getUrlForRoomParticipants(baseUrl, roomToken), password)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.retry(3)
@ -331,6 +382,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
@Override
public void onNext(CallOverall callOverall) {
inChat = true;
startPing();
pullChatMessages(0);
currentCall = callOverall.getOcs().getData();
}
@ -345,16 +397,21 @@ public class ChatController extends BaseController implements MessagesListAdapte
}
});
} else {
inChat = true;
startPing();
pullChatMessages(0);
}
}
private void sendMessage(String message) {
Map<String, String> fieldMap = new HashMap<>();
fieldMap.put("message", message);
fieldMap.put("actorDisplayName", currentUser.getDisplayName());
fieldMap.put("actorDisplayName", conversationUser.getDisplayName());
ncApi.sendChatMessage(ApiUtils.getCredentials(currentUser.getUserId(), currentUser.getToken()),
ApiUtils.getUrlForChat(currentUser.getBaseUrl(), roomToken), fieldMap)
ncApi.sendChatMessage(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.retry(3, observable -> inChat)
@ -385,6 +442,35 @@ public class ChatController extends BaseController implements MessagesListAdapte
});
}
private void startPing() {
ncApi.pingCall(credentials, ApiUtils.getUrlForCallPing(baseUrl, roomToken))
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.repeatWhen(observable -> observable.delay(5000, TimeUnit.MILLISECONDS))
.takeWhile(observable -> inChat)
.retry(3, observable -> inChat)
.subscribe(new Observer<GenericOverall>() {
@Override
public void onSubscribe(Disposable d) {
disposableList.add(d);
}
@Override
public void onNext(GenericOverall genericOverall) {
}
@Override
public void onError(Throwable e) {
}
@Override
public void onComplete() {
}
});
}
private void pullChatMessages(int lookIntoFuture) {
if (!lookingIntoFuture && lookIntoFuture == 1) {
lookingIntoFuture = true;
@ -406,8 +492,8 @@ public class ChatController extends BaseController implements MessagesListAdapte
}
if (lookIntoFuture == 1) {
ncApi.pullChatMessages(ApiUtils.getCredentials(currentUser.getUserId(), currentUser.getToken()),
ApiUtils.getUrlForChat(currentUser.getBaseUrl(), roomToken), fieldMap)
ncApi.pullChatMessages(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.takeWhile(observable -> inChat)
@ -435,8 +521,8 @@ public class ChatController extends BaseController implements MessagesListAdapte
});
} else {
ncApi.pullChatMessages(ApiUtils.getCredentials(currentUser.getUserId(), currentUser.getToken()),
ApiUtils.getUrlForChat(currentUser.getBaseUrl(), roomToken), fieldMap)
ncApi.pullChatMessages(ApiUtils.getCredentials(conversationUser.getUserId(), conversationUser.getToken()),
ApiUtils.getUrlForChat(conversationUser.getBaseUrl(), roomToken), fieldMap)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.retry(3, observable -> inChat)
@ -471,7 +557,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
if (!isFromTheFuture) {
for (int i = 0; i < chatMessageList.size(); i++) {
chatMessageList.get(i).setBaseUrl(currentUser.getBaseUrl());
chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
if (globalLastKnownPastMessageId == -1 || chatMessageList.get(i).getJsonMessageId() <
globalLastKnownPastMessageId) {
globalLastKnownPastMessageId = chatMessageList.get(i).getJsonMessageId();
@ -488,7 +574,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
} else {
for (int i = 0; i < chatMessageList.size(); i++) {
chatMessageList.get(i).setBaseUrl(currentUser.getBaseUrl());
chatMessageList.get(i).setBaseUrl(conversationUser.getBaseUrl());
boolean shouldScroll = layoutManager.findFirstVisibleItemPosition() == 0 ||
adapter.getItemCount() == 0;
@ -548,7 +634,6 @@ public class ChatController extends BaseController implements MessagesListAdapte
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
inflater.inflate(R.menu.menu_conversation, menu);
globalMenu = menu;
}
@ -582,7 +667,7 @@ public class ChatController extends BaseController implements MessagesListAdapte
if (currentCall != null && !TextUtils.isEmpty(currentCall.getSessionId())) {
Bundle bundle = new Bundle();
bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomToken);
bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(currentUser));
bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(conversationUser));
bundle.putString(BundleKeys.KEY_CALL_SESSION, currentCall.getSessionId());
if (isVoiceOnlyCall) {

View File

@ -129,7 +129,7 @@ public class EntryMenuController extends BaseController {
bundle = new Bundle();
bundle.putParcelable(BundleKeys.KEY_ROOM, Parcels.wrap(room));
bundle.putString(BundleKeys.KEY_CALL_URL, callUrl);
bundle.putString(BundleKeys.KEY_CALL_PASSWORD, editText.getText().toString());
bundle.putString(BundleKeys.KEY_CONVERSATION_PASSWORD, editText.getText().toString());
bundle.putInt(BundleKeys.KEY_OPERATION_CODE, operationCode);
getRouter().pushController(RouterTransaction.with(new OperationsMenuController(bundle))
.pushChangeHandler(new HorizontalChangeHandler())

View File

@ -125,7 +125,7 @@ public class OperationsMenuController extends BaseController {
this.room = Parcels.unwrap(args.getParcelable(BundleKeys.KEY_ROOM));
}
this.callPassword = args.getString(BundleKeys.KEY_CALL_PASSWORD, "");
this.callPassword = args.getString(BundleKeys.KEY_CONVERSATION_PASSWORD, "");
this.callUrl = args.getString(BundleKeys.KEY_CALL_URL, "");
if (args.containsKey(BundleKeys.KEY_INVITED_PARTICIPANTS)) {

View File

@ -32,12 +32,11 @@ public class BundleKeys {
public static final String KEY_SHARE_INTENT = "KEY_SHARE_INTENT";
public static final String KEY_APP_ITEM_PACKAGE_NAME = "KEY_APP_ITEM_PACKAGE_NAME";
public static final String KEY_APP_ITEM_NAME = "KEY_APP_ITEM_NAME";
public static final String KEY_CALL_PASSWORD = "KEY_CONVERSATION_PASSWORD";
public static final String KEY_CONVERSATION_PASSWORD = "KEY_CONVERSATION_PASSWORD";
public static final String KEY_CALL_SESSION = "KEY_CONVERSATION_SESSION";
public static final String KEY_ROOM_TOKEN = "KEY_ROOM_TOKEN";
public static final String KEY_USER_ENTITY = "KEY_USER_ENTITY";
public static final String KEY_NEW_CONVERSATION = "KEY_NEW_CONVERSATION";
public static final String KEY_IS_PUBLIC_CALL = "KEY_IS_PUBLIC_CALL";
public static final String KEY_CALL_URL = "KEY_CALL_URL";
public static final String KEY_MODIFIED_BASE_URL = "KEY_MODIFIED_BASE_URL";
public static final String KEY_NOTIFICATION_SUBJECT = "KEY_NOTIFICATION_SUBJECT";
@ -46,6 +45,6 @@ public class BundleKeys {
public static final String KEY_CONVERSATION_TYPE = "KEY_CONVERSATION_TYPE";
public static final String KEY_INVITED_PARTICIPANTS = "KEY_INVITED_PARTICIPANTS";
public static final String KEY_CONVERSATION_NAME = "KEY_CONVERSATION_NAME";
public static final String KEY_ROOM_PASSWORD = "KEY_ROOM_PASSWORD";
public static final String KEY_CALL_VOICE_ONLY = "KEY_CALL_VOICE_ONLY";
public static final String KEY_ACTIVE_CONVERSATION = "KEY_ACTIVE_CONVERSATION";
}