From 0559c0f43212440a76039f3b32846acff3917aca Mon Sep 17 00:00:00 2001 From: Weiyi Li Date: Tue, 14 Apr 2020 12:13:33 +1200 Subject: [PATCH] Updated demo --- .idea/.name | 2 +- .../li2/android/placesample/FrameworkExt.kt | 44 +++++++++++++++---- .../li2/android/placesample/MainFragment.kt | 29 ++---------- settings.gradle | 2 +- 4 files changed, 41 insertions(+), 36 deletions(-) diff --git a/.idea/.name b/.idea/.name index 2f3678c..fbc7c00 100644 --- a/.idea/.name +++ b/.idea/.name @@ -1 +1 @@ -Place&Location Library \ No newline at end of file +Geography Library \ No newline at end of file diff --git a/app/src/main/java/me/li2/android/placesample/FrameworkExt.kt b/app/src/main/java/me/li2/android/placesample/FrameworkExt.kt index 17abef1..62cb5c5 100644 --- a/app/src/main/java/me/li2/android/placesample/FrameworkExt.kt +++ b/app/src/main/java/me/li2/android/placesample/FrameworkExt.kt @@ -14,6 +14,10 @@ import com.jakewharton.rxbinding3.widget.textChanges import io.reactivex.Observable import io.reactivex.android.schedulers.AndroidSchedulers import io.reactivex.schedulers.Schedulers +import me.li2.android.common.framework.openAppSettings +import me.li2.android.location.RequestLocationResult +import me.li2.android.location.ifLocationAllowed +import me.li2.android.location.openSystemLocationSetting import java.util.concurrent.TimeUnit fun Observable.forUi(): Observable = @@ -44,7 +48,6 @@ fun setViewVisibility(view: View, value: Boolean?) { view.visibility = if (value == true) View.VISIBLE else View.GONE } - @BindingAdapter("android:src") fun setImageUrl(view: ImageView, src: String) { Glide.with(view.context) @@ -55,11 +58,36 @@ fun setImageUrl(view: ImageView, src: String) { /** * Location permission request prompt. */ -fun locationPermissionPrompt(context: Context): AlertDialog { +private fun locationPermissionPrompt(context: Context): AlertDialog { return MaterialAlertDialogBuilder(context) - .setTitle("\"Demo App\" Would Like to Access the Location") - .setMessage("This will let you search place, get your current location") - .setPositiveButton("Yep!", null) - .setNegativeButton("Nope!", null) - .create() // MUST NOT show() -} \ No newline at end of file + .setTitle("\"Demo App\" Would Like to Access the Location") + .setMessage("This will let you search place, get your current location") + .setPositiveButton("Yep!", null) + .setNegativeButton("Nope!", null) + .create() // MUST NOT show() +} + +fun Fragment.doWithLocationPermission(action: () -> Unit) { + activity?.ifLocationAllowed(locationPermissionPrompt(requireContext()), onError = { + toast(it.message.toString()) + }, onResult = { result: RequestLocationResult -> + when (result) { + RequestLocationResult.ALLOWED -> { + // location permission granted and service is on, + // it's good time to get last know location + action() + } + RequestLocationResult.PERMISSION_DENIED -> { + toast("permission denied ${System.currentTimeMillis()}") + } + RequestLocationResult.PERMISSION_DENIED_NOT_ASK_AGAIN -> { + // location permission denied, go to App settings + activity?.openAppSettings(requireContext().packageName) + } + RequestLocationResult.SERVICE_OFF -> { + // location service is turned off, go to system settings + activity?.openSystemLocationSetting { isServiceOn -> } + } + } + }) +} diff --git a/app/src/main/java/me/li2/android/placesample/MainFragment.kt b/app/src/main/java/me/li2/android/placesample/MainFragment.kt index 50ace43..35c662e 100644 --- a/app/src/main/java/me/li2/android/placesample/MainFragment.kt +++ b/app/src/main/java/me/li2/android/placesample/MainFragment.kt @@ -12,11 +12,7 @@ import io.reactivex.disposables.CompositeDisposable import io.reactivex.rxkotlin.plusAssign import io.reactivex.rxkotlin.subscribeBy import kotlinx.android.synthetic.main.fragment_main.* -import me.li2.android.common.framework.openAppSettings import me.li2.android.location.LastKnownLocationUtils.requestLastKnownLocation -import me.li2.android.location.RequestLocationResult -import me.li2.android.location.ifLocationAllowed -import me.li2.android.location.openSystemLocationSetting import me.li2.android.maps.MapType import me.li2.android.maps.MapsStaticUtil.generateMapStaticImageUrl import me.li2.android.maps.MarkerInfo @@ -45,28 +41,9 @@ class MainFragment : Fragment() { placeAutoComplete = PlaceAutoComplete(view.context, apiKey) compositeDisposable += btn_get_last_known_location.clicks().throttleFirstShort().subscribe { - activity?.ifLocationAllowed(locationPermissionPrompt(requireContext()), onError = { - toast(it.message.toString()) - }, onResult = { result: RequestLocationResult -> - when (result) { - RequestLocationResult.ALLOWED -> { - // location permission granted and service is on, - // it's good time to get last know location - requestLastLocation() - } - RequestLocationResult.PERMISSION_DENIED -> { - toast("permission denied ${System.currentTimeMillis()}") - } - RequestLocationResult.PERMISSION_DENIED_NOT_ASK_AGAIN -> { - // location permission denied, go to App settings - activity?.openAppSettings(view.context.packageName) - } - RequestLocationResult.SERVICE_OFF -> { - // location service is turned off, go to system settings - activity?.openSystemLocationSetting { isServiceOn -> } - } - } - }) + doWithLocationPermission { + requestLastLocation() + } } compositeDisposable += btn_launch_autocomplete_activity.clicks().subscribe { diff --git a/settings.gradle b/settings.gradle index f78b5f2..56d7f3e 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,4 +1,4 @@ -rootProject.name='Place&Location Library' +rootProject.name='Geography Library' include ':app' include ':location' include ':maps'