Search in sources :

Example 96 with Action1

use of rx.functions.Action1 in project LiYuJapanese by 54wall.

the class MainActivity method setViewPager.

@Override
public void setViewPager(final List<BannerItem> data) {
    if (bannerSubscription != null && bannerSubscription.isUnsubscribed()) {
        bannerSubscription.unsubscribe();
    }
    mBannerViewPager.setAdapter(new BannerPagerAdapter(getSupportFragmentManager(), data));
    mCircleIndicator.setViewPager(mBannerViewPager);
    bannerSubscription = Observable.timer(10, 10, TimeUnit.SECONDS).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io()).subscribe(new Action1<Long>() {

        @Override
        public void call(Long aLong) {
            int next = (mBannerViewPager.getCurrentItem() + 1) % data.size();
            mBannerViewPager.setCurrentItem(next);
        }
    });
}
Also used : BannerPagerAdapter(pri.weiqiang.liyujapanese.ui.adapter.BannerPagerAdapter) Action1(rx.functions.Action1)

Example 97 with Action1

use of rx.functions.Action1 in project AndroidStudy by tinggengyan.

the class RetrofitMainActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_retrofit);
    this.retrofitTextView = (TextView) findViewById(R.id.retrofitTextView);
    // 1. 创建Retrofit2的实例,并设置BaseUrl和Gson转换。
    Retrofit retrofit = new Retrofit.Builder().baseUrl(API).addConverterFactory(GsonConverterFactory.create()).client(new OkHttpClient()).build();
    // 2. 创建请求服务,并为网络请求方法设置参数
    GitHubService gitHubService = retrofit.create(GitHubService.class);
    // Call是Retrofit中重要的一个概念,代表被封装成单个请求/响应的交互行为
    final Call<List<Contributor>> call = gitHubService.contributors("square", "retrofit");
    // 3. 请求网络,并获取响应
    Observable<String> observable = Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            try {
                // execute(同步)或者enqueue(异步)方法,发送请求到网络服务器,并返回一个响应(Response)。
                Response<List<Contributor>> response = call.execute();
                subscriber.onNext(response.body().toString());
                // 因为call只能被执行一次,可以clone一个,再重新执行
                Call<List<Contributor>> cloneCall = call.clone();
                cloneCall.enqueue(new Callback<List<Contributor>>() {

                    @Override
                    public void onResponse(Call<List<Contributor>> call, Response<List<Contributor>> response) {
                        Log.e(TAG, "onResponse:" + response.body().toString());
                    }

                    @Override
                    public void onFailure(Call<List<Contributor>> call, Throwable t) {
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    Action1<String> onNext = new Action1<String>() {

        @Override
        public void call(String o) {
            retrofitTextView.setText(o);
        }
    };
    observable.subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread()).subscribe(onNext);
}
Also used : Call(retrofit2.Call) OkHttpClient(okhttp3.OkHttpClient) Action1(rx.functions.Action1) IOException(java.io.IOException) Observable(rx.Observable) Response(retrofit2.Response) Retrofit(retrofit2.Retrofit) Callback(retrofit2.Callback) List(java.util.List)

Example 98 with Action1

use of rx.functions.Action1 in project knotx by Cognifide.

the class KnotxServerRoutingTest method createSimpleGatewayKnot.

private void createSimpleGatewayKnot(final String address, final String transition) {
    Action1<KnotContext> simpleKnot = knotContext -> {
        ClientResponse clientResponse = new ClientResponse();
        clientResponse.setBody(Buffer.buffer());
        clientResponse.setStatusCode(200);
        knotContext.setClientResponse(clientResponse);
        knotContext.setTransition(transition);
    };
    MockKnotProxy.register(vertx.vertx(), address, simpleKnot);
}
Also used : TestContext(io.vertx.ext.unit.TestContext) Async(io.vertx.ext.unit.Async) KnotContext(io.knotx.dataobjects.KnotContext) RunWith(org.junit.runner.RunWith) HttpResponseStatus(io.netty.handler.codec.http.HttpResponseStatus) Test(org.junit.Test) Logback(io.knotx.junit.rule.Logback) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Action1(rx.functions.Action1) ClientResponse(io.knotx.dataobjects.ClientResponse) RuleChain(org.junit.rules.RuleChain) HttpClientRequest(io.vertx.reactivex.core.http.HttpClientRequest) MultiMap(io.vertx.reactivex.core.MultiMap) Vertx(io.vertx.reactivex.core.Vertx) Rule(org.junit.Rule) Buffer(io.vertx.core.buffer.Buffer) HttpMethod(io.vertx.core.http.HttpMethod) Observable(io.reactivex.Observable) HttpClient(io.vertx.reactivex.core.http.HttpClient) KnotxConfiguration(io.knotx.junit.rule.KnotxConfiguration) RunTestOnContext(io.vertx.ext.unit.junit.RunTestOnContext) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) TestVertxDeployer(io.knotx.junit.rule.TestVertxDeployer) ClientResponse(io.knotx.dataobjects.ClientResponse) HttpClientResponse(io.vertx.reactivex.core.http.HttpClientResponse) KnotContext(io.knotx.dataobjects.KnotContext)

Example 99 with Action1

use of rx.functions.Action1 in project divide by HiddenStage.

the class MyActivity method onCreate.

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    DivideDrawer.attach(this, R.layout.main);
    // setContentView(R.layout.main);
    ButterKnife.inject(this);
    getActionBar().setHomeButtonEnabled(true);
    // au = AuthUtils.get(this, AuthManager.ACCOUNT_TYPE); TODO replace this
    app = (MyApplication) this.getApplication();
    BackendServices.addLoginListener(new LoginListener() {

        @Override
        public void onNext(BackendUser user) {
            System.out.println("loginListener: setUser: " + user);
            if (user != null) {
                setUser(user);
            }
        }
    });
    adapter = new BackendObjectAdaper(this, objectList);
    usersLV.setAdapter(adapter);
    usersLV.setOnItemClickListener(new AdapterView.OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            BackendObject o = adapter.getItem(position);
            Query q = new QueryBuilder().delete().from(BackendObject.class).where(TransientObject.OBJECT_KEY, OPERAND.EQ, o.getObjectKey()).build();
            BackendServices.remote().query(BackendObject.class, q).subscribe(new Action1<Collection<BackendObject>>() {

                @Override
                public void call(Collection<BackendObject> backendObjects) {
                    getObjects();
                }
            });
            BackendServices.local().delete(o);
        }
    });
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            if (BackendUser.isLoggedIn())
                getObjects();
            else
                logger.debug("Not signed in, not querying data.");
        }
    }, 1000);
    Storage s = new XmlStorage(new File(this.getFilesDir() + File.separator + "something.xml"), Storage.MODE_WORLD_WRITEABLE);
    String id = "something";
    System.out.println("Stored: " + s.getString(id, ""));
    System.out.println("Stored: " + s.contains(id));
    s.edit().putString(id, "something2").commit();
    s.edit().putInt("int", 55).commit();
}
Also used : BackendObject(io.divide.client.BackendObject) Action1(rx.functions.Action1) Query(io.divide.shared.transitory.query.Query) Handler(android.os.Handler) QueryBuilder(io.divide.shared.transitory.query.QueryBuilder) BackendUser(io.divide.client.BackendUser) InjectView(butterknife.InjectView) View(android.view.View) AdapterView(android.widget.AdapterView) TextView(android.widget.TextView) ListView(android.widget.ListView) XmlStorage(io.divide.shared.file.XmlStorage) LoginListener(io.divide.client.auth.LoginListener) Storage(io.divide.shared.file.Storage) XmlStorage(io.divide.shared.file.XmlStorage) Collection(java.util.Collection) AdapterView(android.widget.AdapterView) File(java.io.File)

Example 100 with Action1

use of rx.functions.Action1 in project AnDevCon-RxPatterns by colintheshots.

the class Example5 method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example5);
    mColdTextView = (TextView) findViewById(R.id.coldTextView);
    mColdTextView2 = (TextView) findViewById(R.id.coldTextView2);
    mHotTextView = (TextView) findViewById(R.id.hotTextView);
    mHotTextView2 = (TextView) findViewById(R.id.hotTextView2);
    mHotTextView.setText("Click Me!");
    // Cold Observable
    // Every time .subscribe() is called, it runs the function again.
    // Using the .cache() operator is one of many options to make
    // this hot.
    final Observable<String> coldObservable = Observable.create(new Observable.OnSubscribe<String>() {

        @Override
        public void call(Subscriber<? super String> subscriber) {
            for (int i = 0; i <= LOOP_MAX; i++) {
                subscriber.onNext(Integer.toString(i));
                try {
                    Thread.sleep(2000);
                } catch (InterruptedException e) {
                    subscriber.onError(e);
                }
            }
        }
    }).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
    coldObservable.subscribe(new Action1<String>() {

        @Override
        public void call(String s) {
            mColdTextView.setText("Cold: " + s);
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    // pause 5 seconds, then start second subscription from cold
    // observable to show that the function executes again from scratch.
    // CAUTION: Retrofit uses COLD observables by default, which means
    // two separate subscriptions to the same observable will make two
    // network calls. Turn observables HOT if you want the same values
    // for all subscribers. Know which operators convert temperature.
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            coldObservable.subscribe(new Action1<String>() {

                @Override
                public void call(String s) {
                    mColdTextView2.setText("Cold2: " + s);
                }
            }, new Action1<Throwable>() {

                @Override
                public void call(Throwable throwable) {
                    throwable.printStackTrace();
                }
            });
        }
    }, 4000);
    // Hot Observable
    // Hot observables emit events oblivious of subscribers so
    // that every subscriber receives the same events.
    Observable<Void> hotObservable = RxView.clicks(mHotTextView);
    hotObservable.subscribe(new Action1<Void>() {

        @Override
        public void call(Void aVoid) {
            mNumClicks++;
            mHotTextView.setText("Hot: " + Integer.toString(mNumClicks) + " (click again)");
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
    hotObservable.subscribe(new Action1<Void>() {

        @Override
        public void call(Void aVoid) {
            mHotTextView2.setText("Hot2: " + Integer.toString(mNumClicks));
        }
    }, new Action1<Throwable>() {

        @Override
        public void call(Throwable throwable) {
            throwable.printStackTrace();
        }
    });
}
Also used : Action1(rx.functions.Action1) Handler(android.os.Handler) Observable(rx.Observable)

Aggregations

Action1 (rx.functions.Action1)106 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)18 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 OnSubscribe (rx.Observable.OnSubscribe)7 RunTestInLooperThread (io.realm.rule.RunTestInLooperThread)6 IOException (java.io.IOException)6 PluginTestVerifier (com.navercorp.pinpoint.bootstrap.plugin.test.PluginTestVerifier)5 Method (java.lang.reflect.Method)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5