-
-
Save adamarthurryan/6a58e255f10efbfc688988d6ec6b4d95 to your computer and use it in GitHub Desktop.
Injecting ViewModel with Dagger2 on Android using Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class App : Application(), HasActivityInjector { | |
@Inject lateinit var activityInjector: DispatchingAndroidInjector<Activity> | |
override fun activityInjector(): AndroidInjector<Activity> { | |
return activityInjector | |
} | |
override fun onCreate() { | |
super.onCreate() | |
DaggerAppComponent.builder() | |
.build() | |
.inject(this) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.daggerwithkotlin.dagger | |
import com.tinmegali.daggerwithkotlin.App | |
import com.tinmegali.daggerwithkotlin.dagger.activities.ActivitiesModule | |
import com.tinmegali.daggerwithkotlin.dagger.fragments.FragmentsModule | |
import com.tinmegali.daggerwithkotlin.dagger.viewModels.ViewModelModule | |
import dagger.Component | |
import dagger.android.AndroidInjectionModule | |
import javax.inject.Singleton | |
@Singleton | |
@Component(modules = arrayOf( | |
AndroidInjectionModule::class, | |
ViewModelModule::class | |
)) | |
interface AppComponent { | |
fun inject(app: App) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivity : LifecycleActivity() { | |
//... | |
@Inject lateinit var viewModelFactory: ViewModelProvider.Factory | |
var mainViewModel: MainActivityModel? = null | |
override fun onCreate(savedInstanceState: Bundle?) { | |
AndroidInjection.inject(this) // Dagger | |
super.onCreate(savedInstanceState) | |
//.. | |
mainViewModel = ViewModelProviders.of(this, viewModelFactory) | |
.get(MainActivityModel::class.java) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class MainActivityModel | |
@Inject constructor( | |
private var noteDAO: NoteDAO, | |
private var userDAO: UserDAO | |
): ViewModel(), AnkoLogger { | |
private var notesData: LiveData<List<Note>>? = null | |
fun getNotes( ): LiveData<List<Note>>? { | |
return notesData | |
} | |
fun subscribeToNotesDBChanges( callback: OnSync ) { | |
doAsync { | |
notesData = noteDAO.findAllNotedObservable() | |
info("Notes received.") | |
callback.notesReceived() | |
} | |
} | |
public interface OnSync { | |
fun notesReceived( ) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.daggerwithkotlin.dagger.viewModels | |
import com.tinmegali.daggerwithkotlin.MainActivityModel | |
import dagger.Component | |
@Component( modules = arrayOf( | |
ViewModelModule::class | |
)) | |
interface ViewModelComponent { | |
// inject your view models | |
fun inject( mainViewModel: MainActivityModel ) | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.oauth2restclient.dagger | |
import android.arch.lifecycle.ViewModel | |
import android.arch.lifecycle.ViewModelProvider | |
import javax.inject.Inject | |
import javax.inject.Provider | |
import javax.inject.Singleton | |
@Suppress("UNCHECKED_CAST") | |
@Singleton | |
class ViewModelFactory @Inject | |
constructor( | |
private val creators: Map<Class<out ViewModel>, | |
@JvmSuppressWildcards Provider<ViewModel>> | |
) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
var creator: Provider<out ViewModel>? = creators[modelClass] | |
if (creator == null) { | |
for ((key, value) in creators) { | |
if (modelClass.isAssignableFrom(key)) { | |
creator = value | |
break | |
} | |
} | |
} | |
if (creator == null) { | |
throw IllegalArgumentException("unknown model class " + modelClass) | |
} | |
try { | |
return creator.get() as T | |
} catch (e: Exception) { | |
throw RuntimeException(e) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.daggerwithkotlin.dagger.viewModels | |
import android.arch.lifecycle.ViewModel | |
import java.lang.annotation.Documented | |
import java.lang.annotation.Retention | |
import java.lang.annotation.RetentionPolicy | |
import dagger.MapKey | |
import kotlin.reflect.KClass | |
@MustBeDocumented | |
@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER) | |
@Retention(RetentionPolicy.RUNTIME) | |
@MapKey | |
internal annotation class ViewModelKey(val value: KClass<out ViewModel>) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.tinmegali.daggerwithkotlin.dagger.viewModels | |
import android.arch.lifecycle.ViewModel | |
import android.arch.lifecycle.ViewModelProvider | |
import com.tinmegali.daggerwithkotlin.MainActivityModel | |
import com.tinmegali.oauth2restclient.dagger.ViewModelFactory | |
import dagger.Binds | |
import dagger.Module | |
import dagger.multibindings.IntoMap | |
@Module | |
abstract class ViewModelModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey( MainActivityModel::class ) | |
// Bind your View Model here | |
abstract fun bindMainViewModel( mainViewModel: MainActivityModel ): ViewModel | |
@Binds | |
abstract fun bindViewModelFactory( factory: ViewModelFactory): | |
ViewModelProvider.Factory | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
If you use @provides instead of @Inject, ViewModel will be wrong。