Skip to content

Instantly share code, notes, and snippets.

View pokk's full-sized avatar
🌀

Jieyi pokk

🌀
View GitHub Profile
@pokk
pokk / MvvmModifiedAdapter.kt
Last active January 16, 2018 15:34
the adapter was fixed.
artistAdapter = ArtistAdapter(this@ChartIndexFragment, R.layout.item_artist_type_1, artistRes) { holder, item, _ ->
// If a ViewModel doesn't exist, I will create a ViewModel.
if (null == holder.binding.avm)
holder.binding.avm = RecyclerViewSearchArtistChartViewModel(item)
// Otherwise, I just reset the data to the ViewModel.
else
holder.binding.avm?.setArtistItem(item)
}
artistAdapter = ArtistAdapter(this@fragment, R.layout.item_artist_type_1, artistRes) { holder, item, _ ->
// !! The problem will happen here.
// The ViewModel will be created again and again.
holder.binding.avm = RecyclerViewSearchArtistChartViewModel(item)
}
class ActivityModule {
@Provides
@PerActivity
fun provideNavigator(): Navigator = Navigator()
@Provides
@PerActivity
fun provideMainPresenter(): MainContract.Presenter = MainPresenter()
}
@Singleton
@Component(modules = arrayOf(AppModule::class))
interface AppComponent {
object Initializer {
fun init(app: App): AppComponent = DaggerAppComponent.builder()
.appModule(AppModule(app))
.netModule(NetModule(app))
.build()
}
@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
@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"}
@Module
class FragmentMainModule {
@Provides
@PerFragment
fun provideMainPresenter(usecase: CreateFakeUseCase): MainContract.Presenter = MainPresenter(usecase)
@Provides
@PerFragment
fun provideUsecase(threadExecutor: ThreadExecutor,
postExecutionThread: PostExecutionThread,
@Module
abstract class BindingFragmentModule {
@PerFragment
@ContributesAndroidInjector(modules = arrayOf(FragmentMainModule::class))
abstract fun contributeMainFragmentInjector(): MainFragment
}
@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
}
@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
}