使用MVVM和LiveData登录

问题描述 投票:0回答:1

我对LiveData有问题

活动

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ActivityUserManagementBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_user_management);
        binding.setLoginObject(new LoginRequest());

        loginViewModel = new ViewModelProvider(this).get(LoginViewModel.class);
        binding.setLoginClickListener(loginViewModel);

        getLoginResponse();
    }

    private void getLoginResponse() {
        loginViewModel.getLoginViewModelListener().observe(this, new Observer<LoginResponse>() {
            @Override
            public void onChanged(LoginResponse loginResponse) {
                Log.e(TAG, "onChanged: " );
                if (loginResponse != null)
                    Toast.makeText(UserManagementActivity.this, loginResponse.msg, Toast.LENGTH_SHORT).show();
            }
        });
    }

存储库

public class LoginRepository {

    private LoginRequest loginRequest;
    private MutableLiveData<LoginResponse> loginResponseLiveListener;

    public LoginRepository(LoginRequest loginRequest) {
        this.loginRequest = loginRequest;
        getLoginData();
    }

    public LiveData<LoginResponse> getLoginResponseLiveListener() {
        if(loginResponseLiveListener == null)
            loginResponseLiveListener = new MutableLiveData<>();
        Log.e(TAG, "getLoginResponseLiveListener: " );
        return loginResponseLiveListener;
    }

    private void getLoginData() {
        RetrofitClient.INSTANCE.callEndPoint().login(loginRequest).enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {
                if(response.body() != null) {
                    loginResponseLiveListener.setValue(response.body());
                    Log.e(TAG, "onResponse: " + response.body().msg);
                }

            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
                Log.e(TAG, "onFailure: "+ t.getMessage() );
            }
        });
    }
}

ViewModel

public class LoginViewModel extends ViewModel {

    private boolean isValid = true;
    private LoginRepository loginRepository;
    private MutableLiveData<LoginResponse> loginViewModelListener;

    public void onSubmitLoginClicked(View submit, CustomEditText email, CustomEditText password, LoginRequest loginRequest) {
        if (loginValidation(email, password, loginRequest)) {
            loginRequest.setToken(MyTools.getKey(MyApplication.getApplication(), "token"));
            loginRepository = new LoginRepository(loginRequest);

            loginViewModelListener.setValue(loginRepository.getLoginResponseLiveListener().getValue());
        }
    }

    public MutableLiveData<LoginResponse> getLoginViewModelListener(){
        return loginViewModelListener;
    }

    private boolean loginValidation(CustomEditText email, CustomEditText password, LoginRequest loginRequest) {
        if (UserValidation.isNullOrEmpty(loginRequest.email) ||!UserValidation.isEmail(loginRequest.email)) {
            email.setError(MyApplication.getApplication().getString(R.string.error_login_email));
            isValid = false;
        }

        if (UserValidation.isNullOrEmpty(loginRequest.password) ) {
            password.setError(MyApplication.getApplication().getString(R.string.error_login_password));
            isValid = false;
        }

        return isValid;
    }
}

我没有从活动中获得任何东西,因为返回的livedata对象为null!那么问题出在哪里呢?这是干净的代码吗?我将xml中的视图传递给视图模型,以使所有逻辑都基于视图模型。请我需要您的帮助和建议。

java android mvvm repository android-livedata
1个回答
0
投票

LiveData仅在有活动的Observer时发出值。当您拨打此行时

loginViewModelListener.setValue(loginRepository.getLoginResponseLiveListener().getValue());

loginRepository中liveData的值为null,因此viewModel中的liveData发出null,并且viewModel不知道存储库中以后更改的更新值。

[我认为您想观察从viewModel到loginRepository的liveData的变化,并将该值重新发送给活动/分段中的观察者。如果是这样,则需要使用Transformations。他们观察更改,应用转换并发出新值。

在这种情况下,如果不需要对发射值进行任何修改,则可以这样更改:

private MutableLiveData<LoginResponse> loginViewModelListener;
public LoginViewModel(){
     loginViewModelListener = Transformation.map(loginRepository.getLoginResponseLiveListener(),result->result);

}
© www.soinside.com 2019 - 2024. All rights reserved.