Commit f23e8b6b authored by Leonardo Aramaki's avatar Leonardo Aramaki

Reformatted code from MainActivity and set a default drawable for the server...

Reformatted code from MainActivity and set a default drawable for the server list icons whenever ic_launcher isnt available at mipmap
parent 01dc03da
package chat.rocket.android.activity; package chat.rocket.android.activity;
import android.content.Intent; import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar; import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
...@@ -26,6 +28,7 @@ import chat.rocket.android.fragment.chatroom.RoomFragment; ...@@ -26,6 +28,7 @@ import chat.rocket.android.fragment.chatroom.RoomFragment;
import chat.rocket.android.fragment.sidebar.SidebarMainFragment; import chat.rocket.android.fragment.sidebar.SidebarMainFragment;
import chat.rocket.android.helper.KeyboardHelper; import chat.rocket.android.helper.KeyboardHelper;
import chat.rocket.android.service.ConnectivityManager; import chat.rocket.android.service.ConnectivityManager;
import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.widget.RoomToolbar; import chat.rocket.android.widget.RoomToolbar;
import chat.rocket.android.widget.helper.FrescoHelper; import chat.rocket.android.widget.helper.FrescoHelper;
import chat.rocket.core.interactors.CanCreateRoomInteractor; import chat.rocket.core.interactors.CanCreateRoomInteractor;
...@@ -43,330 +46,337 @@ import hugo.weaving.DebugLog; ...@@ -43,330 +46,337 @@ import hugo.weaving.DebugLog;
* Entry-point for Rocket.Chat.Android application. * Entry-point for Rocket.Chat.Android application.
*/ */
public class MainActivity extends AbstractAuthedActivity implements MainContract.View { public class MainActivity extends AbstractAuthedActivity implements MainContract.View {
private RoomToolbar toolbar; private RoomToolbar toolbar;
private StatusTicker statusTicker; private StatusTicker statusTicker;
private SlidingPaneLayout pane; private SlidingPaneLayout pane;
private MainContract.Presenter presenter; private MainContract.Presenter presenter;
@Override @Override
public int getLayoutContainerForFragment() { public int getLayoutContainerForFragment() {
return R.id.activity_main_container; return R.id.activity_main_container;
}
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (RoomToolbar) findViewById(R.id.activity_main_toolbar);
statusTicker = new StatusTicker();
pane = (SlidingPaneLayout) findViewById(R.id.sliding_pane);
setupToolbar();
}
@Override
protected void onResume() {
super.onResume();
if (hostname == null || presenter == null) {
String previousHostname = hostname;
hostname = new RocketChatCache(getApplicationContext()).getSelectedServerHostname();
if (hostname == null) {
showAddServerScreen();
} else {
onHostnameUpdated();
if (!hostname.equalsIgnoreCase(previousHostname)) {
ConnectivityManager.getInstance(getApplicationContext()).resetConnectivityStateList();
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
}
}
} else {
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer();
presenter.bindView(this);
presenter.loadSignedInServers(hostname);
roomId = new RocketChatCache(getApplicationContext()).getSelectedRoomId();
} }
}
@Override @Override
protected void onPause() { protected void onCreate(@Nullable Bundle savedInstanceState) {
if (presenter != null) { super.onCreate(savedInstanceState);
presenter.release(); setContentView(R.layout.activity_main);
toolbar = findViewById(R.id.activity_main_toolbar);
statusTicker = new StatusTicker();
pane = findViewById(R.id.sliding_pane);
setupToolbar();
} }
super.onPause(); @Override
} protected void onResume() {
super.onResume();
private void showAddServerActivity() { ConnectivityManagerApi connectivityManager = ConnectivityManager.getInstance(getApplicationContext());
closeSidebarIfNeeded(); if (hostname == null || presenter == null) {
Intent intent = new Intent(this, AddServerActivity.class); String previousHostname = hostname;
intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP); hostname = new RocketChatCache(getApplicationContext()).getSelectedServerHostname();
intent.putExtra(AddServerActivity.EXTRA_FINISH_ON_BACK_PRESS, true); if (hostname == null) {
startActivity(intent); showAddServerScreen();
} } else {
onHostnameUpdated();
private void setupToolbar() { if (!hostname.equalsIgnoreCase(previousHostname)) {
if (pane != null) { connectivityManager.resetConnectivityStateList();
pane.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() { connectivityManager.keepAliveServer();
@Override }
public void onPanelSlide(View view, float v) { }
//Ref: ActionBarDrawerToggle#setProgress } else {
toolbar.setNavigationIconProgress(v); connectivityManager.keepAliveServer();
presenter.bindView(this);
presenter.loadSignedInServers(hostname);
roomId = new RocketChatCache(getApplicationContext()).getSelectedRoomId();
} }
}
@Override @Override
public void onPanelOpened(View view) { protected void onPause() {
toolbar.setNavigationIconVerticalMirror(true); if (presenter != null) {
presenter.release();
} }
@Override super.onPause();
public void onPanelClosed(View view) {
toolbar.setNavigationIconVerticalMirror(false);
Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.sidebar_fragment_container);
if (fragment != null && fragment instanceof SidebarMainFragment) {
SidebarMainFragment sidebarMainFragment = (SidebarMainFragment) fragment;
sidebarMainFragment.toggleUserActionContainer(false);
sidebarMainFragment.showUserActionContainer(false);
}
}
});
if (toolbar != null) {
toolbar.setNavigationOnClickListener(view -> {
if (pane.isSlideable() && !pane.isOpen()) {
pane.openPane();
}
});
}
} }
closeSidebarIfNeeded();
} private void showAddServerActivity() {
closeSidebarIfNeeded();
private boolean closeSidebarIfNeeded() { Intent intent = new Intent(this, AddServerActivity.class);
// REMARK: Tablet UI doesn't have SlidingPane! intent.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT | Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
if (pane != null && pane.isSlideable() && pane.isOpen()) { intent.putExtra(AddServerActivity.EXTRA_FINISH_ON_BACK_PRESS, true);
pane.closePane(); startActivity(intent);
return true;
} }
return false;
}
@DebugLog private void setupToolbar() {
@Override if (pane != null) {
protected void onHostnameUpdated() { pane.setPanelSlideListener(new SlidingPaneLayout.PanelSlideListener() {
super.onHostnameUpdated(); @Override
public void onPanelSlide(@NonNull View view, float v) {
//Ref: ActionBarDrawerToggle#setProgress
toolbar.setNavigationIconProgress(v);
}
@Override
public void onPanelOpened(@NonNull View view) {
toolbar.setNavigationIconVerticalMirror(true);
}
@Override
public void onPanelClosed(@NonNull View view) {
toolbar.setNavigationIconVerticalMirror(false);
Fragment fragment = getSupportFragmentManager()
.findFragmentById(R.id.sidebar_fragment_container);
if (fragment != null && fragment instanceof SidebarMainFragment) {
SidebarMainFragment sidebarMainFragment = (SidebarMainFragment) fragment;
sidebarMainFragment.toggleUserActionContainer(false);
sidebarMainFragment.showUserActionContainer(false);
}
}
});
if (toolbar != null) {
toolbar.setNavigationOnClickListener(view -> {
if (pane.isSlideable() && !pane.isOpen()) {
pane.openPane();
}
});
}
}
closeSidebarIfNeeded();
}
if (presenter != null) { private boolean closeSidebarIfNeeded() {
presenter.release(); // REMARK: Tablet UI doesn't have SlidingPane!
if (pane != null && pane.isSlideable() && pane.isOpen()) {
pane.closePane();
return true;
}
return false;
} }
RoomInteractor roomInteractor = new RoomInteractor(new RealmRoomRepository(hostname)); @DebugLog
@Override
protected void onHostnameUpdated() {
super.onHostnameUpdated();
CanCreateRoomInteractor createRoomInteractor = new CanCreateRoomInteractor( if (presenter != null) {
new RealmUserRepository(hostname), presenter.release();
new SessionInteractor(new RealmSessionRepository(hostname)) }
);
SessionInteractor sessionInteractor = new SessionInteractor( RoomInteractor roomInteractor = new RoomInteractor(new RealmRoomRepository(hostname));
new RealmSessionRepository(hostname)
);
PublicSettingRepository publicSettingRepository = new RealmPublicSettingRepository(hostname); CanCreateRoomInteractor createRoomInteractor = new CanCreateRoomInteractor(
new RealmUserRepository(hostname),
new SessionInteractor(new RealmSessionRepository(hostname))
);
RocketChatCache rocketChatCache = new RocketChatCache(this); SessionInteractor sessionInteractor = new SessionInteractor(
new RealmSessionRepository(hostname)
);
presenter = new MainPresenter( PublicSettingRepository publicSettingRepository = new RealmPublicSettingRepository(hostname);
roomInteractor,
createRoomInteractor,
sessionInteractor,
new MethodCallHelper(this, hostname),
ConnectivityManager.getInstance(getApplicationContext()),
rocketChatCache,
publicSettingRepository
);
updateSidebarMainFragment(); RocketChatCache rocketChatCache = new RocketChatCache(this);
presenter.bindView(this); presenter = new MainPresenter(
presenter.loadSignedInServers(hostname); roomInteractor,
createRoomInteractor,
sessionInteractor,
new MethodCallHelper(this, hostname),
ConnectivityManager.getInstance(getApplicationContext()),
rocketChatCache,
publicSettingRepository
);
roomId = rocketChatCache.getSelectedRoomId(); updateSidebarMainFragment();
}
private void updateSidebarMainFragment() { presenter.bindView(this);
closeSidebarIfNeeded(); presenter.loadSignedInServers(hostname);
String selectedServerHostname = new RocketChatCache(this).getSelectedServerHostname();
Fragment sidebarFragment = findFragmentByTag(selectedServerHostname); roomId = rocketChatCache.getSelectedRoomId();
if (sidebarFragment == null) {
sidebarFragment = SidebarMainFragment.create(selectedServerHostname);
} }
getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, sidebarFragment, selectedServerHostname) private void updateSidebarMainFragment() {
.commit(); closeSidebarIfNeeded();
getSupportFragmentManager().executePendingTransactions(); String selectedServerHostname = new RocketChatCache(this).getSelectedServerHostname();
} Fragment sidebarFragment = findFragmentByTag(selectedServerHostname);
if (sidebarFragment == null) {
@Override sidebarFragment = SidebarMainFragment.create(selectedServerHostname);
protected void onRoomIdUpdated() {
super.onRoomIdUpdated();
presenter.onOpenRoom(hostname, roomId);
}
@Override
protected boolean onBackPress() {
return closeSidebarIfNeeded() || super.onBackPress();
}
@Override
public void showHome() {
showFragment(new HomeFragment());
}
@Override
public void showRoom(String hostname, String roomId) {
showFragment(RoomFragment.create(hostname, roomId));
closeSidebarIfNeeded();
KeyboardHelper.hideSoftKeyboard(this);
}
@Override
public void showUnreadCount(long roomsCount, int mentionsCount) {
toolbar.setUnreadBadge((int) roomsCount, mentionsCount);
}
@Override
public void showAddServerScreen() {
LaunchUtil.showAddServerActivity(this);
}
@Override
public void showLoginScreen() {
LaunchUtil.showLoginActivity(this, hostname);
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
}
@Override
public void showConnectionError() {
statusTicker.updateStatus(StatusTicker.STATUS_CONNECTION_ERROR,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.fragment_retry_login_error_title, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.fragment_retry_login_retry_title, view ->
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer()));
}
@Override
public void showConnecting() {
statusTicker.updateStatus(StatusTicker.STATUS_TOKEN_LOGIN,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.server_config_activity_authenticating, Snackbar.LENGTH_INDEFINITE));
}
@Override
public void showConnectionOk() {
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
}
@Override
public void showSignedInServers(List<Pair<String, Pair<String, String>>> serverList) {
final SlidingPaneLayout subPane = (SlidingPaneLayout) findViewById(R.id.sub_sliding_pane);
if (subPane != null) {
LinearLayout serverListContainer = subPane.findViewById(R.id.server_list_bar);
View addServerButton = subPane.findViewById(R.id.btn_add_server);
addServerButton.setOnClickListener(view -> showAddServerActivity());
serverListContainer.removeAllViews();
for (Pair<String, Pair<String, String>> server : serverList) {
String serverHostname = server.first;
Pair<String, String> serverInfoPair = server.second;
String logoUrl = serverInfoPair.first;
String siteName = serverInfoPair.second;
View serverView = serverListContainer.findViewWithTag(serverHostname);
if (serverView == null) {
View newServerView = LayoutInflater.from(this).inflate(R.layout.server_row, serverListContainer, false);
SimpleDraweeView serverButton = newServerView.findViewById(R.id.drawee_server_button);
TextView hostnameLabel = newServerView.findViewById(R.id.text_view_server_label);
TextView siteNameLabel = newServerView.findViewById(R.id.text_view_site_name_label);
ImageView dotView = newServerView.findViewById(R.id.selected_server_dot);
newServerView.setTag(serverHostname);
hostnameLabel.setText(serverHostname);
siteNameLabel.setText(siteName);
// Currently selected server
if (hostname.equalsIgnoreCase(serverHostname)) {
newServerView.setSelected(true);
dotView.setVisibility(View.VISIBLE);
} else {
newServerView.setSelected(false);
dotView.setVisibility(View.GONE);
}
newServerView.setOnClickListener(view -> changeServerIfNeeded(serverHostname));
FrescoHelper.INSTANCE.loadImage(serverButton, logoUrl, ContextCompat.getDrawable(this, R.mipmap.ic_launcher));
serverListContainer.addView(newServerView);
} }
} getSupportFragmentManager().beginTransaction()
serverListContainer.addView(addServerButton); .replace(R.id.sidebar_fragment_container, sidebarFragment, selectedServerHostname)
.commit();
getSupportFragmentManager().executePendingTransactions();
} }
}
@Override
@Override protected void onRoomIdUpdated() {
public void refreshRoom() { super.onRoomIdUpdated();
Fragment fragment = getSupportFragmentManager().findFragmentById(getLayoutContainerForFragment()); presenter.onOpenRoom(hostname, roomId);
if (fragment != null && fragment instanceof RoomFragment) { }
RoomFragment roomFragment = (RoomFragment) fragment;
roomFragment.loadMessages(); @Override
protected boolean onBackPress() {
return closeSidebarIfNeeded() || super.onBackPress();
}
@Override
public void showHome() {
showFragment(new HomeFragment());
} }
}
private void changeServerIfNeeded(String serverHostname) { @Override
if (!hostname.equalsIgnoreCase(serverHostname)) { public void showRoom(String hostname, String roomId) {
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext()); showFragment(RoomFragment.create(hostname, roomId));
rocketChatCache.setSelectedServerHostname(serverHostname); closeSidebarIfNeeded();
KeyboardHelper.hideSoftKeyboard(this);
} }
}
@Override
@DebugLog public void showUnreadCount(long roomsCount, int mentionsCount) {
public void onLogout() { toolbar.setUnreadBadge((int) roomsCount, mentionsCount);
if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) {
LaunchUtil.showMainActivity(this);
} else {
onHostnameUpdated();
} }
}
@DebugLog @Override
public void beforeLogoutCleanUp() { public void showAddServerScreen() {
presenter.beforeLogoutCleanUp(); LaunchUtil.showAddServerActivity(this);
} }
//TODO: consider this class to define in layouthelper for more complicated operation. @Override
private static class StatusTicker { public void showLoginScreen() {
public static final int STATUS_DISMISS = 0; LaunchUtil.showLoginActivity(this, hostname);
public static final int STATUS_CONNECTION_ERROR = 1; statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
public static final int STATUS_TOKEN_LOGIN = 2; }
private int status; @Override
private Snackbar snackbar; public void showConnectionError() {
statusTicker.updateStatus(StatusTicker.STATUS_CONNECTION_ERROR,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.fragment_retry_login_error_title, Snackbar.LENGTH_INDEFINITE)
.setAction(R.string.fragment_retry_login_retry_title, view ->
ConnectivityManager.getInstance(getApplicationContext()).keepAliveServer()));
}
public StatusTicker() { @Override
status = STATUS_DISMISS; public void showConnecting() {
statusTicker.updateStatus(StatusTicker.STATUS_TOKEN_LOGIN,
Snackbar.make(findViewById(getLayoutContainerForFragment()),
R.string.server_config_activity_authenticating, Snackbar.LENGTH_INDEFINITE));
} }
public void updateStatus(int status, Snackbar snackbar) { @Override
if (status == this.status) { public void showConnectionOk() {
return; statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
} }
this.status = status;
if (this.snackbar != null) { @Override
this.snackbar.dismiss(); public void showSignedInServers(List<Pair<String, Pair<String, String>>> serverList) {
} final SlidingPaneLayout subPane = findViewById(R.id.sub_sliding_pane);
if (status != STATUS_DISMISS) { if (subPane != null) {
this.snackbar = snackbar; LinearLayout serverListContainer = subPane.findViewById(R.id.server_list_bar);
if (this.snackbar != null) { View addServerButton = subPane.findViewById(R.id.btn_add_server);
this.snackbar.show(); addServerButton.setOnClickListener(view -> showAddServerActivity());
serverListContainer.removeAllViews();
for (Pair<String, Pair<String, String>> server : serverList) {
String serverHostname = server.first;
Pair<String, String> serverInfoPair = server.second;
String logoUrl = serverInfoPair.first;
String siteName = serverInfoPair.second;
View serverView = serverListContainer.findViewWithTag(serverHostname);
if (serverView == null) {
View newServerView = LayoutInflater.from(this).inflate(R.layout.server_row, serverListContainer, false);
SimpleDraweeView serverButton = newServerView.findViewById(R.id.drawee_server_button);
TextView hostnameLabel = newServerView.findViewById(R.id.text_view_server_label);
TextView siteNameLabel = newServerView.findViewById(R.id.text_view_site_name_label);
ImageView dotView = newServerView.findViewById(R.id.selected_server_dot);
newServerView.setTag(serverHostname);
hostnameLabel.setText(serverHostname);
siteNameLabel.setText(siteName);
// Currently selected server
if (hostname.equalsIgnoreCase(serverHostname)) {
newServerView.setSelected(true);
dotView.setVisibility(View.VISIBLE);
} else {
newServerView.setSelected(false);
dotView.setVisibility(View.GONE);
}
newServerView.setOnClickListener(view -> changeServerIfNeeded(serverHostname));
Drawable drawable = ContextCompat.getDrawable(this, R.mipmap.ic_launcher);
if (drawable == null) {
int id = getResources().getIdentifier(
"rocket_chat_notification", "drawable", getPackageName());
drawable = ContextCompat.getDrawable(this, id);
}
FrescoHelper.INSTANCE.loadImage(serverButton, logoUrl, drawable);
serverListContainer.addView(newServerView);
}
}
serverListContainer.addView(addServerButton);
}
}
@Override
public void refreshRoom() {
Fragment fragment = getSupportFragmentManager().findFragmentById(getLayoutContainerForFragment());
if (fragment != null && fragment instanceof RoomFragment) {
RoomFragment roomFragment = (RoomFragment) fragment;
roomFragment.loadMessages();
}
}
private void changeServerIfNeeded(String serverHostname) {
if (!hostname.equalsIgnoreCase(serverHostname)) {
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
rocketChatCache.setSelectedServerHostname(serverHostname);
}
}
@DebugLog
public void onLogout() {
if (new RocketChatCache(getApplicationContext()).getSelectedServerHostname() == null) {
LaunchUtil.showMainActivity(this);
} else {
onHostnameUpdated();
}
}
@DebugLog
public void beforeLogoutCleanUp() {
presenter.beforeLogoutCleanUp();
}
//TODO: consider this class to define in layouthelper for more complicated operation.
private static class StatusTicker {
static final int STATUS_DISMISS = 0;
static final int STATUS_CONNECTION_ERROR = 1;
static final int STATUS_TOKEN_LOGIN = 2;
private int status;
private Snackbar snackbar;
StatusTicker() {
status = STATUS_DISMISS;
}
void updateStatus(int status, Snackbar snackbar) {
if (status == this.status) {
return;
}
this.status = status;
if (this.snackbar != null) {
this.snackbar.dismiss();
}
if (status != STATUS_DISMISS) {
this.snackbar = snackbar;
if (this.snackbar != null) {
this.snackbar.show();
}
}
} }
}
} }
}
} }
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