Commit 3f133546 authored by Filipe de Lima Brito's avatar Filipe de Lima Brito

Update SidebarMainFragment.java

parent 0bb9d23c
...@@ -33,12 +33,12 @@ import chat.rocket.core.SortDirection; ...@@ -33,12 +33,12 @@ import chat.rocket.core.SortDirection;
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.Room; import chat.rocket.core.models.Room;
import chat.rocket.core.models.SpotlightRoom; import chat.rocket.core.models.Spotlight;
import chat.rocket.core.models.User; import chat.rocket.core.models.User;
import chat.rocket.persistence.realm.repositories.RealmRoomRepository; import chat.rocket.persistence.realm.repositories.RealmRoomRepository;
import chat.rocket.persistence.realm.repositories.RealmServerInfoRepository; import chat.rocket.persistence.realm.repositories.RealmServerInfoRepository;
import chat.rocket.persistence.realm.repositories.RealmSessionRepository; import chat.rocket.persistence.realm.repositories.RealmSessionRepository;
import chat.rocket.persistence.realm.repositories.RealmSpotlightRoomRepository; import chat.rocket.persistence.realm.repositories.RealmSpotlightRepository;
import chat.rocket.persistence.realm.repositories.RealmUserRepository; import chat.rocket.persistence.realm.repositories.RealmUserRepository;
import com.jakewharton.rxbinding2.support.v7.widget.RxSearchView; import com.jakewharton.rxbinding2.support.v7.widget.RxSearchView;
import com.jakewharton.rxbinding2.widget.RxCompoundButton; import com.jakewharton.rxbinding2.widget.RxCompoundButton;
...@@ -50,21 +50,17 @@ import java.util.List; ...@@ -50,21 +50,17 @@ import java.util.List;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
public class SidebarMainFragment extends AbstractFragment implements SidebarMainContract.View { public class SidebarMainFragment extends AbstractFragment implements SidebarMainContract.View {
private static final String HOSTNAME = "hostname";
private SidebarMainContract.Presenter presenter; private SidebarMainContract.Presenter presenter;
private RoomListAdapter adapter; private RoomListAdapter adapter;
private String hostname; private String hostname;
private MethodCallHelper methodCallHelper; private MethodCallHelper methodCallHelper;
private RealmSpotlightRoomRepository realmSpotlightRoomRepository; private RealmSpotlightRepository realmSpotlightRepository;
private SearchView searchView; private SearchView searchView;
public SidebarMainFragment() { private static final String HOSTNAME = "hostname";
}
public SidebarMainFragment() {}
/** /**
* create SidebarMainFragment with hostname. * create SidebarMainFragment with hostname.
...@@ -87,7 +83,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -87,7 +83,7 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
hostname = args == null ? null : args.getString(HOSTNAME); hostname = args == null ? null : args.getString(HOSTNAME);
methodCallHelper = new MethodCallHelper(getContext(), hostname); methodCallHelper = new MethodCallHelper(getContext(), hostname);
realmSpotlightRoomRepository = new RealmSpotlightRoomRepository(hostname); realmSpotlightRepository = new RealmSpotlightRepository(hostname);
RealmUserRepository userRepository = new RealmUserRepository(hostname); RealmUserRepository userRepository = new RealmUserRepository(hostname);
...@@ -133,6 +129,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -133,6 +129,8 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
setupLogoutButton(); setupLogoutButton();
setupVersionInfo(); setupVersionInfo();
searchView = rootView.findViewById(R.id.search);
adapter = new RoomListAdapter(); adapter = new RoomListAdapter();
adapter.setOnItemClickListener(new RoomListAdapter.OnItemClickListener() { adapter.setOnItemClickListener(new RoomListAdapter.OnItemClickListener() {
@Override @Override
...@@ -142,42 +140,35 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -142,42 +140,35 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
} }
@Override @Override
public void onItemClick(SpotlightRoom spotlightRoom) { public void onItemClick(Spotlight spotlight) {
searchView.setQuery(null, false); searchView.setQuery(null, false);
searchView.clearFocus(); searchView.clearFocus();
methodCallHelper.joinRoom(spotlightRoom.getId()) methodCallHelper.joinRoom(spotlight.getId())
.onSuccessTask(task -> { .onSuccessTask(task -> {
presenter.onSpotlightRoomSelected(spotlightRoom); presenter.onSpotlightSelected(spotlight);
return null; return null;
}); });
} }
}); });
RecyclerView recyclerView = (RecyclerView) rootView.findViewById(R.id.room_list_container); RecyclerView recyclerView = rootView.findViewById(R.id.room_list_container);
recyclerView.setLayoutManager( recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false));
recyclerView.setAdapter(adapter); recyclerView.setAdapter(adapter);
searchView = (SearchView) rootView.findViewById(R.id.search);
RxSearchView.queryTextChanges(searchView) RxSearchView.queryTextChanges(searchView)
.compose(bindToLifecycle()) .compose(bindToLifecycle())
.debounce(300, TimeUnit.MILLISECONDS) .debounce(100, TimeUnit.MILLISECONDS)
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.switchMap(it -> { .switchMap(charSequence -> {
if (it.length() == 0) { if (charSequence.length() == 0) {
adapter.setMode(RoomListAdapter.MODE_ROOM); adapter.setMode(RoomListAdapter.MODE_ROOM);
return Observable.just(Collections.<SpotlightRoom>emptyList()); return Observable.just(Collections.<Spotlight>emptyList());
} else {
adapter.setMode(RoomListAdapter.MODE_SPOTLIGHT);
final String queryString = charSequence.toString();
methodCallHelper.searchSpotlight(queryString);
return realmSpotlightRepository.getSuggestionsFor(queryString, SortDirection.ASC, 10).toObservable();
} }
adapter.setMode(RoomListAdapter.MODE_SPOTLIGHT_ROOM);
final String queryString = it.toString();
methodCallHelper.searchSpotlightRooms(queryString);
return realmSpotlightRoomRepository.getSuggestionsFor(queryString, SortDirection.DESC, 10)
.toObservable();
}) })
.subscribe( .subscribe(
this::showSearchSuggestions, this::showSearchSuggestions,
...@@ -185,6 +176,10 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -185,6 +176,10 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
); );
} }
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 =
...@@ -305,7 +300,4 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain ...@@ -305,7 +300,4 @@ public class SidebarMainFragment extends AbstractFragment implements SidebarMain
updateRoomListMode(user); updateRoomListMode(user);
} }
private void showSearchSuggestions(List<SpotlightRoom> spotlightRooms) {
adapter.setSpotlightRoomList(spotlightRooms);
}
} }
\ No newline at end of file
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