Skip to content

Commit

Permalink
Implement proper search
Browse files Browse the repository at this point in the history
Signed-off-by: Mario Danic <mario@lovelyhq.com>
  • Loading branch information
mario committed May 18, 2018
1 parent 3806f35 commit c306600
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 30 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ android {
targetSdkVersion 27
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

versionCode 35
versionName "1.2.0beta2"
versionCode 36
versionName "1.2.0beta3"

flavorDimensions "default"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ public class ContactsController extends BaseController implements SearchView.OnQ
private BottomSheet bottomSheet;
private View view;
private int currentPage;
private int currentSearchPage;

private SmoothScrollLinearLayoutManager layoutManager;

Expand All @@ -162,6 +163,7 @@ public class ContactsController extends BaseController implements SearchView.OnQ

private boolean alreadyFetching = false;
private boolean canFetchFurther = true;
private boolean canFetchSearchFurther = true;

public ContactsController() {
super();
Expand Down Expand Up @@ -224,7 +226,6 @@ protected void onViewBound(@NonNull View view) {
if (currentUser != null) {
fetchData(true);
}

}

setupAdapter();
Expand All @@ -237,6 +238,15 @@ private void setupAdapter() {

adapter.setEndlessScrollListener(this, new ProgressItem());

adapter.registerAdapterDataObserver(new RecyclerView.AdapterDataObserver() {
@Override
public void onChanged() {
super.onChanged();
adapter.filterItems();
adapter.onLoadMoreComplete(null);
}
});

adapter.setStickyHeaderElevation(5)
.setUnlinkAllItemsOnRemoveHeaders(true)
.setDisplayHeadersAtStartUp(true)
Expand Down Expand Up @@ -429,19 +439,26 @@ private void fetchData(boolean startFromScratch) {
alreadyFetching = true;
Set<Sharee> shareeHashSet = new HashSet<>();

if (startFromScratch) {
contactItems = new ArrayList<>();
}

userHeaderItems = new HashMap<>();


String query = "";
if (searchView != null && !TextUtils.isEmpty(searchView.getQuery())) {
query = searchView.getQuery().toString();
} else if (startFromScratch) {
contactItems = new ArrayList<>();
}

RetrofitBucket retrofitBucket = ApiUtils.getRetrofitBucketForContactsSearch(currentUser.getBaseUrl(),
"");
query);

int page = 1;
if (!startFromScratch) {
page = currentPage + 1;
if (TextUtils.isEmpty(query)) {
page = currentPage + 1;
} else {
page = currentSearchPage + 1;
}
}

Map<String, Object> modifiedQueryMap = new HashMap<>(retrofitBucket.getQueryMap());
Expand All @@ -461,13 +478,9 @@ public void onSubscribe(Disposable d) {

@Override
public void onNext(Response response) {
canFetchFurther = response.headers().size() > 0 &&
!TextUtils.isEmpty((response.headers().get("Link")));
if (response.body() != null) {
ShareesOverall shareesOverall = (ShareesOverall) response.body();

currentPage = (int) modifiedQueryMap.get("page");

if (shareesOverall.getOcs().getData().getUsers() != null) {
shareeHashSet.addAll(shareesOverall.getOcs().getData().getUsers());
}
Expand All @@ -478,7 +491,19 @@ public void onNext(Response response) {
getExactUsers().getExactSharees());
}

if (TextUtils.isEmpty((CharSequence) modifiedQueryMap.get("search"))) {
canFetchFurther = !shareeHashSet.isEmpty();
currentPage = (int) modifiedQueryMap.get("page");
} else {
canFetchSearchFurther = !shareeHashSet.isEmpty();
currentSearchPage = (int) modifiedQueryMap.get("page");
}


Participant participant;

List<AbstractFlexibleItem> newUserItemList = new ArrayList<>();
newUserItemList.addAll(contactItems);
for (Sharee sharee : shareeHashSet) {
if (!sharee.getValue().getShareWith().equals(currentUser.getUsername())) {
participant = new Participant();
Expand All @@ -499,17 +524,23 @@ public void onNext(Response response) {
userHeaderItems.get(headerTitle));

if (!contactItems.contains(newContactItem)) {
contactItems.add(newContactItem);
newUserItemList.add(newContactItem);
}

}

}


boolean shouldFilterManually = false;
if (newUserItemList.size() == contactItems.size()) {
shouldFilterManually = true;
}

contactItems = newUserItemList;
userHeaderItems = new HashMap<>();

Collections.sort(contactItems, (o1, o2) -> {
Collections.sort(newUserItemList, (o1, o2) -> {
String firstName;
String secondName;

Expand All @@ -528,16 +559,15 @@ public void onNext(Response response) {
return firstName.compareToIgnoreCase(secondName);
});

if (startFromScratch) {
adapter.updateDataSet(contactItems, true);

if (!shouldFilterManually) {
adapter.updateDataSet(newUserItemList, false);
} else {
adapter.filterItems();
adapter.onLoadMoreComplete(null);
adapter = new FlexibleAdapter<>(contactItems, getActivity(), false);
recyclerView.setAdapter(adapter);
setupAdapter();
adapter.notifyDataSetChanged();
}
searchItem.setVisible(contactItems.size() > 0);

searchItem.setVisible(newUserItemList.size() > 0);
swipeRefreshLayout.setRefreshing(false);


Expand Down Expand Up @@ -661,11 +691,15 @@ public boolean onQueryTextChange(String newText) {

if (!TextUtils.isEmpty(searchQuery)) {
adapter.setFilter(searchQuery);
searchQuery = "";
adapter.filterItems();
searchQuery = "";
} else {
adapter.setFilter(newText);
adapter.filterItems(300);
if (TextUtils.isEmpty(newText)) {
adapter.filterItems();
} else {
fetchData(true);
}
}
}

Expand Down Expand Up @@ -852,15 +886,17 @@ public void noMoreLoad(int newItemsSize) {

@Override
public void onLoadMore(int lastPosition, int currentPage) {
if (adapter.hasFilter()) {
adapter.onLoadMoreComplete(null);
return;
String query = "";

if (searchView != null && !TextUtils.isEmpty(searchView.getQuery())) {
query = searchView.getQuery().toString();
}

if (!alreadyFetching && canFetchFurther) {
if (!alreadyFetching && ((searchView != null && searchView.isIconified() && canFetchFurther)
|| (!TextUtils.isEmpty(query) && canFetchSearchFurther))) {
fetchData(false);
} else {
return;
adapter.onLoadMoreComplete(null);
}
}
}
5 changes: 4 additions & 1 deletion app/src/main/java/com/nextcloud/talk/utils/ApiUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,15 @@ public static String getUserAgent() {
return userAgent + BuildConfig.VERSION_NAME;
}

public static RetrofitBucket getRetrofitBucketForContactsSearch(String baseUrl, String searchQuery) {
public static RetrofitBucket getRetrofitBucketForContactsSearch(String baseUrl, @Nullable String searchQuery) {
RetrofitBucket retrofitBucket = new RetrofitBucket();
retrofitBucket.setUrl(baseUrl + ocsApiVersion + "/apps/files_sharing/api/v1/sharees");

Map<String, String> queryMap = new HashMap<>();

if (searchQuery == null) {
searchQuery = "";
}
queryMap.put("format", "json");
queryMap.put("search", searchQuery);
queryMap.put("itemType", "call");
Expand Down

0 comments on commit c306600

Please sign in to comment.