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 | |
.shareIn(externalScope, SharingStarted.Eagerly, replay = 10) | |
} |
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.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 LocationDataSource( | |
private val locationClient: FusedLocationProviderClient | |
) { | |
val locationsSource: Flow<Location> = callbackFlow<Location> { | |
val callback = object : LocationCallback() { | |
override fun onLocationResult(result: LocationResult?) { | |
result ?: return | |
try { offer(result.lastLocation) } catch(e: Exception) {} | |
} | |
} |
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 // Setup Hilt in your app | |
class MyApplication : Application() { ... } | |
// Make Hilt aware of this ViewModel | |
@HiltViewModel | |
class LoginViewModel @Inject constructor( | |
private val savedStateHandle: SavedStateHandle, | |
/* ... Other dependencies Hilt takes care of ... */ | |
) : ViewModel() { ... } |
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) | |
// Collects from the flow when the View is at least STARTED and | |
// SUSPENDS the collection when the lifecycle is STOPPED. | |
// Collecting the flow cancels when the View is DESTROYED. | |
lifecycleScope.launchWhenStarted { | |
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
@Composable | |
fun LocationUI(locationFlow: Flow<Location>) { | |
val location by locationFlow.collectAsStateWithLifecycle() | |
// Current location, do something with it | |
} |
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) | |
// Listen to one flow in a lifecycle-aware manner using flowWithLifecycle | |
lifecycleScope.launch { | |
locationProvider.locationFlow() | |
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) | |
.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 coroutine | |
lifecycleScope.launch { | |
repeatOnLifecycle(Lifecycle.State.RESUMED) { | |
// Repeat when the lifecycle is RESUMED, cancel when PAUSED | |
} |
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.addRepeatingJob(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) | |
// Collects from the flow when the lifecycle is at least STARTED and | |
// STOPS the collection when it's STOPPED. | |
// It automatically restarts collecting when the lifecycle is STARTED again. | |
lifecycleOwner.addRepeatingJob(Lifecycle.State.STARTED) { | |
locationProvider.locationFlow().collect { | |
// New location! Update the map |