Last active
May 24, 2021 19:06
-
-
Save barbeau/bbfe3280b59462bae94bc1ddb218bc68 to your computer and use it in GitHub Desktop.
Medium article - Room + Flow for location updates
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
@AndroidEntryPoint | |
class MainActivity : AppCompatActivity(), SharedPreferences.OnSharedPreferenceChangeListener { | |
... | |
// Repository (in this case, Room database) from which to receive location updates via Flow, injected via Hilt | |
@Inject | |
lateinit var repository: LocationRepository | |
override fun onCreate(savedInstanceState: Bundle?) { | |
... | |
// Observe locations via Flow as they are inserted into Room by the Service | |
repository.getLocations() | |
.flowWithLifecycle(lifecycle, Lifecycle.State.STARTED) | |
.onEach { | |
if (it.isNotEmpty()) { | |
logResultsToScreen("Foreground location: ${it[it.size - 1].toText()}") | |
} | |
} | |
.launchIn(lifecycleScope) | |
} | |
... | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment