From b8b2bd090f23ae5bea00357104ba236709f918c2 Mon Sep 17 00:00:00 2001 From: Anthony Restaino Date: Sun, 13 Mar 2016 18:13:19 -0400 Subject: [PATCH] Fixed crashes --- .../lightning/fragment/BookmarksFragment.java | 35 ++++++++++++------- .../browser/lightning/react/Observable.java | 5 +-- 2 files changed, 24 insertions(+), 16 deletions(-) diff --git a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java index 748e6b4..c82e703 100644 --- a/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java +++ b/app/src/main/java/acr/browser/lightning/fragment/BookmarksFragment.java @@ -47,6 +47,11 @@ import acr.browser.lightning.database.HistoryItem; import acr.browser.lightning.dialog.LightningDialogBuilder; import acr.browser.lightning.preference.PreferenceManager; import acr.browser.lightning.async.ImageDownloadTask; +import acr.browser.lightning.react.Action; +import acr.browser.lightning.react.Observable; +import acr.browser.lightning.react.OnSubscribe; +import acr.browser.lightning.react.Schedulers; +import acr.browser.lightning.react.Subscriber; import acr.browser.lightning.utils.ThemeUtils; import acr.browser.lightning.view.LightningView; @@ -85,16 +90,16 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, // Colors private int mIconColor, mScrollIndex; - // Init asynchronously the bookmark manager - private final Runnable mInitBookmarkManager = new Runnable() { - @Override - public void run() { - final Context context = getContext(); - mBookmarkAdapter = new BookmarkViewAdapter(context, mBookmarks); - setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); - mBookmarksListView.setAdapter(mBookmarkAdapter); - } - }; + private Observable initBookmarkManager() { + return Observable.create(new Action() { + @Override + public void onSubscribe(@NonNull Subscriber subscriber) { + mBookmarkAdapter = new BookmarkViewAdapter(getContext(), mBookmarks); + setBookmarkDataSet(mBookmarkManager.getBookmarksFromFolder(null, true), false); + subscriber.onNext(mBookmarkAdapter); + } + }); + } @Override public void onCreate(@Nullable Bundle savedInstanceState) { @@ -166,8 +171,14 @@ public class BookmarksFragment extends Fragment implements View.OnClickListener, setupNavigationButton(view, R.id.action_reading, R.id.icon_reading); setupNavigationButton(view, R.id.action_toggle_desktop, R.id.icon_desktop); - // Must be called here, only here we have a reference to the ListView - BrowserApp.getTaskThread().execute(mInitBookmarkManager); + initBookmarkManager().subscribeOn(Schedulers.worker()) + .observeOn(Schedulers.main()) + .subscribe(new OnSubscribe() { + @Override + public void onNext(@Nullable BookmarkViewAdapter item) { + mBookmarksListView.setAdapter(mBookmarkAdapter); + } + }); return view; } diff --git a/app/src/main/java/acr/browser/lightning/react/Observable.java b/app/src/main/java/acr/browser/lightning/react/Observable.java index e9b06b7..5535b30 100644 --- a/app/src/main/java/acr/browser/lightning/react/Observable.java +++ b/app/src/main/java/acr/browser/lightning/react/Observable.java @@ -177,12 +177,9 @@ public class Observable { @Override public void onError(@NonNull final Throwable throwable) { - if (!mOnCompleteExecuted && mOnSubscribe != null) { + if (mOnSubscribe != null) { mOnCompleteExecuted = true; mObservable.executeOnObserverThread(new OnErrorRunnable<>(mOnSubscribe, throwable)); - } else { - Log.e(TAG, "onComplete already called"); - throw new RuntimeException("onComplete already called"); } }