Commit b83c2e63 authored by Yusuke Iwaki's avatar Yusuke Iwaki

implement setUsername and joinDefaultChannels and so on ...

parent f791fce4
......@@ -5,6 +5,7 @@ import android.os.Bundle;
import chat.rocket.android.LaunchUtil;
import chat.rocket.android.RocketChatCache;
import chat.rocket.android.model.ServerConfig;
import chat.rocket.android.model.ddp.RoomSubscription;
import chat.rocket.android.realm_helper.RealmListObserver;
import chat.rocket.android.realm_helper.RealmStore;
import chat.rocket.android.service.RocketChatService;
......@@ -36,16 +37,28 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
private void updateServerConfigIdIfNeeded(SharedPreferences prefs) {
String newServerConfigId = prefs.getString(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID, null);
if (serverConfigId == null) {
if (newServerConfigId != null) {
if (newServerConfigId != null && assertServerConfigExists(newServerConfigId, prefs)) {
updateServerConfigId(newServerConfigId);
}
} else {
if (!serverConfigId.equals(newServerConfigId)) {
if (!serverConfigId.equals(newServerConfigId)
&& assertServerConfigExists(newServerConfigId, prefs)) {
updateServerConfigId(newServerConfigId);
}
}
}
private boolean assertServerConfigExists(String serverConfigId, SharedPreferences prefs) {
if (RealmStore.get(serverConfigId) == null) {
prefs.edit()
.remove(RocketChatCache.KEY_SELECTED_SERVER_CONFIG_ID)
.remove(RocketChatCache.KEY_SELECTED_ROOM_ID)
.apply();
return false;
}
return true;
}
private void updateServerConfigId(String serverConfigId) {
this.serverConfigId = serverConfigId;
onServerConfigIdUpdated();
......@@ -54,16 +67,32 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
private void updateRoomIdIfNeeded(SharedPreferences prefs) {
String newRoomId = prefs.getString(RocketChatCache.KEY_SELECTED_ROOM_ID, null);
if (roomId == null) {
if (newRoomId != null) {
if (newRoomId != null && assertRoomSubscriptionExists(newRoomId, prefs)) {
updateRoomId(newRoomId);
}
} else {
if (!roomId.equals(newRoomId)) {
if (!roomId.equals(newRoomId) && assertRoomSubscriptionExists(newRoomId, prefs)) {
updateRoomId(newRoomId);
}
}
}
private boolean assertRoomSubscriptionExists(String roomId, SharedPreferences prefs) {
if (!assertServerConfigExists(serverConfigId, prefs)) {
return false;
}
RoomSubscription room = RealmStore.get(serverConfigId).executeTransactionForRead(realm ->
realm.where(RoomSubscription.class).equalTo("rid", roomId).findFirst());
if (room == null) {
prefs.edit()
.remove(RocketChatCache.KEY_SELECTED_ROOM_ID)
.apply();
return false;
}
return true;
}
private void updateRoomId(String roomId) {
this.roomId = roomId;
onRoomIdUpdated();
......
......@@ -119,6 +119,18 @@ public class MethodCallHelper {
));
}
/**
* set current user's name.
*/
public Task<String> setUsername(final String username) {
return call("setUsername", TIMEOUT_MS, () -> new JSONArray().put(username));
}
public Task<Void> joinDefaultChannels() {
return call("joinDefaultChannels", TIMEOUT_MS)
.onSuccessTask(task -> Task.forResult(null));
}
/**
* Login with username/email and password.
*/
......
......@@ -112,6 +112,8 @@ public class UserRegistrationDialogFragment extends DialogFragment {
MethodCallHelper methodCallHelper = new MethodCallHelper(serverConfigId);
methodCallHelper.registerUser(username, email, password, password)
.onSuccessTask(task -> methodCallHelper.loginWithEmail(email, password))
.onSuccessTask(task -> methodCallHelper.setUsername(username)) //TODO: should prompt!
.onSuccessTask(task -> methodCallHelper.joinDefaultChannels())
.onSuccessTask(task -> {
dismiss();
return task;
......
......@@ -5,6 +5,7 @@ import android.widget.ImageView;
import android.widget.TextView;
import chat.rocket.android.R;
import chat.rocket.android.helper.Avatar;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.model.ddp.User;
/**
......@@ -24,7 +25,9 @@ public class UserRenderer extends AbstractRenderer<User> {
return this;
}
new Avatar(hostname, object.getUsername()).into(imageView);
if (!TextUtils.isEmpty(object.getUsername())) {
new Avatar(hostname, object.getUsername()).into(imageView);
}
return this;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment