为什么无法使用改良型MVVM召回API

时间:2019-11-12 14:02:38

标签: android mvvm architecture retrofit viewmodel

我正在使用MVVM架构模式在(登录)屏幕上工作,当使用错误的电子邮件或密码调用API并从API获取响应,然后输入正确的电子邮件和密码时遇到了一个问题,API没有回想,旧的响应保持回报。

此外,我使用的MVVM是否正确?

这是我的代码

public interface LoginApi {

    @POST("/api/Login")
    Call<LoginResponse> login(@Body LoginParams loginParams);
}
public class RetrofitService {

    private static Retrofit retrofit;
    private static final Object LOCK = new Object();
    private static final String BASE_URL = "https://base.url";

    public static Retrofit getRetrofitInstance() {

        OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
        httpClient.addInterceptor(new Interceptor() {
            @Override
            public Response intercept(Interceptor.Chain chain) throws IOException {
                Request original = chain.request();

                Request.Builder requestBuilder = original.newBuilder()
                        .addHeader("Content-Type", "application/json");

                Request request = requestBuilder.build();
                return chain.proceed(request);
            }
        });

        OkHttpClient client = httpClient.build();

        HttpLoggingInterceptor logging = new HttpLoggingInterceptor();
        logging.setLevel(HttpLoggingInterceptor.Level.BODY);
        httpClient.addInterceptor(logging);
        httpClient.readTimeout(60, TimeUnit.SECONDS);
        httpClient.connectTimeout(60, TimeUnit.SECONDS);
        if (retrofit == null) {
            synchronized (LOCK) {
                retrofit = new retrofit2.Retrofit.Builder()
                        .baseUrl(BASE_URL)
                        .client(client)
                        .addConverterFactory(GsonConverterFactory.create())
                        .client(httpClient.build())
                        .build();
            }
        }

        return retrofit;
    }

    public static <S> S createService(Class<S> serviceClass) {
        return getRetrofitInstance().create(serviceClass);
    }
}

public class LoginRepository {

    private static LoginRepository loginRepository;

    static LoginRepository getInstance() {
        if (loginRepository == null) {
            loginRepository = new LoginRepository();
        }
        return loginRepository;
    }

    private LoginApi loginApi;

    private LoginRepository() {
        loginApi = RetrofitService.createService(LoginApi.class);
    }

    public MutableLiveData<LoginResponse> login(LoginParams loginParams) {
        MutableLiveData<LoginResponse> loginData = new MutableLiveData<>();
        loginApi.login(loginParams).enqueue(new Callback<LoginResponse>() {
            @Override
            public void onResponse(Call<LoginResponse> call,
                                   Response<LoginResponse> response) {
                loginData.setValue(response.body());
            }

            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) {
            }
        });
        return loginData;
    }
}

public class LoginViewModel extends ViewModel {

    private MutableLiveData<LoginResponse> mutableLiveData;
    private LoginRepository loginRepository;
    private LoginParams loginParams = new LoginParams();

    public void init(Map<String, String> loginMap) {
        if (mutableLiveData != null) {
            return;
        }
        loginRepository = LoginRepository.getInstance();
        loginParams.setEmail(loginMap.get("loginEmail"));
        loginParams.setPassword(loginMap.get("loginPassword"));

        mutableLiveData = loginRepository.login(loginParams);
    }

    public LiveData<LoginResponse> getLoginRepository() {
        return mutableLiveData;
    }

}

public class LoginFragment extends BaseFragment {

    @BindView(R.id.et_email_login)
    AppCompatEditText etEmailLogin;
    @BindView(R.id.et_password_login)
    AppCompatEditText etPasswordLogin;

    public static LoginFragment getInstance() {

        return new LoginFragment();
    }

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

    }

    @Override
    public View inflateFragmentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_login, container, false);

        ButterKnife.bind(this, view);

        return view;
    }

    @OnClick(R.id.btn_enter_login)
    public void onClickLogin() {
            getMainActivity().showProgressBar();

            LoginViewModel loginViewModel = ViewModelProviders.of(getActivity()).get(LoginViewModel.class);
            loginViewModel.init(createLoginParam());
            observeViewModel(loginViewModel);
            loginViewModel.getLoginRepository();
    }

    private void observeViewModel(LoginViewModel loginViewModel) {
        loginViewModel.getLoginRepository().observe(this, loginResponse -> {
            getMainActivity().hideProgressBar();

            if (loginResponse == null) {
                // handle error here
                return;
            }
            if (loginResponse.isSuccess()) {
                getMainActivity().showResult(loginResponse.getUserId());


            } else {
                getMainActivity().showResult(loginResponse.getErrorMessage());
            }
        });
    }


    private Map<String, String> createLoginParam() {
        Map<String, String> loginMap = new HashMap<>();
        loginMap.put("loginEmail", etEmailLogin.getText().toString());
        loginMap.put("loginPassword", etPasswordLogin.getText().toString());

        return loginMap;
    }

}

1 个答案:

答案 0 :(得分:0)

一段时间后,我发现问题出在LoginViewModel中,因为我没有创建mutableLiveData的新实例,我使用了预先创建的实例,每次都给出相同的响应。因此,我的问题通过消除这种情况得以解决。

if (mutableLiveData != null) {
            return;
    }