Commit 036eaba6 authored by Leonardo Aramaki's avatar Leonardo Aramaki

Fixed multiple instatiation of the SidebarMainFragment and its

presenter. Subscribing to each room change on different connected
servers.
parent 611fe792
...@@ -205,4 +205,18 @@ abstract class AbstractAuthedActivity extends AbstractFragmentActivity { ...@@ -205,4 +205,18 @@ 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
)
);
}
} }
...@@ -75,10 +75,22 @@ abstract class AbstractFragmentActivity extends RxAppCompatActivity { ...@@ -75,10 +75,22 @@ abstract class AbstractFragmentActivity extends RxAppCompatActivity {
.commit(); .commit();
} }
protected void showFragmentWithTag(Fragment fragment, String tag) {
getSupportFragmentManager().beginTransaction()
.replace(getLayoutContainerForFragment(), fragment, tag)
.addToBackStack(null)
.commit();
}
protected void showFragmentWithBackStack(Fragment fragment) { protected void showFragmentWithBackStack(Fragment fragment) {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(getLayoutContainerForFragment(), fragment) .replace(getLayoutContainerForFragment(), fragment)
.addToBackStack(null) .addToBackStack(null)
.commit(); .commit();
} }
@Nullable
protected Fragment findFragmentByTag(String tag) {
return getSupportFragmentManager().findFragmentByTag(tag);
}
} }
...@@ -197,9 +197,17 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -197,9 +197,17 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
private void updateSidebarMainFragment() { private void updateSidebarMainFragment() {
closeSidebarIfNeeded(); closeSidebarIfNeeded();
String selectedServerHostname = new RocketChatCache(this).getSelectedServerHostname();
Fragment sidebarFragment = findFragmentByTag(selectedServerHostname);
if (sidebarFragment == null) {
sidebarFragment = SidebarMainFragment.create(selectedServerHostname);
}
subscribeToNewConfigChanges();
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(hostname)) .replace(R.id.sidebar_fragment_container, sidebarFragment, selectedServerHostname)
.addToBackStack(null)
.commit(); .commit();
getSupportFragmentManager().executePendingTransactions();
} }
@Override @Override
...@@ -220,7 +228,11 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -220,7 +228,11 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
@Override @Override
public void showRoom(String hostname, String roomId) { public void showRoom(String hostname, String roomId) {
showFragment(RoomFragment.create(hostname, roomId)); Fragment roomFragment = findFragmentByTag(roomId);
if (roomFragment == null) {
roomFragment = RoomFragment.create(hostname, roomId);
}
showFragmentWithTag(roomFragment, roomId);
closeSidebarIfNeeded(); closeSidebarIfNeeded();
KeyboardHelper.hideSoftKeyboard(this); KeyboardHelper.hideSoftKeyboard(this);
} }
...@@ -311,7 +323,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -311,7 +323,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
if (!hostname.equalsIgnoreCase(serverHostname)) { if (!hostname.equalsIgnoreCase(serverHostname)) {
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext()); RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
rocketChatCache.setSelectedServerHostname(serverHostname); rocketChatCache.setSelectedServerHostname(serverHostname);
onHostnameUpdated();
} }
} }
......
...@@ -8,7 +8,9 @@ import android.support.v4.app.DialogFragment; ...@@ -8,7 +8,9 @@ import android.support.v4.app.DialogFragment;
import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView; import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.SearchView; import android.support.v7.widget.SearchView;
import android.view.LayoutInflater;
import android.view.View; import android.view.View;
import android.view.ViewGroup;
import android.widget.CompoundButton; import android.widget.CompoundButton;
import android.widget.TextView; import android.widget.TextView;
...@@ -100,15 +102,29 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -100,15 +102,29 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
); );
} }
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
presenter.bindView(this);
return view;
}
@Override
public void onDestroyView() {
presenter.release();
super.onDestroyView();
}
@Override @Override
public void onResume() { public void onResume() {
super.onResume(); super.onResume();
presenter.bindView(this);
} }
@Override @Override
public void onPause() { public void onPause() {
presenter.release();
super.onPause(); super.onPause();
} }
......
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