From d53befdbf91f79b5b7d7faada5a1aca1443bc0cd Mon Sep 17 00:00:00 2001 From: stom79 Date: Tue, 6 Nov 2018 19:50:09 +0100 Subject: Possibility to check who boosted or added to fav a toot - #549 --- .../mastodon/activities/TootInfoActivity.java | 156 +++++++++++++++++++++ .../asynctasks/RetrieveAccountsAsyncTask.java | 10 +- .../java/fr/gouv/etalab/mastodon/client/API.java | 78 +++++++++++ .../etalab/mastodon/drawers/StatusListAdapter.java | 11 ++ .../fragments/DisplayAccountsFragment.java | 6 +- 5 files changed, 257 insertions(+), 4 deletions(-) create mode 100644 app/src/main/java/fr/gouv/etalab/mastodon/activities/TootInfoActivity.java (limited to 'app/src/main/java/fr/gouv/etalab/mastodon') diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootInfoActivity.java b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootInfoActivity.java new file mode 100644 index 000000000..d658e380b --- /dev/null +++ b/app/src/main/java/fr/gouv/etalab/mastodon/activities/TootInfoActivity.java @@ -0,0 +1,156 @@ +/* Copyright 2017 Thomas Schneider + * + * This file is a part of Mastalab + * + * This program is free software; you can redistribute it and/or modify it under the terms of the + * GNU General Public License as published by the Free Software Foundation; either version 3 of the + * License, or (at your option) any later version. + * + * Mastalab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even + * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General + * Public License for more details. + * + * You should have received a copy of the GNU General Public License along with Mastalab; if not, + * see . */ +package fr.gouv.etalab.mastodon.activities; + + +import android.content.Context; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.support.design.widget.NavigationView; +import android.support.design.widget.TabLayout; +import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentStatePagerAdapter; +import android.support.v4.view.PagerAdapter; +import android.support.v4.view.ViewPager; +import android.view.MenuItem; +import android.view.View; +import android.view.ViewGroup; +import android.view.Window; +import android.widget.LinearLayout; +import android.widget.RelativeLayout; +import android.widget.TableLayout; +import android.widget.Toast; + +import fr.gouv.etalab.mastodon.R; +import fr.gouv.etalab.mastodon.asynctasks.RetrieveAccountsAsyncTask; +import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; +import fr.gouv.etalab.mastodon.fragments.DisplayAccountsFragment; +import fr.gouv.etalab.mastodon.fragments.DisplayStatusFragment; +import fr.gouv.etalab.mastodon.fragments.TabLayoutTootsFragment; +import fr.gouv.etalab.mastodon.helper.Helper; + + + +/** + * Created by Thomas on 05/11/2018. + * Toot info activity class + */ + +public class TootInfoActivity extends BaseActivity { + + + private String toot_id; + private TabLayout tabLayout; + private ViewPager mPager; + private String userID; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + requestWindowFeature(Window.FEATURE_NO_TITLE); + SharedPreferences sharedpreferences = getSharedPreferences(Helper.APP_PREFS, android.content.Context.MODE_PRIVATE); + int theme = sharedpreferences.getInt(Helper.SET_THEME, Helper.THEME_DARK); + switch (theme){ + case Helper.THEME_LIGHT: + setTheme(R.style.AppTheme); + break; + case Helper.THEME_DARK: + setTheme(R.style.AppThemeDark); + break; + case Helper.THEME_BLACK: + setTheme(R.style.AppThemeBlack); + break; + default: + setTheme(R.style.AppThemeDark); + } + setContentView(R.layout.activity_toot_info); + getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); + Bundle b = getIntent().getExtras(); + if( getSupportActionBar() != null) + getSupportActionBar().hide(); + if( b != null){ + toot_id = b.getString("toot_id", null); + } + if( toot_id == null){ + Toast.makeText(this, R.string.toast_error, Toast.LENGTH_SHORT).show(); + finish(); + } + userID = sharedpreferences.getString(Helper.PREF_KEY_ID, null); + tabLayout = findViewById(R.id.tabLayout); + mPager = findViewById(R.id.viewpager); + tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.reblog))); + tabLayout.addTab(tabLayout.newTab().setText(getString(R.string.favourite))); + + PagerAdapter mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager()); + mPager.setAdapter(mPagerAdapter); + + mPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() { + @Override + public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { + + } + + @Override + public void onPageSelected(int position) { + TabLayout.Tab tab = tabLayout.getTabAt(position); + if( tab != null) + tab.select(); + } + + @Override + public void onPageScrollStateChanged(int state) { + + } + }); + } + + + private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter { + + ScreenSlidePagerAdapter(FragmentManager fm) { + super(fm); + } + + @Override + public Fragment getItem(int position) { + Bundle bundle = new Bundle(); + switch (position){ + case 0: + DisplayAccountsFragment displayAccountsFragment = new DisplayAccountsFragment(); + bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.REBLOGGED); + bundle.putString("targetedId", toot_id); + displayAccountsFragment.setArguments(bundle); + return displayAccountsFragment; + case 1: + displayAccountsFragment = new DisplayAccountsFragment(); + bundle.putSerializable("type", RetrieveAccountsAsyncTask.Type.FAVOURITED); + bundle.putString("targetedId", toot_id); + displayAccountsFragment.setArguments(bundle); + return displayAccountsFragment; + } + return null; + } + + + @Override + public int getCount() { + return 2; + + } + } + + +} diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsAsyncTask.java b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsAsyncTask.java index 65e112ca2..2a20d533c 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsAsyncTask.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/asynctasks/RetrieveAccountsAsyncTask.java @@ -44,7 +44,9 @@ public class RetrieveAccountsAsyncTask extends AsyncTask { MUTED, FOLLOWING, FOLLOWERS, - CHANNELS + CHANNELS, + REBLOGGED, + FAVOURITED } public RetrieveAccountsAsyncTask(Context context, String instance, String name, OnRetrieveAccountsInterface onRetrieveAccountsInterface){ @@ -75,6 +77,12 @@ public class RetrieveAccountsAsyncTask extends AsyncTask { API api = new API(this.contextReference.get()); switch (action){ + case REBLOGGED: + apiResponse = api.getRebloggedBy(targetedId, max_id); + break; + case FAVOURITED: + apiResponse = api.getFavouritedBy(targetedId, max_id); + break; case BLOCKED: apiResponse = api.getBlocks(max_id); break; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java index 4fa1fb769..cdb00fe67 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/client/API.java @@ -16,6 +16,7 @@ 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; @@ -432,6 +433,83 @@ public class API { } + + /** + * Retrieves accounts that reblogged the status *synchronously* + * + * @param statusId String Id of the status + * @param max_id String id max + * @return APIResponse + */ + @SuppressWarnings("SameParameterValue") + public APIResponse getRebloggedBy(String statusId, String max_id) { + + HashMap params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + params.put("limit", "80"); + accounts = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/reblogged_by", statusId)), 60, params, prefKeyOauthTokenT); + accounts = parseAccountResponse(new JSONArray(response)); + apiResponse.setSince_id(httpsConnection.getSince_id()); + apiResponse.setMax_id(httpsConnection.getMax_id()); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } 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 accounts that favourited the status *synchronously* + * + * @param statusId String Id of the status + * @param max_id String id max + * @return APIResponse + */ + @SuppressWarnings("SameParameterValue") + public APIResponse getFavouritedBy(String statusId, String max_id) { + + HashMap params = new HashMap<>(); + if (max_id != null) + params.put("max_id", max_id); + params.put("limit", "80"); + accounts = new ArrayList<>(); + try { + HttpsConnection httpsConnection = new HttpsConnection(context); + String response = httpsConnection.get(getAbsoluteUrl(String.format("/statuses/%s/favourited_by", statusId)), 60, params, prefKeyOauthTokenT); + accounts = parseAccountResponse(new JSONArray(response)); + apiResponse.setSince_id(httpsConnection.getSince_id()); + apiResponse.setMax_id(httpsConnection.getMax_id()); + } catch (HttpsConnection.HttpsConnectionException e) { + setError(e.getStatusCode(), e); + e.printStackTrace(); + } 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 one status *synchronously* * diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java index f085e4887..5a07a465a 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/drawers/StatusListAdapter.java @@ -91,6 +91,7 @@ import fr.gouv.etalab.mastodon.activities.MediaActivity; import fr.gouv.etalab.mastodon.activities.ShowAccountActivity; import fr.gouv.etalab.mastodon.activities.ShowConversationActivity; import fr.gouv.etalab.mastodon.activities.TootActivity; +import fr.gouv.etalab.mastodon.activities.TootInfoActivity; import fr.gouv.etalab.mastodon.asynctasks.PostActionAsyncTask; import fr.gouv.etalab.mastodon.asynctasks.RetrieveFeedsAsyncTask; import fr.gouv.etalab.mastodon.client.API; @@ -1342,6 +1343,16 @@ public class StatusListAdapter extends RecyclerView.Adapter implements OnPostAct //noinspection deprecation builderInner.setMessage(Html.fromHtml(status.getContent())); break; + case R.id.action_info: + Intent intent = new Intent(context, TootInfoActivity.class); + Bundle b = new Bundle(); + if( status.getReblog() != null) + b.putString("toot_id", status.getReblog().getId()); + else + b.putString("toot_id", status.getId()); + intent.putExtras(b); + context.startActivity(intent); + return true; case R.id.action_open_browser: Helper.openBrowser(context, status.getReblog()!=null?status.getReblog().getUrl():status.getUrl()); return true; diff --git a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java index c75152ebc..64021bf28 100644 --- a/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java +++ b/app/src/main/java/fr/gouv/etalab/mastodon/fragments/DisplayAccountsFragment.java @@ -110,7 +110,7 @@ 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) + if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING || type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED) 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); @@ -132,7 +132,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou firstLoad = true; flag_loading = true; swiped = true; - if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING) + if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING|| type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED) 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); @@ -163,7 +163,7 @@ public class DisplayAccountsFragment extends Fragment implements OnRetrieveAccou break; } - if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING) + if (type == RetrieveAccountsAsyncTask.Type.FOLLOWERS || type == RetrieveAccountsAsyncTask.Type.FOLLOWING|| type == RetrieveAccountsAsyncTask.Type.REBLOGGED || type == RetrieveAccountsAsyncTask.Type.FAVOURITED) 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); -- cgit v1.2.3