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