Display Channel

This commit is contained in:
stom79 2018-10-22 17:09:25 +02:00
parent 269d7f6b55
commit f605fdda95
4 changed files with 100 additions and 24 deletions

View File

@ -25,6 +25,7 @@ import android.graphics.drawable.Drawable;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.TabLayout;
@ -113,7 +114,6 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
private StatusListAdapter statusListAdapter;
private FloatingActionButton account_follow;
private static final int NUM_PAGES = 3;
private ViewPager mPager;
private String accountId;
private TabLayout tabLayout;
@ -599,18 +599,20 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
}
}
});
mPager = findViewById(R.id.account_viewpager);
if( !peertubeAccount) {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.toots)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.following)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.followers)));
mPager.setOffscreenPageLimit(3);
}else {
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.videos)));
tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.channels)));
mPager.setOffscreenPageLimit(2);
}
mPager = findViewById(R.id.account_viewpager);
mPager.setOffscreenPageLimit(3);
PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
@ -858,7 +860,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
.load(account.getAvatar())
.into(new SimpleTarget<Bitmap>() {
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
public void onResourceReady(@NonNull Bitmap resource, Transition<? super Bitmap> transition) {
RoundedBitmapDrawable circularBitmapDrawable = RoundedBitmapDrawableFactory.create(getResources(), Helper.addBorder(resource, account_pp.getContext()));
circularBitmapDrawable.setCircular(true);
account_pp.setImageDrawable(circularBitmapDrawable);
@ -994,6 +996,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
* Pager adapter for the 4 fragments
*/
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
@ -1019,13 +1022,13 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
}
case 1:
if( peertubeAccount){
DisplayStatusFragment displayStatusFragment = new DisplayStatusFragment();
bundle = new Bundle();
bundle.putSerializable("type", RetrieveFeedsAsyncTask.Type.REMOTE_INSTANCE);
bundle.putString("search_peertube", account.getHost());
bundle.putBoolean("account_channel",true);
displayStatusFragment.setArguments(bundle);
return displayStatusFragment;
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.CHANNELS);
bundle.putString("targetedId", accountId);
bundle.putString("instance",account.getAcct().split("@")[1]);
bundle.putString("name",account.getAcct().split("@")[0]);
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
}else{
DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment();
bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FOLLOWING);
@ -1040,6 +1043,7 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
bundle.putString("targetedId", accountId);
displayAccountsFragment.setArguments(bundle);
return displayAccountsFragment;
}
return null;
}
@ -1059,7 +1063,10 @@ public class ShowAccountActivity extends BaseActivity implements OnPostActionInt
@Override
public int getCount() {
return NUM_PAGES;
if( peertubeAccount)
return 2;
else
return 3;
}
}

View File

@ -37,12 +37,22 @@ public class RetrieveAccountsAsyncTask extends AsyncTask<Void, Void, Void> {
private OnRetrieveAccountsInterface listener;
private String targetedId;
private WeakReference<Context> contextReference;
private String instance, name;
public enum Type{
BLOCKED,
MUTED,
FOLLOWING,
FOLLOWERS
FOLLOWERS,
CHANNELS
}
public RetrieveAccountsAsyncTask(Context context, String instance, String name, OnRetrieveAccountsInterface onRetrieveAccountsInterface){
this.contextReference = new WeakReference<>(context);
this.instance = instance;
this.name = name;
this.listener = onRetrieveAccountsInterface;
this.action = Type.CHANNELS;
}
public RetrieveAccountsAsyncTask(Context context, Type action, String targetedId, String max_id, OnRetrieveAccountsInterface onRetrieveAccountsInterface){
@ -77,6 +87,9 @@ public class RetrieveAccountsAsyncTask extends AsyncTask<Void, Void, Void> {
case FOLLOWERS:
apiResponse = api.getFollowers(targetedId, max_id);
break;
case CHANNELS:
apiResponse = api.getPeertubeChannel(instance, name);
break;
}
return null;
}

View File

@ -16,6 +16,8 @@ package fr.gouv.etalab.mastodon.client;
import android.content.Context;
import android.content.SharedPreferences;
import android.util.Log;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
@ -26,6 +28,7 @@ import java.lang.*;
import java.net.URLEncoder;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.text.Format;
import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
@ -601,6 +604,34 @@ public class API {
}
/**
* Retrieves Peertube videos from an instance *synchronously*
* @return APIResponse
*/
public APIResponse getPeertubeChannel(String instance, String name) {
List<Account> accounts = new ArrayList<>();
try {
HttpsConnection httpsConnection = new HttpsConnection(context);
Log.v(Helper.TAG,"url: " + String.format("https://"+instance+"/api/v1/accounts/%s/video-channels", name));
String response = httpsConnection.get(String.format("https://"+instance+"/api/v1/accounts/%s/video-channels", name), 60, null, null);
JSONArray jsonArray = new JSONObject(response).getJSONArray("data");
accounts = parseAccountResponsePeertube(context, instance, jsonArray);
} catch (HttpsConnection.HttpsConnectionException e) {
setError(e.getStatusCode(), e);
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (KeyManagementException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
apiResponse.setAccounts(accounts);
return apiResponse;
}
/**
* Retrieves Peertube videos from an instance *synchronously*
* @return APIResponse
@ -2833,6 +2864,21 @@ public class API {
return list;
}
private List<Account> parseAccountResponsePeertube(Context context, String instance, JSONArray jsonArray){
List<Account> accounts = new ArrayList<>();
try {
int i = 0;
while (i < jsonArray.length() ) {
JSONObject resobj = jsonArray.getJSONObject(i);
Account account = parseAccountResponsePeertube(context, instance, resobj);
accounts.add(account);
i++;
}
} catch (JSONException e) {
setDefaultError(e);
}
return accounts;
}
/**
* Parse json response an unique peertube account
@ -2841,6 +2887,7 @@ public class API {
*/
private static Account parseAccountResponsePeertube(Context context, String instance, JSONObject resobj){
Account account = new Account();
Log.v(Helper.TAG, String.valueOf(resobj));
try {
account.setId(resobj.get("id").toString());
account.setUsername(resobj.get("name").toString());

View File

@ -24,6 +24,7 @@ import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
@ -62,7 +63,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
private RelativeLayout mainLoader, nextElementLoader, textviewNoAction;
private boolean firstLoad;
private SwipeRefreshLayout swipeRefreshLayout;
private String targetedId;
private String targetedId, instance, name;
private boolean swiped;
private RecyclerView lv_accounts;
@ -78,6 +79,8 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
if (bundle != null) {
type = (RetrieveAccountsAsyncTask.Type) bundle.get("type");
targetedId = bundle.getString("targetedId", null);
instance = bundle.getString("instance", null);
name = bundle.getString("name", null);
}
max_id = null;
firstLoad = true;
@ -108,10 +111,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
if (firstVisibleItem + visibleItemCount == totalItemCount) {
if (!flag_loading) {
flag_loading = true;
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
nextElementLoader.setVisibility(View.VISIBLE);
}
} else {
@ -128,10 +133,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
firstLoad = true;
flag_loading = true;
swiped = true;
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
});
SharedPreferences sharedpreferences = context.getSharedPreferences(Helper.APP_PREFS, Context.MODE_PRIVATE);
@ -157,10 +164,12 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou
break;
}
if (type != RetrieveAccountsAsyncTask.Type.FOLLOWERS && type != RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING)
asyncTask = new RetrieveAccountsAsyncTask(context, type, targetedId, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else if (type == RetrieveAccountsAsyncTask.Type.CHANNELS)
asyncTask = new RetrieveAccountsAsyncTask(context, instance, name, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
else
asyncTask = new RetrieveAccountsAsyncTask(context, type, max_id, DisplayAccountsFragment.this).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
return rootView;
}