use of org.matrix.androidsdk.rest.model.search.SearchResult in project matrix-android-sdk by matrix-org.
the class MatrixMessageListFragment method onSearchResponse.
/**
* Manage the search response.
*
* @param searchResponse the search response
* @param onSearchResultListener the search result listener
*/
protected void onSearchResponse(final SearchResponse searchResponse, final OnSearchResultListener onSearchResultListener) {
List<SearchResult> searchResults = searchResponse.searchCategories.roomEvents.results;
ArrayList<MessageRow> messageRows = new ArrayList<>(searchResults.size());
for (SearchResult searchResult : searchResults) {
RoomState roomState = null;
if (null != mRoom) {
roomState = mRoom.getState();
}
if (null == roomState) {
Room room = mSession.getDataHandler().getStore().getRoom(searchResult.result.roomId);
if (null != room) {
roomState = room.getState();
}
}
boolean isValidMessage = false;
if ((null != searchResult.result) && (null != searchResult.result.getContent())) {
JsonObject object = searchResult.result.getContentAsJsonObject();
if (null != object) {
isValidMessage = (0 != object.entrySet().size());
}
}
if (isValidMessage) {
messageRows.add(new MessageRow(searchResult.result, roomState));
}
}
Collections.reverse(messageRows);
mAdapter.clear();
mAdapter.addAll(messageRows);
mNextBatch = searchResponse.searchCategories.roomEvents.nextBatch;
if (null != onSearchResultListener) {
try {
onSearchResultListener.onSearchSucceed(messageRows.size());
} catch (Exception e) {
Log.e(LOG_TAG, "onSearchResponse failed with " + e.getMessage());
}
}
}
use of org.matrix.androidsdk.rest.model.search.SearchResult in project matrix-android-sdk by matrix-org.
the class MatrixMessageListFragment method requestSearchHistory.
/**
* Search the pattern on a pagination server side.
*/
public void requestSearchHistory() {
// there is no more server message
if (TextUtils.isEmpty(mNextBatch)) {
mIsBackPaginating = false;
return;
}
mIsBackPaginating = true;
final int firstPos = mMessageListView.getFirstVisiblePosition();
final String fPattern = mPattern;
final int countBeforeUpdate = mAdapter.getCount();
showLoadingBackProgress();
List<String> roomIds = null;
if (null != mRoom) {
roomIds = Arrays.asList(mRoom.getRoomId());
}
ApiCallback<SearchResponse> callback = new ApiCallback<SearchResponse>() {
@Override
public void onSuccess(final SearchResponse searchResponse) {
// check that the pattern was not modified before the end of the search
if (TextUtils.equals(mPattern, fPattern)) {
List<SearchResult> searchResults = searchResponse.searchCategories.roomEvents.results;
// is there any result to display
if (0 != searchResults.size()) {
mAdapter.setNotifyOnChange(false);
for (SearchResult searchResult : searchResults) {
MessageRow row = new MessageRow(searchResult.result, (null == mRoom) ? null : mRoom.getState());
mAdapter.insert(row, 0);
}
mNextBatch = searchResponse.searchCategories.roomEvents.nextBatch;
// Scroll the list down to where it was before adding rows to the top
getUiHandler().post(new Runnable() {
@Override
public void run() {
final int expectedFirstPos = firstPos + (mAdapter.getCount() - countBeforeUpdate);
// trick to avoid that the list jump to the latest item.
mMessageListView.lockSelectionOnResize();
mAdapter.notifyDataSetChanged();
// do not use count because some messages are not displayed
// so we compute the new pos
mMessageListView.setSelection(expectedFirstPos);
mMessageListView.post(new Runnable() {
@Override
public void run() {
mIsBackPaginating = false;
// fill the history
if (mMessageListView.getFirstVisiblePosition() <= 2) {
requestSearchHistory();
}
}
});
}
});
} else {
mIsBackPaginating = false;
}
hideLoadingBackProgress();
}
}
private void onError() {
mIsBackPaginating = false;
hideLoadingBackProgress();
}
// the request will be auto restarted when a valid network will be found
@Override
public void onNetworkError(Exception e) {
Log.e(LOG_TAG, "Network error: " + e.getMessage());
onError();
}
@Override
public void onMatrixError(MatrixError e) {
Log.e(LOG_TAG, "Matrix error" + " : " + e.errcode + " - " + e.getMessage());
onError();
}
@Override
public void onUnexpectedError(Exception e) {
Log.e(LOG_TAG, "onUnexpectedError error" + e.getMessage());
onError();
}
};
if (mIsMediaSearch) {
mSession.searchMediasByName(mPattern, roomIds, mNextBatch, callback);
} else {
mSession.searchMessagesByText(mPattern, roomIds, mNextBatch, callback);
}
}
Aggregations