Skip to content

Instantly share code, notes, and snippets.

View catalinghita8's full-sized avatar

Catalin Ghita catalinghita8

View GitHub Profile
class LocalDataSource() {
private val jsonFile = resources.getFile(JSON_FILE_TAG)
fun getCachedData() = jsonFile.toString()
}
class DataViewModel() {
private val localDataClient = LocalDataSource()
fun getData() = localDataClient.getCachedData()
class DataViewModelTest {
lateinit var dataViewModel: DataViewModel
@Before
fun setup() {
dataViewModel = DataViewModel(RemoteDataSource()) // This will fail!
}
}
class LocalDataFragment: Fragment() {
fun initializeViewModel() {
val injectedDataClient: DataSource = DependencyInjector.localDataClient
val viewModel = DataViewModel(injectedDataClient)
viewModel.getCachedData()
}
}
class RemoteDataFragment: Fragment() {
fun initializeViewModel() {
interface DataSource {
fun getData(): String
}
class LocalDataClient: DataSource {
override fun getData() = sharedPreferences.getString(KEY)
}
class RemoteDataClient: DataSource {
override fun getData() = retrofit.getValue(KEY)
// Container of objects shared across the whole project
class DependencyInjector {
private val objectA: A = A()
private val objectB: B = B()
val repository: Repository = Repository(objectA, objectB)
}
fun main {
val injectedRepository = DependencyInjector.repository
val vm = ViewModel(injectedRepository)
class ViewModel {
private val repository: Repository = Repository (A(), B())
fun doSomething() {
repository.use()
}
}
class Repository(private val objectA: A, private val objectB: B) {
fun use() {
class ViewModel(private val repository: Repository) {
fun doSomething() {
repository.use()
}
}
class Repository(private val objectA, private val objectB: B) {
fun use() {
objectA.use()
objectB.use()
class ViewModel {
private val repository: Repository = Repository()
fun doSomething() {
repository.use()
}
}
class Repository {
private val objectA: A = A()
@Provides
@ActivityScoped
String provideActivityId(RegistrationActivity activity) {
return activity.getIntent().getStringExtra(ID);
}
dependencies {
...
implementation group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.5.0'
implementation group: 'edu.stanford.nlp', name: 'stanford-corenlp', version: '3.5.0', classifier: 'models'
}