0
votes

I'm still learning RxJava, I know the basic what is Observable & Observers, But still confused how, where & when to use filter, map, flatmap, etc

Do you have any good suggest how to convert this code to RxJava ?

void locationChanged(LocationResult locationResult) {
        for (Location location : locationResult.getLocations()) {
        Double lat = location.getLatitude();
        Double lng = location.getLongitude();

        boolean isMock;
        if (Build.VERSION.SDK_INT >= 18) {
            isMock = location.isFromMockProvider();
        } else {
            isMock = !Settings.Secure.getString(getContentResolver(), Settings.Secure.ALLOW_MOCK_LOCATION).equals("0");
        }

        for (int k = 0; k < offices.size(); k++) {
            Office office = offices.get(k);

            Double d_lat = Double.valueOf(office.getOc_lat());
            Double d_long = Double.valueOf(office.getOc_long());
            Double d_radius = Double.valueOf(office.getOc_radius());

            Location.distanceBetween(lat, lng, d_lat, d_long, resultApi);
            String s_distanceToOffice = String.valueOf(resultApi[0]);
            Double d_distanceToOffice = Double.parseDouble(s_distanceToOffice);
            //Log.e(TAG, "locationChanged: Distance -> " + d_distanceToOffice);

            // check-in in radius
            if (d_distanceToOffice < d_radius) {
                buttonOutOfRadius.setVisibility(View.GONE);
                progressBarPosition.setVisibility(View.GONE);
                buttonRecord.setVisibility(View.VISIBLE);

                // checking fake gps
                if (isMock) {
                    processTheMock();
                } else {
                    in_area = "Y";

                    site_name = office.getOc_site();
                    site_id = office.getOc_id();
                    site_lat = office.getOc_lat();
                    site_long = office.getOc_long();

                    processNoMock(lat, lng, site_name);
                }
                stopLocationUpdates();
                break;
            }

            else if (d_distanceToOffice > d_radius) {
                progressBarPosition.setVisibility(View.GONE);
                buttonOutOfRadius.setVisibility(View.VISIBLE);
                buttonRecord.setVisibility(View.GONE);
            }
        }

    }
    stopLocationUpdates();
}

Which code needs to be changed and unnecessary to change ? What I've done like this :

Observable.fromIterable(offices)
                    .subscribeOn(Schedulers.io())
                    .observeOn(AndroidSchedulers.mainThread())
                    .subscribe()

do i need to just copy paste the code inside looping "for(..) into "oNext(Office office)" ?

It's nice to know from your experience.

1

1 Answers

0
votes

After few days i got the the good flow to refactor it into Rx,

Here my code in kotlin, easy to read, if you have any better alternative for this, please share to me

private fun locationChanged(locationResult: LocationResult) {
    for (location in locationResult.locations) {

        val lat = location.latitude
        val lng = location.longitude
        var dDistanceToOffice: Double
        var dRadius: Double

        Observable.fromIterable(offices)
                .subscribeOn(Schedulers.newThread())
                .filter {
                    val dLat = it.oc_lat!!.toDouble()
                    val dLong = it.oc_long!!.toDouble()
                    dRadius = it.oc_radius!!.toDouble()

                    // Check the distance "MyLocation" to "OfficeLocation"
                    Location.distanceBetween(lat, lng, dLat, dLong, resultApi)
                    val sDistanceToOffice = resultApi[0].toString()
                    dDistanceToOffice = sDistanceToOffice.toDouble()

                    dDistanceToOffice < dRadius
                }
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(object : Observer<Office> {
                    override fun onSubscribe(d: Disposable) {
                        disposable = d
                    }

                    override fun onNext(office: Office) {
                        inArea = "Y"
                        siteName = office.oc_site
                        siteId = office.oc_id
                        siteLat = office.oc_lat!!
                        siteLong = office.oc_long!!
                    }

                    override fun onError(e: Throwable) {
                        Log.e(TAG, "locationChanged() Error: ${e.message}")
                    }

                    override fun onComplete() {
                        // checking the data in Area = N or Y
                        if (isMock(location)) {
                            processTheMock()
                        } else {
                            if (inArea == "Y") {
                                hideButtonOutRadius()
                                hideProgressbarPosition()
                                showButtonCheckin()
                                processNoMock(lat, lng, "In Radius", siteName!!)
                            } else {
                                retrofitGoogleGeocoding(lat, lng)
                                inArea = "N"
                                siteName = newAddress
                                siteLat = lat.toString()
                                siteLong = lng.toString()
                                hideButtonOutRadius()
                                hideProgressbarPosition()
                                showButtonCheckin()
                                processNoMock(lat, lng, "In Radius", siteName!!)
                            }
                        }
                        stopLocationUpdates()
                    }
                })
    } // end of location
}