class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
- val greetingHandler = applicationComponent.greetingHandlerFactory.create(assistedArg = "this is an assisted arg")
+ val greetingHandler = applicationComponent.greetingHandlerCreator("this is an assisted arg")
View ViewModel.kt
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
import androidx.lifecycle.ViewModel as AndroidXViewModel | |
import androidx.lifecycle.viewModelScope as androidXViewModelScope | |
actual abstract class ViewModel actual constructor() : AndroidXViewModel() { | |
actual val viewModelScope: CoroutineScope = androidXViewModelScope | |
actual override fun onCleared() { | |
super.onCleared() | |
} | |
} |
View ViewModel.kt
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
actual abstract class ViewModel { | |
actual val viewModelScope = MainScope() | |
protected actual open fun onCleared() {} | |
fun clear() { | |
onCleared() | |
viewModelScope.cancel() | |
} | |
} |
View ViewModel.kt
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
expect abstract class ViewModel() { | |
val viewModelScope: CoroutineScope | |
protected open fun onCleared() | |
} |
View Retained.kt
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
// In an activity: | |
private val presenter by retain { entry -> | |
// entry exposes the viewModelScope, savedStateHandle, and a way to listen to onClear() | |
MyPresenter() | |
} | |
// Or in a composable, implicitly: | |
val presenter = retain { MyPresenter() } // this will look at LocalViewModelStoreOwner.current | |
// Or explicitly: | |
val activity = ... |
View ContentView.swift
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
private let greetingHandler = InjectApplicationComponent() | |
.greetingHandlerCreator("assisted arg!") |
View PlatformGreeter.kt
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
// iOS source set | |
interface PlatformComponent { | |
@Provides fun IosPlatformGreeter.bind(): PlatformGreeter = this | |
} | |
// Android source set | |
interface PlatformComponent { | |
@Provides fun AndroidPlatformGreeter.bind(): PlatformGreeter = this | |
} |
View MainActivity.md
View GreeterApplication.md
class GreeterApplication : Application(), ApplicationComponentProvider {
override val component by lazy(LazyThreadSafetyMode.NONE) {
- DaggerApplicationComponent.factory().build(applicationContext)
+ ApplicationComponent::class.create(applicationContext)
}
}
View goodbyeTypealiases.md
- typealias SpecialGreeting1 = String
- typealias SpecialGreeting2 = String
+ @JvmInline value class SpecialGreeting1(val value: String)
+ @JvmInline value class SpecialGreeting2(val value: String)
...
- fun provideSpecialGreeting1(): SpecialGreeting1 = "Heeeeey"
+ fun provideSpecialGreeting1(): SpecialGreeting1 = SpecialGreeting1("Heeeeey")
...
- platformGreeter().greet(finalGreeting = specialGreeting1)
View ApplicationComponent&MainActivity.md
@Component @Singleton
abstract class ApplicationComponent(
@get:Provides val context: Context,
- ) : GreetingComponent {
- abstract val greetingHandlerCreator: (String) -> GreetingHandler
- }
+ ) : GreetingComponent
NewerOlder