Skip to content

Commit

Permalink
Abandon channels and bulk delete files tasks. Some visual tweaks.
Browse files Browse the repository at this point in the history
  • Loading branch information
akinwale committed May 18, 2020
1 parent a42442b commit 5eb35e3
Show file tree
Hide file tree
Showing 17 changed files with 380 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/io/lbry/browser/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -823,7 +823,7 @@ private void toggleUrlSuggestions(boolean visible) {
closeIcon.setVisibility(visible ? View.VISIBLE : View.GONE);
}

private int getScaledValue(int value) {
public int getScaledValue(int value) {
float scale = getResources().getDisplayMetrics().density;
return (int) (value * scale + 0.5f);
}
Expand Down
23 changes: 23 additions & 0 deletions app/src/main/java/io/lbry/browser/adapter/ClaimListAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import java.util.List;
import java.util.Map;

import io.lbry.browser.MainActivity;
import io.lbry.browser.R;
import io.lbry.browser.listener.SelectionModeListener;
import io.lbry.browser.model.Claim;
Expand Down Expand Up @@ -54,6 +55,7 @@ public class ClaimListAdapter extends RecyclerView.Adapter<ClaimListAdapter.View
private boolean inSelectionMode;
@Setter
private SelectionModeListener selectionModeListener;
private float scale;

public ClaimListAdapter(List<Claim> items, Context context) {
this.context = context;
Expand All @@ -63,6 +65,9 @@ public ClaimListAdapter(List<Claim> items, Context context) {
quickClaimUrlMap = new HashMap<>();
notFoundClaimIdMap = new HashMap<>();
notFoundClaimUrlMap = new HashMap<>();
if (context != null) {
scale = context.getResources().getDisplayMetrics().density;
}
}

public List<Claim> getSelectedItems() {
Expand All @@ -87,6 +92,10 @@ public Claim getFeaturedItem() {
return null;
}

public List<Claim> getItems() {
return new ArrayList<>(this.items);
}

public void clearItems() {
clearSelectedItems();
this.items.clear();
Expand Down Expand Up @@ -122,6 +131,11 @@ public void setItems(List<Claim> claims) {
notifyDataSetChanged();
}

public void removeItems(List<Claim> claims) {
items.removeAll(claims);
notifyDataSetChanged();
}

public void removeItem(Claim claim) {
items.remove(claim);
selectedItems.remove(claim);
Expand Down Expand Up @@ -261,9 +275,18 @@ public ClaimListAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int view
return new ClaimListAdapter.ViewHolder(v);
}

public int getScaledValue(int value) {
return (int) (value * scale + 0.5f);
}

@Override
public void onBindViewHolder(ClaimListAdapter.ViewHolder vh, int position) {
int type = getItemViewType(position);
int paddingTop = position == 0 ? 16 : 8;
int paddingBottom = position == getItemCount() - 1 ? 16 : 8;
int paddingTopScaled = getScaledValue(paddingTop);
int paddingBottomScaled = getScaledValue(paddingBottom);
vh.itemView.setPadding(vh.itemView.getPaddingLeft(), paddingTopScaled, vh.itemView.getPaddingRight(), paddingBottomScaled);

Claim original = items.get(position);
boolean isRepost = Claim.TYPE_REPOST.equalsIgnoreCase(original.getValueType());
Expand Down
15 changes: 14 additions & 1 deletion app/src/main/java/io/lbry/browser/model/lbryinc/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,19 @@ public class User {
private String primaryEmail;
private String rewardStatusChangeTrigger;
private String updatedAt;
private List<String> youtubeChannels;
private List<YoutubeChannel> youtubeChannels;
private List<String> deviceTypes;

@Data
public static class YoutubeChannel {
String ytChannelName;
String lbryChannelName;
String channelClaimId;
String syncStatus;
String statusToken;
boolean transferable;
String transferState;
List<String> publishToAddress;
String publicKey;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package io.lbry.browser.tasks.claim;

import android.os.AsyncTask;
import android.view.View;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.lbry.browser.exceptions.ApiCallException;
import io.lbry.browser.tasks.GenericTaskHandler;
import io.lbry.browser.utils.Helper;
import io.lbry.browser.utils.Lbry;

public class AbandonChannelTask extends AsyncTask<Void, Void, Boolean> {
private List<String> claimIds;
private List<String> successfulClaimIds;
private List<String> failedClaimIds;
private List<Exception> failedExceptions;
private View progressView;
private AbandonHandler handler;

public AbandonChannelTask(List<String> claimIds, View progressView, AbandonHandler handler) {
this.claimIds = claimIds;
this.progressView = progressView;
this.handler = handler;
}

protected void onPreExecute() {
Helper.setViewVisibility(progressView, View.VISIBLE);
}

public Boolean doInBackground(Void... params) {
successfulClaimIds = new ArrayList<>();
failedClaimIds = new ArrayList<>();
failedExceptions = new ArrayList<>();

for (String claimId : claimIds) {
try {
Map<String, Object> options = new HashMap<>();
options.put("claim_id", claimId);
options.put("blocking", false);
JSONObject result = (JSONObject) Lbry.genericApiCall(Lbry.METHOD_CHANNEL_ABANDON, options);
successfulClaimIds.add(claimId);
} catch (ApiCallException ex) {
failedClaimIds.add(claimId);
failedExceptions.add(ex);
}
}

return true;
}

protected void onPostExecute(Boolean result) {
Helper.setViewVisibility(progressView, View.GONE);
if (handler != null) {
handler.onComplete(successfulClaimIds, failedClaimIds, failedExceptions);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package io.lbry.browser.tasks.claim;

import java.util.List;

public interface AbandonHandler {
void onComplete(List<String> successfulClaimIds, List<String> failedClaimIds, List<Exception> errors);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package io.lbry.browser.tasks.file;

import android.os.AsyncTask;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import io.lbry.browser.exceptions.ApiCallException;
import io.lbry.browser.tasks.GenericTaskHandler;
import io.lbry.browser.utils.Lbry;

// Just run delete on the specified claim IDs (no need for a handler)
public class BulkDeleteFilesTask extends AsyncTask<Void, Void, Boolean> {
private List<String> claimIds;
public BulkDeleteFilesTask(List<String> claimIds) {
this.claimIds = claimIds;
}
protected Boolean doInBackground(Void... params) {
for (String claimId : claimIds) {
try {
Map<String, Object> options = new HashMap<>();
options.put("claim_id", claimId);
options.put("delete_from_download_dir", true);
Lbry.genericApiCall(Lbry.METHOD_FILE_DELETE, options);
} catch (ApiCallException ex) {
// pass
}
}
return true;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ protected Boolean doInBackground(Void... params) {
options.put("delete_from_download_dir", true);
return (boolean) Lbry.genericApiCall(Lbry.METHOD_FILE_DELETE, options);
} catch (ApiCallException ex) {
error = ex;
return false;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
Expand All @@ -19,6 +20,7 @@
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;

import java.util.ArrayList;
import java.util.HashMap;
Expand All @@ -32,6 +34,8 @@
import io.lbry.browser.listener.SelectionModeListener;
import io.lbry.browser.model.Claim;
import io.lbry.browser.model.NavMenuItem;
import io.lbry.browser.tasks.claim.AbandonChannelTask;
import io.lbry.browser.tasks.claim.AbandonHandler;
import io.lbry.browser.tasks.claim.ClaimListResultHandler;
import io.lbry.browser.tasks.claim.ClaimListTask;
import io.lbry.browser.ui.BaseFragment;
Expand Down Expand Up @@ -144,7 +148,7 @@ private void fetchChannels() {
ClaimListTask task = new ClaimListTask(Claim.TYPE_CHANNEL, getLoading(), new ClaimListResultHandler() {
@Override
public void onSuccess(List<Claim> claims) {
Lbry.ownChannels = new ArrayList<>(claims);
Lbry.ownChannels = Helper.filterDeletedClaims(new ArrayList<>(claims));
Context context = getContext();
if (adapter == null) {
adapter = new ClaimListAdapter(claims, context);
Expand Down Expand Up @@ -260,7 +264,7 @@ public boolean onActionItemClicked(androidx.appcompat.view.ActionMode actionMode
.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
//handleDeleteSelectedClaims(selectedClaims);
handleDeleteSelectedClaims(selectedClaims);
}
}).setNegativeButton(R.string.no, null);
builder.show();
Expand All @@ -270,4 +274,48 @@ public void onClick(DialogInterface dialogInterface, int i) {

return false;
}

private void handleDeleteSelectedClaims(List<Claim> selectedClaims) {
List<String> claimIds = new ArrayList<>();

for (Claim claim : selectedClaims) {
claimIds.add(claim.getClaimId());
}

if (actionMode != null) {
actionMode.finish();
}

Helper.setViewVisibility(channelList, View.INVISIBLE);
Helper.setViewVisibility(fabNewChannel, View.INVISIBLE);
AbandonChannelTask task = new AbandonChannelTask(claimIds, bigLoading, new AbandonHandler() {
@Override
public void onComplete(List<String> successfulClaimIds, List<String> failedClaimIds, List<Exception> errors) {
View root = getView();
if (root != null) {
if (failedClaimIds.size() > 0) {
Snackbar.make(root, R.string.one_or_more_channels_failed_abandon, Snackbar.LENGTH_LONG).
setBackgroundTint(Color.RED).setTextColor(Color.WHITE).show();
} else if (successfulClaimIds.size() == claimIds.size()) {
try {
String message = getResources().getQuantityString(R.plurals.channels_deleted, successfulClaimIds.size());
Snackbar.make(root, message, Snackbar.LENGTH_LONG).show();
} catch (IllegalStateException ex) {
// pass
}
}
}

Lbry.abandonedClaimIds.addAll(successfulClaimIds);
if (adapter != null) {
adapter.setItems(Helper.filterDeletedClaims(adapter.getItems()));
}

Helper.setViewVisibility(channelList, View.VISIBLE);
Helper.setViewVisibility(fabNewChannel, View.VISIBLE);
checkNoChannels();
}
});
task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ private void checkParams() {
}

resetViewCount();
resetFee();
checkNewClaimAndUrl(newClaim, newUrl);

if (newClaim != null) {
Expand Down Expand Up @@ -364,6 +365,7 @@ private void onNewClaim(String url) {
currentUrl = url;
logUrlEvent(url);
resetViewCount();
resetFee();
View root = getView();
if (root != null) {
((RecyclerView) root.findViewById(R.id.file_view_related_content_list)).setAdapter(null);
Expand Down Expand Up @@ -431,6 +433,7 @@ public void onError(Exception error) {

public void openClaimUrl(String url) {
resetViewCount();
resetFee();
currentUrl = url;

ClaimCacheKey key = new ClaimCacheKey();
Expand Down Expand Up @@ -508,6 +511,7 @@ private void setPlayerForPlayerView() {
View root = getView();
if (root != null) {
PlayerView view = root.findViewById(R.id.file_view_exoplayer_view);
view.setPlayer(null);
view.setPlayer(MainActivity.appPlayer);
}
}
Expand Down Expand Up @@ -896,6 +900,10 @@ public void onSuccess() {
getView().findViewById(R.id.file_view_action_download).setVisibility(View.VISIBLE);
getView().findViewById(R.id.file_view_unsupported_container).setVisibility(View.GONE);
actionDelete.setEnabled(true);

claim.setFile(null);
Lbry.unsetFilesForCachedClaims(Arrays.asList(claim.getClaimId()));

restoreMainActionButton();
}

Expand Down Expand Up @@ -1135,9 +1143,20 @@ private void setCurrentPlayer(Player currentPlayer) {
}

private void resetViewCount() {
TextView textViewCount = getView().findViewById(R.id.file_view_view_count);
Helper.setViewText(textViewCount, null);
Helper.setViewVisibility(textViewCount, View.GONE);
View root = getView();
if (root != null) {
TextView textViewCount = root.findViewById(R.id.file_view_view_count);
Helper.setViewText(textViewCount, null);
Helper.setViewVisibility(textViewCount, View.GONE);
}
}
private void resetFee() {
View root = getView();
if (root != null) {
TextView feeView = root.findViewById(R.id.file_view_fee);
feeView.setText(null);
Helper.setViewVisibility(root.findViewById(R.id.file_view_fee_container), View.GONE);
}
}

private void loadViewCount() {
Expand Down
Loading

0 comments on commit 5eb35e3

Please sign in to comment.