如何在Android中使用Dagger2将字符串传递给ViewModel / Repository类?

时间:2019-07-23 22:39:11

标签: android dependency-injection dagger-2 dagger

这是我的片段类:

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();
    }
}

现在这是MyViewModel类:

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;
    }
}

这个MyRepository类:

@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
    }
}

也请看看我的AppModule类:

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

MyViewModelModule类中:

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

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

我尝试过的是在ViewModel / repository构造函数中添加字符串,但是没有任何运气。请帮助我将uid传递给ViewModel或存储库类。谢谢!

1 个答案:

答案 0 :(得分:2)

一个可能的解决方案是每个视图模型有1个工厂,然后将UID设置为工厂,然后工厂将其传递给视图模型

finalrow = Cells(Worksheets("page1").Rows.Count, 1).End(xlUp).Row
For j = 12 To 14
    For i = finalrow + 8 To 1 Step -1
        Do While IsEmpty(Cells(i, j))
            If IsEmpty(Cells(i - 1, j)) Then
                FirstRow = i - 1
                LastRow = FirstRow
            Else
                LastRow = i - 1
                FirstRow = Cells(i - 1, j).End(xlUp).Row
            End If
            Cells(LastRow + 1, j) = Application.WorksheetFunction.Sum(Range(Cells(FirstRow, j), Cells(LastRow, j)))
            If Cells(LastRow + 1, 12) >= 1 Then
                Cells(LastRow + 1, 11).FormulaLocal = "Total Sum"
                If Cells(LastRow + 1, 11) = "Total Sum" Then
                    Cells(LastRow + 1, j) = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                End If
                Cells(LastRow + 3, 11).FormulaLocal = "China"
                If Cells(LastRow + 3, 11) = "China" Then
                    Cells(LastRow + 3, j).FormulaLocal = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                    If Cells(LastRow + 2, 11) = "" Then
                        Worksheets("Page1").Cells(LastRow + 2, j).ClearContents
                    End If
                End If
                Cells(LastRow + 4, 11).FormulaLocal = "Abu Dhabi"
                If Cells(LastRow + 4, 11) = "Abu Dhabi" Then
                    Cells(LastRow + 4, j).FormulaLocal = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                End If
                Cells(LastRow + 5, 11).FormulaLocal = "Other"
                If Cells(LastRow + 5, 11) = "Other" Then
                    Cells(LastRow + 5, j).FormulaLocal = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                End If
                Cells(LastRow + 6, 11).FormulaLocal = "H1 & H2"
                If Cells(LastRow + 6, 11) = "H1 & H2" Then
                    Cells(LastRow + 6, j).FormulaLocal = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                End If
                Cells(LastRow + 7, 11).FormulaLocal = "Product"
                If Cells(LastRow + 7, 11) = "Product" Then
                    Cells(LastRow + 7, j).FormulaLocal = "=Sum(" & Range(Cells(FirstRow, j), Cells(LastRow, j)).Address(False, False) & ")"
                    If Cells(LastRow + 7, 12) >= 1 Then
                        For Z = 11 To 14         '
                            Cells(LastRow + 12, 11).EntireRow.ClearContents
                            Cells(LastRow + 13, 11).EntireRow.ClearContents
                            Cells(LastRow + 14, 11).EntireRow.ClearContents
                            Cells(LastRow + 8, 11).FormulaLocal = "Delete"
                            Cells(LastRow + 9, 11).FormulaLocal = "Delete"
                            Cells(LastRow + 10, 11).FormulaLocal = "Delete"
                            Cells(LastRow + 11, 11).FormulaLocal = "Delete"
                        Next Z
                    End If
                End If
            End If
        Loop
    Next i
Next j

然后

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]")
            }
}

编辑:用Java

@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();
}