From c82e9e2ad270f841988fd22b2128aa40f3e86ea6 Mon Sep 17 00:00:00 2001 From: Thomas Date: Thu, 3 Sep 2020 18:57:10 +0200 Subject: [PATCH] Change displayed videos when clicking a followed account --- .../fedilabtube/ShowAccountActivity.java | 17 +++---- .../asynctasks/RetrieveFeedsAsyncTask.java | 17 ++++++- .../RetrieveRelationshipAsyncTask.java | 15 +++--- .../fedilabtube/client/PeertubeAPI.java | 45 +++++++++++++--- .../fedilabtube/client/entities/Account.java | 9 ++++ .../drawer/AccountsHorizontalListAdapter.java | 51 +++++++++++++------ .../fedilabtube/drawer/PeertubeAdapter.java | 2 +- .../fragment/DisplayStatusFragment.java | 41 +++++++++------ .../OnRetrieveRelationshipInterface.java | 4 +- .../main/res/layout/activity_show_account.xml | 14 ++--- .../res/layout/drawer_horizontal_account.xml | 1 + app/src/main/res/layout/fragment_video.xml | 17 ++++++- app/src/main/res/values/strings.xml | 3 +- 13 files changed, 167 insertions(+), 69 deletions(-) diff --git a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java index b5f822e..0eaccfd 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java +++ b/app/src/main/java/app/fedilab/fedilabtube/ShowAccountActivity.java @@ -25,7 +25,7 @@ import android.text.SpannableString; import android.text.method.LinkMovementMethod; import android.view.View; import android.view.ViewGroup; -import android.widget.ImageButton; +import android.widget.Button; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; @@ -79,7 +79,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi private List statuses; private StatusListAdapter statusListAdapter; - private ImageButton account_follow; + private Button account_follow; private ViewPager mPager; private TabLayout tabLayout; private TextView account_note; @@ -93,7 +93,7 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi private Account account; private String accountId; private boolean ischannel; - private AsyncTask retrieveRelationship; + private AsyncTask> retrieveRelationship; private action doAction; @Override @@ -353,16 +353,15 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi } @Override - public void onRetrieveRelationship(Relationship relationship, Error error) { + public void onRetrieveRelationship(List relationships, Error error) { if (error != null) { Toasty.error(ShowAccountActivity.this, error.getError(), Toast.LENGTH_LONG).show(); return; } - this.relationship = relationship; + this.relationship = relationships.get(0); manageButtonVisibility(); - //The authenticated account is followed by the account if (relationship != null && relationship.isFollowed_by() && !accountId.equals(userId)) { TextView account_followed_by = findViewById(R.id.account_followed_by); @@ -395,26 +394,24 @@ public class ShowAccountActivity extends AppCompatActivity implements OnPostActi } account_follow.setEnabled(true); if (relationship.isFollowing()) { - account_follow.setImageResource(R.drawable.ic_user_minus); + account_follow.setText(R.string.action_unfollow); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { account_follow.setBackgroundTintList(ColorStateList.valueOf(ContextCompat.getColor(ShowAccountActivity.this, R.color.red_1))); } doAction = action.UNFOLLOW; - account_follow.setContentDescription(getString(R.string.action_unfollow)); if (ischannel) { account_follow.setVisibility(View.VISIBLE); } else { account_follow.setVisibility(View.GONE); } } else if (!relationship.isFollowing()) { - account_follow.setImageResource(R.drawable.ic_user_plus); + account_follow.setText(R.string.action_follow); doAction = action.FOLLOW; if (ischannel) { account_follow.setVisibility(View.VISIBLE); } else { account_follow.setVisibility(View.GONE); } - account_follow.setContentDescription(getString(R.string.action_follow)); } else { account_follow.setVisibility(View.GONE); doAction = action.NOTHING; diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java index 87f34a7..5a2b670 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java +++ b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveFeedsAsyncTask.java @@ -38,7 +38,7 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { private OnRetrieveFeedsInterface listener; private WeakReference contextReference; private String target; - + private String forAccount; public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { this.contextReference = new WeakReference<>(context); @@ -56,6 +56,15 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { this.target = target; } + public RetrieveFeedsAsyncTask(Context context, Type action, String max_id, String target, String forAccount, OnRetrieveFeedsInterface onRetrieveFeedsInterface) { + this.contextReference = new WeakReference<>(context); + this.action = action; + this.max_id = max_id; + this.listener = onRetrieveFeedsInterface; + this.target = target; + this.forAccount = forAccount; + } + @Override protected Void doInBackground(Void... params) { @@ -88,7 +97,11 @@ public class RetrieveFeedsAsyncTask extends AsyncTask { break; case PSUBSCRIPTIONS: peertubeAPI = new PeertubeAPI(this.contextReference.get()); - apiResponse = peertubeAPI.getSubscriptionsTL(max_id); + if (forAccount == null) { + apiResponse = peertubeAPI.getSubscriptionsTL(max_id); + } else { + apiResponse = peertubeAPI.getVideosChannel(forAccount, max_id); + } break; case POVERVIEW: peertubeAPI = new PeertubeAPI(this.contextReference.get()); diff --git a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java index e4b50ef..2f6de3c 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java +++ b/app/src/main/java/app/fedilab/fedilabtube/asynctasks/RetrieveRelationshipAsyncTask.java @@ -18,6 +18,7 @@ import android.content.Context; import android.os.AsyncTask; import java.lang.ref.WeakReference; +import java.util.List; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Error; @@ -25,11 +26,10 @@ import app.fedilab.fedilabtube.client.entities.Relationship; import app.fedilab.fedilabtube.interfaces.OnRetrieveRelationshipInterface; -public class RetrieveRelationshipAsyncTask extends AsyncTask { +public class RetrieveRelationshipAsyncTask extends AsyncTask> { private String accountId; - private Relationship relationship; private OnRetrieveRelationshipInterface listener; private Error error; private WeakReference contextReference; @@ -41,18 +41,17 @@ public class RetrieveRelationshipAsyncTask extends AsyncTask { } @Override - protected Void doInBackground(Void... params) { + protected List doInBackground(Void... params) { PeertubeAPI api = new PeertubeAPI(this.contextReference.get()); - relationship = new Relationship(); - relationship.setFollowing(api.isFollowing(accountId)); + List relationships = api.isFollowing(accountId); error = api.getError(); - return null; + return relationships; } @Override - protected void onPostExecute(Void result) { - listener.onRetrieveRelationship(relationship, error); + protected void onPostExecute(List relationships) { + listener.onRetrieveRelationship(relationships, error); } } diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java index d219235..8d0eaef 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/PeertubeAPI.java @@ -56,6 +56,7 @@ import app.fedilab.fedilabtube.client.entities.PeertubeNotification; import app.fedilab.fedilabtube.client.entities.PeertubeVideoNotification; import app.fedilab.fedilabtube.client.entities.Playlist; import app.fedilab.fedilabtube.client.entities.PlaylistElement; +import app.fedilab.fedilabtube.client.entities.Relationship; import app.fedilab.fedilabtube.client.entities.Status; import app.fedilab.fedilabtube.helper.Helper; import app.fedilab.fedilabtube.sqlite.AccountDAO; @@ -897,27 +898,37 @@ public class PeertubeAPI { return account; } + + public List isFollowing(String uri) { + ArrayList uris = new ArrayList<>(); + uris.add(uri); + return isFollowing(uris); + } + /** * Returns a relationship between the authenticated account and an account * - * @param uri String accounts fetched + * @param uris Array String accounts uri fetched * @return Relationship entity */ - public boolean isFollowing(String uri) { + public List isFollowing(ArrayList uris) { HashMap params = new HashMap<>(); - - params.put("uris", uri); + StringBuilder parameters = new StringBuilder(); + for (String uri : uris) + parameters.append("uris[]=").append(uri).append("&"); + parameters = new StringBuilder(parameters.substring(0, parameters.length() - 1).substring(7)); + params.put("uris[]", parameters.toString()); try { HttpsConnection httpsConnection = new HttpsConnection(context); String response = httpsConnection.get(getAbsoluteUrl("/users/me/subscriptions/exist"), 60, params, prefKeyOauthTokenT); - return new JSONObject(response).getBoolean(uri); + return parseRelationShip(uris, new JSONObject(response)); } catch (HttpsConnection.HttpsConnectionException e) { setError(e.getStatusCode(), e); } catch (NoSuchAlgorithmException | IOException | KeyManagementException | JSONException e) { e.printStackTrace(); } - return false; + return null; } /** @@ -1749,6 +1760,28 @@ public class PeertubeAPI { return apiResponse; } + /** + * Parse json response for peertube relationship + * + * @param jsonObject JSONObject + * @return List + */ + private List parseRelationShip(ArrayList uris, JSONObject jsonObject) { + List relationships = new ArrayList<>(); + try { + for (String uri : uris) { + Relationship relationship = new Relationship(); + relationship.setId(uri); + relationship.setFollowing(jsonObject.getBoolean(uri)); + relationships.add(relationship); + } + return relationships; + } catch (JSONException e) { + setDefaultError(e); + } + return null; + } + /** * Parse json response for peertube notifications * diff --git a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Account.java b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Account.java index 922adae..da16d54 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/client/entities/Account.java +++ b/app/src/main/java/app/fedilab/fedilabtube/client/entities/Account.java @@ -80,6 +80,7 @@ public class Account implements Parcelable { private String invite_request; private String created_by_application_id; private String invited_by_account_id; + private boolean isSelected; public Account() { } @@ -216,6 +217,14 @@ public class Account implements Parcelable { this.muting_notifications = muting_notifications; } + public boolean isSelected() { + return isSelected; + } + + public void setSelected(boolean selected) { + isSelected = selected; + } + public String getHost() { return host; diff --git a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java index eea0fd6..aa4dab2 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java +++ b/app/src/main/java/app/fedilab/fedilabtube/drawer/AccountsHorizontalListAdapter.java @@ -15,22 +15,23 @@ package app.fedilab.fedilabtube.drawer; * see . */ import android.content.Context; -import android.content.Intent; -import android.os.Bundle; +import android.graphics.Color; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; +import android.widget.LinearLayout; import android.widget.TextView; import android.widget.Toast; import androidx.annotation.NonNull; +import androidx.core.content.ContextCompat; +import androidx.core.graphics.ColorUtils; import androidx.recyclerview.widget.RecyclerView; import java.util.List; import app.fedilab.fedilabtube.R; -import app.fedilab.fedilabtube.ShowAccountActivity; import app.fedilab.fedilabtube.client.PeertubeAPI; import app.fedilab.fedilabtube.client.entities.Account; import app.fedilab.fedilabtube.client.entities.Error; @@ -41,14 +42,15 @@ import es.dmoral.toasty.Toasty; public class AccountsHorizontalListAdapter extends RecyclerView.Adapter implements OnPostActionInterface { + EventListener listener; private List accounts; - private Context context; private AccountsHorizontalListAdapter accountsListAdapter; - public AccountsHorizontalListAdapter(List accounts) { + public AccountsHorizontalListAdapter(List accounts, EventListener listener) { this.accounts = accounts; this.accountsListAdapter = this; + this.listener = listener; } @NonNull @@ -71,16 +73,12 @@ public class AccountsHorizontalListAdapter extends RecyclerView.Adapter { - Intent intent = new Intent(context, ShowAccountActivity.class); - Bundle b = new Bundle(); - b.putBoolean("peertubeaccount", true); - b.putBoolean("ischannel", true); - b.putString("targetedid", account.getAcct()); - b.putParcelable("account", account); - intent.putExtras(b); - context.startActivity(intent); - }); + + if (account.isSelected()) { + holder.main_container.setBackgroundColor(ColorUtils.setAlphaComponent(ContextCompat.getColor(context, R.color.colorAccent), 50)); + } else { + holder.main_container.setBackgroundColor(Color.TRANSPARENT); + } } @@ -145,15 +143,36 @@ public class AccountsHorizontalListAdapter extends RecyclerView.Adapter { + forAccount = null; + pullToRefresh(); + }); return rootView; } @@ -311,6 +318,7 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter if (apiResponse != null && apiResponse.getAccounts() != null && apiResponse.getAccounts().size() > 0) { if (lv_accounts.getVisibility() == View.GONE) { lv_accounts.setVisibility(View.VISIBLE); + display_all.setVisibility(View.VISIBLE); } int previousPosition = accounts.size(); accounts.addAll(apiResponse.getAccounts()); @@ -396,17 +404,15 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter public void pullToRefresh() { - if (peertubes.size() > 0) { - int size = peertubes.size(); - peertubes.clear(); - peertubes = new ArrayList<>(); - max_id = "0"; - peertubeAdapater.notifyItemRangeRemoved(0, size); - if (search_peertube == null) { //Not a Peertube search - asyncTask = new RetrieveFeedsAsyncTask(context, type, "0", targetedId, DisplayStatusFragment.this).execute(); - } else { - asyncTask = new RetrievePeertubeSearchAsyncTask(context, "0", search_peertube, DisplayStatusFragment.this).execute(); - } + int size = peertubes.size(); + peertubes.clear(); + peertubes = new ArrayList<>(); + max_id = "0"; + peertubeAdapater.notifyItemRangeRemoved(0, size); + if (search_peertube == null) { //Not a Peertube search + asyncTask = new RetrieveFeedsAsyncTask(context, type, "0", targetedId, forAccount, DisplayStatusFragment.this).execute(); + } else { + asyncTask = new RetrievePeertubeSearchAsyncTask(context, "0", search_peertube, DisplayStatusFragment.this).execute(); } } @@ -416,6 +422,11 @@ public class DisplayStatusFragment extends Fragment implements OnPostActionInter } + @Override + public void click(String forAccount) { + this.forAccount = forAccount; + pullToRefresh(); + } static class GridSpacingItemDecoration extends RecyclerView.ItemDecoration { diff --git a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java b/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java index 1f226d5..959f461 100644 --- a/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java +++ b/app/src/main/java/app/fedilab/fedilabtube/interfaces/OnRetrieveRelationshipInterface.java @@ -14,9 +14,11 @@ package app.fedilab.fedilabtube.interfaces; * You should have received a copy of the GNU General Public License along with TubeLab; if not, * see . */ +import java.util.List; + import app.fedilab.fedilabtube.client.entities.Error; import app.fedilab.fedilabtube.client.entities.Relationship; public interface OnRetrieveRelationshipInterface { - void onRetrieveRelationship(Relationship relationship, Error error); + void onRetrieveRelationship(List relationships, Error error); } diff --git a/app/src/main/res/layout/activity_show_account.xml b/app/src/main/res/layout/activity_show_account.xml index dd0e764..9a6e974 100644 --- a/app/src/main/res/layout/activity_show_account.xml +++ b/app/src/main/res/layout/activity_show_account.xml @@ -55,7 +55,7 @@ android:layout_marginEnd="10dp" android:contentDescription="@string/go_back" android:src="@drawable/ic_baseline_arrow_back_24" - android:tint="@android:color/white" + app:tint="@android:color/white" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> @@ -121,11 +121,11 @@ - - + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintRight_toLeftOf="@+id/display_all" + /> +