0

I need to return values from contacts which are retrieved from the phone with an observable. The problem, is the returning value.

I have the next method in a bridge:

@JavascriptInterface
    public String getFavoriteUsers() {
        return getBridgeHelper().getFavoriteUsers();
    }

It is calling a method in a helper (which is the class how do the tasks) and inside of this helper:

@SuppressLint("CheckResult")
    public String getFavoriteUsers() {

        Observable.fromCallable(() -> {
            SharedPreferences sp = getContext().getSharedPreferences(Constants.SHARED_PREFERENCES_NAME, Context.MODE_PRIVATE);
            String userIdStores = sp.getString(Constants.SP_FAVOURITES, "[]");
            return new Gson().toJson(ContactsManager.findContactsById(ContactsData.getInstance().contactsStored, userIdStores));
        })
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(
                        result -> returnObservedContacts(result),
                        throwable -> LoggerManager.handlesError(TAG, throwable, throwable.getMessage())
                );
    }

So, I need to return this result to the previous bridge, but I don't have a clear idea how to do that.

Thanks

SOLUTION:

The solution is to removes subscribe from the Observable and add blockingFirst at the end. Then, we can make the observable to return a value.

Víctor Martín
  • 2,680
  • 2
  • 39
  • 80

1 Answers1

0

Instead of returning a result from getFavoriteUsers, Create a method inside the <script> tag inside your website, which accepts the list of contacts as a parameter, Then call this method.

This StackOverflow answer explains how to call a javascript function

Edit: Since loading from SharedPreference doesn't take much time and you don't have access to the website, you can load the contacts on the OnCreate of the webview page and save results to a global variable, then call loadUrl of the webview. After that return the globally saved result on getFavoriteUsers

a0x2
  • 1,356
  • 1
  • 12
  • 23