Вариант использования не вводится для фрагмента ViewModelFactory - PullRequest
0 голосов
/ 25 сентября 2018

Я следую за чистой архитектурой с mvvm.Когда я вставляю фабрику viewmodel во фрагмент и пытаюсь выполнить сценарий использования из viewmodel фрагмента, он дает NPE для варианта использования.Я также ввел вариант использования.

Пожалуйста, посмотрите код и укажите ошибку

MainModule.java

@Module
public class MainModule {

@Provides
LoginViewModel provideLoginViewModel() {
    return new LoginViewModel();
}

@Provides
@Named("LoginActivity")
ViewModelProvider.Factory provideActivityViewModelFactory(LoginViewModel loginViewModel) {
    return new ViewModelProviderFactory<>(loginViewModel);
}

@Provides
PasscodeViewModel providePasscodeViewModel(SetPasscode setPasscode) {
    return new PasscodeViewModel(setPasscode);
}

@Provides
@Named("PasscodeFragment")
ViewModelProvider.Factory provideFragmentViewModelFactory(PasscodeViewModel passcodeViewModel) {
    return new ViewModelProviderFactory<>(passcodeViewModel);
}

}

PasscodeFragment.java

public class PasscodeFragment extends BaseFragment<PasscodeViewModel> implements PasscodeNavigator, View.OnClickListener {

@Inject
@Named("PasscodeFragment")
ViewModelProvider.Factory viewModelFactory;

PasscodeViewModel passcodeViewModel;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    passcodeViewModel.setNavigator(this);
}

public PasscodeFragment() {
}

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    ((MainApplication) getActivity().getApplicationContext()).getComponent().inject(this);
    View view = inflater.inflate(R.layout.fragment_passcode, container, false);
    initViews(view);
    return view;
}
 @Override
public PasscodeViewModel getViewModel() {
    passcodeViewModel = ViewModelProviders.of(this, viewModelFactory).get(PasscodeViewModel.class);
    return passcodeViewModel;
}

PasscodeViewModel.java (ЗДЕСЬ SETPASSCODE НЕДОСТУПЕН)

public class PasscodeViewModel extends BaseViewModel<PasscodeNavigator> {

//use case
private SetPasscode setPasscode;

//data
private String tempPin;
private boolean isConfirming = false;

public PasscodeViewModel() {
}

public PasscodeViewModel(SetPasscode setPasscode) {
    this.setPasscode = setPasscode;
}

public void checkForConfirmation(String passcode) {
    try {
        if (passcode.length() == 4) {
            if (isConfirming) {
                //second attempt
                if (tempPin.equals(passcode)) {
                    //done
                    Log.e("ganesh", passcode);
                    setPasscode(passcode);
                } else {
                    //reset
                    isConfirming = false;
                    tempPin = "";
                    getNavigator().onPinNotMatched();
                    getNavigator().clearViewsForFirstAttempt();
                }
            } else {
                //first attempt
                tempPin = passcode;
                isConfirming = true;
                getNavigator().clearViewsForSecondAttempt();
            }
        }
    } catch (NumberFormatException e) {
        getNavigator().wrongPinFormat();
        getNavigator().clearViewsForFirstAttempt();
    }
}

private void setPasscode(String passcode) {
//setPasscode IS NULL HERE
    setPasscode.execute(new DisposableSingleObserver<Boolean>() {
        @Override
        public void onSuccess(Boolean aBoolean) {
            if (aBoolean)
                getNavigator().onPinSetSuccess();
            else
                getNavigator().onPinSetFailed();
        }

        @Override
        public void onError(Throwable e) {
            getNavigator().onPinSetFailed();
        }
    }, SetPasscode.Params.setPasscode(passcode));

}

@Override
protected void onCleared() {
    if (setPasscode != null)
        setPasscode.dispose();
}

}

SetPasscode.java

public class SetPasscode extends SingleUseCase<Boolean, SetPasscode.Params> {

private UserRepository repository;

public SetPasscode(PostExecutionThread postExecutionThread, UserRepository repository) {
    super(postExecutionThread);
    this.repository = repository;
}

@Override
public Single<Boolean> buildUseCaseObservable(SetPasscode.Params params) {
    return repository.setPasscode(params.passcode);
}


public static final class Params {

    private String passcode;

    private Params(String passcode) {
        this.passcode = passcode;
    }

    public static SetPasscode.Params setPasscode(String passcode) {
        return new SetPasscode.Params(passcode);
    }
}
}

LoginActivity.java

public class LoginActivity extends BaseActivity<LoginViewModel> implements LoginNavigator {

@Inject
@Named("LoginActivity")
ViewModelProvider.Factory viewModelFactory;

LoginViewModel loginViewModel;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login);
    getSupportActionBar().hide();
    ((MainApplication) getApplicationContext()).getComponent().inject(this);
    loginViewModel = ViewModelProviders.of(this, viewModelFactory).get(LoginViewModel.class);
    loginViewModel.setNavigator(this);
    initViews();
}

 @Override
public LoginViewModel getViewModel() {
    return loginViewModel;
}
...