From a62f47aacb443df4f680ffb1321e84b7f54966f3 Mon Sep 17 00:00:00 2001 From: Alex Ning Date: Wed, 14 Aug 2019 10:57:41 +0800 Subject: Edit flair for posts is now available. --- .../infinityforreddit/FetchFlairs.java | 99 ++++++++++++++++++++++ .../infinityforreddit/FetchFlairsInSubreddit.java | 95 --------------------- .../docilealligator/infinityforreddit/Flair.java | 70 +++++++++++++++ .../FlairBottomSheetFragment.java | 22 +++-- .../FlairBottomSheetRecyclerViewAdapter.java | 8 +- .../infinityforreddit/JSONUtils.java | 1 + .../infinityforreddit/ParsePost.java | 35 ++++---- .../ml/docilealligator/infinityforreddit/Post.java | 30 ++++--- .../infinityforreddit/PostImageActivity.java | 13 ++- .../infinityforreddit/PostLinkActivity.java | 13 ++- .../infinityforreddit/PostTextActivity.java | 13 ++- .../infinityforreddit/PostVideoActivity.java | 13 ++- .../infinityforreddit/RedditAPI.java | 4 + .../infinityforreddit/RedditUtils.java | 3 + .../infinityforreddit/ViewPostDetailActivity.java | 41 ++++++++- 15 files changed, 298 insertions(+), 162 deletions(-) create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java delete mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java create mode 100644 app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java (limited to 'app/src/main/java/ml') diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java new file mode 100644 index 00000000..073067d2 --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairs.java @@ -0,0 +1,99 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.AsyncTask; + +import androidx.annotation.NonNull; + +import org.json.JSONArray; +import org.json.JSONException; + +import java.util.ArrayList; + +import retrofit2.Call; +import retrofit2.Callback; +import retrofit2.Response; +import retrofit2.Retrofit; + +class FetchFlairs { + interface FetchFlairsInSubredditListener { + void fetchSuccessful(ArrayList flairs); + void fetchFailed(); + } + + static void fetchFlairsInSubreddit(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { + RedditAPI api = oauthRetrofit.create(RedditAPI.class); + + Call flairsCall = api.getFlairs(RedditUtils.getOAuthHeader(accessToken), subredditName); + flairsCall.enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if (response.isSuccessful()) { + new ParseFlairsAsyncTask(response.body(), new ParseFlairsAsyncTask.ParseFlairsAsyncTaskListener() { + @Override + public void parseSuccessful(ArrayList flairs) { + fetchFlairsInSubredditListener.fetchSuccessful(flairs); + } + + @Override + public void parseFailed() { + fetchFlairsInSubredditListener.fetchFailed(); + } + }).execute(); + } else if(response.code() == 403) { + //No flairs + fetchFlairsInSubredditListener.fetchSuccessful(null); + } else { + fetchFlairsInSubredditListener.fetchFailed(); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + fetchFlairsInSubredditListener.fetchFailed(); + } + }); + } + + private static class ParseFlairsAsyncTask extends AsyncTask, ArrayList> { + interface ParseFlairsAsyncTaskListener { + void parseSuccessful(ArrayList flairs); + void parseFailed(); + } + + private String response; + private ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener; + + ParseFlairsAsyncTask(String response, ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener) { + this.response = response; + this.parseFlairsAsyncTaskListener = parseFlairsAsyncTaskListener; + } + + @Override + protected ArrayList doInBackground(Void... voids) { + try { + JSONArray jsonArray = new JSONArray(response); + ArrayList flairs = new ArrayList<>(); + for(int i = 0; i < jsonArray.length(); i++) { + String id = jsonArray.getJSONObject(i).getString(JSONUtils.ID_KEY); + String text = jsonArray.getJSONObject(i).getString(JSONUtils.TEXT_KEY); + boolean editable = jsonArray.getJSONObject(i).getBoolean(JSONUtils.TEXT_EDITABLE_KEY); + + flairs.add(new Flair(id, text, editable)); + } + return flairs; + } catch (JSONException e) { + e.printStackTrace(); + } + return null; + } + + @Override + protected void onPostExecute(ArrayList strings) { + if(strings != null) { + parseFlairsAsyncTaskListener.parseSuccessful(strings); + } else { + parseFlairsAsyncTaskListener.parseFailed(); + } + } + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java deleted file mode 100644 index 4a04ee2c..00000000 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchFlairsInSubreddit.java +++ /dev/null @@ -1,95 +0,0 @@ -package ml.docilealligator.infinityforreddit; - -import android.os.AsyncTask; - -import androidx.annotation.NonNull; - -import org.json.JSONArray; -import org.json.JSONException; - -import java.util.ArrayList; - -import retrofit2.Call; -import retrofit2.Callback; -import retrofit2.Response; -import retrofit2.Retrofit; - -class FetchFlairsInSubreddit { - interface FetchFlairsInSubredditListener { - void fetchSuccessful(ArrayList flairs); - void fetchFailed(); - } - - static void fetchFlairs(Retrofit oauthRetrofit, String accessToken, String subredditName, FetchFlairsInSubredditListener fetchFlairsInSubredditListener) { - RedditAPI api = oauthRetrofit.create(RedditAPI.class); - - Call flairsCall = api.getFlairs(RedditUtils.getOAuthHeader(accessToken), subredditName); - flairsCall.enqueue(new Callback() { - @Override - public void onResponse(@NonNull Call call, @NonNull Response response) { - if (response.isSuccessful()) { - new ParseFlairsAsyncTask(response.body(), new ParseFlairsAsyncTask.ParseFlairsAsyncTaskListener() { - @Override - public void parseSuccessful(ArrayList flairs) { - fetchFlairsInSubredditListener.fetchSuccessful(flairs); - } - - @Override - public void parseFailed() { - fetchFlairsInSubredditListener.fetchFailed(); - } - }).execute(); - } else if(response.code() == 403) { - //No flairs - fetchFlairsInSubredditListener.fetchSuccessful(null); - } else { - fetchFlairsInSubredditListener.fetchFailed(); - } - } - - @Override - public void onFailure(@NonNull Call call, @NonNull Throwable t) { - fetchFlairsInSubredditListener.fetchFailed(); - } - }); - } - - private static class ParseFlairsAsyncTask extends AsyncTask, ArrayList> { - interface ParseFlairsAsyncTaskListener { - void parseSuccessful(ArrayList flairs); - void parseFailed(); - } - - private String response; - private ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener; - - ParseFlairsAsyncTask(String response, ParseFlairsAsyncTaskListener parseFlairsAsyncTaskListener) { - this.response = response; - this.parseFlairsAsyncTaskListener = parseFlairsAsyncTaskListener; - } - - @Override - protected ArrayList doInBackground(Void... voids) { - try { - JSONArray jsonArray = new JSONArray(response); - ArrayList flairs = new ArrayList<>(); - for(int i = 0; i < jsonArray.length(); i++) { - flairs.add(jsonArray.getJSONObject(i).getString(JSONUtils.TEXT_KEY)); - } - return flairs; - } catch (JSONException e) { - e.printStackTrace(); - } - return null; - } - - @Override - protected void onPostExecute(ArrayList strings) { - if(strings != null) { - parseFlairsAsyncTaskListener.parseSuccessful(strings); - } else { - parseFlairsAsyncTaskListener.parseFailed(); - } - } - } -} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java new file mode 100644 index 00000000..e60566ce --- /dev/null +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Flair.java @@ -0,0 +1,70 @@ +package ml.docilealligator.infinityforreddit; + +import android.os.Parcel; +import android.os.Parcelable; + +class Flair implements Parcelable { + private String id; + private String text; + private boolean editable; + + Flair(String id, String text, boolean editable) { + this.id = id; + this.text = text; + this.editable = editable; + } + + protected Flair(Parcel in) { + id = in.readString(); + text = in.readString(); + editable = in.readByte() != 0; + } + + public static final Creator CREATOR = new Creator() { + @Override + public Flair createFromParcel(Parcel in) { + return new Flair(in); + } + + @Override + public Flair[] newArray(int size) { + return new Flair[size]; + } + }; + + public String getId() { + return id; + } + + public void setId(String id) { + this.id = id; + } + + public String getText() { + return text; + } + + public void setText(String text) { + this.text = text; + } + + public boolean isEditable() { + return editable; + } + + public void setEditable(boolean editable) { + this.editable = editable; + } + + @Override + public int describeContents() { + return 0; + } + + @Override + public void writeToParcel(Parcel parcel, int i) { + parcel.writeString(id); + parcel.writeString(text); + parcel.writeByte((byte) (editable ? 1 : 0)); + } +} diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java index bd8af127..6a9987cf 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetFragment.java @@ -11,6 +11,7 @@ import android.view.ViewGroup; import android.widget.ProgressBar; import android.widget.TextView; +import androidx.annotation.NonNull; import androidx.fragment.app.Fragment; import androidx.recyclerview.widget.LinearLayoutManager; import androidx.recyclerview.widget.RecyclerView; @@ -33,7 +34,7 @@ import retrofit2.Retrofit; public class FlairBottomSheetFragment extends BottomSheetDialogFragment { interface FlairSelectionCallback { - void flairSelected(String flair); + void flairSelected(Flair flair); } static final String EXTRA_ACCESS_TOKEN = "EAT"; @@ -53,13 +54,17 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { @Named("oauth") Retrofit mOauthRetrofit; + @Inject + @Named("no_oauth") + Retrofit mRetrofit; + public FlairBottomSheetFragment() { // Required empty public constructor } @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, + public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_flair_bottom_sheet, container, false); @@ -74,7 +79,11 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { rootView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR); } - mAdapter = new FlairBottomSheetRecyclerViewAdapter(flair -> ((FlairSelectionCallback) mAcitivity).flairSelected(flair)); + mAdapter = new FlairBottomSheetRecyclerViewAdapter(flair -> { + ((FlairSelectionCallback) mAcitivity).flairSelected(flair); + dismiss(); + }); + recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); recyclerView.setAdapter(mAdapter); @@ -87,10 +96,10 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { } private void fetchFlairs() { - FetchFlairsInSubreddit.fetchFlairs(mOauthRetrofit, mAccessToken, - mSubredditName, new FetchFlairsInSubreddit.FetchFlairsInSubredditListener() { + FetchFlairs.fetchFlairsInSubreddit(mOauthRetrofit, mAccessToken, + mSubredditName, new FetchFlairs.FetchFlairsInSubredditListener() { @Override - public void fetchSuccessful(ArrayList flairs) { + public void fetchSuccessful(ArrayList flairs) { progressBar.setVisibility(View.GONE); if(flairs == null || flairs.size() == 0) { errorTextView.setVisibility(View.VISIBLE); @@ -110,5 +119,4 @@ public class FlairBottomSheetFragment extends BottomSheetDialogFragment { } }); } - } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetRecyclerViewAdapter.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetRecyclerViewAdapter.java index 37e8d15b..f3cf3328 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetRecyclerViewAdapter.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FlairBottomSheetRecyclerViewAdapter.java @@ -15,10 +15,10 @@ import butterknife.ButterKnife; class FlairBottomSheetRecyclerViewAdapter extends RecyclerView.Adapter { interface ItemClickListener { - void onClick(String flair); + void onClick(Flair flair); } - private ArrayList flairs; + private ArrayList flairs; private ItemClickListener itemClickListener; FlairBottomSheetRecyclerViewAdapter(ItemClickListener itemClickListener) { @@ -33,7 +33,7 @@ class FlairBottomSheetRecyclerViewAdapter extends RecyclerView.Adapter flairs) { + void changeDataset(ArrayList flairs) { this.flairs = flairs; notifyDataSetChanged(); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java index d1a32d2d..78e3659c 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/JSONUtils.java @@ -72,4 +72,5 @@ public class JSONUtils { static final String SHORT_NAME_KEY = "short_name"; static final String DESCRIPTION_HTML_KEY = "description_html"; static final String ARCHIVED_KEY = "archived"; + static final String TEXT_EDITABLE_KEY = "text_editable"; } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java index 7d71e152..cce48b19 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ParsePost.java @@ -148,6 +148,7 @@ class ParsePost { private static Post parseBasicData(JSONObject data, Locale locale, int i) throws JSONException { String id = data.getString(JSONUtils.ID_KEY); String fullName = data.getString(JSONUtils.NAME_KEY); + String subredditName = data.getString(JSONUtils.SUBREDDIT_KEY); String subredditNamePrefixed = data.getString(JSONUtils.SUBREDDIT_NAME_PREFIX_KEY); String author = data.getString(JSONUtils.AUTHOR_KEY); long postTime = data.getLong(JSONUtils.CREATED_UTC_KEY) * 1000; @@ -191,22 +192,22 @@ class ParsePost { if(data.has(JSONUtils.CROSSPOST_PARENT_LIST)) { //Cross post data = data.getJSONArray(JSONUtils.CROSSPOST_PARENT_LIST).getJSONObject(0); - return parseData(data, permalink, id, fullName, subredditNamePrefixed, + return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, true, i); } else { - return parseData(data, permalink, id, fullName, subredditNamePrefixed, + return parseData(data, permalink, id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, previewWidth, previewHeight, score, voteType, gilded, flair, spoiler, nsfw, stickied, archived, false, i); } } private static Post parseData(JSONObject data, String permalink, String id, String fullName, - String subredditNamePrefixed, String author, String formattedPostTime, - String title, String previewUrl, int previewWidth, int previewHeight, - int score, int voteType, int gilded, String flair, boolean spoiler, - boolean nsfw, boolean stickied, boolean archived, boolean isCrosspost, - int i) throws JSONException { + String subredditName, String subredditNamePrefixed, String author, + String formattedPostTime, String title, String previewUrl, int previewWidth, + int previewHeight, int score, int voteType, int gilded, String flair, + boolean spoiler, boolean nsfw, boolean stickied, boolean archived, + boolean isCrosspost, int i) throws JSONException { Post post; boolean isVideo = data.getBoolean(JSONUtils.IS_VIDEO_KEY); @@ -217,7 +218,7 @@ class ParsePost { //Text post Log.i("text", Integer.toString(i)); int postType = Post.TEXT_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -229,7 +230,7 @@ class ParsePost { //No preview link post Log.i("no preview link", Integer.toString(i)); int postType = Post.NO_PREVIEW_LINK_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -251,7 +252,7 @@ class ParsePost { int postType = Post.VIDEO_TYPE; String videoUrl = Html.fromHtml(redditVideoObject.getString(JSONUtils.DASH_URL_KEY)).toString(); - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost, true); @@ -268,7 +269,7 @@ class ParsePost { String videoUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.MP4_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); String gifDownloadUrl = Html.fromHtml(variations.getJSONObject(JSONUtils.VARIANTS_KEY).getJSONObject(JSONUtils.GIF_KEY).getJSONObject(JSONUtils.SOURCE_KEY).getString(JSONUtils.URL_KEY)).toString(); - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost, false); post.setPreviewWidth(previewWidth); @@ -283,7 +284,7 @@ class ParsePost { String videoUrl = Html.fromHtml(data.getJSONObject(JSONUtils.PREVIEW_KEY) .getJSONObject(JSONUtils.REDDIT_VIDEO_PREVIEW_KEY).getString(JSONUtils.DASH_URL_KEY)).toString(); - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost, true); post.setPreviewWidth(previewWidth); @@ -296,7 +297,7 @@ class ParsePost { Log.i("image", Integer.toString(i)); int postType = Post.IMAGE_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); @@ -308,7 +309,7 @@ class ParsePost { Log.i("text with image", Integer.toString(i)); int postType = Post.TEXT_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); @@ -325,7 +326,7 @@ class ParsePost { Log.i("link", Integer.toString(i)); int postType = Post.LINK_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); if(data.isNull(JSONUtils.SELFTEXT_KEY)) { @@ -345,7 +346,7 @@ class ParsePost { Log.i("CP image", Integer.toString(i)); int postType = Post.IMAGE_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, previewUrl, url, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); post.setPreviewWidth(previewWidth); @@ -355,7 +356,7 @@ class ParsePost { Log.i("CP no preview link", Integer.toString(i)); int postType = Post.NO_PREVIEW_LINK_TYPE; - post = new Post(id, fullName, subredditNamePrefixed, author, formattedPostTime, title, + post = new Post(id, fullName, subredditName, subredditNamePrefixed, author, formattedPostTime, title, url, url, permalink, score, postType, voteType, gilded, flair, spoiler, nsfw, stickied, archived, isCrosspost); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java index 0e23d359..70413c41 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/Post.java @@ -17,6 +17,7 @@ class Post implements Parcelable { private String id; private String fullName; + private String subredditName; private String subredditNamePrefixed; private String subredditIconUrl; private String author; @@ -45,12 +46,13 @@ class Post implements Parcelable { private boolean isDashVideo; private boolean isDownloadableGifOrVideo; - Post(String id, String fullName, String subredditNamePrefixed, String author, String postTime, - String title, String previewUrl, String permalink, int score, int postType, int voteType, - int gilded, String flair, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, - boolean isCrosspost, boolean isDashVideo) { + Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, + String postTime, String title, String previewUrl, String permalink, int score, int postType, + int voteType, int gilded, String flair, boolean spoiler, boolean nsfw, boolean stickied, + boolean archived, boolean isCrosspost, boolean isDashVideo) { this.id = id; this.fullName = fullName; + this.subredditName = subredditName; this.subredditNamePrefixed = subredditNamePrefixed; this.author = author; this.authorNamePrefixed = "u/" + author; @@ -71,12 +73,13 @@ class Post implements Parcelable { this.isDashVideo = isDashVideo; } - Post(String id, String fullName, String subredditNamePrefixed, String author, String postTime, - String title, String previewUrl, String url, String permalink, int score, int postType, - int voteType, int gilded, String flair, boolean spoiler, boolean nsfw, boolean stickied, + Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, + String postTime, String title, String previewUrl, String url, String permalink, int score, + int postType, int voteType, int gilded, String flair, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean isCrosspost) { this.id = id; this.fullName = fullName; + this.subredditName = subredditName; this.subredditNamePrefixed = subredditNamePrefixed; this.author = author; this.authorNamePrefixed = "u/" + author; @@ -97,11 +100,12 @@ class Post implements Parcelable { this.isCrosspost = isCrosspost; } - Post(String id, String fullName, String subredditNamePrefixed, String author, String postTime, - String title, String permalink, int score, int postType, int voteType, int gilded, String flair, - boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean isCrosspost) { + Post(String id, String fullName, String subredditName, String subredditNamePrefixed, String author, + String postTime, String title, String permalink, int score, int postType, int voteType, int gilded, + String flair, boolean spoiler, boolean nsfw, boolean stickied, boolean archived, boolean isCrosspost) { this.id = id; this.fullName = fullName; + this.subredditName = subredditName; this.subredditNamePrefixed = subredditNamePrefixed; this.author = author; this.authorNamePrefixed = "u/" + author; @@ -123,6 +127,7 @@ class Post implements Parcelable { protected Post(Parcel in) { id = in.readString(); fullName = in.readString(); + subredditName = in.readString(); subredditNamePrefixed = in.readString(); subredditIconUrl = in.readString(); author = in.readString(); @@ -172,6 +177,10 @@ class Post implements Parcelable { return fullName; } + String getSubredditName() { + return subredditName; + } + String getSubredditNamePrefixed() { return subredditNamePrefixed; } @@ -345,6 +354,7 @@ class Post implements Parcelable { public void writeToParcel(Parcel parcel, int i) { parcel.writeString(id); parcel.writeString(fullName); + parcel.writeString(subredditName); parcel.writeString(subredditNamePrefixed); parcel.writeString(subredditIconUrl); parcel.writeString(author); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java index 823fa1cd..7d2a999b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostImageActivity.java @@ -95,7 +95,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS private boolean isPosting; private Uri imageUri; - private String flair = null; + private Flair flair; private boolean isSpoiler = false; private boolean isNSFW = false; @@ -158,7 +158,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); - flair = savedInstanceState.getString(FLAIR_STATE); + flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); @@ -182,7 +182,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } if(flair != null) { - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); } if(isSpoiler) { @@ -415,7 +415,7 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); outState.putBoolean(IS_POSTING_STATE, isPosting); - outState.putString(FLAIR_STATE, flair); + outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); @@ -466,11 +466,10 @@ public class PostImageActivity extends AppCompatActivity implements FlairBottomS } @Override - public void flairSelected(String flair) { + public void flairSelected(Flair flair) { this.flair = flair; - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); - flairSelectionBottomSheetFragment.dismiss(); } @Subscribe diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java index 4960b627..15b47cce 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostLinkActivity.java @@ -75,7 +75,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh private boolean loadSubredditIconSuccessful = true; private boolean isPosting; - private String flair = null; + private Flair flair; private boolean isSpoiler = false; private boolean isNSFW = false; @@ -133,7 +133,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); - flair = savedInstanceState.getString(FLAIR_STATE); + flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); @@ -152,7 +152,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } if(flair != null) { - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); } if(isSpoiler) { @@ -335,7 +335,7 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); outState.putBoolean(IS_POSTING_STATE, isPosting); - outState.putString(FLAIR_STATE, flair); + outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); @@ -371,11 +371,10 @@ public class PostLinkActivity extends AppCompatActivity implements FlairBottomSh } @Override - public void flairSelected(String flair) { + public void flairSelected(Flair flair) { this.flair = flair; - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); - flairSelectionBottomSheetFragment.dismiss(); } @Subscribe diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java index e4e94243..7d401c90 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostTextActivity.java @@ -75,7 +75,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh private boolean loadSubredditIconSuccessful = true; private boolean isPosting; - private String flair = null; + private Flair flair; private boolean isSpoiler = false; private boolean isNSFW = false; @@ -126,7 +126,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); - flair = savedInstanceState.getString(FLAIR_STATE); + flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); @@ -145,7 +145,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } if(flair != null) { - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); } if(isSpoiler) { @@ -331,7 +331,7 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh outState.putBoolean(SUBREDDIT_IS_USER_STATE, subredditIsUser); outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); outState.putBoolean(IS_POSTING_STATE, isPosting); - outState.putString(FLAIR_STATE, flair); + outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); @@ -367,11 +367,10 @@ public class PostTextActivity extends AppCompatActivity implements FlairBottomSh } @Override - public void flairSelected(String flair) { + public void flairSelected(Flair flair) { this.flair = flair; - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); - flairSelectionBottomSheetFragment.dismiss(); } @Subscribe diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java index 919502f1..b0ab91bb 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/PostVideoActivity.java @@ -90,7 +90,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS private boolean loadSubredditIconSuccessful = true; private boolean isPosting; - private String flair = null; + private Flair flair; private boolean isSpoiler = false; private boolean isNSFW = false; @@ -149,7 +149,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS subredditIsUser = savedInstanceState.getBoolean(SUBREDDIT_IS_USER_STATE); loadSubredditIconSuccessful = savedInstanceState.getBoolean(LOAD_SUBREDDIT_ICON_STATE); isPosting = savedInstanceState.getBoolean(IS_POSTING_STATE); - flair = savedInstanceState.getString(FLAIR_STATE); + flair = savedInstanceState.getParcelable(FLAIR_STATE); isSpoiler = savedInstanceState.getBoolean(IS_SPOILER_STATE); isNSFW = savedInstanceState.getBoolean(IS_NSFW_STATE); mNullAccessToken = savedInstanceState.getBoolean(NULL_ACCESS_TOKEN_STATE); @@ -180,7 +180,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS } if(flair != null) { - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); } if(isSpoiler) { @@ -416,7 +416,7 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS } outState.putBoolean(LOAD_SUBREDDIT_ICON_STATE, loadSubredditIconSuccessful); outState.putBoolean(IS_POSTING_STATE, isPosting); - outState.putString(FLAIR_STATE, flair); + outState.putParcelable(FLAIR_STATE, flair); outState.putBoolean(IS_SPOILER_STATE, isSpoiler); outState.putBoolean(IS_NSFW_STATE, isNSFW); outState.putBoolean(NULL_ACCESS_TOKEN_STATE, mNullAccessToken); @@ -466,11 +466,10 @@ public class PostVideoActivity extends AppCompatActivity implements FlairBottomS } @Override - public void flairSelected(String flair) { + public void flairSelected(Flair flair) { this.flair = flair; - flairTextView.setText(flair); + flairTextView.setText(flair.getText()); flairTextView.setBackgroundColor(getResources().getColor(R.color.backgroundColorPrimaryDark)); - mFlairSelectionBottomSheetFragment.dismiss(); } @Subscribe diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java index 4deb3abd..2f25001a 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditAPI.java @@ -147,4 +147,8 @@ public interface RedditAPI { @FormUrlEncoded @POST("/api/unspoiler") Call unmarkSpoiler(@HeaderMap Map headers, @FieldMap Map params); + + @FormUrlEncoded + @POST("{subredditNamePrefixed}/api/selectflair") + Call selectFlair(@Path("subredditNamePrefixed") String subredditName, @HeaderMap Map headers, @FieldMap Map params); } diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java index 670dd280..5279f97b 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/RedditUtils.java @@ -76,6 +76,9 @@ public class RedditUtils { static final String FILEPATH_KEY = "filepath"; static final String MIMETYPE_KEY = "mimetype"; + static final String LINK_KEY = "link"; + static final String FLAIR_TEMPLATE_ID_KEY = "flair_template_id"; + static Map getHttpBasicAuthHeader() { Map params = new HashMap<>(); String credentials = String.format("%s:%s", RedditUtils.CLIENT_ID, ""); diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java index 964e1809..0ec34c57 100644 --- a/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java +++ b/app/src/main/java/ml/docilealligator/infinityforreddit/ViewPostDetailActivity.java @@ -54,7 +54,7 @@ import retrofit2.Retrofit; import static ml.docilealligator.infinityforreddit.CommentActivity.EXTRA_COMMENT_DATA_KEY; import static ml.docilealligator.infinityforreddit.CommentActivity.WRITE_COMMENT_REQUEST_CODE; -public class ViewPostDetailActivity extends AppCompatActivity { +public class ViewPostDetailActivity extends AppCompatActivity implements FlairBottomSheetFragment.FlairSelectionCallback { static final String EXTRA_POST_DATA = "EPD"; static final String EXTRA_POST_LIST_POSITION = "EPLI"; @@ -241,6 +241,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { } else { spoilerItem.setTitle(R.string.action_mark_spoiler); } + + mMenu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); } mAdapter = new CommentAndPostRecyclerViewAdapter(ViewPostDetailActivity.this, mRetrofit, mOauthRetrofit, mRedditDataRoomDatabase, mGlide, mAccessToken, mAccountName, mPost, @@ -642,6 +644,8 @@ public class ViewPostDetailActivity extends AppCompatActivity { } else { spoilerItem.setTitle(R.string.action_mark_spoiler); } + + menu.findItem(R.id.action_edit_flair_view_post_detail_activity).setVisible(true); } return true; } @@ -707,6 +711,14 @@ public class ViewPostDetailActivity extends AppCompatActivity { markSpoiler(); } return true; + case R.id.action_edit_flair_view_post_detail_activity: + FlairBottomSheetFragment flairBottomSheetFragment = new FlairBottomSheetFragment(); + Bundle bundle = new Bundle(); + bundle.putString(FlairBottomSheetFragment.EXTRA_ACCESS_TOKEN, mAccessToken); + bundle.putString(FlairBottomSheetFragment.EXTRA_SUBREDDIT_NAME, mPost.getSubredditName()); + flairBottomSheetFragment.setArguments(bundle); + flairBottomSheetFragment.show(getSupportFragmentManager(), flairBottomSheetFragment.getTag()); + return true; case android.R.id.home: onBackPressed(); return true; @@ -768,4 +780,31 @@ public class ViewPostDetailActivity extends AppCompatActivity { mLoadSubredditIconAsyncTask.cancel(true); } } + + @Override + public void flairSelected(Flair flair) { + Map params = new HashMap<>(); + params.put(RedditUtils.API_TYPE_KEY, RedditUtils.API_TYPE_JSON); + params.put(RedditUtils.FLAIR_TEMPLATE_ID_KEY, flair.getId()); + params.put(RedditUtils.LINK_KEY, mPost.getFullName()); + params.put(RedditUtils.TEXT_KEY, flair.getText()); + + mOauthRetrofit.create(RedditAPI.class).selectFlair(mPost.getSubredditNamePrefixed(), + RedditUtils.getOAuthHeader(mAccessToken), params).enqueue(new Callback() { + @Override + public void onResponse(@NonNull Call call, @NonNull Response response) { + if(response.isSuccessful()) { + refresh(true); + showMessage(R.string.update_flair_success); + } else { + showMessage(R.string.update_flair_failed); + } + } + + @Override + public void onFailure(@NonNull Call call, @NonNull Throwable t) { + showMessage(R.string.update_flair_failed); + } + }); + } } -- cgit v1.2.3