use of rx.functions.Action1 in project AndroidStudy by tinggengyan.
the class RXJavaActivity method simpleAction.
// 简单的使用action这个接口
private void simpleAction() {
// 简单的使用Action1这个接口,来变现单个参数的观察者参数
// 所有只含有一个参数的回调都可以用这个简单的接口
Action1 onNextAction = new Action1() {
@Override
public void call(Object o) {
System.out.println("next:" + o.toString());
}
};
Action1 onErrorAction = new Action1() {
@Override
public void call(Object o) {
System.out.println("error:" + o.toString());
}
};
// 对于无参的回调,则可以用Action0这个接口简单的实现
Action0 onCompletedAction = new Action0() {
@Override
public void call() {
System.out.println("Complete");
}
};
String[] words = { "Hello", "Hi", "Aloha" };
Observable observable = Observable.from(words);
observable.subscribe(onNextAction);
observable.subscribe(onNextAction, onErrorAction);
observable.subscribe(onNextAction, onErrorAction, onCompletedAction);
}
use of rx.functions.Action1 in project AndroidStudy by tinggengyan.
the class RXJavaActivity method simpleFrom.
// 快捷创建事件队列: from:将传入的数组或 Iterable 拆分成具体对象后,依次发送出来。
private void simpleFrom() {
Action1 action1 = new Action1() {
@Override
public void call(Object o) {
textViewMain.setText(o.toString());
}
};
String[] words = { "Hello", "Hi", "Aloha" };
Observable observable = Observable.from(words);
observable.subscribe(action1);
}
use of rx.functions.Action1 in project CryptoBuddy by Patchett.
the class NewsListActivity method getNewsObservable.
public void getNewsObservable(int whatToDo, boolean cache) {
// Example of framework isolation by using observables
// An standard Rx Action.
Action1<News[]> subscriber = new Action1<News[]>() {
@Override
public void call(News[] newsRestResults) {
List<NewsItem> myNews = new ArrayList<>();
if (newsRestResults != null && newsRestResults.length > 0) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
for (News news : newsRestResults) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
Log.e("News", "call successful");
} else {
swipeRefreshLayout.setRefreshing(false);
Log.e("News", "call failed");
}
}
};
switch(whatToDo) {
case 1:
// Wrapped observable call
NewsService.getObservableNews(this, true, new Action1<RestResults<News[]>>() {
@Override
public void call(RestResults<News[]> newsRestResults) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
if (newsRestResults.isSuccessful()) {
List<NewsItem> myNews = new ArrayList<>();
for (News news : newsRestResults.getResultEntity()) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
} else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
break;
case 2:
// Observable instance from EasyRest
if (newsObservable == null) {
newsObservable = NewsService.getPlainObservableNews(this, cache).subscribeOn(Schedulers.io()).observeOn(AndroidSchedulers.mainThread());
}
newsObservable.subscribe(subscriber);
break;
default:
// Wrapped observable call
NewsService.getObservableNews(this, true, new Action1<RestResults<News[]>>() {
@Override
public void call(RestResults<News[]> newsRestResults) {
Parcelable recyclerViewState;
recyclerViewState = recyclerView.getLayoutManager().onSaveInstanceState();
if (newsRestResults.isSuccessful()) {
List<NewsItem> myNews = new ArrayList<>();
for (News news : newsRestResults.getResultEntity()) {
NewsItem newsItem = new NewsItem(news.getTitle(), news.getUrl(), news.getBody(), news.getImageurl(), news.getSource(), news.getPublishedOn());
if (!myNews.contains(newsItem))
myNews.add(newsItem);
}
adapter.setData(myNews);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
recyclerView.getLayoutManager().onRestoreInstanceState(recyclerViewState);
} else {
swipeRefreshLayout.setRefreshing(false);
}
}
});
}
}
use of rx.functions.Action1 in project rxjava-file by davidmoten.
the class OperatorFileTailer method reportNewLines.
private static Func1<Object, Observable<byte[]>> reportNewLines(final File file, final AtomicLong currentPosition, final int maxBytesPerEmission) {
return new Func1<Object, Observable<byte[]>>() {
@Override
public Observable<byte[]> call(Object event) {
// reset current position if file is moved or deleted
if (event instanceof WatchEvent) {
WatchEvent<?> w = (WatchEvent<?>) event;
String kind = w.kind().name();
if (kind.equals(StandardWatchEventKinds.ENTRY_CREATE.name())) {
currentPosition.set(0);
}
}
long length = file.length();
if (length > currentPosition.get()) {
try {
final FileInputStream fis = new FileInputStream(file);
fis.skip(currentPosition.get());
// termination or unsubscription
return Observable.using(new Func0<InputStream>() {
@Override
public InputStream call() {
return fis;
}
}, new Func1<InputStream, Observable<byte[]>>() {
@Override
public Observable<byte[]> call(InputStream t1) {
return Bytes.from(fis, maxBytesPerEmission).doOnNext(new Action1<byte[]>() {
@Override
public void call(byte[] bytes) {
currentPosition.addAndGet(bytes.length);
}
});
}
}, new Action1<InputStream>() {
@Override
public void call(InputStream is) {
try {
is.close();
} catch (IOException e) {
// don't care
}
}
});
} catch (IOException e) {
return Observable.error(e);
}
} else
return Observable.empty();
}
};
}
use of rx.functions.Action1 in project photon-model by vmware.
the class AzureComputeEnumerationAdapterService method vmEnumerationCompletion.
/**
* Completion handler for VM enumeration call. For async calls to list VMs, Azure returns
* Observable<Page<VirtualMachineInner>>. The following completion subscribes to the Observable
* and Overrides call(<T>) to include logic to process pages of VMs when we receive them.
*/
private Action1<Page<VirtualMachineInner>> vmEnumerationCompletion(EnumerationContext ctx, ComputeEnumerationSubStages next) {
Action1<Page<VirtualMachineInner>> vmEnumCompletion = new Action1<Page<VirtualMachineInner>>() {
@Override
public void call(Page<VirtualMachineInner> virtualMachineInnerPage) {
List<VirtualMachineInner> virtualMachineInners = virtualMachineInnerPage.items();
ctx.enumNextPageLink = virtualMachineInnerPage.nextPageLink();
if (virtualMachineInners == null || virtualMachineInners.size() == 0) {
ctx.subStage = ComputeEnumerationSubStages.GET_COMPUTE_STATES;
handleSubStage(ctx);
return;
}
logInfo(() -> String.format("Retrieved %d VMs from Azure", virtualMachineInners.size()));
logFine(() -> String.format("Next page link %s", ctx.enumNextPageLink));
for (VirtualMachineInner virtualMachine : virtualMachineInners) {
// We don't want to process VMs that are being terminated.
if (AZURE_VM_TERMINATION_STATES.contains(virtualMachine.provisioningState())) {
logFine(() -> String.format("Not processing %s", virtualMachine.id()));
continue;
}
// Azure for some case changes the case of the vm id.
String vmId = virtualMachine.id().toLowerCase();
ctx.virtualMachines.put(vmId, virtualMachine);
ctx.vmIds.add(vmId);
}
logFine(() -> String.format("Processing %d VMs", ctx.vmIds.size()));
ctx.subStage = next;
handleSubStage(ctx);
}
};
return injectOperationContext(vmEnumCompletion);
}
Aggregations