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
interface ViewModelAssistedFactory<T : ViewModel> { | |
fun create(handle: SavedStateHandle): T | |
} |
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 DetailViewModel @AssistedInject constructor( | |
private val githubApi: GithubApi, | |
@Assisted private val handle: SavedStateHandle | |
) : ViewModel() { | |
fun loadData() { | |
val id = handle["id"] ?: "default" | |
viewModelScope.launch { | |
val response = githubApi.getCommit(id) | |
// Handle response | |
} |
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
compileOnly("com.squareup.inject:assisted-inject-annotations-dagger2:0.3.3") | |
kapt("com.squareup.inject:assisted-inject-processor-dagger2:0.3.3") |
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 DetailViewModel @Inject constructor( | |
private val githubApi: GithubApi, | |
private val handle: SavedStateHandle | |
) : ViewModel() { | |
fun loadData() { | |
val id = handle["id"] ?: "default" | |
viewModelScope.launch { | |
val response = githubApi.getCommit(id) | |
// Handle response | |
} |
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
@Module | |
abstract class DetailModule { | |
@Binds | |
@IntoMap | |
@ViewModelKey(DetailViewModel::class) | |
abstract fun bindViewModel(viewModel: DetailViewModel): 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
class ViewModelFactory @Inject constructor( | |
private val creators: MutableMap<Class<out ViewModel>, Provider<ViewModel>> | |
) : ViewModelProvider.Factory { | |
override fun <T : ViewModel> create(modelClass: Class<T>): T { | |
return creators[modelClass]?.get() as? T | |
?: throw IllegalArgumentException("The requested ViewModel isn't bound") | |
} | |
} |
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 DetailActivity : AppCompatActivity() { | |
private val githubApi = GithubApi() | |
private val viewModel by viewModels { DetailViewModelFactory(githubApi, this) } | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Use viewModel here | |
viewModel.loadData() | |
} |
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 DetailViewModelFactory( | |
private val githubApi: GithubApi, | |
owner: SavedStateRegistryOwner, | |
defaultArgs: Bundle? = null | |
) : AbstractSavedStateViewModelFactory(owner, defaultArgs) { | |
override fun <T : ViewModel> create( | |
key: String, | |
modelClass: Class<T>, | |
handle: SavedStateHandle | |
): T { |
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 DetailViewModel( | |
private val githubApi: GithubApi, | |
private val handle: SavedStateHandle | |
) : ViewModel() { | |
fun loadData() { | |
val id = handle["id"] ?: "default" | |
viewModelScope.launch { | |
val response = githubApi.getCommit(id) | |
// Handle response | |
} |
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 DetailActivity : AppCompatActivity() { | |
private val viewModel: DetailViewModel by viewModels() | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Use viewModel here | |
} | |
} |