Skip to content

Commit

Permalink
Workaround in Java due to Dagger/Kotlin not playing well together as …
Browse files Browse the repository at this point in the history
…of now

see: google/dagger#1478
  • Loading branch information
TomGilbert committed Apr 15, 2019
1 parent 639b8b5 commit 1549409
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ package com.example.twcgilbert.postsapp.common.di

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import dagger.MapKey
import javax.inject.Inject
import javax.inject.Provider
import javax.inject.Singleton
import kotlin.reflect.KClass

@Singleton
class ViewModelFactory @Inject constructor(
Expand All @@ -16,9 +14,4 @@ class ViewModelFactory @Inject constructor(

@Suppress("UNCHECKED_CAST")
override fun <T : ViewModel?> create(modelClass: Class<T>): T = viewModels[modelClass]?.get() as T
}

@Target(AnnotationTarget.FUNCTION, AnnotationTarget.PROPERTY_GETTER, AnnotationTarget.PROPERTY_SETTER)
@Retention(AnnotationRetention.RUNTIME)
@MapKey
internal annotation class ViewModelKey(val value: KClass<out ViewModel>)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.example.twcgilbert.postsapp.common.di;

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

import androidx.lifecycle.ViewModel;
import dagger.MapKey;

/**
* Workaround in Java due to Dagger/Kotlin not playing well together as of now
* https://github.com/google/dagger/issues/1478
*/
@MapKey
@Documented
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewModelKey {
Class<? extends ViewModel> value();
}

0 comments on commit 1549409

Please sign in to comment.