Search in sources :

Example 6 with User

use of com.odysee.app.model.lbryinc.User in project odysee-android by OdyseeTeam.

the class RewardVerificationFragment method checkRewardApproved.

private void checkRewardApproved(final boolean firstCheck) {
    if (firstRunStepHandler != null) {
        firstRunStepHandler.onRequestInProgress(true);
    }
    optionsPager.setVisibility(View.INVISIBLE);
    optionsTabs.setVisibility(View.INVISIBLE);
    FetchCurrentUserTask task = new FetchCurrentUserTask(getContext(), new FetchCurrentUserTask.FetchUserTaskHandler() {

        @Override
        public void onSuccess(User user) {
            if (firstRunStepHandler != null) {
                firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_REWARDS);
            }
            Lbryio.currentUser = user;
            if (user.isIdentityVerified() && user.isRewardApproved()) {
                // verified for rewards
                LbryAnalytics.logEvent(LbryAnalytics.EVENT_REWARD_ELIGIBILITY_COMPLETED);
                textSummary.setText(R.string.reward_eligible);
                return;
            }
            if (!firstCheck) {
                // show manual verification if the user is not yet reward approved
                // and this is not the check when the page loads
                optionsPager.setCurrentItem(3);
            }
            optionsPager.setVisibility(View.VISIBLE);
            optionsTabs.setVisibility(View.VISIBLE);
        }

        @Override
        public void onError(Exception error) {
            showFetchUserError(error != null ? error.getMessage() : getString(R.string.fetch_current_user_error));
            optionsPager.setVisibility(View.VISIBLE);
            optionsTabs.setVisibility(View.VISIBLE);
            if (firstRunStepHandler != null) {
                firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_REWARDS);
            }
        }
    });
    task.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
Also used : User(com.odysee.app.model.lbryinc.User) FetchCurrentUserTask(com.odysee.app.tasks.lbryinc.FetchCurrentUserTask)

Example 7 with User

use of com.odysee.app.model.lbryinc.User in project odysee-android by OdyseeTeam.

the class SignInFragment method performSignIn.

private void performSignIn(final String email, final String password) {
    if (requestInProgress) {
        return;
    }
    if (!emailSignInChecked) {
        requestInProgress = true;
        if (firstRunStepHandler != null) {
            firstRunStepHandler.onRequestInProgress(false);
        }
        beforeSignInTransition();
        executor.execute(new Runnable() {

            @Override
            public void run() {
                Map<String, String> options = new HashMap<>();
                options.put("email", email);
                try {
                    Object response = Lbryio.parseResponse(Lbryio.call("user", "exists", options, Helper.METHOD_POST, getContext()));
                    requestInProgress = false;
                    if (response instanceof JSONObject) {
                        JSONObject json = (JSONObject) response;
                        boolean hasPassword = Helper.getJSONBoolean("has_password", false, json);
                        if (!hasPassword) {
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            return;
                        }
                        // email exists, we can use sign in flow. Show password field
                        setCurrentEmail(email);
                        emailSignInChecked = true;
                        displaySignInControls();
                        displayMagicLink();
                    }
                } catch (LbryioRequestException | LbryioResponseException ex) {
                    if (ex instanceof LbryioResponseException) {
                        LbryioResponseException rex = (LbryioResponseException) ex;
                        if (rex.getStatusCode() == 412) {
                            // old email verification flow
                            setCurrentEmail(email);
                            handleUserSignInWithoutPassword(email);
                            Bundle bundle = new Bundle();
                            bundle.putString("email", currentEmail);
                            LbryAnalytics.logEvent(LbryAnalytics.EVENT_EMAIL_ADDED, bundle);
                            LbryAnalytics.logEvent("");
                            requestInProgress = false;
                            return;
                        }
                    }
                    requestInProgress = false;
                    restoreControls(true);
                    if (firstRunStepHandler != null) {
                        firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
                    }
                    showError(ex.getMessage());
                }
            }
        });
        return;
    }
    if (Helper.isNullOrEmpty(password)) {
        showError(getString(R.string.please_enter_signin_password));
        return;
    }
    beforeSignInTransition();
    requestInProgress = true;
    executor.execute(new Runnable() {

        @Override
        public void run() {
            Map<String, String> options = new HashMap<>();
            options.put("email", email);
            options.put("password", password);
            try {
                Object response = Lbryio.parseResponse(Lbryio.call("user", "signin", options, Helper.METHOD_POST, getContext()));
                requestInProgress = false;
                if (response instanceof JSONObject) {
                    Type type = new TypeToken<User>() {
                    }.getType();
                    Gson gson = new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES).create();
                    User user = gson.fromJson(response.toString(), type);
                    Lbryio.currentUser = user;
                    addOdyseeAccountExplicitly(user.getPrimaryEmail());
                    finishWithWalletSync();
                    return;
                }
            } catch (LbryioRequestException | LbryioResponseException ex) {
                if (ex instanceof LbryioResponseException) {
                    if (((LbryioResponseException) ex).getStatusCode() == 409) {
                        handleEmailVerificationFlow(email);
                        return;
                    }
                }
                showError(ex.getMessage());
                requestInProgress = false;
                restoreControls(true);
                if (firstRunStepHandler != null) {
                    firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
                }
                return;
            }
            requestInProgress = false;
            restoreControls(true);
            if (firstRunStepHandler != null) {
                firstRunStepHandler.onRequestCompleted(FirstRunActivity.FIRST_RUN_STEP_ACCOUNT);
            }
            showError(getString(R.string.unknown_error_occurred));
        }
    });
}
Also used : User(com.odysee.app.model.lbryinc.User) GsonBuilder(com.google.gson.GsonBuilder) Bundle(android.os.Bundle) Gson(com.google.gson.Gson) Type(java.lang.reflect.Type) JSONObject(org.json.JSONObject) TypeToken(com.google.gson.reflect.TypeToken) JSONObject(org.json.JSONObject) Map(java.util.Map) HashMap(java.util.HashMap) LbryioResponseException(com.odysee.app.exceptions.LbryioResponseException)

Aggregations

User (com.odysee.app.model.lbryinc.User)7 Bundle (android.os.Bundle)3 ViewPager2 (androidx.viewpager2.widget.ViewPager2)3 Gson (com.google.gson.Gson)3 GsonBuilder (com.google.gson.GsonBuilder)3 LbryioResponseException (com.odysee.app.exceptions.LbryioResponseException)3 FetchCurrentUserTask (com.odysee.app.tasks.lbryinc.FetchCurrentUserTask)3 Type (java.lang.reflect.Type)3 JSONObject (org.json.JSONObject)3 TypeToken (com.google.gson.reflect.TypeToken)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 Intent (android.content.Intent)1 AuthTokenInvalidatedException (com.odysee.app.exceptions.AuthTokenInvalidatedException)1 LbryioRequestException (com.odysee.app.exceptions.LbryioRequestException)1 Response (okhttp3.Response)1