刀柄无法使用



我试图理解HILT,我正在创建一个示例项目,我习惯了创建模块和组件,但现在有了一个柄,有InstallIn(),也许我必须和他们一起玩,像以前一样。例如,我习惯为每个特性创建一个模块:

LoginModule(包含:ViewModel, activity/fragment,数据源,用例等)ProductModule(包含:ViewModel, activity/fragment,数据源,用例等)

从现在起,我已经创建了一个带柄的NetworkModule:

@Module
@InstallIn(ApplicationComponent::class)
object NetworkModule {
@Provides
fun provideRetrofit(httpClient: OkHttpClient): Retrofit =
Retrofit.Builder()
.baseUrl(BASE_URL)
.client(httpClient)
.build()
@Provides
fun provideOkHttpClient(
httpLoggingInterceptor: HttpLoggingInterceptor,
@ErrorInterceptorOkHttpClient errorInterceptor: Interceptor,
): OkHttpClient = OkHttpClient()
.newBuilder()
.addInterceptor(httpLoggingInterceptor)
.addInterceptor(errorInterceptor)
.build()

@ErrorInterceptorOkHttpClient
@Provides
fun provideErrorInterceptor(): Interceptor = ErrorInterceptor()
@Provides
fun provideHttpLogginInterceptor(): HttpLoggingInterceptor {
val loggingInterceptor = HttpLoggingInterceptor()
loggingInterceptor.level = when {
BuildConfig.DEBUG -> HttpLoggingInterceptor.Level.BODY
else -> HttpLoggingInterceptor.Level.NONE
}
return loggingInterceptor
}
@Provides
fun provideMyService(retrofit: Retrofit): MyService =
retrofit.create(MyService::class.java)
}

然后我有一个功能或者说是应用程序的一部分我想在这里显示一些东西一个列表,我创建了这个:


@Module
@InstallIn(FragmentComponent::class)
object MainActivityModule {
@Provides
fun proviceDataSource(
myService: MyService,
myMapper: MyMapper,
): MyDatasource = MyDatasourceImpl(myService, myMapper)
@Provides
fun provicesMyMapper(): MyMapper = MapperImpl()
}

在Activity中添加

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

在Fragment中,我没有任何东西可能这就是我做错的地方?

何时使用ApplicationComponentSingletonComponent等?因为我使用dagger(不是dagger安卓)我有一个组件工厂,我有所有这些组件(LoginComponent, GoogleMapComponent, ReferalCodeComponent)所有这些都是一个功能(谈论多模块项目),我在那里创建这些组件的工厂,现在如何与柄这样做的方式?

当使用多个模块时,如果我遵循我的想法,我会在一个库中定义多个模块,并提供给接口模块使用。

您的问题是如何使用您在接口模块中定义的工厂方法?在这一点上我不太明白你的问题。

如果你使用MVVM架构,你可以将这些组件注入ViewModel。这些组件(如LoginComponent)提供的Flow可以在ViewModel中访问。当然,通过ViewModel注入的参数也可以很容易地访问您提供的组件的相应方法。

下面是我演示中的ViewModel:

@HiltViewModel
class NickNameViewModel @Inject constructor(
private val repo: DataRepo,
application: Application,
): AndroidViewModel(application) {
fun updateUserNickName(
userId: String,
userKey: String,
userNickName: String,
resultCallBack: (Boolean, String) -> Unit = {_, _ -> },
) {
viewModelScope.launch {
repo.updateUserInfo(
userId = userId,
userKey = userKey,
userNickName = userNickName,
).collect {
//.....
}
}
}
}
}

在上面的ViewModel中,DetaRepo组件是通过构造注入注入到ViewModel中的。

DataRepo中提供的方法可以通过updateUserNickName方法轻松访问。

下面是片段中ViewModel的代码:
@AndroidEntryPoint
class NickNameModifyFragment: Fragment(R.layout.nick_name_modify_fragment) {
companion object {
const val nickNickNameResult = "nickNickNameResult"
}
private var _binding: NickNameModifyFragmentBinding? = null
private val viewModel by viewModels<NickNameViewModel>()
}

应该注意的是,如果Hilt在Fragment的ViewModel中,你仍然需要在附加的Activity中添加@AndroidEntryPoint注释。

希望给你带来好运。

如果你使用的是SingletonComponent,那么在使用@ providers注释时,你也应该使用@singleton注释

@Module
@InstallIn(SingletonComponent::class)
object AppModule {
@Singleton @Provides
fun provideServiceGenerator(
context: Context
) = ServiceGenerator(context)
}

类似于FragmentComponent你需要注释@Provides @FragmentScoped

正如我读到你提到的UseCase和其他东西,你必须检查hilt viewModel,因为它会自动生成很多样板代码,并根据活动或片段生命周期处理依赖

@Module
@InstallIn(ViewModelComponent::class)
object ViewModelModule {
//repo
@Provides
@ViewModelScoped
fun providesTicketsRepo(
checkOutApi: CheckOutApi
)= TicketsRepo(checkOutApi)
}

@HiltViewModel
class TicketsAndCouponViewModel
@Inject constructor(
val ticketsRepo: TicketsRepo
) : ViewModel() { /****/}

内部片段和活动

val viewModel by viewModels<TicketsAndCouponViewModel>()

相关内容

  • 没有找到相关文章

最新更新