Replace AsyncTask with RxJava
This commit is contained in:
parent
b182deaadc
commit
1193cd400e
|
@ -21,8 +21,8 @@ import androidx.appcompat.app.AlertDialog;
|
|||
import androidx.appcompat.app.AppCompatActivity;
|
||||
import androidx.core.app.ActivityCompat;
|
||||
import de.danoeh.antennapod.R;
|
||||
import de.danoeh.antennapod.asynctask.OpmlImportWorker;
|
||||
import de.danoeh.antennapod.core.export.opml.OpmlElement;
|
||||
import de.danoeh.antennapod.core.export.opml.OpmlReader;
|
||||
import de.danoeh.antennapod.core.preferences.UserPreferences;
|
||||
|
||||
import de.danoeh.antennapod.core.storage.DownloadRequestException;
|
||||
|
@ -30,6 +30,7 @@ import de.danoeh.antennapod.core.storage.DownloadRequester;
|
|||
import de.danoeh.antennapod.databinding.OpmlSelectionBinding;
|
||||
import de.danoeh.antennapod.model.feed.Feed;
|
||||
import io.reactivex.Completable;
|
||||
import io.reactivex.Observable;
|
||||
import io.reactivex.android.schedulers.AndroidSchedulers;
|
||||
import io.reactivex.schedulers.Schedulers;
|
||||
import org.apache.commons.io.ByteOrderMark;
|
||||
|
@ -53,6 +54,7 @@ public class OpmlImportActivity extends AppCompatActivity {
|
|||
private ArrayAdapter<String> listAdapter;
|
||||
private MenuItem selectAll;
|
||||
private MenuItem deselectAll;
|
||||
private ArrayList<OpmlElement> readElements;
|
||||
|
||||
@Override
|
||||
protected void onCreate(@Nullable Bundle savedInstanceState) {
|
||||
|
@ -71,7 +73,7 @@ public class OpmlImportActivity extends AppCompatActivity {
|
|||
checkedCount++;
|
||||
}
|
||||
}
|
||||
if(checkedCount == listAdapter.getCount()) {
|
||||
if (checkedCount == listAdapter.getCount()) {
|
||||
selectAll.setVisible(false);
|
||||
deselectAll.setVisible(true);
|
||||
} else {
|
||||
|
@ -92,7 +94,7 @@ public class OpmlImportActivity extends AppCompatActivity {
|
|||
if (!checked.valueAt(i)) {
|
||||
continue;
|
||||
}
|
||||
OpmlElement element = OpmlImportHolder.getReadElements().get(checked.keyAt(i));
|
||||
OpmlElement element = readElements.get(checked.keyAt(i));
|
||||
Feed feed = new Feed(element.getXmlUrl(), null, element.getText());
|
||||
try {
|
||||
requester.downloadFeed(getApplicationContext(), feed);
|
||||
|
@ -151,8 +153,8 @@ public class OpmlImportActivity extends AppCompatActivity {
|
|||
|
||||
private List<String> getTitleList() {
|
||||
List<String> result = new ArrayList<>();
|
||||
if (OpmlImportHolder.getReadElements() != null) {
|
||||
for (OpmlElement element : OpmlImportHolder.getReadElements()) {
|
||||
if (readElements != null) {
|
||||
for (OpmlElement element : readElements) {
|
||||
result.add(element.getText());
|
||||
}
|
||||
}
|
||||
|
@ -220,39 +222,36 @@ public class OpmlImportActivity extends AppCompatActivity {
|
|||
/** Starts the import process. */
|
||||
private void startImport() {
|
||||
viewBinding.progressBar.setVisibility(View.VISIBLE);
|
||||
try {
|
||||
|
||||
Observable.fromCallable(() -> {
|
||||
InputStream opmlFileStream = getContentResolver().openInputStream(uri);
|
||||
BOMInputStream bomInputStream = new BOMInputStream(opmlFileStream);
|
||||
ByteOrderMark bom = bomInputStream.getBOM();
|
||||
String charsetName = (bom == null) ? "UTF-8" : bom.getCharsetName();
|
||||
Reader reader = new InputStreamReader(bomInputStream, charsetName);
|
||||
|
||||
OpmlImportWorker importWorker = new OpmlImportWorker(this, reader) {
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<OpmlElement> result) {
|
||||
super.onPostExecute(result);
|
||||
if (result != null) {
|
||||
Log.d(TAG, "Parsing was successful");
|
||||
OpmlImportHolder.setReadElements(result);
|
||||
listAdapter = new ArrayAdapter<>(OpmlImportActivity.this,
|
||||
android.R.layout.simple_list_item_multiple_choice,
|
||||
getTitleList());
|
||||
viewBinding.feedlist.setAdapter(listAdapter);
|
||||
} else {
|
||||
Log.d(TAG, "Parser error occurred");
|
||||
}
|
||||
viewBinding.progressBar.setVisibility(View.GONE);
|
||||
}
|
||||
};
|
||||
importWorker.executeAsync();
|
||||
} catch (Exception e) {
|
||||
Log.d(TAG, Log.getStackTraceString(e));
|
||||
String message = getString(R.string.opml_reader_error);
|
||||
new AlertDialog.Builder(this)
|
||||
.setMessage(message + " " + e.getMessage())
|
||||
.setPositiveButton(android.R.string.ok, null)
|
||||
.show();
|
||||
}
|
||||
OpmlReader opmlReader = new OpmlReader();
|
||||
ArrayList<OpmlElement> result = opmlReader.readDocument(reader);
|
||||
reader.close();
|
||||
return result;
|
||||
})
|
||||
.subscribeOn(Schedulers.io())
|
||||
.observeOn(AndroidSchedulers.mainThread())
|
||||
.subscribe(
|
||||
result -> {
|
||||
viewBinding.progressBar.setVisibility(View.GONE);
|
||||
Log.d(TAG, "Parsing was successful");
|
||||
readElements = result;
|
||||
listAdapter = new ArrayAdapter<>(OpmlImportActivity.this,
|
||||
android.R.layout.simple_list_item_multiple_choice,
|
||||
getTitleList());
|
||||
viewBinding.feedlist.setAdapter(listAdapter);
|
||||
}, e -> {
|
||||
viewBinding.progressBar.setVisibility(View.GONE);
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(this);
|
||||
alert.setTitle(R.string.error_label);
|
||||
alert.setMessage(getString(R.string.opml_reader_error) + e.getMessage());
|
||||
alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss());
|
||||
alert.create().show();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,31 +0,0 @@
|
|||
package de.danoeh.antennapod.activity;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.danoeh.antennapod.core.export.opml.OpmlElement;
|
||||
|
||||
/**
|
||||
* Hold infos gathered by Ompl-Import
|
||||
* <p/>
|
||||
* Created with IntelliJ IDEA.
|
||||
* User: ligi
|
||||
* Date: 1/23/13
|
||||
* Time: 2:15 PM
|
||||
*/
|
||||
public class OpmlImportHolder {
|
||||
|
||||
private OpmlImportHolder(){}
|
||||
|
||||
private static ArrayList<OpmlElement> readElements;
|
||||
|
||||
public static ArrayList<OpmlElement> getReadElements() {
|
||||
return readElements;
|
||||
}
|
||||
|
||||
public static void setReadElements(ArrayList<OpmlElement> _readElements) {
|
||||
readElements = _readElements;
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
|
@ -1,93 +0,0 @@
|
|||
package de.danoeh.antennapod.asynctask;
|
||||
|
||||
import android.app.ProgressDialog;
|
||||
import android.content.Context;
|
||||
import android.os.AsyncTask;
|
||||
import androidx.appcompat.app.AlertDialog;
|
||||
import android.util.Log;
|
||||
|
||||
import org.xmlpull.v1.XmlPullParserException;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Reader;
|
||||
import java.util.ArrayList;
|
||||
|
||||
import de.danoeh.antennapod.core.R;
|
||||
import de.danoeh.antennapod.core.export.opml.OpmlElement;
|
||||
import de.danoeh.antennapod.core.export.opml.OpmlReader;
|
||||
|
||||
public class OpmlImportWorker extends AsyncTask<Void, Void, ArrayList<OpmlElement>> {
|
||||
private static final String TAG = "OpmlImportWorker";
|
||||
|
||||
private final Context context;
|
||||
private Exception exception;
|
||||
private ProgressDialog progDialog;
|
||||
|
||||
private final Reader reader;
|
||||
|
||||
public OpmlImportWorker(Context context, Reader reader) {
|
||||
super();
|
||||
this.context = context;
|
||||
this.reader = reader;
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ArrayList<OpmlElement> doInBackground(Void... params) {
|
||||
Log.d(TAG, "Starting background work");
|
||||
|
||||
if (reader == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
OpmlReader opmlReader = new OpmlReader();
|
||||
try {
|
||||
ArrayList<OpmlElement> result = opmlReader.readDocument(reader);
|
||||
reader.close();
|
||||
return result;
|
||||
} catch (XmlPullParserException e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
return null;
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
exception = e;
|
||||
return null;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPostExecute(ArrayList<OpmlElement> result) {
|
||||
if (reader != null) {
|
||||
try {
|
||||
reader.close();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
progDialog.dismiss();
|
||||
if (exception != null) {
|
||||
Log.d(TAG, "An error occurred while trying to parse the opml document");
|
||||
AlertDialog.Builder alert = new AlertDialog.Builder(context);
|
||||
alert.setTitle(R.string.error_label);
|
||||
alert.setMessage(context.getString(R.string.opml_reader_error)
|
||||
+ exception.getMessage());
|
||||
alert.setNeutralButton(android.R.string.ok, (dialog, which) -> dialog.dismiss());
|
||||
alert.create().show();
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
protected void onPreExecute() {
|
||||
progDialog = new ProgressDialog(context);
|
||||
progDialog.setMessage(context.getString(R.string.please_wait));
|
||||
progDialog.setIndeterminate(true);
|
||||
progDialog.setCancelable(false);
|
||||
progDialog.show();
|
||||
}
|
||||
|
||||
public void executeAsync() {
|
||||
executeOnExecutor(THREAD_POOL_EXECUTOR);
|
||||
}
|
||||
|
||||
}
|
|
@ -105,7 +105,6 @@
|
|||
<string name="shownotes_label">Shownotes</string>
|
||||
<string name="shownotes_contentdescription">swipe up to read shownotes</string>
|
||||
<string name="episodes_suffix">\u0020episodes</string>
|
||||
<string name="processing_label">Processing</string>
|
||||
<string name="close_label">Close</string>
|
||||
<string name="retry_label">Retry</string>
|
||||
<string name="auto_download_label">Include in auto downloads</string>
|
||||
|
|
Loading…
Reference in New Issue