Search in sources :

Example 1 with PeerSearchEntity

use of im.actor.core.entity.PeerSearchEntity in project actor-platform by actorapp.

the class GlobalSearchSource method loadGlobalResults.

private void loadGlobalResults(String query, ArrayList<SearchResult> localResults, Consumer<List<SearchResult>> callback) {
    context().getSearchModule().findPeers(query).then(r -> {
        ArrayList<SearchResult> results = new ArrayList<>();
        outer: for (PeerSearchEntity peerSearch : r) {
            for (SearchResult l : localResults) {
                if (peerSearch.getPeer().equals(l.getPeer())) {
                    continue outer;
                }
            }
            if (peerSearch.getPeer().getPeerType() == PeerType.GROUP) {
                Group group = context().getGroupsModule().getGroups().getValue(peerSearch.getPeer().getPeerId());
                results.add(new SearchResult(peerSearch.getPeer(), group.getAvatar(), group.getTitle(), peerSearch.getOptMatchString()));
            } else if (peerSearch.getPeer().getPeerType() == PeerType.PRIVATE) {
                UserVM user = context().getUsersModule().getUsers().get(peerSearch.getPeer().getPeerId());
                results.add(new SearchResult(peerSearch.getPeer(), user.getAvatar().get(), user.getName().get(), peerSearch.getOptMatchString()));
            }
        }
        if (results.size() > 0) {
            ArrayList<SearchResult> combined = new ArrayList<>();
            combined.addAll(localResults);
            combined.addAll(results);
            callback.apply(combined);
        }
    });
}
Also used : Group(im.actor.core.entity.Group) PeerSearchEntity(im.actor.core.entity.PeerSearchEntity) UserVM(im.actor.core.viewmodel.UserVM) ArrayList(java.util.ArrayList) SearchResult(im.actor.core.entity.SearchResult)

Example 2 with PeerSearchEntity

use of im.actor.core.entity.PeerSearchEntity in project actor-platform by actorapp.

the class GlobalSearchBaseFragment method onCreateOptionsMenu.

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.fragment_global_search, menu);
    searchMenu = menu.findItem(R.id.search);
    if (messenger().getAppState().getIsAppEmpty().get()) {
        searchMenu.setVisible(false);
    } else {
        searchMenu.setVisible(true);
    }
    searchView = (SearchView) searchMenu.getActionView();
    searchView.setIconifiedByDefault(true);
    MenuItemCompat.setOnActionExpandListener(searchMenu, new MenuItemCompat.OnActionExpandListener() {

        @Override
        public boolean onMenuItemActionExpand(MenuItem item) {
            showSearch();
            return true;
        }

        @Override
        public boolean onMenuItemActionCollapse(MenuItem item) {
            hideSearch();
            return true;
        }
    });
    searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

        @Override
        public boolean onQueryTextSubmit(String s) {
            return false;
        }

        @Override
        public boolean onQueryTextChange(String s) {
            searchQuery = s.trim();
            if (isSearchVisible) {
                if (s.trim().length() > 0) {
                    String activeSearchQuery = searchQuery;
                    searchDisplay.initSearch(s.trim().toLowerCase(), false);
                    scrolledToEnd = false;
                    searchAdapter.setQuery(s.trim().toLowerCase());
                    globalSearchResults.clear();
                    messenger().findPeers(s).start(new CommandCallback<List<PeerSearchEntity>>() {

                        @Override
                        public void onResult(List<PeerSearchEntity> res) {
                            if (searchQuery.equals(activeSearchQuery)) {
                                int order = 0;
                                outer: for (PeerSearchEntity pse : res) {
                                    for (int i = 0; i < searchDisplay.getSize(); i++) {
                                        if (searchDisplay.getItem(i).getPeer().equals(pse.getPeer())) {
                                            continue outer;
                                        }
                                    }
                                    Avatar avatar;
                                    Peer peer = pse.getPeer();
                                    String name;
                                    if (peer.getPeerType() == PeerType.PRIVATE) {
                                        UserVM userVM = users().get(peer.getPeerId());
                                        name = userVM.getName().get();
                                        avatar = userVM.getAvatar().get();
                                    } else if (peer.getPeerType() == PeerType.GROUP) {
                                        GroupVM groupVM = groups().get(peer.getPeerId());
                                        name = groupVM.getName().get();
                                        avatar = groupVM.getAvatar().get();
                                    } else {
                                        continue;
                                    }
                                    String optMatchString = pse.getOptMatchString();
                                    globalSearchResults.add(new SearchEntity(pse.getPeer(), order++, avatar, optMatchString == null ? name : optMatchString));
                                }
                                if (globalSearchResults.size() > 0) {
                                    globalSearchResults.add(new SearchEntityHeader(order++));
                                }
                                checkGlobalSearch();
                                onSearchChanged();
                            }
                        }

                        @Override
                        public void onError(Exception e) {
                        }
                    });
                } else {
                    searchDisplay.initEmpty();
                }
            }
            return false;
        }
    });
}
Also used : MenuItemCompat(android.support.v4.view.MenuItemCompat) GroupVM(im.actor.core.viewmodel.GroupVM) Peer(im.actor.core.entity.Peer) MenuItem(android.view.MenuItem) Avatar(im.actor.core.entity.Avatar) PeerSearchEntity(im.actor.core.entity.PeerSearchEntity) UserVM(im.actor.core.viewmodel.UserVM) SearchView(android.support.v7.widget.SearchView) SearchEntity(im.actor.core.entity.SearchEntity) PeerSearchEntity(im.actor.core.entity.PeerSearchEntity) ArrayList(java.util.ArrayList) BindedDisplayList(im.actor.runtime.generic.mvvm.BindedDisplayList) List(java.util.List) DisplayList(im.actor.runtime.generic.mvvm.DisplayList) CommandCallback(im.actor.core.viewmodel.CommandCallback)

Aggregations

PeerSearchEntity (im.actor.core.entity.PeerSearchEntity)2 UserVM (im.actor.core.viewmodel.UserVM)2 ArrayList (java.util.ArrayList)2 MenuItemCompat (android.support.v4.view.MenuItemCompat)1 SearchView (android.support.v7.widget.SearchView)1 MenuItem (android.view.MenuItem)1 Avatar (im.actor.core.entity.Avatar)1 Group (im.actor.core.entity.Group)1 Peer (im.actor.core.entity.Peer)1 SearchEntity (im.actor.core.entity.SearchEntity)1 SearchResult (im.actor.core.entity.SearchResult)1 CommandCallback (im.actor.core.viewmodel.CommandCallback)1 GroupVM (im.actor.core.viewmodel.GroupVM)1 BindedDisplayList (im.actor.runtime.generic.mvvm.BindedDisplayList)1 DisplayList (im.actor.runtime.generic.mvvm.DisplayList)1 List (java.util.List)1