[Android] UserListApp - Dependency Injection - Part4

encorex32268

LiHan

Posted on June 13, 2023

[Android] UserListApp - Dependency Injection - Part4

Create Application

@HiltAndroidApp
class UserApplication  : Application()
Enter fullscreen mode Exit fullscreen mode

Add Name ... on AndroidManifest

  <application
        android:name=".UserApplication"
    ....
Enter fullscreen mode Exit fullscreen mode

Add User Internet Permission

    <uses-permission android:name="android.permission.INTERNET"></uses-permission>

Enter fullscreen mode Exit fullscreen mode

Create UserModule.kt

這個檔案是為了提前產生物件.比如說:UserApi , UserRepostioryImpl

@Module
@InstallIn(SingletonComponent::class)
object UserModule {

    @Provides
    @Singleton
    fun providesUserApi() : UserApi {
        return Retrofit.Builder()
           .baseUrl(UserApi.BASE_URL)
           .addConverterFactory(GsonConverterFactory.create())
           .build().create()
    }

    @Provides
    @Singleton
    fun providesUserRepository(
        userApi : UserApi
    ) : UserRepository {
        return UserRepositoryImpl(userApi)
    }
}
Enter fullscreen mode Exit fullscreen mode
💖 💪 🙅 🚩
encorex32268
LiHan

Posted on June 13, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related