From 613634adcfe5b952d658d13cf598641a9df99999 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Mon, 25 May 2020 00:32:21 +0100 Subject: [PATCH] get transaction id for first_publish and new_channel rewards --- .../tasks/lbryinc/ClaimRewardTask.java | 38 ++++++++++++++++++- .../ui/findcontent/FileViewFragment.java | 11 +++++- 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/tasks/lbryinc/ClaimRewardTask.java b/app/src/main/java/io/lbry/browser/tasks/lbryinc/ClaimRewardTask.java index b9c32a77..cf2c9319 100644 --- a/app/src/main/java/io/lbry/browser/tasks/lbryinc/ClaimRewardTask.java +++ b/app/src/main/java/io/lbry/browser/tasks/lbryinc/ClaimRewardTask.java @@ -4,9 +4,12 @@ import android.content.Context; import android.os.AsyncTask; import android.view.View; +import org.json.JSONArray; +import org.json.JSONException; import org.json.JSONObject; import java.text.DecimalFormat; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -14,6 +17,8 @@ import io.lbry.browser.R; import io.lbry.browser.exceptions.ApiCallException; import io.lbry.browser.exceptions.LbryioRequestException; import io.lbry.browser.exceptions.LbryioResponseException; +import io.lbry.browser.model.Claim; +import io.lbry.browser.model.lbryinc.Reward; import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.Lbry; import io.lbry.browser.utils.Lbryio; @@ -43,6 +48,15 @@ public class ClaimRewardTask extends AsyncTask { public String doInBackground(Void... params) { String message = null; try { + String txid = null; + if (Reward.TYPE_FIRST_CHANNEL.equalsIgnoreCase(type)) { + // fetch a channel + txid = fetchSingleClaimTxid(Claim.TYPE_CHANNEL); + } else if (Reward.TYPE_FIRST_PUBLISH.equalsIgnoreCase(type)) { + // fetch a publish + txid = fetchSingleClaimTxid(Claim.TYPE_STREAM); + } + // Get a new wallet address for the reward String address = (String) Lbry.genericApiCall(Lbry.METHOD_ADDRESS_UNUSED); Map options = new HashMap<>(); @@ -50,8 +64,11 @@ public class ClaimRewardTask extends AsyncTask { options.put("wallet_address", address); if (!Helper.isNullOrEmpty(rewardCode)) { options.put("code", rewardCode); - } + if (!Helper.isNullOrEmpty(txid)) { + options.put("transaction_id", txid); + } + JSONObject reward = (JSONObject) Lbryio.parseResponse( Lbryio.call("reward", "claim", options, Helper.METHOD_POST, null)); amountClaimed = Helper.getJSONDouble("reward_amount", 0, reward); @@ -61,7 +78,7 @@ public class ClaimRewardTask extends AsyncTask { amountClaimed == 1 ? 1 : 2, new DecimalFormat(Helper.LBC_CURRENCY_FORMAT_PATTERN).format(amountClaimed)) : ""; message = Helper.getJSONString("reward_notification", defaultMessage, reward); - } catch (ApiCallException | LbryioRequestException | LbryioResponseException ex) { + } catch (ApiCallException | JSONException | LbryioRequestException | LbryioResponseException ex) { error = ex; } @@ -79,6 +96,23 @@ public class ClaimRewardTask extends AsyncTask { } } + private String fetchSingleClaimTxid(String claimType) throws ApiCallException, JSONException { + Map options = new HashMap<>(); + options.put("claim_type", claimType); + options.put("page", 1); + options.put("page_size", 1); + options.put("resolve", true); + + JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_CLAIM_LIST, options); + JSONArray items = result.getJSONArray("items"); + if (items.length() > 0) { + Claim claim = Claim.fromJSONObject(items.getJSONObject(0)); + return claim.getTxid(); + } + + return null; + } + public interface ClaimRewardHandler { void onSuccess(double amountClaimed, String message); void onError(Exception error); 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 37a9dd91..d82de0ad 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 @@ -12,6 +12,7 @@ import android.graphics.Color; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; +import android.os.Handler; import android.text.format.DateUtils; import android.view.ContextMenu; import android.view.LayoutInflater; @@ -1190,7 +1191,15 @@ public class FileViewFragment extends BaseFragment implements Claim.GenericMetadata metadata = claim.getValue(); if (!Helper.isNullOrEmpty(claim.getThumbnailUrl())) { ImageView thumbnailView = root.findViewById(R.id.file_view_thumbnail); - Glide.with(getContext().getApplicationContext()).load(claim.getThumbnailUrl()).centerCrop().into(thumbnailView); + new Handler().postDelayed(new Runnable() { + @Override + public void run() { + if (claim != null && context != null && thumbnailView != null) { + Glide.with(context.getApplicationContext()).load(claim.getThumbnailUrl()).centerCrop().into(thumbnailView); + } + } + }, 200); + } else { // display first x letters of claim name, with random background }