123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638 |
- /*
- * Nextcloud Talk application
- *
- * @author Mario Danic
- * Copyright (C) 2017 Mario Danic (mario@lovelyhq.com)
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation, either version 3 of the License, or
- * at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- package com.nextcloud.talk.controllers;
- import android.app.SearchManager;
- import android.content.Context;
- import android.content.Intent;
- import android.os.Bundle;
- import android.os.Handler;
- import android.support.annotation.NonNull;
- import android.support.annotation.Nullable;
- import android.support.design.widget.BottomNavigationView;
- import android.support.v4.view.MenuItemCompat;
- import android.support.v4.widget.SwipeRefreshLayout;
- import android.support.v7.widget.DividerItemDecoration;
- import android.support.v7.widget.RecyclerView;
- import android.support.v7.widget.SearchView;
- import android.text.InputType;
- import android.text.TextUtils;
- import android.view.LayoutInflater;
- import android.view.Menu;
- import android.view.MenuInflater;
- import android.view.MenuItem;
- import android.view.View;
- import android.view.ViewGroup;
- import android.view.ViewTreeObserver;
- import android.view.inputmethod.EditorInfo;
- import android.widget.Button;
- import android.widget.LinearLayout;
- import com.bluelinelabs.conductor.RouterTransaction;
- import com.bluelinelabs.conductor.changehandler.HorizontalChangeHandler;
- import com.bluelinelabs.conductor.changehandler.VerticalChangeHandler;
- import com.bluelinelabs.conductor.internal.NoOpControllerChangeHandler;
- import com.nextcloud.talk.R;
- import com.nextcloud.talk.activities.CallActivity;
- import com.nextcloud.talk.adapters.items.EmptyFooterItem;
- import com.nextcloud.talk.adapters.items.NewCallHeaderItem;
- import com.nextcloud.talk.adapters.items.UserHeaderItem;
- import com.nextcloud.talk.adapters.items.UserItem;
- import com.nextcloud.talk.api.NcApi;
- import com.nextcloud.talk.application.NextcloudTalkApplication;
- import com.nextcloud.talk.controllers.base.BaseController;
- import com.nextcloud.talk.models.RetrofitBucket;
- import com.nextcloud.talk.models.database.UserEntity;
- import com.nextcloud.talk.models.json.participants.Participant;
- import com.nextcloud.talk.models.json.rooms.RoomOverall;
- import com.nextcloud.talk.models.json.sharees.Sharee;
- import com.nextcloud.talk.models.json.sharees.ShareesOverall;
- import com.nextcloud.talk.utils.ApiUtils;
- import com.nextcloud.talk.utils.bundle.BundleKeys;
- import com.nextcloud.talk.utils.database.user.UserUtils;
- import org.parceler.Parcels;
- import java.util.ArrayList;
- import java.util.Collections;
- import java.util.HashMap;
- import java.util.HashSet;
- import java.util.List;
- import java.util.Set;
- import javax.inject.Inject;
- import autodagger.AutoInjector;
- import butterknife.BindView;
- import butterknife.OnClick;
- import butterknife.Optional;
- import eu.davidea.fastscroller.FastScroller;
- import eu.davidea.flexibleadapter.FlexibleAdapter;
- import eu.davidea.flexibleadapter.SelectableAdapter;
- import eu.davidea.flexibleadapter.common.SmoothScrollLinearLayoutManager;
- import eu.davidea.flexibleadapter.items.AbstractFlexibleItem;
- import eu.davidea.flexibleadapter.items.IFlexible;
- import eu.davidea.flipview.FlipView;
- import io.reactivex.Observer;
- import io.reactivex.android.schedulers.AndroidSchedulers;
- import io.reactivex.disposables.Disposable;
- import io.reactivex.schedulers.Schedulers;
- import retrofit2.HttpException;
- @AutoInjector(NextcloudTalkApplication.class)
- public class ContactsController extends BaseController implements SearchView.OnQueryTextListener,
- FlexibleAdapter.OnItemClickListener, FastScroller.OnScrollStateChangeListener {
- public static final String TAG = "ContactsController";
- private static final String KEY_SEARCH_QUERY = "ContactsController.searchQuery";
- @Inject
- UserUtils userUtils;
- @Inject
- NcApi ncApi;
- @BindView(R.id.recycler_view)
- RecyclerView recyclerView;
- @BindView(R.id.swipe_refresh_layout)
- SwipeRefreshLayout swipeRefreshLayout;
- @Nullable
- @BindView(R.id.bottom_buttons_layout)
- LinearLayout bottomButtonsLinearLayout;
- @BindView(R.id.fast_scroller)
- FastScroller fastScroller;
- @Nullable
- @BindView(R.id.clear_button)
- Button clearButton;
- private UserEntity userEntity;
- private Disposable contactsQueryDisposable;
- private Disposable cacheQueryDisposable;
- private FlexibleAdapter adapter;
- private List<AbstractFlexibleItem> contactItems = new ArrayList<>();
- private SmoothScrollLinearLayoutManager layoutManager;
- private MenuItem searchItem;
- private SearchView searchView;
- private String searchQuery;
- private boolean isNewConversationView;
- private boolean isPublicCall;
- private HashMap<String, UserHeaderItem> userHeaderItems = new HashMap<String, UserHeaderItem>();
- public ContactsController() {
- super();
- setHasOptionsMenu(true);
- }
- public ContactsController(Bundle args) {
- super(args);
- setHasOptionsMenu(true);
- if (args.containsKey(BundleKeys.KEY_NEW_CONVERSATION)) {
- isNewConversationView = true;
- }
- }
- @Override
- protected View inflateView(@NonNull LayoutInflater inflater, @NonNull ViewGroup container) {
- return inflater.inflate(R.layout.controller_generic_rv, container, false);
- }
- @Override
- protected void onAttach(@NonNull View view) {
- super.onAttach(view);
- if (isNewConversationView) {
- checkAndHandleBottomButtons();
- if (getActionBar() != null) {
- getActionBar().setDisplayHomeAsUpEnabled(true);
- }
- }
- }
- @Override
- protected void onViewBound(@NonNull View view) {
- super.onViewBound(view);
- NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
- FlipView.resetLayoutAnimationDelay(true, 1000L);
- FlipView.stopLayoutAnimation();
- userEntity = userUtils.getCurrentUser();
- if (userEntity == null) {
- if (getParentController().getRouter() != null) {
- getParentController().getRouter().setRoot((RouterTransaction.with(new ServerSelectionController())
- .pushChangeHandler(new HorizontalChangeHandler())
- .popChangeHandler(new HorizontalChangeHandler())));
- }
- }
- if (adapter == null) {
- adapter = new FlexibleAdapter<>(contactItems, getActivity(), false);
- adapter.setNotifyChangeOfUnfilteredItems(true)
- .setMode(SelectableAdapter.Mode.MULTI);
- if (userEntity != null) {
- fetchData();
- }
- }
- adapter.setStickyHeaderElevation(5)
- .setUnlinkAllItemsOnRemoveHeaders(true)
- .setDisplayHeadersAtStartUp(true)
- .setStickyHeaders(true);
- adapter.addListener(this);
- prepareViews();
- }
- @Optional
- @OnClick(R.id.clear_button)
- public void onClearButtonClick() {
- if (adapter != null) {
- List<Integer> selectedPositions = adapter.getSelectedPositions();
- for (Integer position : selectedPositions) {
- if (adapter.getItem(position) instanceof UserItem) {
- UserItem userItem = (UserItem) adapter.getItem(position);
- adapter.toggleSelection(position);
- if (userItem != null) {
- userItem.flipItemSelection();
- }
- }
- }
- }
- checkAndHandleBottomButtons();
- }
- @Optional
- @OnClick(R.id.done_button)
- public void onDoneButtonClick() {
- }
- private void initSearchView() {
- if (getActivity() != null) {
- SearchManager searchManager = (SearchManager) getActivity().getSystemService(Context.SEARCH_SERVICE);
- if (searchItem != null) {
- searchView = (SearchView) MenuItemCompat.getActionView(searchItem);
- searchView.setMaxWidth(Integer.MAX_VALUE);
- searchView.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER);
- searchView.setImeOptions(EditorInfo.IME_ACTION_DONE | EditorInfo.IME_FLAG_NO_FULLSCREEN);
- searchView.setQueryHint(getResources().getString(R.string.nc_search));
- if (searchManager != null) {
- searchView.setSearchableInfo(searchManager.getSearchableInfo(getActivity().getComponentName()));
- }
- searchView.setOnQueryTextListener(this);
- }
- }
- final View mSearchEditFrame = searchView
- .findViewById(android.support.v7.appcompat.R.id.search_edit_frame);
- BottomNavigationView bottomNavigationView = null;
- if (getParentController() != null && getParentController().getView() != null) {
- bottomNavigationView = getParentController().getView().findViewById(R.id.navigation);
- }
- Handler handler = new Handler();
- ViewTreeObserver vto = mSearchEditFrame.getViewTreeObserver();
- BottomNavigationView finalBottomNavigationView = bottomNavigationView;
- vto.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
- int oldVisibility = -1;
- @Override
- public void onGlobalLayout() {
- int currentVisibility = mSearchEditFrame.getVisibility();
- if (currentVisibility != oldVisibility) {
- if (currentVisibility == View.VISIBLE) {
- if (finalBottomNavigationView != null) {
- handler.postDelayed(() -> finalBottomNavigationView.setVisibility(View.GONE), 100);
- }
- } else {
- handler.postDelayed(() -> {
- if (finalBottomNavigationView != null) {
- finalBottomNavigationView.setVisibility(View.VISIBLE);
- }
- searchItem.setVisible(contactItems.size() > 0);
- }, 500);
- }
- oldVisibility = currentVisibility;
- }
- }
- });
- }
- @Override
- public boolean onOptionsItemSelected(@NonNull MenuItem item) {
- switch (item.getItemId()) {
- case android.R.id.home:
- getRouter().popCurrentController();
- return true;
- default:
- return super.onOptionsItemSelected(item);
- }
- }
- @Override
- public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
- super.onCreateOptionsMenu(menu, inflater);
- inflater.inflate(R.menu.menu_conversation_plus_filter, menu);
- searchItem = menu.findItem(R.id.action_search);
- menu.findItem(R.id.action_new_conversation).setVisible(false);
- initSearchView();
- }
- @Override
- public void onPrepareOptionsMenu(Menu menu) {
- super.onPrepareOptionsMenu(menu);
- searchItem.setVisible(contactItems.size() > 0);
- if (adapter.hasSearchText()) {
- searchItem.expandActionView();
- searchView.setQuery(adapter.getSearchText(), false);
- }
- }
- private void fetchData() {
- dispose(null);
- Set<Sharee> shareeHashSet = new HashSet<>();
- contactItems = new ArrayList<>();
- userHeaderItems = new HashMap<>();
- RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForContactsSearch(userEntity.getBaseUrl(),
- "");
- contactsQueryDisposable = ncApi.getContactsWithSearchParam(
- ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()),
- retrofitBucket.getUrl(), retrofitBucket.getQueryMap())
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe((ShareesOverall shareesOverall) -> {
- if (shareesOverall != null) {
- if (shareesOverall.getOcs().getData().getUsers() != null) {
- shareeHashSet.addAll(shareesOverall.getOcs().getData().getUsers());
- }
- if (shareesOverall.getOcs().getData().getExactUsers() != null &&
- shareesOverall.getOcs().getData().getExactUsers().getExactSharees() != null) {
- shareeHashSet.addAll(shareesOverall.getOcs().getData().
- getExactUsers().getExactSharees());
- }
- Participant participant;
- for (Sharee sharee : shareeHashSet) {
- if (!sharee.getValue().getShareWith().equals(userEntity.getUsername())) {
- participant = new Participant();
- participant.setName(sharee.getLabel());
- String headerTitle;
- headerTitle = sharee.getLabel().substring(0, 1).toUpperCase();
- UserHeaderItem userHeaderItem;
- if (!userHeaderItems.containsKey(headerTitle)) {
- userHeaderItem = new UserHeaderItem(headerTitle);
- userHeaderItems.put(headerTitle, userHeaderItem);
- }
- participant.setUserId(sharee.getValue().getShareWith());
- contactItems.add(new UserItem(participant, userEntity,
- userHeaderItems.get(headerTitle)));
- }
- }
- userHeaderItems = new HashMap<>();
- Collections.sort(contactItems, (o1, o2) -> {
- String firstName;
- String secondName;
- if (o1 instanceof UserItem) {
- firstName = ((UserItem) o1).getModel().getName();
- } else {
- firstName = ((UserHeaderItem) o1).getModel();
- }
- if (o2 instanceof UserItem) {
- secondName = ((UserItem) o2).getModel().getName();
- } else {
- secondName = ((UserHeaderItem) o2).getModel();
- }
- return firstName.compareToIgnoreCase(secondName);
- });
- if (isNewConversationView) {
- contactItems.add(0, new NewCallHeaderItem());
- }
- adapter.updateDataSet(contactItems, true);
- searchItem.setVisible(contactItems.size() > 0);
- swipeRefreshLayout.setRefreshing(false);
- if (isNewConversationView) {
- checkAndHandleBottomButtons();
- }
- }
- }, throwable -> {
- if (searchItem != null) {
- searchItem.setVisible(false);
- }
- if (throwable instanceof HttpException) {
- HttpException exception = (HttpException) throwable;
- switch (exception.code()) {
- case 401:
- if (getParentController() != null &&
- getParentController().getRouter() != null) {
- getParentController().getRouter().pushController((RouterTransaction.with
- (new WebViewLoginController(userEntity.getBaseUrl(),
- true))
- .pushChangeHandler(new VerticalChangeHandler())
- .popChangeHandler(new VerticalChangeHandler())));
- }
- break;
- default:
- break;
- }
- }
- swipeRefreshLayout.setRefreshing(false);
- dispose(contactsQueryDisposable);
- }
- , () -> {
- swipeRefreshLayout.setRefreshing(false);
- dispose(contactsQueryDisposable);
- });
- }
- private void prepareViews() {
- layoutManager = new SmoothScrollLinearLayoutManager(getActivity());
- recyclerView.setLayoutManager(layoutManager);
- recyclerView.setHasFixedSize(true);
- recyclerView.setAdapter(adapter);
- recyclerView.addItemDecoration(new DividerItemDecoration(
- recyclerView.getContext(),
- layoutManager.getOrientation()
- ));
- swipeRefreshLayout.setOnRefreshListener(this::fetchData);
- swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimary);
- fastScroller.addOnScrollStateChangeListener(this);
- adapter.setFastScroller(fastScroller);
- fastScroller.setBubbleTextCreator(position -> {
- IFlexible abstractFlexibleItem = adapter.getItem(position);
- if (abstractFlexibleItem instanceof UserItem) {
- return ((UserItem) adapter.getItem(position)).getHeader().getModel();
- } else {
- return ((UserHeaderItem) adapter.getItem(position)).getModel();
- }
- });
- }
- private void dispose(@Nullable Disposable disposable) {
- if (disposable != null && !disposable.isDisposed()) {
- disposable.dispose();
- } else if (disposable == null) {
- if (contactsQueryDisposable != null && !contactsQueryDisposable.isDisposed()) {
- contactsQueryDisposable.dispose();
- contactsQueryDisposable = null;
- }
- if (cacheQueryDisposable != null && !cacheQueryDisposable.isDisposed()) {
- cacheQueryDisposable.dispose();
- cacheQueryDisposable = null;
- }
- }
- }
- @Override
- public void onSaveViewState(@NonNull View view, @NonNull Bundle outState) {
- adapter.onSaveInstanceState(outState);
- super.onSaveViewState(view, outState);
- if (searchView != null && !TextUtils.isEmpty(searchView.getQuery())) {
- outState.putString(KEY_SEARCH_QUERY, searchView.getQuery().toString());
- }
- }
- @Override
- public void onRestoreViewState(@NonNull View view, @NonNull Bundle savedViewState) {
- super.onRestoreViewState(view, savedViewState);
- searchQuery = savedViewState.getString(KEY_SEARCH_QUERY, "");
- if (adapter != null) {
- adapter.onRestoreInstanceState(savedViewState);
- }
- }
- @Override
- public void onDestroy() {
- super.onDestroy();
- dispose(null);
- }
- @Override
- public boolean onQueryTextChange(String newText) {
- if (adapter.hasNewSearchText(newText) || !TextUtils.isEmpty(searchQuery)) {
- if (!TextUtils.isEmpty(searchQuery)) {
- adapter.setSearchText(searchQuery);
- searchQuery = "";
- adapter.filterItems();
- } else {
- adapter.setSearchText(newText);
- adapter.filterItems(300);
- }
- }
- if (swipeRefreshLayout != null) {
- swipeRefreshLayout.setEnabled(!adapter.hasSearchText());
- }
- return true;
- }
- @Override
- public boolean onQueryTextSubmit(String query) {
- return onQueryTextChange(query);
- }
- @Override
- public boolean onItemClick(int position) {
- if (adapter.getItem(position) instanceof UserItem) {
- if (!isNewConversationView) {
- UserItem userItem = (UserItem) adapter.getItem(position);
- RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForCreateRoom(userEntity.getBaseUrl(), "1",
- userItem.getModel().getUserId());
- ncApi.createRoom(ApiUtils.getCredentials(userEntity.getUsername(), userEntity.getToken()),
- retrofitBucket.getUrl(), retrofitBucket.getQueryMap())
- .subscribeOn(Schedulers.newThread())
- .observeOn(AndroidSchedulers.mainThread())
- .subscribe(new Observer<RoomOverall>() {
- @Override
- public void onSubscribe(Disposable d) {
- }
- @Override
- public void onNext(RoomOverall roomOverall) {
- overridePushHandler(new NoOpControllerChangeHandler());
- overridePopHandler(new NoOpControllerChangeHandler());
- Intent callIntent = new Intent(getActivity(), CallActivity.class);
- Bundle bundle = new Bundle();
- bundle.putString(BundleKeys.KEY_ROOM_TOKEN, roomOverall.getOcs().getData().getToken());
- bundle.putParcelable(BundleKeys.KEY_USER_ENTITY, Parcels.wrap(userEntity));
- callIntent.putExtras(bundle);
- startActivity(callIntent);
- }
- @Override
- public void onError(Throwable e) {
- }
- @Override
- public void onComplete() {
- }
- });
- } else {
- ((UserItem) adapter.getItem(position)).flipItemSelection();
- adapter.toggleSelection(position);
- checkAndHandleBottomButtons();
- }
- } else if (adapter.getItem(position) instanceof NewCallHeaderItem) {
- adapter.toggleSelection(position);
- isPublicCall = adapter.isSelected(position);
- ((NewCallHeaderItem) adapter.getItem(position)).togglePublicCall(isPublicCall);
- checkAndHandleBottomButtons();
- }
- return true;
- }
- private void checkAndHandleBottomButtons() {
- if (adapter != null && bottomButtonsLinearLayout != null && clearButton != null) {
- if (adapter.getSelectedItemCount() > 0 || isPublicCall) {
- if (bottomButtonsLinearLayout.getVisibility() != View.VISIBLE) {
- bottomButtonsLinearLayout.setVisibility(View.VISIBLE);
- }
- if (isPublicCall && adapter.getSelectedItemCount() < 2) {
- clearButton.setVisibility(View.GONE);
- } else {
- clearButton.setVisibility(View.VISIBLE);
- }
- } else {
- bottomButtonsLinearLayout.setVisibility(View.GONE);
- }
- } else if (bottomButtonsLinearLayout != null) {
- bottomButtonsLinearLayout.setVisibility(View.GONE);
- }
- if (bottomButtonsLinearLayout != null && bottomButtonsLinearLayout.getVisibility() == View.VISIBLE) {
- if (adapter.getScrollableFooters().size() == 0) {
- adapter.addScrollableFooterWithDelay(new EmptyFooterItem(999), 0, layoutManager
- .findLastVisibleItemPosition() == adapter.getItemCount() - 1);
- }
- } else {
- if (adapter != null) {
- adapter.removeAllScrollableFooters();
- }
- }
- }
- @Override
- protected String getTitle() {
- if (!isNewConversationView) {
- return getResources().getString(R.string.nc_app_name);
- } else {
- return getResources().getString(R.string.nc_select_contacts);
- }
- }
- @Override
- public void onFastScrollerStateChange(boolean scrolling) {
- swipeRefreshLayout.setEnabled(!scrolling);
- }
- }
|