如何使用Dagger2将自定义对象从活动类传递到存储库类?

时间:2019-07-31 16:26:20

标签: android dependency-injection dagger-2 dagger android-viewmodel

这是我在MyActivity类中观察数据的方式:

@Module
public class MyActivity extends DaggerAppCompatActivity {
    @Inject MyViewModelFactory factory;

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

    public void addData() {
        //How to pass this custom object to ViewModel???
        CustomClass obj = new CustomClass("CustomData");
        MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
        LiveData<Data> liveData = viewModel.getLiveData();
        liveData.observe(this, this);
    }
}

这是我的ModelFactory课:

@Singleton
public class MyViewModelFactory implements ViewModelProvider.Factory {
    private final Map<Class<? extends ViewModel>, Provider<ViewModel>> creators;

    @Inject
    MyViewModelFactory(Map<Class<? extends ViewModel>, Provider<ViewModel>> creators) {
        this.creators = creators;
    }

    @NonNull
    @Override
    public <T extends ViewModel> T create(@NonNull Class<T> modelClass) {
        Provider<? extends ViewModel> creator = creators.get(modelClass);
        if (creator == null) {
            for (Map.Entry<Class<? extends ViewModel>, Provider<ViewModel>> entry : creators.entrySet()) {
                if (modelClass.isAssignableFrom(entry.getKey())) {
                    creator = entry.getValue();
                    break;
                }
            }
        }

        if (creator == null) {
            throw new IllegalArgumentException("unknown model class " + modelClass);
        }

        try {
            return (T) creator.get();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}

这是我的ViewModel班:

public class MyViewModel extends ViewModel {
    private LiveData<Data> dataLiveData;
    private MyRepository repository;

    @Inject
    MyViewModel(MyRepository repository) {
        this.repository = repository;
        //I need it here!!!
        dataLiveData = repository.addDataToLiveData(obj);
    }

    LiveData<Data> getLiveData() {
        return dataLiveData;
    }
}

这是我的Repository班:

@Singleton
public class MyRepository {
    private MyApi myApi;

    @Inject
    MyRepository(MyApi myApi) {
        this.myApi = myApi;
    }

    LiveData<Data> addDataToLiveData(CustomClass obj) {
        myApi.getData(obj); //Make Api call and use obj
    }
}

如何将CustomClass对象从MyActivity传递到ViewModel类?

1 个答案:

答案 0 :(得分:1)

在ViewModel中制作一个接受您的CustomClass对象的函数,然后从Activity中调用该函数。

public class MyViewModel extends ViewModel {
    private LiveData<Data> dataLiveData;
    private MyRepository repository;


    @Inject
    MyViewModel(MyRepository repository) {
        this.repository = repository;
    }

    LiveData<Data> getLiveData() {
        return dataLiveData;
    }

    public void setCustomObject(CustomObject customObj){
       repository.addDataToLiveData(obj);

    }
}

编辑:要纠正错误,请执行以下操作:

 public void addData() {
        //How to pass this custom object to ViewModel???
        CustomClass obj = new CustomClass("CustomData");
        MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
        //ADD IT HERE
        viewModel.setCustomObj(obj);
        LiveData<Data> liveData = viewModel.getLiveData();
        liveData.observe(this, this);
    }