use of io.reactivex.SingleOnSubscribe in project kripton by xcesco.
the class BindKripton180RawInsertSelectDataSource method execute.
public <T> Single<T> execute(final SingleTransaction<T> transaction) {
SingleOnSubscribe<T> emitter = new SingleOnSubscribe<T>() {
@Override
public void subscribe(SingleEmitter<T> emitter) {
boolean needToOpened = !BindKripton180RawInsertSelectDataSource.this.isOpenInWriteMode();
boolean success = false;
@SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
// no onComplete;
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
if (needToOpened) {
close();
}
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Single<T> result = Single.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
use of io.reactivex.SingleOnSubscribe in project kripton by xcesco.
the class BindApp0DataSource method execute.
public <T> Single<T> execute(final SingleTransaction<T> transaction) {
SingleOnSubscribe<T> emitter = new SingleOnSubscribe<T>() {
@Override
public void subscribe(SingleEmitter<T> emitter) {
boolean needToOpened = !BindApp0DataSource.this.isOpenInWriteMode();
boolean success = false;
@SuppressWarnings("resource") SQLiteDatabase connection = needToOpened ? openWritableDatabase() : database();
DataSourceSingleThread currentDaoFactory = _daoFactorySingleThread.bindToThread();
currentDaoFactory.onSessionOpened();
try {
connection.beginTransaction();
if (transaction != null && TransactionResult.COMMIT == transaction.onExecute(currentDaoFactory, emitter)) {
connection.setTransactionSuccessful();
success = true;
}
// no onComplete;
} catch (Throwable e) {
Logger.error(e.getMessage());
e.printStackTrace();
emitter.onError(e);
currentDaoFactory.onSessionClear();
} finally {
try {
connection.endTransaction();
} catch (Throwable e) {
}
if (needToOpened) {
close();
}
if (success) {
currentDaoFactory.onSessionClosed();
} else {
currentDaoFactory.onSessionClear();
}
}
return;
}
};
Single<T> result = Single.create(emitter);
if (globalSubscribeOn != null)
result.subscribeOn(globalSubscribeOn);
if (globalObserveOn != null)
result.observeOn(globalObserveOn);
return result;
}
use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.
the class SpecialThanksFragment method onViewCreated.
@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setDivider(null);
getListView().setSelector(android.R.color.transparent);
translatorsLoader = Single.create((SingleOnSubscribe<ArrayList<SimpleIconListAdapter.ListItem>>) emitter -> {
ArrayList<SimpleIconListAdapter.ListItem> translators = new ArrayList<>();
BufferedReader reader = new BufferedReader(new InputStreamReader(getContext().getAssets().open("special_thanks.csv"), "UTF-8"));
String line;
while ((line = reader.readLine()) != null) {
String[] info = line.split(";");
translators.add(new SimpleIconListAdapter.ListItem(info[0], info[1], info[2]));
}
emitter.onSuccess(translators);
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(translators -> setListAdapter(new SimpleIconListAdapter<>(getContext(), translators)), error -> Toast.makeText(getContext(), error.getMessage(), Toast.LENGTH_LONG).show());
}
use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.
the class CombinedSearcher method search.
public Single<List<PodcastSearchResult>> search(String query) {
ArrayList<Disposable> disposables = new ArrayList<>();
List<List<PodcastSearchResult>> singleResults = new ArrayList<>(Collections.nCopies(PodcastSearcherRegistry.getSearchProviders().size(), null));
CountDownLatch latch = new CountDownLatch(PodcastSearcherRegistry.getSearchProviders().size());
for (int i = 0; i < PodcastSearcherRegistry.getSearchProviders().size(); i++) {
PodcastSearcherRegistry.SearcherInfo searchProviderInfo = PodcastSearcherRegistry.getSearchProviders().get(i);
PodcastSearcher searcher = searchProviderInfo.searcher;
if (searchProviderInfo.weight <= 0.00001f || searcher.getClass() == CombinedSearcher.class) {
latch.countDown();
continue;
}
final int index = i;
disposables.add(searcher.search(query).subscribe(e -> {
singleResults.set(index, e);
latch.countDown();
}, throwable -> {
Log.d(TAG, Log.getStackTraceString(throwable));
latch.countDown();
}));
}
return Single.create((SingleOnSubscribe<List<PodcastSearchResult>>) subscriber -> {
latch.await();
List<PodcastSearchResult> results = weightSearchResults(singleResults);
subscriber.onSuccess(results);
}).doOnDispose(() -> {
for (Disposable disposable : disposables) {
if (disposable != null) {
disposable.dispose();
}
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
use of io.reactivex.SingleOnSubscribe in project AntennaPod by AntennaPod.
the class GpodnetPodcastSearcher method search.
public Single<List<PodcastSearchResult>> search(String query) {
return Single.create((SingleOnSubscribe<List<PodcastSearchResult>>) subscriber -> {
try {
GpodnetService service = new GpodnetService(AntennapodHttpClient.getHttpClient(), SynchronizationCredentials.getHosturl(), SynchronizationCredentials.getDeviceID(), SynchronizationCredentials.getUsername(), SynchronizationCredentials.getPassword());
List<GpodnetPodcast> gpodnetPodcasts = service.searchPodcasts(query, 0);
List<PodcastSearchResult> results = new ArrayList<>();
for (GpodnetPodcast podcast : gpodnetPodcasts) {
results.add(PodcastSearchResult.fromGpodder(podcast));
}
subscriber.onSuccess(results);
} catch (GpodnetServiceException e) {
e.printStackTrace();
subscriber.onError(e);
}
}).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
Aggregations