Skip to content

Instantly share code, notes, and snippets.

Avatar
🌍
i hope you are going to have an amazing day

Alexandr Minkin Rasalexman

🌍
i hope you are going to have an amazing day
View GitHub Profile
View IProviderInterface.kt
interface IProviderInterface
data class ProviderClass(override val id: String, override val name: String = "Provider") : IProviderInterface
val MY_PROVIDER_SCOPE_NAME = "MY_PROVIDER_SCOPE_NAME"
kodi {
bind<IProviderInterface>() at MY_PROVIDER_SCOPE_NAME with provider { ProviderClass(UUID.randomUUID().toString()) }
}
val providerInstaceByScope = instance<IProviderInterface>(scope = MY_PROVIDER_SCOPE_NAME)
View IProviderInterface.kt
interface IProviderInterface
data class ProviderClass(override val id: String, override val name: String = "Provider") : IProviderInterface
val MY_PROVIDER_SCOPE_NAME = "MY_PROVIDER_SCOPE_NAME"
kodi {
bind<IProviderInterface>() at MY_PROVIDER_SCOPE_NAME with provider { ProviderClass(UUID.randomUUID().toString()) }
}
val providerInstaceByScope = instance<IProviderInterface>(scope = MY_PROVIDER_SCOPE_NAME)
View Extensions.kt
@BindSingle(
toClass = OkHttpClient::class,
toModule = "OkHttpModule"
)
fun createOkHttpClient(cache: Cache? = null): OkHttpClient {
val httpClient = OkHttpClient.Builder()
httpClient.connectTimeout(60, TimeUnit.SECONDS)
httpClient.writeTimeout(60, TimeUnit.SECONDS)
httpClient.readTimeout(60, TimeUnit.SECONDS)
View CustomObject.kt
object CustomObject {
@BindSingle(toClass = IAnotherClass::class)
fun providingMethod(
@WithInstance(with = "IAnotherClass::class") input: IMyClass
): IAnotherClass {
return input
}
}
//It is generate `Singleton` method for binding like:
@Rasalexman
Rasalexman / IAnotherClass.kt
Last active May 13, 2020
BindSingle example
View IAnotherClass.kt
interface IAnotherClass
interface IMyClass : IAnotherClass
@BindSingle(toClass = IMyClass::class)
class MyClass(
private val anotherOne: IAnotherClass,
@IgonreInstance private val ignoredReference: String = "MyIgnoredReference"
) : IMyClass
// It is generate `Singleton` method for binding like:
View MainApplication.kt
class MainApplication : Application() {
override fun onCreate() {
super.onCreate()
kodi {
import(systemModule)
}
}
}
View SystemModule.kt
val systemModule: IKodiModule = kodiModule {
bind<IRetrieveAndUpdateFirebaseTokenUseCase>() with provider {
RetrieveAndUpdateFirebaseTokenUseCase(
userPreference = instance<IUserPreference>(),
updateFirebaseTokenUseCase = instance<IUpdateFirebaseTokenUseCase>()
)
}
} withScope "FIREBASE_SCOPE"
@Rasalexman
Rasalexman / MainActivity.kt
Created May 6, 2020
Dependency injection example
View MainActivity.kt
class MainActivity : AppCompatActivity(), IKodi {
private val myClass: MyClass by immutableInstance() // 1.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myClassInstance = instance<MyClass>() // 2
}
@Rasalexman
Rasalexman / MainActivity.kt
Created May 6, 2020
Getting the Kodi dependency from graph
View MainActivity.kt
// import
class MainActivity : AppCompatActivity(), IKodi {
val myAmazingClass: MyClass by immutableInstance() // 1.
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
// or you can get instance directly from DI
val myInstance = instance<MyClass>() // 2.
@Rasalexman
Rasalexman / liveData.kt
Last active Jan 19, 2020
Data convertion
View liveData.kt
liveData(context = viewModelScope.coroutineContext + Dispatcher.IO) {
 emitSource(
pagesRepository.getNextPageData(eventPageLiveData).asLiveData().map { dataList ->
dataList.convertToList<MyUIModel>()
}
)
}
You can’t perform that action at this time.