@@ -14,6 +14,10 @@ import com.jakewharton.rxbinding3.widget.textChanges
14
14
import io.reactivex.Observable
15
15
import io.reactivex.android.schedulers.AndroidSchedulers
16
16
import io.reactivex.schedulers.Schedulers
17
+ import me.li2.android.common.framework.openAppSettings
18
+ import me.li2.android.location.RequestLocationResult
19
+ import me.li2.android.location.ifLocationAllowed
20
+ import me.li2.android.location.openSystemLocationSetting
17
21
import java.util.concurrent.TimeUnit
18
22
19
23
fun <T > Observable<T>.forUi (): Observable <T > =
@@ -44,7 +48,6 @@ fun setViewVisibility(view: View, value: Boolean?) {
44
48
view.visibility = if (value == true ) View .VISIBLE else View .GONE
45
49
}
46
50
47
-
48
51
@BindingAdapter(" android:src" )
49
52
fun setImageUrl (view : ImageView , src : String ) {
50
53
Glide .with (view.context)
@@ -55,11 +58,36 @@ fun setImageUrl(view: ImageView, src: String) {
55
58
/* *
56
59
* Location permission request prompt.
57
60
*/
58
- fun locationPermissionPrompt (context : Context ): AlertDialog {
61
+ private fun locationPermissionPrompt (context : Context ): AlertDialog {
59
62
return MaterialAlertDialogBuilder (context)
60
- .setTitle(" \" Demo App\" Would Like to Access the Location" )
61
- .setMessage(" This will let you search place, get your current location" )
62
- .setPositiveButton(" Yep!" , null )
63
- .setNegativeButton(" Nope!" , null )
64
- .create() // MUST NOT show()
65
- }
63
+ .setTitle(" \" Demo App\" Would Like to Access the Location" )
64
+ .setMessage(" This will let you search place, get your current location" )
65
+ .setPositiveButton(" Yep!" , null )
66
+ .setNegativeButton(" Nope!" , null )
67
+ .create() // MUST NOT show()
68
+ }
69
+
70
+ fun Fragment.doWithLocationPermission (action : () -> Unit ) {
71
+ activity?.ifLocationAllowed(locationPermissionPrompt(requireContext()), onError = {
72
+ toast(it.message.toString())
73
+ }, onResult = { result: RequestLocationResult ->
74
+ when (result) {
75
+ RequestLocationResult .ALLOWED -> {
76
+ // location permission granted and service is on,
77
+ // it's good time to get last know location
78
+ action()
79
+ }
80
+ RequestLocationResult .PERMISSION_DENIED -> {
81
+ toast(" permission denied ${System .currentTimeMillis()} " )
82
+ }
83
+ RequestLocationResult .PERMISSION_DENIED_NOT_ASK_AGAIN -> {
84
+ // location permission denied, go to App settings
85
+ activity?.openAppSettings(requireContext().packageName)
86
+ }
87
+ RequestLocationResult .SERVICE_OFF -> {
88
+ // location service is turned off, go to system settings
89
+ activity?.openSystemLocationSetting { isServiceOn -> }
90
+ }
91
+ }
92
+ })
93
+ }
0 commit comments