How to pass a String to a ViewModel/Repository cla

2019-08-16 18:36发布

问题:

This is my fragment class:

public class MyFragment extends DaggerFragment {
    @Inject MyViewModelFactory factory;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        String uid = "123"; //How to pass this String??
        MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
        LiveData<User> liveData = viewModel.getUserLiveData();
    }
}

Now this is MyViewModel class:

public class MyViewModel extends ViewModel {
    private MutableLiveData<User> liveData;

    @Inject
    MyViewModel(MyRepository repository) {
        userLiveData = repository.addUserToLiveData();
        //Here I need the value of that String "123"
    }

    LiveData<User> getUserLiveData() {
        return liveData;
    }
}

And this MyRepository class:

@Singleton
class MyRepository {
    private Retrofit retrofit;

    @Inject
    MyRepository(Retrofit retrofit) {
        //Or here I need the value of that String "123"
    }

    MutableLiveData<User> addUserToLiveData() {
        //Make api call using retrofit
    }
}

Please also take a look at my AppModule class:

@Module
public class AppModule {
    @Singleton
    @Provides
    static Retrofit provideRetrofit(){
        return new Retrofit.Builder()... //Initialize retrofit
    }
}

And at MyViewModelModule class:

@Module
abstract class MyViewModelModule {
    @Binds
    abstract ViewModelProvider.Factory bindMyViewModelFactory(MyViewModelFactory factory);

    @Binds
    @IntoMap
    @ViewModelKey(MyViewModel.class)
    abstract ViewModel provideMyViewModel(MyViewModel viewModel);
}

What I have tried is to add the string in the ViewModel/repository constructor but without any luck. Please help me pass that uid to the ViewModel or repository class. Thanks!

回答1:

A possible solution is to have 1 factory per viewmodel, then you set the UID to the factory and the factory passes it to the viewmodel

class MyViewModelFactory @Inject constructor(
        private val repository: Repository
): ViewModelProvider.Factory {

    lateinit var uid: String

    @Suppress("UNCHECKED_CAST")
    override fun <T : ViewModel> create(modelClass: Class<T>): T =
            if (modelClass.isAssignableFrom(MyViewModel::class.java)) {
                    MyViewModel(
                        repository,
                        uid
                ) as T
            } else {
                throw IllegalArgumentException("Unknown ViewModel class [$modelClass]")
            }
}

then

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    String uid = "123"; //How to pass this String??
    factory.setUid(uid);
    MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
    LiveData<User> liveData = viewModel.getUserLiveData();
}

Edit: in Java

public final class MyViewModelFactory implements Factory {
    private final Repository repository;
    private String uid;

   @Inject
   public MyViewModelFactory(Repository repository) {
      this.repository = repository;
   }

   @NotNull
   public ViewModel create(@NotNull Class modelClass) {
      if (modelClass.isAssignableFrom(MyViewModel.class)) {
         return new MyViewModel(repository, uid);
      } else {
         throw (Throwable)(new IllegalArgumentException("Unknown ViewModel class [" + modelClass + ']'));
      }
   }

   public void setUid(@NonNull final String uid) {
       this.uid = uid;
   }
}

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
    String uid = "123"; //How to pass this String??
    factory.setUid(uid);
    MyViewModel viewModel = ViewModelProviders.of(this, factory).get(MyViewModel.class);
    LiveData<User> liveData = viewModel.getUserLiveData();
}