Search in sources :

Example 1 with Action1

use of rx.functions.Action1 in project Android-ReactiveLocation by mcharmas.

the class PlacesActivity method onLocationPermissionGranted.

@Override
protected void onLocationPermissionGranted() {
    compositeSubscription = new CompositeSubscription();
    compositeSubscription.add(reactiveLocationProvider.getCurrentPlace(null).subscribe(new Action1<PlaceLikelihoodBuffer>() {

        @Override
        public void call(PlaceLikelihoodBuffer buffer) {
            PlaceLikelihood likelihood = buffer.get(0);
            if (likelihood != null) {
                currentPlaceView.setText(likelihood.getPlace().getName());
            }
            buffer.release();
        }
    }));
    Observable<String> queryObservable = RxTextView.textChanges(queryView).map(new Func1<CharSequence, String>() {

        @Override
        public String call(CharSequence charSequence) {
            return charSequence.toString();
        }
    }).debounce(1, TimeUnit.SECONDS).filter(new Func1<String, Boolean>() {

        @Override
        public Boolean call(String s) {
            return !TextUtils.isEmpty(s);
        }
    });
    Observable<Location> lastKnownLocationObservable = reactiveLocationProvider.getLastKnownLocation();
    Observable<AutocompletePredictionBuffer> suggestionsObservable = Observable.combineLatest(queryObservable, lastKnownLocationObservable, new Func2<String, Location, QueryWithCurrentLocation>() {

        @Override
        public QueryWithCurrentLocation call(String query, Location currentLocation) {
            return new QueryWithCurrentLocation(query, currentLocation);
        }
    }).flatMap(new Func1<QueryWithCurrentLocation, Observable<AutocompletePredictionBuffer>>() {

        @Override
        public Observable<AutocompletePredictionBuffer> call(QueryWithCurrentLocation q) {
            if (q.location == null)
                return Observable.empty();
            double latitude = q.location.getLatitude();
            double longitude = q.location.getLongitude();
            LatLngBounds bounds = new LatLngBounds(new LatLng(latitude - 0.05, longitude - 0.05), new LatLng(latitude + 0.05, longitude + 0.05));
            return reactiveLocationProvider.getPlaceAutocompletePredictions(q.query, bounds, null);
        }
    });
    compositeSubscription.add(suggestionsObservable.subscribe(new Action1<AutocompletePredictionBuffer>() {

        @Override
        public void call(AutocompletePredictionBuffer buffer) {
            List<AutocompleteInfo> infos = new ArrayList<>();
            for (AutocompletePrediction prediction : buffer) {
                infos.add(new AutocompleteInfo(prediction.getFullText(null).toString(), prediction.getPlaceId()));
            }
            buffer.release();
            placeSuggestionsList.setAdapter(new ArrayAdapter<>(PlacesActivity.this, android.R.layout.simple_list_item_1, infos));
        }
    }));
}
Also used : PlaceLikelihoodBuffer(com.google.android.gms.location.places.PlaceLikelihoodBuffer) AutocompletePredictionBuffer(com.google.android.gms.location.places.AutocompletePredictionBuffer) ArrayList(java.util.ArrayList) LatLng(com.google.android.gms.maps.model.LatLng) Func2(rx.functions.Func2) Action1(rx.functions.Action1) LatLngBounds(com.google.android.gms.maps.model.LatLngBounds) Observable(rx.Observable) CompositeSubscription(rx.subscriptions.CompositeSubscription) AutocompletePrediction(com.google.android.gms.location.places.AutocompletePrediction) PlaceLikelihood(com.google.android.gms.location.places.PlaceLikelihood) Location(android.location.Location)

Example 2 with Action1

use of rx.functions.Action1 in project Android-ReactiveLocation by mcharmas.

the class PlacesResultActivity method onLocationPermissionGranted.

@Override
protected void onLocationPermissionGranted() {
    compositeSubscription = new CompositeSubscription();
    compositeSubscription.add(reactiveLocationProvider.getPlaceById(placeId).subscribe(new Action1<PlaceBuffer>() {

        @Override
        public void call(PlaceBuffer buffer) {
            Place place = buffer.get(0);
            if (place != null) {
                placeNameView.setText(place.getName());
                placeLocationView.setText(place.getLatLng().latitude + ", " + place.getLatLng().longitude);
                placeAddressView.setText(place.getAddress());
            }
            buffer.release();
        }
    }));
}
Also used : Action1(rx.functions.Action1) CompositeSubscription(rx.subscriptions.CompositeSubscription) PlaceBuffer(com.google.android.gms.location.places.PlaceBuffer) Place(com.google.android.gms.location.places.Place)

Example 3 with Action1

use of rx.functions.Action1 in project archi by ivacf.

the class RepositoryViewModel method loadFullUser.

private void loadFullUser(String url) {
    ArchiApplication application = ArchiApplication.get(context);
    GithubService githubService = application.getGithubService();
    subscription = githubService.userFromUrl(url).observeOn(AndroidSchedulers.mainThread()).subscribeOn(application.defaultSubscribeScheduler()).subscribe(new Action1<User>() {

        @Override
        public void call(User user) {
            Log.i(TAG, "Full user data loaded " + user);
            ownerName.set(user.name);
            ownerEmail.set(user.email);
            ownerLocation.set(user.location);
            ownerEmailVisibility.set(user.hasEmail() ? View.VISIBLE : View.GONE);
            ownerLocationVisibility.set(user.hasLocation() ? View.VISIBLE : View.GONE);
            ownerLayoutVisibility.set(View.VISIBLE);
        }
    });
}
Also used : GithubService(uk.ivanc.archimvvm.model.GithubService) Action1(rx.functions.Action1) User(uk.ivanc.archimvvm.model.User) ArchiApplication(uk.ivanc.archimvvm.ArchiApplication)

Example 4 with Action1

use of rx.functions.Action1 in project Hystrix by Netflix.

the class HystrixThreadEventStreamTest method testThreadIsolatedResponseFromCache.

@Test
public void testThreadIsolatedResponseFromCache() throws Exception {
    CountDownLatch commandLatch = new CountDownLatch(1);
    CountDownLatch threadPoolLatch = new CountDownLatch(1);
    Subscriber<List<HystrixCommandCompletion>> commandListSubscriber = getLatchedSubscriber(commandLatch);
    readCommandStream.observe().buffer(500, TimeUnit.MILLISECONDS).take(1).doOnNext(new Action1<List<HystrixCommandCompletion>>() {

        @Override
        public void call(List<HystrixCommandCompletion> hystrixCommandCompletions) {
            System.out.println("LIST : " + hystrixCommandCompletions);
            assertEquals(3, hystrixCommandCompletions.size());
        }
    }).subscribe(commandListSubscriber);
    Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch);
    readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber);
    ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS).setExecutedInThread();
    ExecutionResult cache1 = ExecutionResult.from(HystrixEventType.RESPONSE_FROM_CACHE);
    ExecutionResult cache2 = ExecutionResult.from(HystrixEventType.RESPONSE_FROM_CACHE);
    writeToStream.executionDone(result, commandKey, threadPoolKey);
    writeToStream.executionDone(cache1, commandKey, threadPoolKey);
    writeToStream.executionDone(cache2, commandKey, threadPoolKey);
    assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS));
    assertTrue(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS));
}
Also used : Action1(rx.functions.Action1) List(java.util.List) ExecutionResult(com.netflix.hystrix.ExecutionResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Example 5 with Action1

use of rx.functions.Action1 in project Hystrix by Netflix.

the class HystrixThreadEventStreamTest method testSemaphoreIsolatedResponseFromCache.

@Test
public void testSemaphoreIsolatedResponseFromCache() throws Exception {
    CountDownLatch commandLatch = new CountDownLatch(1);
    CountDownLatch threadPoolLatch = new CountDownLatch(1);
    Subscriber<List<HystrixCommandCompletion>> commandListSubscriber = getLatchedSubscriber(commandLatch);
    readCommandStream.observe().buffer(500, TimeUnit.MILLISECONDS).take(1).doOnNext(new Action1<List<HystrixCommandCompletion>>() {

        @Override
        public void call(List<HystrixCommandCompletion> hystrixCommandCompletions) {
            System.out.println("LIST : " + hystrixCommandCompletions);
            assertEquals(3, hystrixCommandCompletions.size());
        }
    }).subscribe(commandListSubscriber);
    Subscriber<HystrixCommandCompletion> threadPoolSubscriber = getLatchedSubscriber(threadPoolLatch);
    readThreadPoolStream.observe().take(1).subscribe(threadPoolSubscriber);
    ExecutionResult result = ExecutionResult.from(HystrixEventType.SUCCESS);
    ExecutionResult cache1 = ExecutionResult.from(HystrixEventType.RESPONSE_FROM_CACHE);
    ExecutionResult cache2 = ExecutionResult.from(HystrixEventType.RESPONSE_FROM_CACHE);
    writeToStream.executionDone(result, commandKey, threadPoolKey);
    writeToStream.executionDone(cache1, commandKey, threadPoolKey);
    writeToStream.executionDone(cache2, commandKey, threadPoolKey);
    assertTrue(commandLatch.await(1000, TimeUnit.MILLISECONDS));
    assertFalse(threadPoolLatch.await(1000, TimeUnit.MILLISECONDS));
}
Also used : Action1(rx.functions.Action1) List(java.util.List) ExecutionResult(com.netflix.hystrix.ExecutionResult) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Action1 (rx.functions.Action1)108 Test (org.junit.Test)33 Action0 (rx.functions.Action0)28 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)24 UiThreadTest (android.support.test.annotation.UiThreadTest)20 Observable (rx.Observable)20 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)12 HystrixRuntimeException (com.netflix.hystrix.exception.HystrixRuntimeException)11 ArrayList (java.util.ArrayList)11 CountDownLatch (java.util.concurrent.CountDownLatch)11 TestSubscriber (rx.observers.TestSubscriber)10 AllTypes (io.realm.entities.AllTypes)9 List (java.util.List)9 TestCircuitBreaker (com.netflix.hystrix.HystrixCircuitBreakerTest.TestCircuitBreaker)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 Func1 (rx.functions.Func1)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5