尝试使用twilio SDK从Twilio聊天中获取所有频道。想要等待频道列表加载(使用Observables),然后在我的UI中显示它。下面是我要做什么的一个粗略想法:
private List<Paginator<ChannelDescriptor> getAllChannels() {
ChatClient.Properties props = new ChatClient.Properties.Builder()
.createProperties();
ChatClient chatClient = ChatClient.create(context.getApplicationContext(),
accessToken,
props,
null);
List<Paginator<ChannelDescriptor> channelList = new ArrayList<>()
chatClient.getChannels().getUserChannelsList(new CallbackListener<Paginator<ChannelDescriptor>>() {
@Override
public void onSuccess(Paginator<ChannelDescriptor> firstPaginator) {
channelList.add(firstPaginator);
Paginator<ChannelDescriptor> nextPaginator = firstPaginator;
while (nextPaginator != null && nextPaginator.hasNextPage()) {
nextPaginator = loadNextChatChannelPage(firstPaginator);
if(nextPaginator != null) {
channelList.add(nextPaginator);
}
}
}
});
return channelList;
}
public Paginator<ChannelDescriptor> loadNextChatChannelPage(Paginator<ChannelDescriptor> paginator) {
paginator.requestNextPage(new CallbackListener<Paginator<ChannelDescriptor>>() {
@Override
public void onSuccess(Paginator<ChannelDescriptor> channelDescriptorPaginator) {
return channelDescriptorPaginator;
}
@Override
public void onError(ErrorInfo errorInfo) {
return null.
}
}));
}
答案 0 :(得分:0)
我最终要做的是这个
/**
* Start loading the Twilio chat channel pages
*
* @return Single containing a list of all the chat channel pages
*/
public Single<List<Paginator<ChannelDescriptor>>> loadAllChatChannelPages(ChatClientManager chatClientManager) {
return Single.create(emitter -> chatClientManager.getChatClient().getChannels().getUserChannelsList(new CallbackListener<Paginator<ChannelDescriptor>>() {
@Override
public void onSuccess(Paginator<ChannelDescriptor> channelDescriptorPaginator) {
if(channelDescriptorPaginator != null) {
emitter.onSuccess(channelDescriptorPaginator);
}
}
@Override
public void onError(ErrorInfo errorInfo) {
String errorMessage = "";
if(errorInfo != null) {
errorMessage = errorInfo.getMessage();
}
emitter.onError(new Throwable(errorMessage));
}
})).subscribeOn(Schedulers.io())
.flatMap(firstPaginator -> {
if(firstPaginator != null) {
return loadChannelPaginator((Paginator<ChannelDescriptor>) firstPaginator).toList()
.subscribeOn(Schedulers.io());
} else {
return Single.error(new Throwable("Could not get chat channels"));
}
});
}
/**
* Recursively loads the chat channel pages and returns them as a single observable
*
* @param paginator this needs to be the first chat channel paginator from the chat client
* @return Observable containing a flattened version of all the available chat channel paginators
*/
private Observable<Paginator<ChannelDescriptor>> loadChannelPaginator(Paginator<ChannelDescriptor> paginator) {
if (paginator.hasNextPage()) {
return Observable.mergeDelayError(
Observable.just(paginator),
loadNextChatChannelPage(paginator)
.flatMap(this::loadChannelPaginator));
}
return Observable.just(paginator);
}
/**
* Loads a single chat channel page
*
* @param previousPage the previous page of chat channels
* @return Observable containing the next chat channel page
*/
private Observable<Paginator<ChannelDescriptor>> loadNextChatChannelPage(Paginator<ChannelDescriptor> previousPage) {
return Observable.create(emitter -> previousPage.requestNextPage(new CallbackListener<Paginator<ChannelDescriptor>>() {
@Override
public void onSuccess(Paginator<ChannelDescriptor> channelDescriptorPaginator) {
if(channelDescriptorPaginator != null) {
emitter.onNext(channelDescriptorPaginator);
}
emitter.onComplete();
}
@Override
public void onError(ErrorInfo errorInfo) {
if(errorInfo != null) {
String errorMessage = errorInfo.getMessage();
Timber.e(errorMessage);
}
// emitter.onError(new Throwable(errorMessage));
emitter.onComplete();
}
}));
}
在上面的代码中,loadAllChatChannelPages加载第一个分页器。
如果不为null,则loadChannelPaginator接管并通过执行loadNextChatChannelPage递归地获取每个下一个分页器,该方法为每个单独的分页器返回一个可观察值。
然后,mergeDelayError将所有分页器展平,并将它们作为一个单一Observable返回。
最后,在getAllChannels中,我应用了Observable.toList(),这将返回一个Single,其中包含我需要的分页聊天频道列表。