Skip to content

Instantly share code, notes, and snippets.

View catalinghita8's full-sized avatar

Catalin Ghita catalinghita8

View GitHub Profile
@AndroidEntryPoint
class MessageActivity : AppCompatActivity() {
@Inject
lateinit var mInjectedFragment: MessageFragment
}
@HiltAndroidApp
class MessageApplication : Application() { }
apply plugin: 'kotlin-kapt'
apply plugin: 'dagger.hilt.android.plugin'
...
dependencies {
...
implementation "com.google.dagger:hilt-android:2.28-alpha"
implementation 'androidx.hilt:hilt-lifecycle-viewmodel:1.0.0-alpha02'
kapt "com.google.dagger:hilt-android-compiler:2.28-alpha"
class WeatherListFragment: Fragment() {
fun onCreate() {
val vm = DataViewModel(DependencyInjector.injectedRepository)
}
}
class WeatherItemDetailsFragment: Fragment() {
fun onCreate() {
val vm = DataViewModel(DependencyInjector.injectedRepository)
}
class WeatherListFragment: Fragment() {
fun onCreate() {
val vm = DataViewModel(DependencyInjector.injectedRepoistory)
}
}
class WeatherItemDetailsFragment: Fragment() {
fun onCreate() {
val vm = DataViewModel(DependencyInjector.injectedRepoistory)
}
class DataViewModel(val repository: WeatherRepository)
class WeatherRepository(val localClient: LocalDataClient,
val inMemoryClient: InMemoryDataClient,
val remoteClient: RemoteDataClient)
class LocalDataClient(val localDatabase: LocalDatabase)
class InMemoryDataClient(val memoryCache: MemoryCache)
class RemoteDataClient(val retrofit: RetrofitClient)
class WeatherListFragment: Fragment() {
fun onCreate() {
val repo = WeatherRepository(LocalDataClient(LocalDataBase()),
InMemoryDataClient(MemoryCache()),
RemoteDataClient(RetrofitCLient()))
val vm = DataViewModel(repo)
}
}
class WeatherItemDetailsFragment: Fragment() {
class DataViewModelTest {
@Mock
lateinit var mockedDataSource: DataSource // Our mocked dependency
lateinit var dataViewModel: DataViewModel
@Before
fun setup() {
dataViewModel = DataViewModel(mockedDataSource) // This will now work!
class DataViewModelTest {
lateinit var dataViewModel: DataViewModel
@Mock
lateinit var dataManager: DataSource
@Before
fun setup() {
MockitoAnnotations.initMocks(this)
loyaltyClient = LoyaltyClient(userLoyaltyRestClient, userSessionManager)
interface DataSource {
fun getCachedData(): String
}
class LocalDataClient: DataSource {
private val jsonFile = resources.getFile(JSON_FILE_TAG)
override fun getCachedData() = jsonFile.toString()
}