Skip to content

Instantly share code, notes, and snippets.

View tfcporciuncula's full-sized avatar

Fred Porciúncula tfcporciuncula

View GitHub Profile
@HiltViewModel
class MyViewModel @Inject constructor(
private val savedStateHandle: SavedStateHandle,
// other dependencies
) : ViewModel() {
...
}
@tfcporciuncula
tfcporciuncula / 1 - ci.yml
Last active January 10, 2024 14:02
Keeping a project dependency graph on the README up to date automatically by always generating it on CI with a GitHub Action
generate-dependency-graph:
name: Generate Dependency Graph
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Setup Graphviz
uses: ts-graphviz/setup-graphviz@v1
// 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 = ...
actual abstract class ViewModel {
actual val viewModelScope = MainScope()
protected actual open fun onCleared() {}
fun clear() {
onCleared()
viewModelScope.cancel()
}
}
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()
}
}
expect abstract class ViewModel() {
val viewModelScope: CoroutineScope
protected open fun onCleared()
}
@Qualifier annotation class SpecialGreeting1
@Qualifier annotation class SpecialGreeting2
@Module
@InstallIn(SingletonComponent::class)
object GreetingModule {
@Provides @SpecialGreeting1
fun provideSpecialGreeting1(): String = "Heeeeey"
@Provides @SpecialGreeting2
private let greetingHandler = InjectApplicationComponent()
.greetingHandlerCreator("assisted arg!")
// iOS source set
interface PlatformComponent {
@Provides fun IosPlatformGreeter.bind(): PlatformGreeter = this
}
// Android source set
interface PlatformComponent {
@Provides fun AndroidPlatformGreeter.bind(): PlatformGreeter = this
}
 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")