Commit e893780a authored by Leonardo Aramaki's avatar Leonardo Aramaki

Simplified multi-server roomId selection cache on multi-server

parent 3ec4779a
......@@ -14,6 +14,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.UUID;
import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.log.RCLog;
import chat.rocket.core.utils.Pair;
......@@ -106,11 +107,33 @@ public class RocketChatCache {
}
public String getSelectedRoomId() {
return getString(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID, null);
try {
JSONObject jsonObject = getSelectedRoomIdJsonObject();
return jsonObject.optString(getSelectedServerHostname(), null);
} catch (JSONException e) {
RCLog.e(e);
Logger.report(e);
}
return null;
}
public void setSelectedRoomId(String roomId) {
setString(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID, roomId);
try {
JSONObject jsonObject = getSelectedRoomIdJsonObject();
jsonObject.put(getSelectedServerHostname(), roomId);
setString(KEY_SELECTED_ROOM_ID, jsonObject.toString());
} catch (JSONException e) {
RCLog.e(e);
Logger.report(e);
}
}
private JSONObject getSelectedRoomIdJsonObject() throws JSONException {
String json = getString(KEY_SELECTED_ROOM_ID, null);
if (json == null) {
return new JSONObject();
}
return new JSONObject(json);
}
public String getOrCreatePushId() {
......@@ -131,7 +154,7 @@ public class RocketChatCache {
}
public Flowable<Optional<String>> getSelectedRoomIdPublisher() {
return getValuePublisher(getSelectedServerHostname() + KEY_SELECTED_ROOM_ID);
return getValuePublisher(KEY_SELECTED_ROOM_ID);
}
private SharedPreferences getSharedPreferences() {
......
......@@ -6,6 +6,9 @@ import android.support.annotation.Nullable;
import com.hadisatrio.optional.Optional;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.List;
import chat.rocket.android.LaunchUtil;
......@@ -196,7 +199,8 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
compositeDisposable.add(
rocketChatCache.getSelectedRoomIdPublisher()
.map(Optional::get)
.distinctUntilChanged()
.map(this::convertStringToJsonObject)
.map(jsonObject -> jsonObject.optString(rocketChatCache.getSelectedServerHostname(), null))
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
......@@ -206,17 +210,10 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity {
);
}
protected void subscribeToNewConfigChanges() {
compositeDisposable.add(
rocketChatCache.getSelectedRoomIdPublisher()
.map(Optional::get)
.distinctUntilChanged()
.subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
this::updateRoomIdIfNeeded,
Logger::report
)
);
private JSONObject convertStringToJsonObject(String json) throws JSONException {
if (json == null) {
return new JSONObject();
}
return new JSONObject(json);
}
}
......@@ -203,10 +203,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
if (sidebarFragment == null) {
sidebarFragment = SidebarMainFragment.create(selectedServerHostname);
}
subscribeToNewConfigChanges();
getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, sidebarFragment, selectedServerHostname)
.addToBackStack(null)
.commit();
getSupportFragmentManager().executePendingTransactions();
}
......@@ -229,11 +227,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
@Override
public void showRoom(String hostname, String roomId) {
Fragment roomFragment = findFragmentByTag(roomId);
if (roomFragment == null) {
roomFragment = RoomFragment.create(hostname, roomId);
}
showFragmentWithTag(roomFragment, roomId);
showFragment(RoomFragment.create(hostname, roomId));
closeSidebarIfNeeded();
KeyboardHelper.hideSoftKeyboard(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