Skip to content

Commit

Permalink
Migrate from RxJava 2 to RxJava 3
Browse files Browse the repository at this point in the history
  • Loading branch information
li2 committed May 4, 2021
1 parent 0559c0f commit 2d56346
Show file tree
Hide file tree
Showing 16 changed files with 62 additions and 41 deletions.
6 changes: 6 additions & 0 deletions .idea/compiler.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .idea/runConfigurations.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,9 @@ dependencies {
implementation "com.google.android.material:material:$rootProject.vGoogleMaterial"

//Rx
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.vRxAndroid"
implementation "io.reactivex.rxjava2:rxkotlin:$rootProject.vRxKotlin"
implementation "com.jakewharton.rxbinding3:rxbinding:$rootProject.vRxBinding"
implementation "io.reactivex.rxjava3:rxandroid:$rootProject.vRxAndroid"
implementation "io.reactivex.rxjava3:rxkotlin:$rootProject.vRxKotlin"
implementation "com.jakewharton.rxbinding4:rxbinding:$rootProject.vRxBinding"

//Timber
implementation "com.jakewharton.timber:timber:$vTimber"
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/me/li2/android/placesample/FrameworkExt.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ import androidx.databinding.BindingAdapter
import androidx.fragment.app.Fragment
import com.bumptech.glide.Glide
import com.google.android.material.dialog.MaterialAlertDialogBuilder
import com.jakewharton.rxbinding3.widget.textChanges
import io.reactivex.Observable
import io.reactivex.android.schedulers.AndroidSchedulers
import io.reactivex.schedulers.Schedulers
import com.jakewharton.rxbinding4.widget.textChanges
import io.reactivex.rxjava3.android.schedulers.AndroidSchedulers
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.schedulers.Schedulers
import me.li2.android.common.framework.openAppSettings
import me.li2.android.location.RequestLocationResult
import me.li2.android.location.ifLocationAllowed
Expand Down
8 changes: 4 additions & 4 deletions app/src/main/java/me/li2/android/placesample/MainFragment.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@ import android.view.ViewGroup
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import com.google.android.gms.maps.model.LatLng
import com.jakewharton.rxbinding3.view.clicks
import io.reactivex.disposables.CompositeDisposable
import io.reactivex.rxkotlin.plusAssign
import io.reactivex.rxkotlin.subscribeBy
import com.jakewharton.rxbinding4.view.clicks
import io.reactivex.rxjava3.disposables.CompositeDisposable
import io.reactivex.rxjava3.kotlin.plusAssign
import io.reactivex.rxjava3.kotlin.subscribeBy
import kotlinx.android.synthetic.main.fragment_main.*
import me.li2.android.location.LastKnownLocationUtils.requestLastKnownLocation
import me.li2.android.maps.MapType
Expand Down
10 changes: 5 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ buildscript {
// Specify global library versions.
apply from: 'library_versions.gradle'
ext.vKotlin = '1.3.61'
ext.vGradle = '3.5.3'
ext.vGradle = '4.1.2'
repositories {
google()
jcenter()
Expand Down Expand Up @@ -33,7 +33,7 @@ allprojects {
vTargetSdk = 29

// My Libraries Dependencies
vLi2Common = '0.1.6'
vLi2Common = '0.2.1'

// Android X Dependencies
vAndroidxAppcompat = '1.1.0'
Expand All @@ -46,9 +46,9 @@ allprojects {

// Rx Dependencies
vRxActivityResult = '2.2.1'
vRxAndroid = '2.1.1'
vRxBinding = '3.0.0-alpha2'
vRxKotlin = '2.3.0'
vRxAndroid = '3.0.0'
vRxBinding = '4.0.0'
vRxKotlin = '3.0.1'

// Misc
vTimber = "4.7.1"
Expand Down
2 changes: 1 addition & 1 deletion gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@ distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-5.4.1-all.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-6.5-all.zip
7 changes: 4 additions & 3 deletions location/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion vMinSdk
targetSdkVersion vTargetSdk
versionCode 1
versionName "0.1.1"
versionName "1.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -42,9 +42,10 @@ dependencies {
implementation "com.google.android.gms:play-services-location:17.0.0"

//Rx
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.vRxAndroid"
implementation "io.reactivex.rxjava2:rxkotlin:$rootProject.vRxKotlin"
implementation "io.reactivex.rxjava3:rxandroid:$rootProject.vRxAndroid"
implementation "io.reactivex.rxjava3:rxkotlin:$rootProject.vRxKotlin"
implementation "com.petarmarijanovic:rx-activity-result:$rootProject.vRxActivityResult"
implementation "com.github.akarnokd:rxjava3-bridge:3.0.0"

//Timber
implementation "com.jakewharton.timber:timber:$vTimber"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package me.li2.android.location

import android.annotation.SuppressLint
import android.content.Context
import android.location.Location
import android.os.Looper
Expand All @@ -8,8 +9,8 @@ import androidx.lifecycle.LifecycleObserver
import androidx.lifecycle.OnLifecycleEvent
import com.google.android.gms.location.*
import com.google.android.gms.location.LocationRequest.PRIORITY_HIGH_ACCURACY
import io.reactivex.Observable
import io.reactivex.ObservableEmitter
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.ObservableEmitter

/**
* https://developer.android.com/training/location/retrieve-current
Expand All @@ -20,6 +21,7 @@ internal class FusedLocationProvider(context: Context) : LifecycleObserver {
private var isRequesting = false
private lateinit var locationCallback: LocationCallback

@SuppressLint("MissingPermission")
fun requestLastLocation(): Observable<Location> {
return Observable.create { emitter ->
client.lastLocation.addOnCompleteListener { task ->
Expand All @@ -33,6 +35,7 @@ internal class FusedLocationProvider(context: Context) : LifecycleObserver {
}
}

@SuppressLint("MissingPermission")
private fun startLocationUpdates(emitter: ObservableEmitter<Location>) {
if (isRequesting) return
locationCallback = object : LocationCallback() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import android.location.Location
import android.location.LocationManager
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.subscribeBy
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.kotlin.subscribeBy
import me.li2.android.common.rx.isLocationPermissionGranted
import me.li2.android.location.LocationServiceUtil.isLocationServiceEnabled

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ import com.google.android.gms.location.LocationRequest
import com.google.android.gms.location.LocationServices
import com.google.android.gms.location.LocationSettingsRequest
import com.petarmarijanovic.rxactivityresult.RxActivityResult
import io.reactivex.Observable
import io.reactivex.rxkotlin.subscribeBy
import hu.akarnokd.rxjava3.bridge.RxJavaBridge
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.kotlin.subscribeBy


internal object LocationServiceUtil {

Expand All @@ -36,11 +38,13 @@ internal object LocationServiceUtil {
if (exception is ResolvableApiException) {
try {
// Show the dialog by calling startResolutionForResult() in Rx
RxActivityResult(activity).start(exception.resolution).subscribeBy(onSuccess = {
// resultCode is RESULT_CANCELED when user click Ok, this issue happens on Pixel2 Android 10, it works on Nexus5 Android 6
// https://issuetracker.google.com/issues/118347902
emitter.onNext(isLocationServiceEnabled(activity))
emitter.onComplete()
RxActivityResult(activity).start(exception.resolution)
.`as`(RxJavaBridge.toV3Single())
.subscribeBy(onSuccess = {
// resultCode is RESULT_CANCELED when user click Ok, this issue happens on Pixel2 Android 10, it works on Nexus5 Android 6
// https://issuetracker.google.com/issues/118347902
emitter.onNext(isLocationServiceEnabled(activity))
emitter.onComplete()
}, onError = {
emitter.onError(it)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,14 @@ import android.provider.Settings
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.FragmentActivity
import com.petarmarijanovic.rxactivityresult.RxActivityResult
import io.reactivex.Observable
import io.reactivex.disposables.Disposable
import io.reactivex.rxkotlin.subscribeBy
import hu.akarnokd.rxjava3.bridge.RxJavaBridge
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.disposables.Disposable
import io.reactivex.rxjava3.kotlin.subscribeBy
import me.li2.android.common.rx.PermissionResult
import me.li2.android.common.rx.checkAndRequestLocationPermission


/**
* Show location permission ((allow, deny, deny & don't ask again)) and service prompt dialog
* to ask user to grant location permission and to turn on location service.
Expand Down Expand Up @@ -42,6 +44,7 @@ fun FragmentActivity.ifLocationAllowed(
fun FragmentActivity.openSystemLocationSetting(onResult: (Boolean) -> Unit): Disposable {
return RxActivityResult(this)
.start(Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS))
.`as`(RxJavaBridge.toV3Single())
.subscribeBy {
onResult(LocationServiceUtil.isLocationServiceEnabled(this))
}
Expand Down
2 changes: 1 addition & 1 deletion maps/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion vMinSdk
targetSdkVersion vTargetSdk
versionCode 1
versionName "0.1.0"
versionName "1.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down
5 changes: 3 additions & 2 deletions place/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ android {
minSdkVersion vMinSdk
targetSdkVersion vTargetSdk
versionCode 1
versionName "0.3.0"
versionName "1.0.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'
Expand Down Expand Up @@ -38,8 +38,9 @@ dependencies {
api "com.google.android.libraries.places:places:$rootProject.vGooglePlaces"

//Rx
implementation "io.reactivex.rxjava2:rxandroid:$rootProject.vRxAndroid"
implementation "io.reactivex.rxjava3:rxandroid:$rootProject.vRxAndroid"
implementation "com.petarmarijanovic:rx-activity-result:$rootProject.vRxActivityResult"
implementation "com.github.akarnokd:rxjava3-bridge:3.0.0"

//Timber
implementation "com.jakewharton.timber:timber:$vTimber"
Expand Down
6 changes: 4 additions & 2 deletions place/src/main/java/me/li2/android/place/PlaceAutoComplete.kt
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,9 @@ import com.google.android.libraries.places.widget.Autocomplete
import com.google.android.libraries.places.widget.AutocompleteActivity
import com.google.android.libraries.places.widget.model.AutocompleteActivityMode
import com.petarmarijanovic.rxactivityresult.RxActivityResult
import io.reactivex.Observable
import io.reactivex.Single
import hu.akarnokd.rxjava3.bridge.RxJavaBridge
import io.reactivex.rxjava3.core.Observable
import io.reactivex.rxjava3.core.Single

/**
* Search places with Google Places SDK in the following aspects:
Expand Down Expand Up @@ -74,6 +75,7 @@ class PlaceAutoComplete(private val context: Context, apiKey: String) {
.build(activity)
return RxActivityResult(activity)
.start(intent)
.`as`(RxJavaBridge.toV3Single())
.map { result ->
when (result.resultCode) {
Activity.RESULT_OK -> Autocomplete.getPlaceFromIntent(result.data)
Expand Down

0 comments on commit 2d56346

Please sign in to comment.