This file contains hidden or 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 LocalDataSource() { | |
| private val jsonFile = resources.getFile(JSON_FILE_TAG) | |
| fun getCachedData() = jsonFile.toString() | |
| } | |
| class DataViewModel() { | |
| private val localDataClient = LocalDataSource() | |
| fun getData() = localDataClient.getCachedData() |
This file contains hidden or 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 DataViewModelTest { | |
| lateinit var dataViewModel: DataViewModel | |
| @Before | |
| fun setup() { | |
| dataViewModel = DataViewModel(RemoteDataSource()) // This will fail! | |
| } | |
| } |
This file contains hidden or 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 LocalDataFragment: Fragment() { | |
| fun initializeViewModel() { | |
| val injectedDataClient: DataSource = DependencyInjector.localDataClient | |
| val viewModel = DataViewModel(injectedDataClient) | |
| viewModel.getCachedData() | |
| } | |
| } | |
| class RemoteDataFragment: Fragment() { | |
| fun initializeViewModel() { |
This file contains hidden or 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
| interface DataSource { | |
| fun getData(): String | |
| } | |
| class LocalDataClient: DataSource { | |
| override fun getData() = sharedPreferences.getString(KEY) | |
| } | |
| class RemoteDataClient: DataSource { | |
| override fun getData() = retrofit.getValue(KEY) |
This file contains hidden or 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
| // 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) |
This file contains hidden or 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 ViewModel { | |
| private val repository: Repository = Repository (A(), B()) | |
| fun doSomething() { | |
| repository.use() | |
| } | |
| } | |
| class Repository(private val objectA: A, private val objectB: B) { | |
| fun use() { |
This file contains hidden or 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 ViewModel(private val repository: Repository) { | |
| fun doSomething() { | |
| repository.use() | |
| } | |
| } | |
| class Repository(private val objectA, private val objectB: B) { | |
| fun use() { | |
| objectA.use() | |
| objectB.use() |
This file contains hidden or 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 ViewModel { | |
| private val repository: Repository = Repository() | |
| fun doSomething() { | |
| repository.use() | |
| } | |
| } | |
| class Repository { | |
| private val objectA: A = A() |
This file contains hidden or 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
| @Provides | |
| @ActivityScoped | |
| String provideActivityId(RegistrationActivity activity) { | |
| return activity.getIntent().getStringExtra(ID); | |
| } |
This file contains hidden or 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
| 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' | |
| } |