im trying to make my code subcribe to (usermodel) and check if its in database to load data

99 Views Asked by At

I'm trying to subscribe so as to load user info but it doesn't work even when I still have the user in my database

have checked and checked can anyone please help me out

    compositeDisposable.add(myRestaurantAPI.getUser(Common.API_KEY, account.getId())
                                            .subscribeOn(Schedulers.io())
                                            .observeOn(AndroidSchedulers.mainThread())
                                            .subscribe(userModel -> {

                                                        Log.d(TAG, "AuthViewModel: viewmodel is working in splash screen...");

                                                        if (userModel.isSuccess()) // if user is in database
                                                        {
                                                            Log.d(TAG, "AuthViewModel: userModel is working in splash screen...");
                                                            Common.currentUser = userModel.getResult().get(0);
                                                            Intent intent = new Intent(SplashScreen.this, HomeActivity.class);
                                                            startActivity(intent);
                                                            finish();
                                                        } else // if user isnt available yet send them back to update activity
                                                        {
                                                            Intent intent = new Intent(SplashScreen.this, UpdateInfoActivity.class);
                                                            startActivity(intent);
                                                            finish();
                                                        }


                                                        dialog.dismiss();
                                                    },
                                                    throwable -> {
                                                        dialog.dismiss();
                                                        Toast.makeText(SplashScreen.this, "[GET USER API]" + throwable.getMessage(), Toast.LENGTH_SHORT).show();
                                                    }));    

I expect it to load from the database and go directly to HomeActivity but it keeps sending me back to UpdateInfoActivity.. here is my API

    public interface IMyRestaurantAPI {
    @GET("user")
    Observable<UserModel> getUser(@Query("key") String apiKey,
                                  @Query("fbid") String fbid);
    @POST("user")
    @FormUrlEncoded
    Observable<UpdateUserModel> updateUserInfo(@Field("key") String apiKey,
                                               @Field("userPhone") String userPhone,
                                               @Field("userName") String userName,
                                               @Field("userAddress") String userAddress,
                                               @Field("fbid") String fbid);
}

here is my model

    public class UserModel { private boolean success;
private String message; private List<User> result;

    public boolean isSuccess() {
        return success;
    }

    public void setSuccess(boolean success) {
        this.success = success;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public List<User> getResult() {
        return result;
    }

    public void setResult(List<User> result) {
        this.result = result;
    }
}

here is my update info activity its getting data to my database but its not subscribing too

    btn_update.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dialog.show();
                AccountKit.getCurrentAccount(new AccountKitCallback<Account>() {
                    @Override
                    public void onSuccess(Account account) {
                        compositeDisposable.add(myRestaurantAPI.updateUserInfo(Common.API_KEY,
                                        account.getPhoneNumber().toString(),
                                        edt_user_name.getText().toString(),
                                        edt_user_address.getText().toString(),
                                        account.getId())
                                .subscribeOn(Schedulers.io())
                                .observeOn(AndroidSchedulers.mainThread())
                                .subscribe(updateUserModel -> {
                                            Log.d(TAG, "AuthViewModel: viewmodel is working in update111...");
                                    if (updateUserModel.isSuccess())
                                    {
                                        Log.d(TAG, "AuthViewModel: updateUserModel is working in update...");
                                        // refresh if it already has users before
                                        compositeDisposable.add(myRestaurantAPI.getUser(Common.API_KEY, account.getId())
                                                        .subscribeOn(Schedulers.io())
                                                        .observeOn(AndroidSchedulers.mainThread())
                                                        .subscribe(userModel -> {

                                                   if (userModel.isSuccess())
                                                   {
                                                      // Common.currentUser = userModel.getResult().get(0);
                                                       //                                                       startActivity(new Intent(UpdateInfoActivity.this,HomeActivity.class));
                                                       //
                                                       //                                                       finish();
                                                       Common.currentUser = userModel.getResult().get(0);
                                                       Intent intent = new Intent(UpdateInfoActivity.this, HomeActivity.class);
                                                       startActivity(intent);
                                                       finish();

                                                   }
                                                   else
                                                   {
                                                       Toast.makeText(UpdateInfoActivity.this, "[GET USER RESULT]"+userModel.getMessage(), Toast.LENGTH_SHORT).show();
                                                   }

                                                            dialog.dismiss();

                                                        },
                                                       throwable -> {
                                                           dialog.dismiss();
                                                           Toast.makeText(UpdateInfoActivity.this, "[GET USER]"+throwable.getMessage(), Toast.LENGTH_SHORT).show();
                                                       })
                                        );
                                    }
                                    else
                                    {
                                        Toast.makeText(UpdateInfoActivity.this, "[UPDATE USER API RETURN]"+updateUserModel.getMessage(), Toast.LENGTH_SHORT).show();
                                    }
                                    dialog.dismiss();
                                        },
                                        throwable -> {
                                    dialog.dismiss();
                                            Toast.makeText(UpdateInfoActivity.this, "[UPDATE USER API]"+throwable.getMessage(), Toast.LENGTH_SHORT).show();
                                        })
                        );
                    }

                    @Override
                    public void onError(AccountKitError accountKitError) {
                        Toast.makeText(UpdateInfoActivity.this, "[ACCOUT KIT ERROR ]"+accountKitError.getErrorType().getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });
            }
        });  
0

There are 0 best solutions below