mirror of
https://codeberg.org/Bazsalanszky/Infinity-For-Lemmy.git
synced 2025-01-01 13:57:10 +01:00
Finish replacing AsyncTask with Executor in ParsePost.
This commit is contained in:
parent
260bd45433
commit
b01c9b9e2b
@ -3,9 +3,12 @@ package ml.docilealligator.infinityforreddit.activities;
|
|||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.content.SharedPreferences;
|
import android.content.SharedPreferences;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.widget.RelativeLayout;
|
import android.widget.RelativeLayout;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
|
|
||||||
@ -32,6 +35,8 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity {
|
|||||||
SharedPreferences mSharedPreferences;
|
SharedPreferences mSharedPreferences;
|
||||||
@Inject
|
@Inject
|
||||||
CustomThemeWrapper mCustomThemeWrapper;
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
|
@Inject
|
||||||
|
Executor mExecutor;
|
||||||
private boolean isCanceled = false;
|
private boolean isCanceled = false;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@ -44,7 +49,7 @@ public class FetchRandomSubredditOrPostActivity extends BaseActivity {
|
|||||||
|
|
||||||
int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT);
|
int option = getIntent().getIntExtra(EXTRA_RANDOM_OPTION, RandomBottomSheetFragment.RANDOM_SUBREDDIT);
|
||||||
|
|
||||||
FetchPost.fetchRandomPost(mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
|
FetchPost.fetchRandomPost(mExecutor, new Handler(), mRetrofit, option == RandomBottomSheetFragment.RANDOM_NSFW_SUBREDDIT
|
||||||
|| option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() {
|
|| option == RandomBottomSheetFragment.RANDOM_NSFW_POST, new FetchPost.FetchRandomPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void fetchRandomPostSuccess(String postId, String subredditName) {
|
public void fetchRandomPostSuccess(String postId, String subredditName) {
|
||||||
|
@ -11,6 +11,7 @@ import android.media.AudioManager;
|
|||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.Build;
|
import android.os.Build;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
import android.view.Menu;
|
import android.view.Menu;
|
||||||
import android.view.MenuItem;
|
import android.view.MenuItem;
|
||||||
@ -51,6 +52,7 @@ import com.thefuntasty.hauler.HaulerView;
|
|||||||
import org.apache.commons.io.FilenameUtils;
|
import org.apache.commons.io.FilenameUtils;
|
||||||
|
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
@ -152,6 +154,9 @@ public class ViewVideoActivity extends AppCompatActivity {
|
|||||||
@Named("default")
|
@Named("default")
|
||||||
SharedPreferences mSharedPreferences;
|
SharedPreferences mSharedPreferences;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Executor mExecutor;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
@ -430,7 +435,8 @@ public class ViewVideoActivity extends AppCompatActivity {
|
|||||||
List<String> segments = redirectUri.getPathSegments();
|
List<String> segments = redirectUri.getPathSegments();
|
||||||
int commentsIndex = segments.lastIndexOf("comments");
|
int commentsIndex = segments.lastIndexOf("comments");
|
||||||
String postId = segments.get(commentsIndex + 1);
|
String postId = segments.get(commentsIndex + 1);
|
||||||
FetchPost.fetchPost(retrofit, postId, null, new FetchPost.FetchPostListener() {
|
FetchPost.fetchPost(mExecutor, new Handler(), retrofit, postId, null,
|
||||||
|
new FetchPost.FetchPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void fetchPostSuccess(Post post) {
|
public void fetchPostSuccess(Post post) {
|
||||||
if (post.isGfycat()) {
|
if (post.isGfycat()) {
|
||||||
|
@ -10,6 +10,7 @@ import android.graphics.Canvas;
|
|||||||
import android.graphics.drawable.ColorDrawable;
|
import android.graphics.drawable.ColorDrawable;
|
||||||
import android.graphics.drawable.Drawable;
|
import android.graphics.drawable.Drawable;
|
||||||
import android.os.Bundle;
|
import android.os.Bundle;
|
||||||
|
import android.os.Handler;
|
||||||
import android.util.DisplayMetrics;
|
import android.util.DisplayMetrics;
|
||||||
import android.view.HapticFeedbackConstants;
|
import android.view.HapticFeedbackConstants;
|
||||||
import android.view.LayoutInflater;
|
import android.view.LayoutInflater;
|
||||||
@ -1103,7 +1104,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
|||||||
mSwipeRefreshLayout.setRefreshing(false);
|
mSwipeRefreshLayout.setRefreshing(false);
|
||||||
|
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
|
ParsePost.parsePost(mExecutor, new Handler(), response.body(), new ParsePost.ParsePostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParsePostSuccess(Post post) {
|
public void onParsePostSuccess(Post post) {
|
||||||
mPost = post;
|
mPost = post;
|
||||||
@ -1420,7 +1421,7 @@ public class ViewPostDetailFragment extends Fragment implements FragmentCommunic
|
|||||||
} else {
|
} else {
|
||||||
retrofit = mOauthRetrofit;
|
retrofit = mOauthRetrofit;
|
||||||
}
|
}
|
||||||
FetchPost.fetchPost(retrofit, mPost.getId(), mAccessToken,
|
FetchPost.fetchPost(mExecutor, new Handler(), retrofit, mPost.getId(), mAccessToken,
|
||||||
new FetchPost.FetchPostListener() {
|
new FetchPost.FetchPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void fetchPostSuccess(Post post) {
|
public void fetchPostSuccess(Post post) {
|
||||||
|
@ -1,7 +1,11 @@
|
|||||||
package ml.docilealligator.infinityforreddit.post;
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
|
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||||
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
import ml.docilealligator.infinityforreddit.utils.APIUtils;
|
||||||
import retrofit2.Call;
|
import retrofit2.Call;
|
||||||
@ -10,7 +14,8 @@ import retrofit2.Response;
|
|||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public class FetchPost {
|
public class FetchPost {
|
||||||
public static void fetchPost(Retrofit retrofit, String id, String accessToken, FetchPostListener fetchPostListener) {
|
public static void fetchPost(Executor executor, Handler handler, Retrofit retrofit, String id, String accessToken,
|
||||||
|
FetchPostListener fetchPostListener) {
|
||||||
Call<String> postCall;
|
Call<String> postCall;
|
||||||
if (accessToken == null) {
|
if (accessToken == null) {
|
||||||
postCall = retrofit.create(RedditAPI.class).getPost(id);
|
postCall = retrofit.create(RedditAPI.class).getPost(id);
|
||||||
@ -21,7 +26,7 @@ public class FetchPost {
|
|||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
ParsePost.parsePost(response.body(), new ParsePost.ParsePostListener() {
|
ParsePost.parsePost(executor, handler, response.body(), new ParsePost.ParsePostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParsePostSuccess(Post post) {
|
public void onParsePostSuccess(Post post) {
|
||||||
fetchPostListener.fetchPostSuccess(post);
|
fetchPostListener.fetchPostSuccess(post);
|
||||||
@ -44,7 +49,8 @@ public class FetchPost {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void fetchRandomPost(Retrofit retrofit, boolean isNSFW, FetchRandomPostListener fetchRandomPostListener) {
|
public static void fetchRandomPost(Executor executor, Handler handler, Retrofit retrofit, boolean isNSFW,
|
||||||
|
FetchRandomPostListener fetchRandomPostListener) {
|
||||||
Call<String> call;
|
Call<String> call;
|
||||||
if (isNSFW) {
|
if (isNSFW) {
|
||||||
call = retrofit.create(RedditAPI.class).getRandomNSFWPost();
|
call = retrofit.create(RedditAPI.class).getRandomNSFWPost();
|
||||||
@ -56,8 +62,8 @@ public class FetchPost {
|
|||||||
@Override
|
@Override
|
||||||
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
new ParsePost.ParseRandomPostAsyncTask(response.body(), isNSFW, new ParsePost.ParseRandomPostListener() {
|
ParsePost.parseRandomPost(executor, handler, response.body(), isNSFW,
|
||||||
|
new ParsePost.ParseRandomPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParseRandomPostSuccess(String postId, String subredditName) {
|
public void onParseRandomPostSuccess(String postId, String subredditName) {
|
||||||
fetchRandomPostListener.fetchRandomPostSuccess(postId, subredditName);
|
fetchRandomPostListener.fetchRandomPostSuccess(postId, subredditName);
|
||||||
@ -67,7 +73,7 @@ public class FetchPost {
|
|||||||
public void onParseRandomPostFailed() {
|
public void onParseRandomPostFailed() {
|
||||||
fetchRandomPostListener.fetchRandomPostFailed();
|
fetchRandomPostListener.fetchRandomPostFailed();
|
||||||
}
|
}
|
||||||
}).execute();
|
});
|
||||||
} else {
|
} else {
|
||||||
fetchRandomPostListener.fetchRandomPostFailed();
|
fetchRandomPostListener.fetchRandomPostFailed();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,6 @@
|
|||||||
package ml.docilealligator.infinityforreddit.post;
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
import android.net.Uri;
|
import android.net.Uri;
|
||||||
import android.os.AsyncTask;
|
|
||||||
import android.os.Handler;
|
import android.os.Handler;
|
||||||
import android.text.Html;
|
import android.text.Html;
|
||||||
|
|
||||||
@ -31,13 +30,11 @@ public class ParsePost {
|
|||||||
PostFilter postFilter, List<ReadPost> readPostList,
|
PostFilter postFilter, List<ReadPost> readPostList,
|
||||||
ParsePostsListingListener parsePostsListingListener) {
|
ParsePostsListingListener parsePostsListingListener) {
|
||||||
executor.execute(() -> {
|
executor.execute(() -> {
|
||||||
boolean parseFailed = false;
|
|
||||||
LinkedHashSet<Post> newPosts = new LinkedHashSet<>();
|
LinkedHashSet<Post> newPosts = new LinkedHashSet<>();
|
||||||
String lastItem = null;
|
|
||||||
try {
|
try {
|
||||||
JSONObject jsonResponse = new JSONObject(response);
|
JSONObject jsonResponse = new JSONObject(response);
|
||||||
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
JSONArray allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
String lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
||||||
|
|
||||||
//Posts listing
|
//Posts listing
|
||||||
int size;
|
int size;
|
||||||
@ -67,24 +64,59 @@ public class ParsePost {
|
|||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
handler.post(() -> parsePostsListingListener.onParsePostsListingSuccess(newPosts, lastItem));
|
||||||
} catch (JSONException e) {
|
} catch (JSONException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
parseFailed = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!parseFailed) {
|
|
||||||
String finalLastItem = lastItem;
|
|
||||||
handler.post(() -> parsePostsListingListener.onParsePostsListingSuccess(newPosts, finalLastItem));
|
|
||||||
} else {
|
|
||||||
handler.post(parsePostsListingListener::onParsePostsListingFail);
|
handler.post(parsePostsListingListener::onParsePostsListingFail);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void parsePost(String response, ParsePostListener parsePostListener) {
|
public static void parsePost(Executor executor, Handler handler, String response, ParsePostListener parsePostListener) {
|
||||||
PostFilter postFilter = new PostFilter();
|
PostFilter postFilter = new PostFilter();
|
||||||
postFilter.allowNSFW = true;
|
postFilter.allowNSFW = true;
|
||||||
new ParsePostDataAsyncTask(response, postFilter, parsePostListener).execute();
|
|
||||||
|
executor.execute(() -> {
|
||||||
|
try {
|
||||||
|
JSONArray allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
|
if (allData.length() == 0) {
|
||||||
|
handler.post(parsePostListener::onParsePostFail);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||||
|
Post post = parseBasicData(data);
|
||||||
|
handler.post(() -> parsePostListener.onParsePostSuccess(post));
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
handler.post(parsePostListener::onParsePostFail);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public static void parseRandomPost(Executor executor, Handler handler, String response, boolean isNSFW,
|
||||||
|
ParseRandomPostListener parseRandomPostListener) {
|
||||||
|
executor.execute(() -> {
|
||||||
|
try {
|
||||||
|
JSONArray postsArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
||||||
|
if (postsArray.length() == 0) {
|
||||||
|
handler.post(parseRandomPostListener::onParseRandomPostFailed);
|
||||||
|
} else {
|
||||||
|
JSONObject post = postsArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
||||||
|
String subredditName = post.getString(JSONUtils.SUBREDDIT_KEY);
|
||||||
|
String postId;
|
||||||
|
if (isNSFW) {
|
||||||
|
postId = post.getString(JSONUtils.ID_KEY);
|
||||||
|
} else {
|
||||||
|
postId = post.getString(JSONUtils.LINK_ID_KEY).substring("t3_".length());
|
||||||
|
}
|
||||||
|
handler.post(() -> parseRandomPostListener.onParseRandomPostSuccess(postId, subredditName));
|
||||||
|
}
|
||||||
|
} catch (JSONException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
handler.post(parseRandomPostListener::onParseRandomPostFailed);
|
||||||
|
}
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private static Post parseBasicData(JSONObject data) throws JSONException {
|
private static Post parseBasicData(JSONObject data) throws JSONException {
|
||||||
@ -561,13 +593,11 @@ public class ParsePost {
|
|||||||
|
|
||||||
public interface ParsePostsListingListener {
|
public interface ParsePostsListingListener {
|
||||||
void onParsePostsListingSuccess(LinkedHashSet<Post> newPostData, String lastItem);
|
void onParsePostsListingSuccess(LinkedHashSet<Post> newPostData, String lastItem);
|
||||||
|
|
||||||
void onParsePostsListingFail();
|
void onParsePostsListingFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
public interface ParsePostListener {
|
public interface ParsePostListener {
|
||||||
void onParsePostSuccess(Post post);
|
void onParsePostSuccess(Post post);
|
||||||
|
|
||||||
void onParsePostFail();
|
void onParsePostFail();
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -575,166 +605,4 @@ public class ParsePost {
|
|||||||
void onParseRandomPostSuccess(String postId, String subredditName);
|
void onParseRandomPostSuccess(String postId, String subredditName);
|
||||||
void onParseRandomPostFailed();
|
void onParseRandomPostFailed();
|
||||||
}
|
}
|
||||||
|
|
||||||
private static class ParsePostDataAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
||||||
private JSONArray allData;
|
|
||||||
private int nPosts;
|
|
||||||
private PostFilter postFilter;
|
|
||||||
private List<ReadPost> readPostList;
|
|
||||||
private ParsePostsListingListener parsePostsListingListener;
|
|
||||||
private ParsePostListener parsePostListener;
|
|
||||||
private LinkedHashSet<Post> newPosts;
|
|
||||||
private Post post;
|
|
||||||
private String lastItem;
|
|
||||||
private boolean parseFailed;
|
|
||||||
|
|
||||||
ParsePostDataAsyncTask(String response, int nPosts, PostFilter postFilter, List<ReadPost> readPostList,
|
|
||||||
ParsePostsListingListener parsePostsListingListener) {
|
|
||||||
this.parsePostsListingListener = parsePostsListingListener;
|
|
||||||
try {
|
|
||||||
JSONObject jsonResponse = new JSONObject(response);
|
|
||||||
allData = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
|
||||||
lastItem = jsonResponse.getJSONObject(JSONUtils.DATA_KEY).getString(JSONUtils.AFTER_KEY);
|
|
||||||
this.nPosts = nPosts;
|
|
||||||
this.postFilter = postFilter;
|
|
||||||
this.readPostList = readPostList;
|
|
||||||
newPosts = new LinkedHashSet<>();
|
|
||||||
parseFailed = false;
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
parseFailed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
ParsePostDataAsyncTask(String response, PostFilter postFilter,
|
|
||||||
ParsePostListener parsePostListener) {
|
|
||||||
this.parsePostListener = parsePostListener;
|
|
||||||
try {
|
|
||||||
allData = new JSONArray(response).getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
|
||||||
this.postFilter = postFilter;
|
|
||||||
parseFailed = false;
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
parseFailed = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... voids) {
|
|
||||||
if (parseFailed) {
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (newPosts == null) {
|
|
||||||
//Only one post
|
|
||||||
if (allData.length() == 0) {
|
|
||||||
parseFailed = true;
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
try {
|
|
||||||
JSONObject data = allData.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
|
||||||
post = parseBasicData(data);
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
parseFailed = true;
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
//Posts listing
|
|
||||||
int size;
|
|
||||||
if (nPosts < 0 || nPosts > allData.length()) {
|
|
||||||
size = allData.length();
|
|
||||||
} else {
|
|
||||||
size = nPosts;
|
|
||||||
}
|
|
||||||
|
|
||||||
HashSet<ReadPost> readPostHashSet = null;
|
|
||||||
if (readPostList != null) {
|
|
||||||
readPostHashSet = new HashSet<>(readPostList);
|
|
||||||
}
|
|
||||||
for (int i = 0; i < size; i++) {
|
|
||||||
try {
|
|
||||||
if (allData.getJSONObject(i).getString(JSONUtils.KIND_KEY).equals("t3")) {
|
|
||||||
JSONObject data = allData.getJSONObject(i).getJSONObject(JSONUtils.DATA_KEY);
|
|
||||||
Post post = parseBasicData(data);
|
|
||||||
if (readPostHashSet != null && readPostHashSet.contains(ReadPost.convertPost(post))) {
|
|
||||||
post.markAsRead(false);
|
|
||||||
}
|
|
||||||
if (PostFilter.isPostAllowed(post, postFilter)) {
|
|
||||||
newPosts.add(post);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(Void aVoid) {
|
|
||||||
if (!parseFailed) {
|
|
||||||
if (newPosts != null) {
|
|
||||||
parsePostsListingListener.onParsePostsListingSuccess(newPosts, lastItem);
|
|
||||||
} else {
|
|
||||||
parsePostListener.onParsePostSuccess(post);
|
|
||||||
}
|
|
||||||
} else {
|
|
||||||
if (parsePostsListingListener != null) {
|
|
||||||
parsePostsListingListener.onParsePostsListingFail();
|
|
||||||
} else {
|
|
||||||
parsePostListener.onParsePostFail();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class ParseRandomPostAsyncTask extends AsyncTask<Void, Void, Void> {
|
|
||||||
|
|
||||||
private String response;
|
|
||||||
private boolean isNSFW;
|
|
||||||
private ParseRandomPostListener parseRandomPostListener;
|
|
||||||
private String subredditName;
|
|
||||||
private String postId;
|
|
||||||
private boolean parseFailed = false;
|
|
||||||
|
|
||||||
ParseRandomPostAsyncTask(String response, boolean isNSFW, ParseRandomPostListener parseRandomPostListener) {
|
|
||||||
this.response = response;
|
|
||||||
this.isNSFW = isNSFW;
|
|
||||||
this.parseRandomPostListener = parseRandomPostListener;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected Void doInBackground(Void... voids) {
|
|
||||||
try {
|
|
||||||
JSONArray postsArray = new JSONObject(response).getJSONObject(JSONUtils.DATA_KEY).getJSONArray(JSONUtils.CHILDREN_KEY);
|
|
||||||
if (postsArray.length() == 0) {
|
|
||||||
parseFailed = true;
|
|
||||||
} else {
|
|
||||||
JSONObject post = postsArray.getJSONObject(0).getJSONObject(JSONUtils.DATA_KEY);
|
|
||||||
subredditName = post.getString(JSONUtils.SUBREDDIT_KEY);
|
|
||||||
if (isNSFW) {
|
|
||||||
postId = post.getString(JSONUtils.ID_KEY);
|
|
||||||
} else {
|
|
||||||
postId = post.getString(JSONUtils.LINK_ID_KEY).substring("t3_".length());
|
|
||||||
}
|
|
||||||
}
|
|
||||||
} catch (JSONException e) {
|
|
||||||
e.printStackTrace();
|
|
||||||
parseFailed = true;
|
|
||||||
}
|
|
||||||
return null;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onPostExecute(Void aVoid) {
|
|
||||||
super.onPostExecute(aVoid);
|
|
||||||
if (parseFailed) {
|
|
||||||
parseRandomPostListener.onParseRandomPostFailed();
|
|
||||||
} else {
|
|
||||||
parseRandomPostListener.onParseRandomPostSuccess(postId, subredditName);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
package ml.docilealligator.infinityforreddit.post;
|
package ml.docilealligator.infinityforreddit.post;
|
||||||
|
|
||||||
import android.graphics.Bitmap;
|
import android.graphics.Bitmap;
|
||||||
|
import android.os.Handler;
|
||||||
|
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
|
|
||||||
@ -18,6 +19,7 @@ import java.io.StringReader;
|
|||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
import java.util.Locale;
|
import java.util.Locale;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import ml.docilealligator.infinityforreddit.Flair;
|
import ml.docilealligator.infinityforreddit.Flair;
|
||||||
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
|
||||||
@ -31,21 +33,21 @@ import retrofit2.Response;
|
|||||||
import retrofit2.Retrofit;
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
public class SubmitPost {
|
public class SubmitPost {
|
||||||
public static void submitTextOrLinkPost(Retrofit oauthRetrofit, String accessToken,
|
public static void submitTextOrLinkPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||||
Locale locale, String subredditName, String title, String content,
|
Locale locale, String subredditName, String title, String content,
|
||||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||||
SubmitPostListener submitPostListener) {
|
SubmitPostListener submitPostListener) {
|
||||||
submitPost(oauthRetrofit, accessToken, subredditName, title, content,
|
submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, content,
|
||||||
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitImagePost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
public static void submitImagePost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||||
String accessToken, String subredditName, String title, Bitmap image,
|
String accessToken, String subredditName, String title, Bitmap image,
|
||||||
Flair flair, boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) {
|
Flair flair, boolean isSpoiler, boolean isNSFW, SubmitPostListener submitPostListener) {
|
||||||
try {
|
try {
|
||||||
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image);
|
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, image);
|
||||||
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
||||||
submitPost(oauthRetrofit, accessToken,
|
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||||
subredditName, title, imageUrlOrError, flair, isSpoiler, isNSFW,
|
subredditName, title, imageUrlOrError, flair, isSpoiler, isNSFW,
|
||||||
APIUtils.KIND_IMAGE, null, submitPostListener);
|
APIUtils.KIND_IMAGE, null, submitPostListener);
|
||||||
} else {
|
} else {
|
||||||
@ -57,7 +59,7 @@ public class SubmitPost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitVideoPost(Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
public static void submitVideoPost(Executor executor, Handler handler, Retrofit oauthRetrofit, Retrofit uploadMediaRetrofit,
|
||||||
Retrofit uploadVideoRetrofit, String accessToken,
|
Retrofit uploadVideoRetrofit, String accessToken,
|
||||||
String subredditName, String title, File buffer, String mimeType,
|
String subredditName, String title, File buffer, String mimeType,
|
||||||
Bitmap posterBitmap, Flair flair, boolean isSpoiler, boolean isNSFW,
|
Bitmap posterBitmap, Flair flair, boolean isSpoiler, boolean isNSFW,
|
||||||
@ -96,11 +98,11 @@ public class SubmitPost {
|
|||||||
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap);
|
String imageUrlOrError = uploadImage(oauthRetrofit, uploadMediaRetrofit, accessToken, posterBitmap);
|
||||||
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
if (imageUrlOrError != null && !imageUrlOrError.startsWith("Error: ")) {
|
||||||
if (fileType.equals("gif")) {
|
if (fileType.equals("gif")) {
|
||||||
submitPost(oauthRetrofit, accessToken,
|
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||||
subredditName, title, url, flair, isSpoiler, isNSFW,
|
subredditName, title, url, flair, isSpoiler, isNSFW,
|
||||||
APIUtils.KIND_VIDEOGIF, imageUrlOrError, submitPostListener);
|
APIUtils.KIND_VIDEOGIF, imageUrlOrError, submitPostListener);
|
||||||
} else {
|
} else {
|
||||||
submitPost(oauthRetrofit, accessToken,
|
submitPost(executor, handler, oauthRetrofit, accessToken,
|
||||||
subredditName, title, url, flair, isSpoiler, isNSFW,
|
subredditName, title, url, flair, isSpoiler, isNSFW,
|
||||||
APIUtils.KIND_VIDEO, imageUrlOrError, submitPostListener);
|
APIUtils.KIND_VIDEO, imageUrlOrError, submitPostListener);
|
||||||
}
|
}
|
||||||
@ -119,15 +121,15 @@ public class SubmitPost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void submitCrosspost(Retrofit oauthRetrofit, String accessToken,
|
public static void submitCrosspost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||||
String subredditName, String title, String crosspostFullname,
|
String subredditName, String title, String crosspostFullname,
|
||||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||||
SubmitPostListener submitPostListener) {
|
SubmitPostListener submitPostListener) {
|
||||||
submitPost(oauthRetrofit, accessToken, subredditName, title, crosspostFullname,
|
submitPost(executor, handler, oauthRetrofit, accessToken, subredditName, title, crosspostFullname,
|
||||||
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
flair, isSpoiler, isNSFW, kind, null, submitPostListener);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void submitPost(Retrofit oauthRetrofit, String accessToken,
|
private static void submitPost(Executor executor, Handler handler, Retrofit oauthRetrofit, String accessToken,
|
||||||
String subredditName, String title, String content,
|
String subredditName, String title, String content,
|
||||||
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
Flair flair, boolean isSpoiler, boolean isNSFW, String kind,
|
||||||
@Nullable String posterUrl, SubmitPostListener submitPostListener) {
|
@Nullable String posterUrl, SubmitPostListener submitPostListener) {
|
||||||
@ -172,7 +174,8 @@ public class SubmitPost {
|
|||||||
try {
|
try {
|
||||||
Response<String> response = submitPostCall.execute();
|
Response<String> response = submitPostCall.execute();
|
||||||
if (response.isSuccessful()) {
|
if (response.isSuccessful()) {
|
||||||
getSubmittedPost(response.body(), kind, oauthRetrofit, accessToken, submitPostListener);
|
getSubmittedPost(executor, handler, response.body(), kind, oauthRetrofit, accessToken,
|
||||||
|
submitPostListener);
|
||||||
} else {
|
} else {
|
||||||
submitPostListener.submitFailed(response.message());
|
submitPostListener.submitFailed(response.message());
|
||||||
}
|
}
|
||||||
@ -216,8 +219,9 @@ public class SubmitPost {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static void getSubmittedPost(String response, String kind, Retrofit oauthRetrofit,
|
private static void getSubmittedPost(Executor executor, Handler handler, String response, String kind,
|
||||||
String accessToken, SubmitPostListener submitPostListener) throws JSONException, IOException {
|
Retrofit oauthRetrofit, String accessToken,
|
||||||
|
SubmitPostListener submitPostListener) throws JSONException, IOException {
|
||||||
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
|
JSONObject responseObject = new JSONObject(response).getJSONObject(JSONUtils.JSON_KEY);
|
||||||
if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
|
if (responseObject.getJSONArray(JSONUtils.ERRORS_KEY).length() != 0) {
|
||||||
JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
|
JSONArray error = responseObject.getJSONArray(JSONUtils.ERRORS_KEY)
|
||||||
@ -245,7 +249,7 @@ public class SubmitPost {
|
|||||||
Call<String> getPostCall = api.getPostOauth(postId, APIUtils.getOAuthHeader(accessToken));
|
Call<String> getPostCall = api.getPostOauth(postId, APIUtils.getOAuthHeader(accessToken));
|
||||||
Response<String> getPostCallResponse = getPostCall.execute();
|
Response<String> getPostCallResponse = getPostCall.execute();
|
||||||
if (getPostCallResponse.isSuccessful()) {
|
if (getPostCallResponse.isSuccessful()) {
|
||||||
ParsePost.parsePost(getPostCallResponse.body(), new ParsePost.ParsePostListener() {
|
ParsePost.parsePost(executor, handler, getPostCallResponse.body(), new ParsePost.ParsePostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void onParsePostSuccess(Post post) {
|
public void onParsePostSuccess(Post post) {
|
||||||
submitPostListener.submitSuccessful(post);
|
submitPostListener.submitSuccessful(post);
|
||||||
|
@ -33,6 +33,7 @@ import java.io.InputStream;
|
|||||||
import java.io.OutputStream;
|
import java.io.OutputStream;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
import java.util.concurrent.ExecutionException;
|
import java.util.concurrent.ExecutionException;
|
||||||
|
import java.util.concurrent.Executor;
|
||||||
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Named;
|
import javax.inject.Named;
|
||||||
@ -77,6 +78,9 @@ public class SubmitPostService extends Service {
|
|||||||
Retrofit mUploadVideoRetrofit;
|
Retrofit mUploadVideoRetrofit;
|
||||||
@Inject
|
@Inject
|
||||||
CustomThemeWrapper mCustomThemeWrapper;
|
CustomThemeWrapper mCustomThemeWrapper;
|
||||||
|
@Inject
|
||||||
|
Executor mExecutor;
|
||||||
|
private Handler handler;
|
||||||
private ServiceHandler serviceHandler;
|
private ServiceHandler serviceHandler;
|
||||||
|
|
||||||
public SubmitPostService() {
|
public SubmitPostService() {
|
||||||
@ -111,7 +115,8 @@ public class SubmitPostService extends Service {
|
|||||||
submitTextOrLinkPost(accessToken, subredditName, title, content, flair, isSpoiler, isNSFW, kind);
|
submitTextOrLinkPost(accessToken, subredditName, title, content, flair, isSpoiler, isNSFW, kind);
|
||||||
} else if (postType == EXTRA_POST_TYPE_CROSSPOST) {
|
} else if (postType == EXTRA_POST_TYPE_CROSSPOST) {
|
||||||
String content = bundle.getString(EXTRA_CONTENT);
|
String content = bundle.getString(EXTRA_CONTENT);
|
||||||
submitCrosspost(accessToken, subredditName, title, content, flair, isSpoiler, isNSFW);
|
submitCrosspost(mExecutor, handler, accessToken, subredditName, title, content,
|
||||||
|
flair, isSpoiler, isNSFW);
|
||||||
} else if (postType == EXTRA_POST_TYPE_IMAGE) {
|
} else if (postType == EXTRA_POST_TYPE_IMAGE) {
|
||||||
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
|
Uri mediaUri = Uri.parse(bundle.getString(EXTRA_MEDIA_URI));
|
||||||
submitImagePost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW);
|
submitImagePost(accessToken, mediaUri, subredditName, title, flair, isSpoiler, isNSFW);
|
||||||
@ -129,6 +134,7 @@ public class SubmitPostService extends Service {
|
|||||||
// separate thread because the service normally runs in the process's
|
// separate thread because the service normally runs in the process's
|
||||||
// main thread, which we don't want to block. We also make it
|
// main thread, which we don't want to block. We also make it
|
||||||
// background priority so CPU-intensive work doesn't disrupt our UI.
|
// background priority so CPU-intensive work doesn't disrupt our UI.
|
||||||
|
handler = new Handler();
|
||||||
HandlerThread thread = new HandlerThread("ServiceStartArguments",
|
HandlerThread thread = new HandlerThread("ServiceStartArguments",
|
||||||
Process.THREAD_PRIORITY_BACKGROUND);
|
Process.THREAD_PRIORITY_BACKGROUND);
|
||||||
thread.start();
|
thread.start();
|
||||||
@ -185,8 +191,9 @@ public class SubmitPostService extends Service {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private void submitTextOrLinkPost(String accessToken, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, String kind) {
|
private void submitTextOrLinkPost(String accessToken, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW, String kind) {
|
||||||
SubmitPost.submitTextOrLinkPost(mOauthRetrofit, accessToken, getResources().getConfiguration().locale,
|
SubmitPost.submitTextOrLinkPost(mExecutor, handler, mOauthRetrofit, accessToken,
|
||||||
subredditName, title, content, flair, isSpoiler, isNSFW, kind, new SubmitPost.SubmitPostListener() {
|
getResources().getConfiguration().locale, subredditName, title, content, flair, isSpoiler,
|
||||||
|
isNSFW, kind, new SubmitPost.SubmitPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void submitSuccessful(Post post) {
|
public void submitSuccessful(Post post) {
|
||||||
EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null));
|
EventBus.getDefault().post(new SubmitTextOrLinkPostEvent(true, post, null));
|
||||||
@ -203,9 +210,10 @@ public class SubmitPostService extends Service {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private void submitCrosspost(String accessToken, String subredditName, String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
private void submitCrosspost(Executor executor, Handler handler, String accessToken, String subredditName,
|
||||||
SubmitPost.submitCrosspost(mOauthRetrofit, accessToken, subredditName, title, content, flair, isSpoiler,
|
String title, String content, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
||||||
isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() {
|
SubmitPost.submitCrosspost(executor, handler, mOauthRetrofit, accessToken, subredditName, title,
|
||||||
|
content, flair, isSpoiler, isNSFW, APIUtils.KIND_CROSSPOST, new SubmitPost.SubmitPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void submitSuccessful(Post post) {
|
public void submitSuccessful(Post post) {
|
||||||
EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null));
|
EventBus.getDefault().post(new SubmitCrosspostEvent(true, post, null));
|
||||||
@ -225,8 +233,9 @@ public class SubmitPostService extends Service {
|
|||||||
private void submitImagePost(String accessToken, Uri mediaUri, String subredditName, String title, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
private void submitImagePost(String accessToken, Uri mediaUri, String subredditName, String title, Flair flair, boolean isSpoiler, boolean isNSFW) {
|
||||||
try {
|
try {
|
||||||
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
||||||
SubmitPost.submitImagePost(mOauthRetrofit, mUploadMediaRetrofit, accessToken, subredditName, title, resource,
|
SubmitPost.submitImagePost(mExecutor, handler, mOauthRetrofit, mUploadMediaRetrofit,
|
||||||
flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
accessToken, subredditName, title, resource, flair, isSpoiler, isNSFW,
|
||||||
|
new SubmitPost.SubmitPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void submitSuccessful(Post post) {
|
public void submitSuccessful(Post post) {
|
||||||
EventBus.getDefault().post(new SubmitImagePostEvent(true, null));
|
EventBus.getDefault().post(new SubmitImagePostEvent(true, null));
|
||||||
@ -266,9 +275,9 @@ public class SubmitPostService extends Service {
|
|||||||
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
Bitmap resource = Glide.with(this).asBitmap().load(mediaUri).submit().get();
|
||||||
|
|
||||||
if (type != null) {
|
if (type != null) {
|
||||||
SubmitPost.submitVideoPost(mOauthRetrofit, mUploadMediaRetrofit, mUploadVideoRetrofit,
|
SubmitPost.submitVideoPost(mExecutor, handler, mOauthRetrofit, mUploadMediaRetrofit,
|
||||||
accessToken, subredditName, title, new File(cacheFilePath), type, resource, flair,
|
mUploadVideoRetrofit, accessToken, subredditName, title, new File(cacheFilePath),
|
||||||
isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
type, resource, flair, isSpoiler, isNSFW, new SubmitPost.SubmitPostListener() {
|
||||||
@Override
|
@Override
|
||||||
public void submitSuccessful(Post post) {
|
public void submitSuccessful(Post post) {
|
||||||
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null));
|
EventBus.getDefault().post(new SubmitVideoOrGifPostEvent(true, false, null));
|
||||||
|
Loading…
Reference in New Issue
Block a user