use of com.odysee.app.callable.Search in project odysee-android by OdyseeTeam.
the class FileViewFragment method loadRelatedContent.
private void loadRelatedContent() {
// reset the list view
View root = getView();
if (fileClaim != null && root != null) {
Context context = getContext();
List<Claim> loadingPlaceholders = new ArrayList<>();
int loadingPlaceholdersLength = Claim.TYPE_COLLECTION.equalsIgnoreCase(fileClaim.getValueType()) ? fileClaim.getClaimIds().size() : 15;
for (int i = 0; i < loadingPlaceholdersLength; i++) {
Claim placeholder = new Claim();
placeholder.setLoadingPlaceholder(true);
loadingPlaceholders.add(placeholder);
}
relatedContentAdapter = new ClaimListAdapter(loadingPlaceholders, context);
relatedContentAdapter.setContextGroupId(FILE_CONTEXT_GROUP_ID);
RecyclerView relatedContentList = root.findViewById(R.id.file_view_related_content_list);
relatedContentList.setAdapter(relatedContentAdapter);
ProgressBar relatedLoading = root.findViewById(R.id.file_view_related_content_progress);
boolean canShowMatureContent = false;
if (context != null) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
canShowMatureContent = sp.getBoolean(MainActivity.PREFERENCE_KEY_SHOW_MATURE_CONTENT, false);
}
if (!Claim.TYPE_COLLECTION.equalsIgnoreCase(fileClaim.getValueType())) {
String title = fileClaim.getTitle();
String claimId = fileClaim.getClaimId();
final boolean nsfw = canShowMatureContent;
relatedLoading.setVisibility(View.VISIBLE);
// Making a request which explicitly uses a certain value form the amount of results needed
// and no processing any possible exception, so using a callable instead of an AsyncTask
// makes sense for all Android API Levels
Thread t = new Thread(new Runnable() {
@Override
public void run() {
ExecutorService executor = Executors.newSingleThreadExecutor();
LighthouseSearch callable = new LighthouseSearch(title, RELATED_CONTENT_SIZE, 0, nsfw, claimId);
Future<List<Claim>> future = executor.submit(callable);
try {
List<Claim> result = future.get();
if (executor != null && !executor.isShutdown()) {
executor.shutdown();
}
MainActivity a = (MainActivity) getActivity();
if (a != null) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
relatedContentRequestSuccedded(result);
relatedLoading.setVisibility(View.GONE);
}
});
}
} catch (InterruptedException | ExecutionException e) {
if (executor != null && !executor.isShutdown()) {
executor.shutdown();
}
e.printStackTrace();
}
}
});
t.start();
} else {
TextView relatedOrPlayList = root.findViewById(R.id.related_or_playlist);
relatedOrPlayList.setText(fileClaim.getTitle());
relatedOrPlayList.setCompoundDrawablesRelativeWithIntrinsicBounds(R.drawable.ic_cast_connected, 0, 0, 0);
relatedOrPlayList.setPadding(0, 0, 0, 16);
relatedOrPlayList.setTypeface(null, Typeface.BOLD);
Map<String, Object> claimSearchOptions = new HashMap<>(3);
claimSearchOptions.put("claim_ids", fileClaim.getClaimIds());
claimSearchOptions.put("not_tags", canShowMatureContent ? null : new ArrayList<>(Predefined.MATURE_TAGS));
claimSearchOptions.put("page_size", fileClaim.getClaimIds().size());
ExecutorService executor = Executors.newSingleThreadExecutor();
Future<List<Claim>> future = executor.submit(new Search(claimSearchOptions));
try {
List<Claim> playlistClaimItems = future.get();
if (playlistClaimItems != null) {
relatedContentAdapter.setItems(playlistClaimItems);
relatedContentAdapter.setListener(FileViewFragment.this);
View v = getView();
if (v != null) {
relatedContentList.setAdapter(relatedContentAdapter);
relatedContentAdapter.notifyDataSetChanged();
Helper.setViewVisibility(v.findViewById(R.id.file_view_no_related_content), relatedContentAdapter == null || relatedContentAdapter.getItemCount() == 0 ? View.VISIBLE : View.GONE);
}
scrollToCommentHash();
}
} catch (InterruptedException | ExecutionException e) {
e.printStackTrace();
}
}
}
}
use of com.odysee.app.callable.Search in project odysee-android by OdyseeTeam.
the class SearchFragment method search.
public void search(String query, int from) {
boolean queryChanged = checkQuery(query);
if (query.equals("")) {
return;
}
if (!queryChanged && from > 0) {
currentFrom = from;
}
if (queryChanged) {
logSearch(query);
}
searchLoading = true;
Context context = getContext();
boolean canShowMatureContent = false;
if (context != null) {
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getContext());
canShowMatureContent = sp.getBoolean(MainActivity.PREFERENCE_KEY_SHOW_MATURE_CONTENT, false);
}
// modify the request so it returns channels on top
if (currentQuery != null) {
final String[] split = currentQuery.split(" ");
if (split.length == 1 && !currentQuery.startsWith("@")) {
currentQuery = "@".concat(query);
}
}
Activity a = getActivity();
if (a != null) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
loadingView.setVisibility(View.VISIBLE);
}
});
}
ExecutorService executor = Executors.newSingleThreadExecutor();
Callable<List<Claim>> c = new LighthouseSearch(currentQuery, PAGE_SIZE, currentFrom, canShowMatureContent, null);
Thread t = new Thread(new Runnable() {
@Override
public void run() {
Future<List<Claim>> future = executor.submit(c);
try {
List<Claim> results = future.get();
List<Claim> sanitizedClaims = new ArrayList<>(results.size());
for (Claim item : results) {
if (!item.getValueType().equalsIgnoreCase(Claim.TYPE_REPOST)) {
sanitizedClaims.add(item);
}
}
if (a != null) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
Context context = getContext();
if (context != null) {
if (resultListAdapter == null) {
resultListAdapter = new ClaimListAdapter(sanitizedClaims, context);
resultListAdapter.setContextGroupId(SEARCH_CONTEXT_GROUP_ID);
resultListAdapter.addFeaturedItem(buildFeaturedItem(query));
resolveFeaturedItem(buildVanityUrl(query));
resultListAdapter.setListener(SearchFragment.this);
if (resultList != null) {
resultList.setAdapter(resultListAdapter);
}
} else {
resultList.setVisibility(View.VISIBLE);
resultListAdapter.addItems(sanitizedClaims);
}
resultListAdapter.filterBlockedChannels(Lbryio.blockedChannels);
checkNothingToBeShown();
}
}
});
}
// Lighthouse doesn't return "valueType" of the claim, so another request is needed
// to determine if an item is a playlist and get the items on the playlist.
List<String> claimIds = new ArrayList<>();
for (Claim sanitizedClaim : sanitizedClaims) {
if (!sanitizedClaim.getValueType().equalsIgnoreCase(Claim.TYPE_CHANNEL)) {
claimIds.add(sanitizedClaim.getClaimId());
}
}
Map<String, Object> claimSearchOptions = new HashMap<>(2);
claimSearchOptions.put("claim_ids", claimIds);
claimSearchOptions.put("page_size", claimIds.size());
Future<List<Claim>> futureSearch = executor.submit(new Search(claimSearchOptions));
List<Claim> totalResults = futureSearch.get();
// For each claim returned from Lighthouse, replace it by the one using Search API
for (int i = 0; i < sanitizedClaims.size(); i++) {
if (!Claim.TYPE_CHANNEL.equalsIgnoreCase(sanitizedClaims.get(i).getValueType())) {
int finalI = i;
Claim found = totalResults.stream().filter(filteredClaim -> {
return sanitizedClaims.get(finalI).getClaimId().equalsIgnoreCase(filteredClaim.getClaimId());
}).findAny().orElse(null);
if (found != null) {
sanitizedClaims.set(i, found);
if (a != null && resultListAdapter != null) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
if (!found.getValueType().equalsIgnoreCase(Claim.TYPE_REPOST)) {
resultListAdapter.setItem(found.getClaimId(), found);
} else {
resultListAdapter.removeItem(found);
}
}
});
}
}
}
}
contentHasReachedEnd = results.size() < PAGE_SIZE;
searchLoading = false;
if (a != null) {
a.runOnUiThread(new Runnable() {
@Override
public void run() {
loadingView.setVisibility(View.GONE);
int itemCount = resultListAdapter == null ? 0 : resultListAdapter.getItemCount();
if (itemCount == 0) {
filterLink.setVisibility(View.GONE);
} else {
filterLink.setVisibility(View.VISIBLE);
}
}
});
}
} catch (ExecutionException | InterruptedException e) {
e.printStackTrace();
} finally {
if (!executor.isShutdown()) {
executor.shutdown();
}
}
}
});
t.start();
}
Aggregations