Skip to content

Commit

Permalink
Show location updates when the app is active
Browse files Browse the repository at this point in the history
  • Loading branch information
hichamboushaba committed Oct 30, 2021
1 parent b5bfe7d commit 0aac0d2
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 4 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ dependencies {
implementation 'androidx.appcompat:appcompat:1.3.1'
implementation 'com.google.android.material:material:1.4.0'
implementation "androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0"
implementation "androidx.lifecycle:lifecycle-runtime-ktx:2.4.0"
implementation "androidx.fragment:fragment-ktx:1.3.6"
implementation 'com.google.android.gms:play-services-location:18.0.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.1'
Expand Down
41 changes: 40 additions & 1 deletion app/src/main/java/com/hicham/flowlifecycle/MainFragment.kt
Original file line number Diff line number Diff line change
@@ -1,12 +1,51 @@
package com.hicham.flowlifecycle

import android.Manifest
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.os.Bundle
import android.view.View
import androidx.activity.result.contract.ActivityResultContracts
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.hicham.flowlifecycle.databinding.FragmentMainBinding
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.launch

class MainFragment: Fragment(R.layout.fragment_main) {
class MainFragment : Fragment(R.layout.fragment_main) {
private val viewModel: MainViewModel by viewModels()

val requestPermissionLauncher =
registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted: Boolean ->
if (isGranted) {
viewModel.onLocationPermissionGranted()
} else {
// TODO
}
}

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentMainBinding.bind(view)

val hasLocationPermission =
requireContext().checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION) == PERMISSION_GRANTED

if (hasLocationPermission) {
viewModel.onLocationPermissionGranted()
} else {
requestPermissionLauncher.launch(Manifest.permission.ACCESS_FINE_LOCATION)
}

viewLifecycleOwner.lifecycleScope.launch {
viewModel.locationUpdates
.flowWithLifecycle(viewLifecycleOwner.lifecycle)
.onEach { binding.location.text = "${it.latitude} ${it.longitude}" }
.launchIn(this)
}
}
}
30 changes: 30 additions & 0 deletions app/src/main/java/com/hicham/flowlifecycle/MainViewModel.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.hicham.flowlifecycle

import android.annotation.SuppressLint
import android.app.Application
import android.location.Location
import androidx.lifecycle.AndroidViewModel
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.flatMapLatest

class MainViewModel(application: Application) : AndroidViewModel(application) {
private val locationObserver = LocationObserver(application)

private val hasLocationPermission = MutableStateFlow(false)

@SuppressLint("MissingPermission")
val locationUpdates: Flow<Location> = hasLocationPermission
.filter { it }
.flatMapLatest { locationObserver.observeLocationUpdates() }

fun onLocationPermissionGranted() {
hasLocationPermission.value = true
}
}

data class ViewState(
val location: Location,
val nearbyLocations: List<String>
)
6 changes: 3 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@
tools:context=".MainActivity">

<androidx.fragment.app.FragmentContainerView
android:id="@+id/fragment_container"
android:name="com.hicham.flowlifecycle.MainFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:name="com.hicham.flowlifecycle.MainFragment"/>
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>
10 changes: 10 additions & 0 deletions app/src/main/res/layout/fragment_main.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,16 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

0 comments on commit 0aac0d2

Please sign in to comment.