aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlex Ning <chineseperson5@gmail.com>2021-06-23 12:45:46 +0000
committerAlex Ning <chineseperson5@gmail.com>2021-06-23 12:45:46 +0000
commit7067a20696aebb5f43a5e33430f31224a4d33032 (patch)
tree081933ebdb68c58a2189413c04ddf3790a388f52
parentade1097f302b1d07fd3d0456517d79a83c30d576 (diff)
downloadinfinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar.gz
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar.bz2
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar.lz
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar.xz
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.tar.zst
infinity-for-reddit-7067a20696aebb5f43a5e33430f31224a4d33032.zip
Replace AsyncTask with Executor in FetchRules.
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java50
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java6
-rw-r--r--app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java95
3 files changed, 25 insertions, 126 deletions
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java
index 69f0fa58..e1f9c5e7 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/FetchRules.java
@@ -1,6 +1,6 @@
package ml.docilealligator.infinityforreddit;
-import android.os.AsyncTask;
+import android.os.Handler;
import androidx.annotation.NonNull;
@@ -9,6 +9,7 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
+import java.util.concurrent.Executor;
import ml.docilealligator.infinityforreddit.apis.RedditAPI;
import ml.docilealligator.infinityforreddit.utils.JSONUtils;
@@ -24,25 +25,25 @@ public class FetchRules {
void failed();
}
- public static void fetchRules(Retrofit retrofit, String subredditName, FetchRulesListener fetchRulesListener) {
-
+ public static void fetchRules(Executor executor, Handler handler, Retrofit retrofit, String subredditName,
+ FetchRulesListener fetchRulesListener) {
RedditAPI api = retrofit.create(RedditAPI.class);
Call<String> rulesCall = api.getRules(subredditName);
rulesCall.enqueue(new Callback<String>() {
@Override
public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
if (response.isSuccessful()) {
- new ParseRulesAsyncTask(response.body(), new ParseRulesAsyncTask.ParseRulesAsyncTaskListener() {
+ parseRules(executor, handler, response.body(), new FetchRulesListener() {
@Override
- public void parseSuccessful(ArrayList<Rule> rules) {
+ public void success(ArrayList<Rule> rules) {
fetchRulesListener.success(rules);
}
@Override
- public void parseFailed() {
+ public void failed() {
fetchRulesListener.failed();
}
- }).execute();
+ });
} else {
fetchRulesListener.failed();
}
@@ -55,17 +56,9 @@ public class FetchRules {
});
}
- private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> {
- private String response;
- private ParseRulesAsyncTask.ParseRulesAsyncTaskListener parseRulesAsyncTaskListener;
-
- ParseRulesAsyncTask(String response, ParseRulesAsyncTask.ParseRulesAsyncTaskListener parseRulesAsyncTaskListener) {
- this.response = response;
- this.parseRulesAsyncTaskListener = parseRulesAsyncTaskListener;
- }
-
- @Override
- protected ArrayList<Rule> doInBackground(Void... voids) {
+ private static void parseRules(Executor executor, Handler handler, String response,
+ FetchRulesListener fetchRulesListener) {
+ executor.execute(() -> {
try {
JSONArray rulesArray = new JSONObject(response).getJSONArray(JSONUtils.RULES_KEY);
ArrayList<Rule> rules = new ArrayList<>();
@@ -77,26 +70,11 @@ public class FetchRules {
}
rules.add(new Rule(shortName, description));
}
- return rules;
+ handler.post(() -> fetchRulesListener.success(rules));
} catch (JSONException e) {
e.printStackTrace();
+ handler.post(fetchRulesListener::failed);
}
- return null;
- }
-
- @Override
- protected void onPostExecute(ArrayList<Rule> rules) {
- if (rules != null) {
- parseRulesAsyncTaskListener.parseSuccessful(rules);
- } else {
- parseRulesAsyncTaskListener.parseFailed();
- }
- }
-
- interface ParseRulesAsyncTaskListener {
- void parseSuccessful(ArrayList<Rule> rules);
-
- void parseFailed();
- }
+ });
}
}
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java
index 0c88bcb4..c13785bf 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/ReportActivity.java
@@ -3,6 +3,7 @@ package ml.docilealligator.infinityforreddit.activities;
import android.content.SharedPreferences;
import android.os.Build;
import android.os.Bundle;
+import android.os.Handler;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Toast;
@@ -18,6 +19,7 @@ import com.google.android.material.snackbar.Snackbar;
import com.r0adkll.slidr.Slidr;
import java.util.ArrayList;
+import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@@ -67,6 +69,8 @@ public class ReportActivity extends BaseActivity {
RedditDataRoomDatabase mRedditDataRoomDatabase;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
+ @Inject
+ Executor mExecutor;
private String mAccessToken;
private String mFullname;
private String mSubredditName;
@@ -117,7 +121,7 @@ public class ReportActivity extends BaseActivity {
recyclerView.setAdapter(mAdapter);
if (rulesReasons == null) {
- FetchRules.fetchRules(mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
+ FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
@Override
public void success(ArrayList<Rule> rules) {
mAdapter.setRules(ReportReason.convertRulesToReasons(rules));
diff --git a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java
index 0e3021e9..dfc8da16 100644
--- a/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java
+++ b/app/src/main/java/ml/docilealligator/infinityforreddit/activities/RulesActivity.java
@@ -4,6 +4,7 @@ import android.content.SharedPreferences;
import android.content.res.ColorStateList;
import android.os.Build;
import android.os.Bundle;
+import android.os.Handler;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
@@ -24,6 +25,7 @@ import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
+import java.util.concurrent.Executor;
import javax.inject.Inject;
import javax.inject.Named;
@@ -64,6 +66,8 @@ public class RulesActivity extends BaseActivity {
SharedPreferences mSharedPreferences;
@Inject
CustomThemeWrapper mCustomThemeWrapper;
+ @Inject
+ Executor mExecutor;
private String mSubredditName;
private RulesRecyclerViewAdapter mAdapter;
@@ -119,9 +123,7 @@ public class RulesActivity extends BaseActivity {
mAdapter = new RulesRecyclerViewAdapter(this, mCustomThemeWrapper);
recyclerView.setAdapter(mAdapter);
- //fetchRules();
-
- FetchRules.fetchRules(mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
+ FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
@Override
public void success(ArrayList<Rule> rules) {
progressBar.setVisibility(View.GONE);
@@ -159,46 +161,6 @@ public class RulesActivity extends BaseActivity {
errorTextView.setTextColor(mCustomThemeWrapper.getSecondaryTextColor());
}
- /*private void fetchRules() {
- progressBar.setVisibility(View.VISIBLE);
- errorTextView.setVisibility(View.GONE);
-
- RedditAPI api = mRetrofit.create(RedditAPI.class);
- Call<String> rulesCall = api.getRules(mSubredditName);
- rulesCall.enqueue(new Callback<String>() {
- @Override
- public void onResponse(@NonNull Call<String> call, @NonNull Response<String> response) {
- if (response.isSuccessful()) {
- new ParseRulesAsyncTask(response.body(), new ParseRulesAsyncTask.ParseRulesAsyncTaskListener() {
- @Override
- public void parseSuccessful(ArrayList<Rule> rules) {
- progressBar.setVisibility(View.GONE);
- if (rules == null || rules.size() == 0) {
- errorTextView.setVisibility(View.VISIBLE);
- errorTextView.setText(R.string.no_rule);
- errorTextView.setOnClickListener(view -> {
- });
- }
- mAdapter.changeDataset(rules);
- }
-
- @Override
- public void parseFailed() {
- displayError();
- }
- }).execute();
- } else {
- displayError();
- }
- }
-
- @Override
- public void onFailure(@NonNull Call<String> call, @NonNull Throwable t) {
- displayError();
- }
- });
- }*/
-
private void displayError() {
progressBar.setVisibility(View.GONE);
errorTextView.setVisibility(View.VISIBLE);
@@ -206,7 +168,7 @@ public class RulesActivity extends BaseActivity {
errorTextView.setOnClickListener(view -> {
progressBar.setVisibility(View.VISIBLE);
errorTextView.setVisibility(View.GONE);
- FetchRules.fetchRules(mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
+ FetchRules.fetchRules(mExecutor, new Handler(), mRetrofit, mSubredditName, new FetchRules.FetchRulesListener() {
@Override
public void success(ArrayList<Rule> rules) {
progressBar.setVisibility(View.GONE);
@@ -247,49 +209,4 @@ public class RulesActivity extends BaseActivity {
public void onAccountSwitchEvent(SwitchAccountEvent event) {
finish();
}
-
- /*private static class ParseRulesAsyncTask extends AsyncTask<Void, ArrayList<Rule>, ArrayList<Rule>> {
- private String response;
- private ParseRulesAsyncTaskListener parseRulesAsyncTaskListener;
-
- ParseRulesAsyncTask(String response, ParseRulesAsyncTaskListener parseRulesAsyncTaskListener) {
- this.response = response;
- this.parseRulesAsyncTaskListener = parseRulesAsyncTaskListener;
- }
-
- @Override
- protected ArrayList<Rule> doInBackground(Void... voids) {
- try {
- JSONArray rulesArray = new JSONObject(response).getJSONArray(JSONUtils.RULES_KEY);
- ArrayList<Rule> rules = new ArrayList<>();
- for (int i = 0; i < rulesArray.length(); i++) {
- String shortName = rulesArray.getJSONObject(i).getString(JSONUtils.SHORT_NAME_KEY);
- String description = null;
- if (rulesArray.getJSONObject(i).has(JSONUtils.DESCRIPTION_KEY)) {
- description = Utils.modifyMarkdown(rulesArray.getJSONObject(i).getString(JSONUtils.DESCRIPTION_KEY));
- }
- rules.add(new Rule(shortName, description));
- }
- return rules;
- } catch (JSONException e) {
- e.printStackTrace();
- }
- return null;
- }
-
- @Override
- protected void onPostExecute(ArrayList<Rule> rules) {
- if (rules != null) {
- parseRulesAsyncTaskListener.parseSuccessful(rules);
- } else {
- parseRulesAsyncTaskListener.parseFailed();
- }
- }
-
- interface ParseRulesAsyncTaskListener {
- void parseSuccessful(ArrayList<Rule> rules);
-
- void parseFailed();
- }
- }*/
}