Commit d21901cb authored by Leonardo Aramaki's avatar Leonardo Aramaki

Implement in home presenter the load call for the currently logged in servers

parent 35a75978
...@@ -14,6 +14,8 @@ import android.widget.LinearLayout; ...@@ -14,6 +14,8 @@ import android.widget.LinearLayout;
import com.facebook.drawee.backends.pipeline.Fresco; import com.facebook.drawee.backends.pipeline.Fresco;
import com.facebook.drawee.view.SimpleDraweeView; import com.facebook.drawee.view.SimpleDraweeView;
import java.util.List;
import chat.rocket.android.LaunchUtil; import chat.rocket.android.LaunchUtil;
import chat.rocket.android.R; import chat.rocket.android.R;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
...@@ -28,6 +30,9 @@ import chat.rocket.android.widget.helper.AvatarHelper; ...@@ -28,6 +30,9 @@ import chat.rocket.android.widget.helper.AvatarHelper;
import chat.rocket.core.interactors.CanCreateRoomInteractor; import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.interactors.SessionInteractor; import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.core.repositories.PublicSettingRepository;
import chat.rocket.core.utils.Pair;
import chat.rocket.persistence.realm.repositories.RealmPublicSettingRepository;
import chat.rocket.persistence.realm.repositories.RealmRoomRepository; import chat.rocket.persistence.realm.repositories.RealmRoomRepository;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository; import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
import chat.rocket.persistence.realm.repositories.RealmUserRepository; import chat.rocket.persistence.realm.repositories.RealmUserRepository;
...@@ -60,6 +65,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -60,6 +65,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
super.onResume(); super.onResume();
if (presenter != null) { if (presenter != null) {
presenter.bindViewOnly(this); presenter.bindViewOnly(this);
presenter.loadSignedInServers(hostname);
} }
} }
...@@ -119,8 +125,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -119,8 +125,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
closeUserActionContainer(); closeUserActionContainer();
} }
}); });
updateServerListBar();
} }
private void showAddServerActivity() { private void showAddServerActivity() {
...@@ -159,6 +163,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -159,6 +163,7 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
new RealmSessionRepository(hostname) new RealmSessionRepository(hostname)
); );
PublicSettingRepository publicSettingRepository = new RealmPublicSettingRepository(hostname);
presenter = new MainPresenter( presenter = new MainPresenter(
roomInteractor, roomInteractor,
...@@ -166,7 +171,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -166,7 +171,8 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
sessionInteractor, sessionInteractor,
new MethodCallHelper(this, hostname), new MethodCallHelper(this, hostname),
ConnectivityManager.getInstance(getApplicationContext()), ConnectivityManager.getInstance(getApplicationContext()),
new RocketChatCache(this) new RocketChatCache(this),
publicSettingRepository
); );
updateSidebarMainFragment(); updateSidebarMainFragment();
...@@ -174,45 +180,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -174,45 +180,6 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
presenter.bindView(this); presenter.bindView(this);
} }
@DebugLog
private void updateServerListBar() {
final SlidingPaneLayout subPane = (SlidingPaneLayout) findViewById(R.id.sub_sliding_pane);
if (subPane != null) {
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
//TODO: get the server avatar uri and set
rocketChatCache.addHostname(hostname, null);
LinearLayout serverListContainer = subPane.findViewById(R.id.server_list_bar);
for (String serverHostname : rocketChatCache.getServerList()) {
if (serverListContainer.findViewWithTag(serverHostname) == null) {
int serverCount = serverListContainer.getChildCount();
SimpleDraweeView serverButton =
(SimpleDraweeView) LayoutInflater.from(this).inflate(R.layout.server_button, serverListContainer, false);
serverButton.setTag(serverHostname);
serverButton.setOnClickListener(view -> changeServerIfNeeded(serverHostname));
Drawable drawable = AvatarHelper.INSTANCE.getTextDrawable(serverHostname,this);
serverButton.getHierarchy().setPlaceholderImage(drawable);
serverButton.setController(Fresco.newDraweeControllerBuilder().setAutoPlayAnimations(true).build());
serverListContainer.addView(serverButton, serverCount - 1);
serverListContainer.requestLayout();
}
}
}
}
private void changeServerIfNeeded(String serverHostname) {
if (!hostname.equalsIgnoreCase(serverHostname)) {
closeSidebarIfNeeded();
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
rocketChatCache.setSelectedServerHostname(serverHostname);
recreate();
}
}
private void updateSidebarMainFragment() { private void updateSidebarMainFragment() {
getSupportFragmentManager().beginTransaction() getSupportFragmentManager().beginTransaction()
.replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(hostname)) .replace(R.id.sidebar_fragment_container, SidebarMainFragment.create(hostname))
...@@ -287,6 +254,41 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract ...@@ -287,6 +254,41 @@ public class MainActivity extends AbstractAuthedActivity implements MainContract
statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null); statusTicker.updateStatus(StatusTicker.STATUS_DISMISS, null);
} }
@Override
public void showSignedInServers(List<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);
for (Pair<String, String> server : serverList) {
if (serverListContainer.findViewWithTag(server.first) == null) {
int serverCount = serverListContainer.getChildCount();
SimpleDraweeView serverButton =
(SimpleDraweeView) LayoutInflater.from(this).inflate(R.layout.server_button, serverListContainer, false);
serverButton.setTag(server.first);
serverButton.setOnClickListener(view -> changeServerIfNeeded(server.first));
Drawable drawable = AvatarHelper.INSTANCE.getTextDrawable(server.first,this);
serverButton.getHierarchy().setPlaceholderImage(drawable);
serverButton.setController(Fresco.newDraweeControllerBuilder().setUri(server.second).setAutoPlayAnimations(true).build());
serverListContainer.addView(serverButton, serverCount - 1);
serverListContainer.requestLayout();
}
}
}
}
private void changeServerIfNeeded(String serverHostname) {
if (!hostname.equalsIgnoreCase(serverHostname)) {
closeSidebarIfNeeded();
RocketChatCache rocketChatCache = new RocketChatCache(getApplicationContext());
rocketChatCache.setSelectedServerHostname(serverHostname);
recreate();
}
}
//TODO: consider this class to define in layouthelper for more complicated operation. //TODO: consider this class to define in layouthelper for more complicated operation.
private static class StatusTicker { private static class StatusTicker {
public static final int STATUS_DISMISS = 0; public static final int STATUS_DISMISS = 0;
......
package chat.rocket.android.activity; package chat.rocket.android.activity;
import java.util.List;
import chat.rocket.android.shared.BaseContract; import chat.rocket.android.shared.BaseContract;
import chat.rocket.core.utils.Pair;
public interface MainContract { public interface MainContract {
...@@ -21,6 +24,8 @@ public interface MainContract { ...@@ -21,6 +24,8 @@ public interface MainContract {
void showConnecting(); void showConnecting();
void showConnectionOk(); void showConnectionOk();
void showSignedInServers(List<Pair<String, String>> serverList);
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
...@@ -30,5 +35,7 @@ public interface MainContract { ...@@ -30,5 +35,7 @@ public interface MainContract {
void onRetryLogin(); void onRetryLogin();
void bindViewOnly(View view); void bindViewOnly(View view);
void loadSignedInServers(String hostname);
} }
} }
package chat.rocket.android.activity; package chat.rocket.android.activity;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.util.Pair;
import com.hadisatrio.optional.Optional;
import org.jetbrains.annotations.NotNull;
import org.json.JSONObject;
import chat.rocket.android.BackgroundLooper; import chat.rocket.android.BackgroundLooper;
import chat.rocket.android.RocketChatCache; import chat.rocket.android.RocketChatCache;
import chat.rocket.android.api.MethodCallHelper; import chat.rocket.android.api.MethodCallHelper;
import chat.rocket.android.helper.LogIfError; import chat.rocket.android.helper.LogIfError;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.log.RCLog;
import chat.rocket.android.service.ConnectivityManagerApi; import chat.rocket.android.service.ConnectivityManagerApi;
import chat.rocket.android.service.ServerConnectivity; import chat.rocket.android.service.ServerConnectivity;
import chat.rocket.android.shared.BasePresenter; import chat.rocket.android.shared.BasePresenter;
import chat.rocket.core.PublicSettingsConstants;
import chat.rocket.core.interactors.CanCreateRoomInteractor; import chat.rocket.core.interactors.CanCreateRoomInteractor;
import chat.rocket.core.interactors.RoomInteractor; import chat.rocket.core.interactors.RoomInteractor;
import chat.rocket.core.interactors.SessionInteractor; import chat.rocket.core.interactors.SessionInteractor;
import chat.rocket.core.models.Session; import chat.rocket.core.models.Session;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.core.repositories.PublicSettingRepository;
import chat.rocket.core.utils.Pair;
import hu.akarnokd.rxjava.interop.RxJavaInterop; import hu.akarnokd.rxjava.interop.RxJavaInterop;
import io.reactivex.Flowable; import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers; import io.reactivex.android.schedulers.AndroidSchedulers;
...@@ -30,19 +38,21 @@ public class MainPresenter extends BasePresenter<MainContract.View> ...@@ -30,19 +38,21 @@ public class MainPresenter extends BasePresenter<MainContract.View>
private final MethodCallHelper methodCallHelper; private final MethodCallHelper methodCallHelper;
private final ConnectivityManagerApi connectivityManagerApi; private final ConnectivityManagerApi connectivityManagerApi;
private final RocketChatCache rocketChatCache; private final RocketChatCache rocketChatCache;
private final PublicSettingRepository publicSettingRepository;
public MainPresenter(RoomInteractor roomInteractor, public MainPresenter(RoomInteractor roomInteractor,
CanCreateRoomInteractor canCreateRoomInteractor, CanCreateRoomInteractor canCreateRoomInteractor,
SessionInteractor sessionInteractor, SessionInteractor sessionInteractor,
MethodCallHelper methodCallHelper, MethodCallHelper methodCallHelper,
ConnectivityManagerApi connectivityManagerApi, ConnectivityManagerApi connectivityManagerApi,
RocketChatCache rocketChatCache) { RocketChatCache rocketChatCache, PublicSettingRepository publicSettingRepository) {
this.roomInteractor = roomInteractor; this.roomInteractor = roomInteractor;
this.canCreateRoomInteractor = canCreateRoomInteractor; this.canCreateRoomInteractor = canCreateRoomInteractor;
this.sessionInteractor = sessionInteractor; this.sessionInteractor = sessionInteractor;
this.methodCallHelper = methodCallHelper; this.methodCallHelper = methodCallHelper;
this.connectivityManagerApi = connectivityManagerApi; this.connectivityManagerApi = connectivityManagerApi;
this.rocketChatCache = rocketChatCache; this.rocketChatCache = rocketChatCache;
this.publicSettingRepository = publicSettingRepository;
} }
@Override @Override
...@@ -53,6 +63,26 @@ public class MainPresenter extends BasePresenter<MainContract.View> ...@@ -53,6 +63,26 @@ public class MainPresenter extends BasePresenter<MainContract.View>
setUserOnline(); setUserOnline();
} }
@Override
public void loadSignedInServers(@NotNull String hostname) {
final Disposable disposable = publicSettingRepository.getById(PublicSettingsConstants.Assets.TILE_144)
.filter(Optional::isPresent)
.map(Optional::get)
.map(publicSetting -> {
JSONObject jsonObject = new JSONObject(publicSetting.getValue());
rocketChatCache.addHostname(hostname, jsonObject.optString("defaultUrl"));
return rocketChatCache.getServerList();
})
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread())
.subscribe(
view::showSignedInServers,
RCLog::e
);
addSubscription(disposable);
}
@Override @Override
public void bindView(@NonNull MainContract.View view) { public void bindView(@NonNull MainContract.View view) {
super.bindView(view); super.bindView(view);
......
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