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