From f9de0d7937a31141a752bfb4a56758993905004b Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 1 Jun 2020 08:48:17 +0100 Subject: [PATCH] do not prompt again for content that is already purchased --- .../browser/tasks/claim/PurchaseListTask.java | 25 ++++++-- .../ui/findcontent/FileViewFragment.java | 59 +++++++++++++++---- 2 files changed, 68 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/tasks/claim/PurchaseListTask.java b/app/src/main/java/io/lbry/browser/tasks/claim/PurchaseListTask.java index 46037086..06b05ba2 100644 --- a/app/src/main/java/io/lbry/browser/tasks/claim/PurchaseListTask.java +++ b/app/src/main/java/io/lbry/browser/tasks/claim/PurchaseListTask.java @@ -18,12 +18,19 @@ import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.Lbry; public class PurchaseListTask extends AsyncTask> { + private String claimId; private int page; private int pageSize; private ClaimSearchResultHandler handler; private View progressView; private Exception error; + public PurchaseListTask(String claimId, View progressView, ClaimSearchResultHandler handler) { + this.claimId = claimId; + this.progressView = progressView; + this.handler = handler; + } + public PurchaseListTask(int page, int pageSize, View progressView, ClaimSearchResultHandler handler) { this.page = page; this.pageSize = pageSize; @@ -38,8 +45,15 @@ public class PurchaseListTask extends AsyncTask> { List claims = null; try { Map options = new HashMap<>(); - options.put("page", page); - options.put("page_size", pageSize); + if (!Helper.isNullOrEmpty(claimId)) { + options.put("claim_id", claimId); + } + if (page > 0) { + options.put("page", page); + } + if (pageSize > 0) { + options.put("page_size", pageSize); + } options.put("resolve", true); JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_PURCHASE_LIST, options); @@ -47,9 +61,10 @@ public class PurchaseListTask extends AsyncTask> { claims = new ArrayList<>(); for (int i = 0; i < items.length(); i++) { Claim claim = Claim.fromJSONObject(items.getJSONObject(i).getJSONObject("claim")); - claims.add(claim); - - Lbry.addClaimToCache(claim); + if (!Helper.isNullOrEmpty(claim.getClaimId())) { + claims.add(claim); + Lbry.addClaimToCache(claim); + } } } catch (ApiCallException | JSONException | ClassCastException ex) { error = ex; diff --git a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java index d1df29e8..589efdbe 100644 --- a/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/findcontent/FileViewFragment.java @@ -131,6 +131,7 @@ import io.lbry.browser.tasks.claim.AbandonStreamTask; import io.lbry.browser.tasks.claim.ClaimListResultHandler; import io.lbry.browser.tasks.claim.ClaimListTask; import io.lbry.browser.tasks.claim.ClaimSearchResultHandler; +import io.lbry.browser.tasks.claim.PurchaseListTask; import io.lbry.browser.tasks.claim.ResolveTask; import io.lbry.browser.tasks.file.DeleteFileTask; import io.lbry.browser.tasks.file.FileListTask; @@ -1624,29 +1625,65 @@ public class FileViewFragment extends BaseFragment implements // Check if the claim is free Claim.GenericMetadata metadata = claim.getValue(); if (metadata instanceof Claim.StreamMetadata) { - Claim.StreamMetadata streamMetadata = (Claim.StreamMetadata) metadata; + View root = getView(); + if (root != null) { + root.findViewById(R.id.file_view_main_action_button).setVisibility(View.INVISIBLE); + root.findViewById(R.id.file_view_main_action_loading).setVisibility(View.VISIBLE); + } if (claim.getFile() == null && !claim.isFree()) { - // TODO: also check ownership from purchase_list - // not free (and the user does not own the claim yet), perform a purchase - confirmPurchaseUrl(); - } else { - if (!claim.isPlayable() && !Lbry.SDK_READY) { + if (!Lbry.SDK_READY) { Snackbar.make(getView().findViewById(R.id.file_view_global_layout), R.string.sdk_initializing_functionality, Snackbar.LENGTH_LONG).show(); + restoreMainActionButton(); return; } - View root = getView(); - if (root != null) { - root.findViewById(R.id.file_view_main_action_button).setVisibility(View.INVISIBLE); - root.findViewById(R.id.file_view_main_action_loading).setVisibility(View.VISIBLE); - handleMainActionForClaim(); + checkAndConfirmPurchaseUrl(); + } else { + if (!claim.isPlayable() && !Lbry.SDK_READY) { + Snackbar.make(getView().findViewById(R.id.file_view_global_layout), R.string.sdk_initializing_functionality, Snackbar.LENGTH_LONG).show(); + restoreMainActionButton(); + return; } + + handleMainActionForClaim(); } } else { showError(getString(R.string.cannot_view_claim)); } } + private void checkAndConfirmPurchaseUrl() { + if (claim != null) { + PurchaseListTask task = new PurchaseListTask(claim.getClaimId(), null, new ClaimSearchResultHandler() { + @Override + public void onSuccess(List claims, boolean hasReachedEnd) { + boolean purchased = false; + if (claims.size() == 1) { + Claim purchasedClaim = claims.get(0); + if (claim.getClaimId().equalsIgnoreCase(purchasedClaim.getClaimId())) { + // already purchased + purchased = true; + } + } + + if (purchased) { + handleMainActionForClaim(); + } else { + restoreMainActionButton(); + confirmPurchaseUrl(); + } + } + + @Override + public void onError(Exception error) { + // pass + restoreMainActionButton(); + } + }); + task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } + } + private void confirmPurchaseUrl() { if (claim != null) { Fee fee = ((Claim.StreamMetadata) claim.getValue()).getFee();