Slide 26
Slide 26 text
もっとKotlinらしくしてみる
匿名クラスのままKotlinに変換されたRxJavaの処理も
subscriptions.add(EntryInfoApi.request(RequestQueueSingleton.get(getApplicationContext()), url)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.map(EntryInfoFunc.mapToEntryInfo())
.filter(object : Func1 {
override fun call(entryInfo: EntryInfo?): Boolean? {
return entryInfo == null
}
})
.subscribe(object : Action1 {
override fun call(entryInfo: EntryInfo) {
countLayout.setVisibility(View.VISIBLE)
titleTextView.setText(entryInfo.getTitle())
bookmarkCountTextView.setText(String.valueOf(entryInfo.getBookmarkCount()))
Picasso.with(getApplicationContext()).load(entryInfo.getThumbnailUrl()).fit().into(thumbnailImageView)
val adapter = EntryInfoPagerAdapter(getSupportFragmentManager())
adapter.addPage(BookmarkListFragment.newFragment(entryInfo.getBookmarkList()), getString(R.string.entry_info_all_bookmarks))
subscriptions.add(Observable.from(entryInfo.getBookmarkList())
.filter(EntryInfoFunc.hasComment())
.toList()
.subscribe(object : Action1> {
override fun call(commentList: List) {
commentCountTextView.setText(String.valueOf(commentList.size()))
adapter.addPage(BookmarkListFragment.newFragment(commentList), getString(R.string.entry_info_comments))
if (AccountDAO.get() != null) {
adapter.addPage(RegisterBookmarkFragment.newFragment(entryInfo.getUrl()), getString(R.string.entry_info_register_bookmark))
}
viewPager.setAdapter(adapter)
viewPager.setCurrentItem(1)
viewPager.setOffscreenPageLimit(2)
tabs.setViewPager(viewPager)
}
}))
}
}, object : Action1 {
override fun call(throwable: Throwable) {
Toast.makeText(getApplicationContext(), R.string.network_error, Toast.LENGTH_SHORT).show()
}