Commit 47b9fdc3 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update SidebarMainContract.java, SidebarMainFragment.java and SidebarMainPresenter.java

Move logic to the presenter.
parent e30e946e
...@@ -20,7 +20,7 @@ public interface SidebarMainContract { ...@@ -20,7 +20,7 @@ public interface SidebarMainContract {
void showRoomList(@NonNull List<Room> roomList); void showRoomList(@NonNull List<Room> roomList);
void show(User user, RocketChatAbsoluteUrl absoluteUrl); void show(User user);
} }
interface Presenter extends BaseContract.Presenter<View> { interface Presenter extends BaseContract.Presenter<View> {
......
...@@ -16,12 +16,10 @@ import chat.rocket.android.R; ...@@ -16,12 +16,10 @@ import chat.rocket.android.R;
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.fragment.AbstractFragment; import chat.rocket.android.fragment.AbstractFragment;
import chat.rocket.android.fragment.chatroom.RocketChatAbsoluteUrl;
import chat.rocket.android.fragment.sidebar.dialog.AddChannelDialogFragment; import chat.rocket.android.fragment.sidebar.dialog.AddChannelDialogFragment;
import chat.rocket.android.fragment.sidebar.dialog.AddDirectMessageDialogFragment; import chat.rocket.android.fragment.sidebar.dialog.AddDirectMessageDialogFragment;
import chat.rocket.android.helper.AbsoluteUrlHelper; import chat.rocket.android.helper.AbsoluteUrlHelper;
import chat.rocket.android.helper.Logger; import chat.rocket.android.helper.Logger;
import chat.rocket.android.helper.TextUtils;
import chat.rocket.android.layouthelper.chatroom.roomlist.ChannelRoomListHeader; import chat.rocket.android.layouthelper.chatroom.roomlist.ChannelRoomListHeader;
import chat.rocket.android.layouthelper.chatroom.roomlist.DirectMessageRoomListHeader; import chat.rocket.android.layouthelper.chatroom.roomlist.DirectMessageRoomListHeader;
import chat.rocket.android.layouthelper.chatroom.roomlist.FavoriteRoomListHeader; import chat.rocket.android.layouthelper.chatroom.roomlist.FavoriteRoomListHeader;
...@@ -51,11 +49,8 @@ import java.util.concurrent.TimeUnit; ...@@ -51,11 +49,8 @@ import java.util.concurrent.TimeUnit;
public class SidebarMainFragment extends AbstractFragment implements SidebarMainContract.View { public class SidebarMainFragment extends AbstractFragment implements SidebarMainContract.View {
private SidebarMainContract.Presenter presenter; private SidebarMainContract.Presenter presenter;
private RoomListAdapter adapter; private RoomListAdapter adapter;
private String hostname;
private MethodCallHelper methodCallHelper;
private SearchView searchView; private SearchView searchView;
private String hostname;
private static final String HOSTNAME = "hostname"; private static final String HOSTNAME = "hostname";
public SidebarMainFragment() {} public SidebarMainFragment() {}
...@@ -77,11 +72,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -77,11 +72,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
public void onCreate(@Nullable Bundle savedInstanceState) { public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
Bundle args = getArguments(); hostname = getArguments().getString(HOSTNAME);
hostname = args == null ? null : args.getString(HOSTNAME);
methodCallHelper = new MethodCallHelper(getContext(), hostname);
RealmUserRepository userRepository = new RealmUserRepository(hostname); RealmUserRepository userRepository = new RealmUserRepository(hostname);
AbsoluteUrlHelper absoluteUrlHelper = new AbsoluteUrlHelper( AbsoluteUrlHelper absoluteUrlHelper = new AbsoluteUrlHelper(
...@@ -97,7 +88,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -97,7 +88,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
userRepository, userRepository,
new RocketChatCache(getContext()), new RocketChatCache(getContext()),
absoluteUrlHelper, absoluteUrlHelper,
TextUtils.isEmpty(hostname) ? null : new MethodCallHelper(getContext(), hostname), new MethodCallHelper(getContext(), hostname),
new RealmSpotlightRepository(hostname) new RealmSpotlightRepository(hostname)
); );
} }
...@@ -141,17 +132,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -141,17 +132,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
public void onItemClick(Spotlight spotlight) { public void onItemClick(Spotlight spotlight) {
searchView.setQuery(null, false); searchView.setQuery(null, false);
searchView.clearFocus(); searchView.clearFocus();
if (spotlight.getType().equals("d")) {
String username = spotlight.getName();
methodCallHelper.createDirectMessage(username);
}
methodCallHelper.joinRoom(spotlight.getId())
.onSuccessTask(task -> {
presenter.onSpotlightSelected(spotlight); presenter.onSpotlightSelected(spotlight);
return null;
});
} }
}); });
...@@ -161,7 +142,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -161,7 +142,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
RxSearchView.queryTextChanges(searchView) RxSearchView.queryTextChanges(searchView)
.compose(bindToLifecycle()) .compose(bindToLifecycle())
.debounce(100, TimeUnit.MILLISECONDS) .debounce(300, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.switchMap(charSequence -> { .switchMap(charSequence -> {
if (charSequence.length() == 0) { if (charSequence.length() == 0) {
...@@ -175,18 +156,12 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -175,18 +156,12 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
.subscribe(this::showSearchSuggestions, Logger::report); .subscribe(this::showSearchSuggestions, Logger::report);
} }
private void showSearchSuggestions(List<Spotlight> spotlightList) {
adapter.setSpotlightList(spotlightList);
}
@SuppressLint("RxLeakedSubscription") @SuppressLint("RxLeakedSubscription")
private void setupUserActionToggle() { private void setupUserActionToggle() {
final CompoundButton toggleUserAction = final CompoundButton toggleUserAction = rootView.findViewById(R.id.toggle_user_action);
((CompoundButton) rootView.findViewById(R.id.toggle_user_action));
toggleUserAction.setFocusableInTouchMode(false); toggleUserAction.setFocusableInTouchMode(false);
rootView.findViewById(R.id.user_info_container) rootView.findViewById(R.id.user_info_container).setOnClickListener(view -> toggleUserAction.toggle());
.setOnClickListener(view -> toggleUserAction.toggle());
RxCompoundButton.checkedChanges(toggleUserAction) RxCompoundButton.checkedChanges(toggleUserAction)
.compose(bindToLifecycle()) .compose(bindToLifecycle())
...@@ -197,6 +172,27 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -197,6 +172,27 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
); );
} }
@Override
public void showScreen() {
rootView.setVisibility(View.VISIBLE);
}
@Override
public void showEmptyScreen() {
rootView.setVisibility(View.INVISIBLE);
}
@Override
public void showRoomList(@NonNull List<Room> roomList) {
adapter.setRooms(roomList);
}
@Override
public void show(User user) {
onRenderCurrentUser(user);
updateRoomListMode(user);
}
private void setupUserStatusButtons() { private void setupUserStatusButtons() {
rootView.findViewById(R.id.btn_status_online).setOnClickListener(view -> { rootView.findViewById(R.id.btn_status_online).setOnClickListener(view -> {
presenter.onUserOnline(); presenter.onUserOnline();
...@@ -216,8 +212,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -216,8 +212,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
}); });
} }
private void onRenderCurrentUser(User user, RocketChatAbsoluteUrl absoluteUrl) { private void onRenderCurrentUser(User user) {
if (user != null && absoluteUrl != null) { if (user != null) {
UserRenderer userRenderer = new UserRenderer(user); UserRenderer userRenderer = new UserRenderer(user);
userRenderer.showAvatar(rootView.findViewById(R.id.current_user_avatar), hostname); userRenderer.showAvatar(rootView.findViewById(R.id.current_user_avatar), hostname);
userRenderer.showUsername(rootView.findViewById(R.id.current_user_name)); userRenderer.showUsername(rootView.findViewById(R.id.current_user_name));
...@@ -255,22 +251,20 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -255,22 +251,20 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> { rootView.findViewById(R.id.btn_logout).setOnClickListener(view -> {
presenter.onLogout(); presenter.onLogout();
closeUserActionContainer(); closeUserActionContainer();
// destroy Activity on logout to be able to recreate most of the environment // destroy Activity on logout to be able to recreate most of the environment
this.getActivity().finish(); this.getActivity().finish();
}); });
} }
private void closeUserActionContainer() { private void closeUserActionContainer() {
final CompoundButton toggleUserAction = final CompoundButton toggleUserAction = rootView.findViewById(R.id.toggle_user_action);
((CompoundButton) rootView.findViewById(R.id.toggle_user_action));
if (toggleUserAction != null && toggleUserAction.isChecked()) { if (toggleUserAction != null && toggleUserAction.isChecked()) {
toggleUserAction.setChecked(false); toggleUserAction.setChecked(false);
} }
} }
private void setupVersionInfo() { private void setupVersionInfo() {
TextView versionInfoView = (TextView) rootView.findViewById(R.id.version_info); TextView versionInfoView = rootView.findViewById(R.id.version_info);
versionInfoView.setText(getString(R.string.version_info_text, BuildConfig.VERSION_NAME)); versionInfoView.setText(getString(R.string.version_info_text, BuildConfig.VERSION_NAME));
} }
...@@ -278,25 +272,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -278,25 +272,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
dialog.show(getFragmentManager(), "AbstractAddRoomDialogFragment"); dialog.show(getFragmentManager(), "AbstractAddRoomDialogFragment");
} }
@Override private void showSearchSuggestions(List<Spotlight> spotlightList) {
public void showScreen() { adapter.setSpotlightList(spotlightList);
rootView.setVisibility(View.VISIBLE);
}
@Override
public void showEmptyScreen() {
rootView.setVisibility(View.INVISIBLE);
}
@Override
public void showRoomList(@NonNull List<Room> roomList) {
adapter.setRooms(roomList);
}
@Override
public void show(User user, RocketChatAbsoluteUrl absoluteUrl) {
onRenderCurrentUser(user, absoluteUrl);
updateRoomListMode(user);
} }
} }
\ No newline at end of file
...@@ -23,10 +23,9 @@ import chat.rocket.core.models.User; ...@@ -23,10 +23,9 @@ import chat.rocket.core.models.User;
import chat.rocket.core.repositories.UserRepository; import chat.rocket.core.repositories.UserRepository;
import java.util.List; import java.util.List;
public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View> implements SidebarMainContract.Presenter {
implements SidebarMainContract.Presenter {
private final String hostname; private final String hostname;
private String userId;
private final RoomInteractor roomInteractor; private final RoomInteractor roomInteractor;
private final UserRepository userRepository; private final UserRepository userRepository;
private final RocketChatCache rocketChatCache; private final RocketChatCache rocketChatCache;
...@@ -71,7 +70,10 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -71,7 +70,10 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
.subscribeOn(AndroidSchedulers.from(BackgroundLooper.get())) .subscribeOn(AndroidSchedulers.from(BackgroundLooper.get()))
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe( .subscribe(
pair -> view.show(pair.first.orNull(), pair.second.orNull()), pair -> {
userId = pair.first.orNull().getId();
view.show(pair.first.orNull());
},
Logger::report Logger::report
); );
...@@ -83,17 +85,34 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -83,17 +85,34 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
rocketChatCache.setSelectedRoomId(room.getRoomId()); rocketChatCache.setSelectedRoomId(room.getRoomId());
} }
@Override
public void onSpotlightSelected(Spotlight spotlight) {
rocketChatCache.setSelectedRoomId(spotlight.getId());
}
@Override @Override
public Flowable<List<Spotlight>> searchSpotlight(String term) { public Flowable<List<Spotlight>> searchSpotlight(String term) {
methodCallHelper.searchSpotlight(term); methodCallHelper.searchSpotlight(term);
return realmSpotlightRepository.getSuggestionsFor(term, 10); return realmSpotlightRepository.getSuggestionsFor(term, 10);
} }
@Override
public void onSpotlightSelected(Spotlight spotlight) {
if (spotlight.getType().equals(Room.TYPE_DIRECT_MESSAGE)) {
String username = spotlight.getName();
methodCallHelper.createDirectMessage(username)
.continueWithTask(task -> {
if (task.isCompleted()) {
rocketChatCache.setSelectedRoomId(spotlight.getId() + userId);
}
return null;
});
} else {
methodCallHelper.joinRoom(spotlight.getId())
.continueWithTask(task -> {
if (task.isCompleted()) {
rocketChatCache.setSelectedRoomId(spotlight.getId());
}
return null;
});
}
}
@Override @Override
public void onUserOnline() { public void onUserOnline() {
updateCurrentUserStatus(User.STATUS_ONLINE); updateCurrentUserStatus(User.STATUS_ONLINE);
...@@ -116,10 +135,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -116,10 +135,8 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
@Override @Override
public void onLogout() { public void onLogout() {
if (methodCallHelper != null) {
methodCallHelper.logout().continueWith(new LogIfError()); methodCallHelper.logout().continueWith(new LogIfError());
} }
}
private void subscribeToRooms() { private void subscribeToRooms() {
final Disposable subscription = roomInteractor.getOpenRooms() final Disposable subscription = roomInteractor.getOpenRooms()
...@@ -135,8 +152,6 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View ...@@ -135,8 +152,6 @@ public class SidebarMainPresenter extends BasePresenter<SidebarMainContract.View
} }
private void updateCurrentUserStatus(String status) { private void updateCurrentUserStatus(String status) {
if (methodCallHelper != null) {
methodCallHelper.setUserStatus(status).continueWith(new LogIfError()); methodCallHelper.setUserStatus(status).continueWith(new LogIfError());
} }
}
} }
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