put parseUrl logic in the viewModel

This commit is contained in:
Shinokuni 2019-03-10 22:17:51 +01:00
parent bc263a05f8
commit 5aa6496d1e
2 changed files with 23 additions and 13 deletions

View File

@ -147,19 +147,8 @@ public class AddFeedActivity extends AppCompatActivity implements View.OnClickLi
else else
finalUrl = url; finalUrl = url;
Single.create((SingleOnSubscribe<List<ParsingResult>>) emitter -> { viewModel.parseUrl(finalUrl)
RSSNetwork rssApi = new RSSNetwork(); .subscribeOn(Schedulers.io())
List<ParsingResult> results = new ArrayList<>();
if (rssApi.isUrlFeedLink(finalUrl)) {
ParsingResult parsingResult = new ParsingResult(finalUrl, null);
results.add(parsingResult);
} else
results.addAll(HtmlParser.getFeedLink(finalUrl));
emitter.onSuccess(results);
}).subscribeOn(Schedulers.io())
.observeOn(AndroidSchedulers.mainThread()) .observeOn(AndroidSchedulers.mainThread())
.subscribe(new SingleObserver<List<ParsingResult>>() { .subscribe(new SingleObserver<List<ParsingResult>>() {
@Override @Override

View File

@ -5,11 +5,16 @@ import android.arch.lifecycle.AndroidViewModel;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import com.readrops.app.repositories.LocalFeedRepository; import com.readrops.app.repositories.LocalFeedRepository;
import com.readrops.app.utils.HtmlParser;
import com.readrops.app.utils.ParsingResult; import com.readrops.app.utils.ParsingResult;
import com.readrops.readropslibrary.localfeed.RSSNetwork;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import io.reactivex.Completable; import io.reactivex.Completable;
import io.reactivex.Single;
import io.reactivex.SingleOnSubscribe;
public class AddFeedsViewModel extends AndroidViewModel { public class AddFeedsViewModel extends AndroidViewModel {
@ -24,4 +29,20 @@ public class AddFeedsViewModel extends AndroidViewModel {
public Completable addFeeds(List<ParsingResult> results) { public Completable addFeeds(List<ParsingResult> results) {
return repository.addFeeds(results); return repository.addFeeds(results);
} }
public Single<List<ParsingResult>> parseUrl(String url) {
return Single.create(emitter -> {
RSSNetwork rssApi = new RSSNetwork();
List<ParsingResult> results = new ArrayList<>();
if (rssApi.isUrlFeedLink(url)) {
ParsingResult parsingResult = new ParsingResult(url, null);
results.add(parsingResult);
} else
results.addAll(HtmlParser.getFeedLink(url));
emitter.onSuccess(results);
});
}
} }