Skip to content

Instantly share code, notes, and snippets.

View pokk's full-sized avatar
🌀

Jieyi pokk

🌀
View GitHub Profile
@pokk
pokk / README.md
Created August 18, 2017 06:53
Ruby has magic candy function as Kotlin

[TOC]

Introduction

Ruby and Kotlin have the same magic candy function!

What's kind of magic candy function?

Kotlin

  • apply (also)
@pokk
pokk / README.kt
Last active August 21, 2017 07:23
Create an object from generic parameter in the class.
// There is a class with generic parameter.
abstract class A<T> {
// This is KClass version.
val t: T by lazy {
(this@A::class
.supertypes.first() // KType of A class
.arguments.first() // Data type of the generic T
.type!!.classifier as KClass<*>) // The class of the generic T
.primaryConstructor!!.call() as T // An object from the generic T
}
@pokk
pokk / README.kt
Created August 29, 2017 11:15
A easy way to set onMeasure function. Of course if you wanna customize more, you can do more things.
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
// Measure all of children's width & height.
this.measureChildren(widthMeasureSpec, heightMeasureSpec)
// Measure width & height of this view_group's layout(layout_width or layout_height will be `match_parent`
// no matter what we set `wrap_content` or `match_patent` when we're using getDefaultSize).
// We'll reset this method by another way for achieving `wrap_content`.
val mini = minOf(getDefaultSize(suggestedMinimumWidth, widthMeasureSpec), getDefaultSize(suggestedMinimumHeight, heightMeasureSpec))
this.setMeasuredDimension(mini, mini)
}
@pokk
pokk / AppComponent.kt
Created November 17, 2017 09:48
app component for my blog.
@Singleton
@Component(modules = arrayOf(AppModule::class,
RepositoryModule::class,
BindingActivityModule::class,
AndroidSupportInjectionModule::class))
interface AppComponent : AndroidInjector<App> {
/** [AndroidInjector] Builder for using on this whole app. */
@Component.Builder
abstract class Builder : AndroidInjector.Builder<App>()
@pokk
pokk / AppModule.kt
Last active December 3, 2017 02:38
app module for my blog.
@Module
class AppModule {
@Provides
@Singleton
fun provideApplication(app: App): Application = app
@Provides
@Singleton
fun provideAppContext(app: App): Context = app.applicationContext
}
@Module
abstract class BindingActivityModule {
/**
* ContributesAndroidInjector is including fragment injector. Only putting FragmentBindModule in modules array,
* the children fragment can obtain the parent's fragment injector.
*/
@PerActivity
@ContributesAndroidInjector(modules = arrayOf(BindingFragmentModule::class))
abstract fun contributeMainActivityInjector(): MainActivity
}
@Module
abstract class BindingFragmentModule {
@PerFragment
@ContributesAndroidInjector(modules = arrayOf(FragmentMainModule::class))
abstract fun contributeMainFragmentInjector(): MainFragment
}
@Module
class FragmentMainModule {
@Provides
@PerFragment
fun provideMainPresenter(usecase: CreateFakeUseCase): MainContract.Presenter = MainPresenter(usecase)
@Provides
@PerFragment
fun provideUsecase(threadExecutor: ThreadExecutor,
postExecutionThread: PostExecutionThread,
@pokk
pokk / cloudSettings
Created November 27, 2017 08:31
Visual Studio Code Settings Sync Gist
{"lastUpload":"2017-11-27T08:31:15.832Z","extensionVersion":"v2.8.6"}
@pokk
pokk / OldApp.kt
Last active December 3, 2017 02:42
class App: Application() {
companion object {
lateinit private var context: Context
@JvmStatic fun appComponent(): AppComponent = (context as App).appComponent
// Provide the global application context.
@JvmStatic fun getAppContext(): Context = context
}
// Before dagger 2.11