-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Show location updates when the app is active
- Loading branch information
1 parent
b5bfe7d
commit 0aac0d2
Showing
5 changed files
with
84 additions
and
4 deletions.
There are no files selected for viewing
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
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
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
30
app/src/main/java/com/hicham/flowlifecycle/MainViewModel.kt
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
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> | ||
) |
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
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
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> |