use of io.reactivex.rxjava3.observers.DisposableSingleObserver in project quran_android by quran.
the class PagerActivity method toggleBookmark.
private void toggleBookmark(final Integer sura, final Integer ayah, final int page) {
compositeDisposable.add(bookmarkModel.toggleBookmarkObservable(sura, ayah, page).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<Boolean>() {
@Override
public void onSuccess(@NonNull Boolean isBookmarked) {
if (sura == null || ayah == null) {
// page bookmark
bookmarksCache.put(page, isBookmarked);
bookmarksMenuItem.setIcon(isBookmarked ? R.drawable.ic_favorite : R.drawable.ic_not_favorite);
} else {
// ayah bookmark
SuraAyah suraAyah = new SuraAyah(sura, ayah);
updateAyahBookmark(suraAyah, isBookmarked);
}
}
@Override
public void onError(@NonNull Throwable e) {
}
}));
}
use of io.reactivex.rxjava3.observers.DisposableSingleObserver in project quran_android by quran.
the class PagerActivity method requestTranslationsList.
private void requestTranslationsList() {
compositeDisposable.add(Single.fromCallable(() -> translationsDBAdapter.getTranslations()).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<List<LocalTranslation>>() {
@Override
public void onSuccess(@NonNull List<LocalTranslation> translationList) {
final List<LocalTranslation> sortedTranslations = new ArrayList<>(translationList);
Collections.sort(sortedTranslations, new LocalTranslationDisplaySort());
int items = sortedTranslations.size();
String[] titles = new String[items];
for (int i = 0; i < items; i++) {
LocalTranslation item = sortedTranslations.get(i);
if (!TextUtils.isEmpty(item.getTranslatorForeign())) {
titles[i] = item.getTranslatorForeign();
} else if (!TextUtils.isEmpty(item.getTranslator())) {
titles[i] = item.getTranslator();
} else {
titles[i] = item.getName();
}
}
Set<String> currentActiveTranslationsFilesNames = quranSettings.getActiveTranslations();
if (currentActiveTranslationsFilesNames.isEmpty() && items > 0) {
currentActiveTranslationsFilesNames = new HashSet<>();
for (int i = 0; i < items; i++) {
currentActiveTranslationsFilesNames.add(sortedTranslations.get(i).getFilename());
}
}
activeTranslationsFilesNames = currentActiveTranslationsFilesNames;
if (translationsSpinnerAdapter != null) {
translationsSpinnerAdapter.updateItems(titles, sortedTranslations, activeTranslationsFilesNames);
}
translationNames = titles;
translations = sortedTranslations;
if (showingTranslation) {
// Since translation items have changed, need to
updateActionBarSpinner();
}
}
@Override
public void onError(@NonNull Throwable e) {
}
}));
}
use of io.reactivex.rxjava3.observers.DisposableSingleObserver in project quran_android by quran.
the class QuranAdvancedSettingsFragment method onCreatePreferences.
@Override
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
addPreferencesFromResource(R.xml.quran_advanced_preferences);
final Context context = requireActivity();
appContext = context.getApplicationContext();
// field injection
((QuranApplication) appContext).getApplicationComponent().inject(this);
final Preference logsPref = findPreference(Constants.PREF_LOGS);
if (BuildConfig.DEBUG || "beta".equals(BuildConfig.BUILD_TYPE)) {
logsPref.setOnPreferenceClickListener(preference -> {
if (logsSubscription == null) {
logsSubscription = Observable.fromIterable(Timber.forest()).filter(tree -> tree instanceof RecordingLogTree).firstElement().map(tree -> ((RecordingLogTree) tree).getLogs()).map(logs -> QuranUtils.getDebugInfo(appContext, quranScreenInfo) + "\n\n" + logs).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableMaybeObserver<String>() {
@Override
public void onSuccess(@NonNull String logs) {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("message/rfc822");
intent.putExtra(Intent.EXTRA_EMAIL, new String[] { appContext.getString(R.string.logs_email) });
intent.putExtra(Intent.EXTRA_TEXT, logs);
intent.putExtra(Intent.EXTRA_SUBJECT, "Logs");
startActivity(Intent.createChooser(intent, appContext.getString(R.string.prefs_send_logs_title)));
logsSubscription = null;
}
@Override
public void onError(@NonNull Throwable e) {
}
@Override
public void onComplete() {
}
});
}
return true;
});
} else {
removeAdvancePreference(logsPref);
}
final Preference importPref = findPreference(Constants.PREF_IMPORT);
importPref.setOnPreferenceClickListener(preference -> {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("*/*");
String[] mimeTypes = new String[] { "application/*", "text/*" };
intent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(intent, REQUEST_CODE_IMPORT);
return true;
});
final Preference exportPref = findPreference(Constants.PREF_EXPORT);
exportPref.setOnPreferenceClickListener(preference -> {
if (exportSubscription == null) {
exportSubscription = bookmarkImportExportModel.exportBookmarksObservable().observeOn(AndroidSchedulers.mainThread()).subscribeWith(new DisposableSingleObserver<Uri>() {
@Override
public void onSuccess(@NonNull Uri uri) {
Intent shareIntent = new Intent(Intent.ACTION_SEND);
shareIntent.setType("application/json");
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
shareIntent.putExtra(Intent.EXTRA_STREAM, uri);
List<ResolveInfo> intents = appContext.getPackageManager().queryIntentActivities(shareIntent, 0);
if (intents.size() > 1) {
// if only one, then that is likely Quran for Android itself, so don't show
// the chooser since it doesn't really make sense.
context.startActivity(Intent.createChooser(shareIntent, context.getString(R.string.prefs_export_title)));
} else {
File exportedPath = new File(appContext.getExternalFilesDir(null), "backups");
String exported = appContext.getString(R.string.exported_data, exportedPath.toString());
ToastCompat.makeText(appContext, exported, Toast.LENGTH_LONG).show();
}
}
@Override
public void onError(@NonNull Throwable e) {
exportSubscription = null;
if (isAdded()) {
ToastCompat.makeText(context, R.string.export_data_error, Toast.LENGTH_LONG).show();
}
}
});
}
return true;
});
internalSdcardLocation = Environment.getExternalStorageDirectory().getAbsolutePath();
listStoragePref = findPreference(getString(R.string.prefs_app_location));
listStoragePref.setEnabled(false);
try {
storageList = StorageUtils.getAllStorageLocations(context.getApplicationContext());
} catch (Exception e) {
Timber.d(e, "Exception while trying to get storage locations");
storageList = new ArrayList<>();
}
// except for the normal Environment.getExternalStorageDirectory
if (storageList.size() <= 1) {
Timber.d("removing advanced settings from preferences");
hideStorageListPref();
} else {
loadStorageOptionsTask = new LoadStorageOptionsTask(context, quranFileUtils);
loadStorageOptionsTask.execute();
}
}
Aggregations