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
@HiltAndroidApp | |
class MyApplication : Application() |
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 ApplicationDiContainer { | |
// Scoped type. Same instance is always returned | |
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) | |
// Unscoped type. Always returns a different instance | |
fun getMyRepository(): MyRepository { | |
return MyRepository(applicationScope) | |
} | |
} |
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 ApplicationDiContainer { | |
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) | |
val myRepository = MyRepository(applicationScope) | |
} | |
class MyApplication : Application() { | |
val applicationDiContainer = ApplicationDiContainer() | |
} |
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 MyRepository(private val externalScope: CoroutineScope) { /* ... */ } | |
class MyApplication : Application() { | |
// Application-scoped types that any class in the app could access | |
// using the applicationContext. | |
val applicationScope = CoroutineScope(SupervisorJob() + Dispatchers.Default) | |
val myRepository = MyRepository(applicationScope) | |
} |
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
// PLEASE, READ | |
// | |
// This is a way to scope ViewModels to the Composition. | |
// However, this doesn't survive configuration changes or procress death on its own. | |
// You can handle all config changes in compose by making the activity handle those in the Manifest file | |
// e.g. android:configChanges="colorMode|density|fontScale|keyboard|keyboardHidden|layoutDirection|locale|mcc|mnc|navigation|orientation|screenLayout|screenSize|smallestScreenSize|touchscreen|uiMode"> | |
// | |
// This is just an exploration to see what's possible in Compose. We don't encourage developers to copy-paste | |
// this code if they don't fully understand the implications of it and if this actually solves the use case to solve. |
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 LocationFragment: Fragment() { | |
override fun onViewCreated(view: View, savedInstanceState: Bundle?) { | |
// ... | |
viewLifecycleOwner.lifecycleScope.launch { | |
viewLifecycleOwner.repeatOnLifecycle(Lifecycle.State.STARTED) { | |
locationProvider.locationFlow().collect { | |
// New location! Update the map | |
} | |
} | |
} |
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 LocationActivity : AppCompatActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
// Create a new coroutine since repeatOnLifecycle is a suspend function | |
lifecycleScope.launch { | |
// The block passed to repeatOnLifecycle is executed when the lifecycle | |
// is at least STARTED and is cancelled when the lifecycle is STOPPED. | |
// It automatically restarts the block when the lifecycle is STARTED again. | |
repeatOnLifecycle(Lifecycle.State.STARTED) { |
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 UserRepository( | |
private val userEventsDataSource: FirestoreUserEventDataSource | |
) { | |
// New collectors will register as a new callback in Firestore. | |
// As this function depends on a `userId`, the flow cannot be | |
// reused by calling shareIn or stateIn in this function. | |
// That will cause a new Shared/StateFlow to be created | |
// every time the function is called. | |
fun getUserEvents(userId: String): Flow<UserEventsResult> = | |
userLocalDataSource.getObservableUserEvents(userId) |
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 UserRepository( | |
private val userLocalDataSource: UserLocalDataSource, | |
private val externalScope: CoroutineScope | |
) { | |
// DO NOT USE shareIn or stateIn in a function like this. | |
// It creates a new SharedFlow/StateFlow per invocation which is not reused! | |
fun getUser(): Flow<User> = | |
userLocalDataSource.getUser() | |
.shareIn(externalScope, WhileSubscribed()) |
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 LocationRepository( | |
private val locationDataSource: LocationDataSource, | |
private val externalScope: CoroutineScope | |
) { | |
val locations: Flow<Location> = | |
locationDataSource.locationsSource.stateIn(externalScope, WhileSubscribed(), EmptyLocation) | |
} |