aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
diff options
context:
space:
mode:
authorKazuki Yamaguchi <k@rhe.jp>2017-10-10 18:15:41 +0900
committerKazuki Yamaguchi <k@rhe.jp>2017-10-10 18:22:33 +0900
commit901356520a22c1160b08b972416085c317579fde (patch)
tree8220a237de8f711cbd21e42f62c3e18d86b2cd00 /app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
parent25404ac5fa085fad902973ac59d6b831ed7f57b7 (diff)
downloadSmileEssence-901356520a22c1160b08b972416085c317579fde.tar.gz
coroutine-ify2017-10-10
Diffstat (limited to 'app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt')
-rw-r--r--app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt158
1 files changed, 82 insertions, 76 deletions
diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
index 2583d019..acfca4ec 100644
--- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
+++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
@@ -7,18 +7,16 @@ import android.text.Html
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.view.View
-import android.widget.AbsListView
-import android.widget.ListView
-import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayout
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection
-import kotlinx.android.synthetic.main.dialog_user_detail.*
import kotlinx.android.synthetic.main.dialog_user_detail.view.*
import net.lacolaco.smileessence.R
import net.lacolaco.smileessence.data.ImageCache
import net.lacolaco.smileessence.entity.User
-import net.lacolaco.smileessence.twitter.task.Timelines
-import net.lacolaco.smileessence.twitter.task.Users
+import net.lacolaco.smileessence.twitter.TwitterTaskException
+import net.lacolaco.smileessence.twitter.task.*
import net.lacolaco.smileessence.util.UIHandler
+import net.lacolaco.smileessence.util.launchBg
+import net.lacolaco.smileessence.util.launchUi
import net.lacolaco.smileessence.view.DialogHelper
import net.lacolaco.smileessence.view.ThreeStateButton
import net.lacolaco.smileessence.view.adapter.TimelineAdapter
@@ -49,22 +47,32 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener
}
1 -> DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user))
2 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) {
- Users.BlockTask(world.account, user.id)
- .onDone { user -> world.notify(R.string.notice_block_succeeded) }
- .onFail { ex -> world.notifyError(R.string.notice_block_failed) }
- .execute()
+ launchBg {
+ try {
+ world.blockAsync(user.id).await()
+ world.notify(R.string.notice_block_succeeded)
+ } catch (e: TwitterTaskException) {
+ world.notifyError(R.string.notice_block_failed)
+ }
+ }
}
- 3 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) {
- Users.UnblockTask(world.account, user.id)
- .onDone { user -> world.notify(R.string.notice_unblock_succeeded) }
- .onFail { x -> world.notifyError(R.string.notice_unblock_failed) }
- .execute()
+ 3 -> launchBg {
+ try {
+ world.unblockAsync(user.id).await()
+ world.notify(R.string.notice_unblock_succeeded)
+ } catch (e: TwitterTaskException) {
+ world.notifyError(R.string.notice_unblock_failed)
+ }
}
4 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) {
- Users.ReportForSpamTask(world.account, user.id)
- .onDone { user -> world.notify(R.string.notice_r4s_succeeded) }
- .onFail { ex -> world.notifyError(R.string.notice_r4s_failed) }
- .execute()
+ launchBg {
+ try {
+ world.reportSpamAsync(user.id).await()
+ world.notify(R.string.notice_r4s_succeeded)
+ } catch (e: TwitterTaskException) {
+ world.notifyError(R.string.notice_r4s_failed)
+ }
+ }
}
5 -> browse(user.aclogTimelineURL)
else -> throw IllegalStateException()
@@ -81,25 +89,24 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener
R.id.button_user_detail_follow -> {
val v = view
ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) {
- val account = world.account
val isFollowing = v.button_user_detail_follow.state == ThreeStateButton.STATE_ON
v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED
- if (isFollowing) {
- Users.UnfollowTask(account, user.id)
- .onDoneUI { result ->
- world.notify(R.string.notice_unfollow_succeeded)
- updateRelationship(v)
- }
- .onFail { x -> world.notifyError(R.string.notice_unfollow_failed) }
- .execute()
- } else {
- Users.FollowTask(account, user.id)
- .onDoneUI { result ->
- world.notify(R.string.notice_follow_succeeded)
- updateRelationship(v)
- }
- .onFail { x -> world.notifyError(R.string.notice_follow_failed) }
- .execute()
+ launchUi {
+ try {
+ if (isFollowing) {
+ world.unfollowAsync(user.id).await()
+ world.notify(R.string.notice_unfollow_succeeded)
+ } else {
+ world.followAsync(user.id).await()
+ world.notify(R.string.notice_follow_succeeded)
+ }
+ updateRelationship(v)
+ } catch (e: TwitterTaskException) {
+ world.notifyError(if (isFollowing)
+ R.string.notice_unfollow_failed
+ else
+ R.string.notice_follow_failed)
+ }
}
}
}
@@ -126,28 +133,22 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener
v.button_user_detail_follow.setOnClickListener(this)
val refreshLayout = v.user_detail_refresh_layout
refreshLayout.setOnRefreshListener {
- if (it == SwipyRefreshLayoutDirection.TOP)
- Timelines.UserTimelineTask(world.account, user.id)
- .setCount(200)
- .setSinceId(adapter.topID)
- .onFail { x -> world.notifyError(R.string.notice_error_get_user_timeline) }
- .onDoneUI { tweets ->
- adapter.addAll(tweets)
- updateListView(v, true)
- refreshLayout.isRefreshing = false
- }
- .execute()
- else
- Timelines.UserTimelineTask(world.account, user.id)
- .setCount(200)
- .setMaxId(adapter.lastID - 1)
- .onFail { x -> world.notifyError(R.string.notice_error_get_user_timeline) }
- .onDoneUI { tweets ->
- adapter.addAll(tweets)
- updateListView(v, false)
- refreshLayout.isRefreshing = false
- }
- .execute()
+ launchUi {
+ try {
+ if (it == SwipyRefreshLayoutDirection.TOP) {
+ val tweets = world.getUserTimelineAsync(user.id, sinceId = adapter.topID).await()
+ adapter.addAll(tweets)
+ updateListView(v, true)
+ } else {
+ val tweets = world.getUserTimelineAsync(user.id, maxId = adapter.lastID - 1).await()
+ adapter.addAll(tweets)
+ updateListView(v, false)
+ }
+ } catch (e: TwitterTaskException) {
+ world.notifyError(R.string.notice_error_get_user_timeline)
+ }
+ refreshLayout.isRefreshing = false
+ }
}
v.tabhost.setup()
@@ -171,17 +172,17 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener
}
private fun executeUserTimelineTask(v: View, adapter: TimelineAdapter) {
- val account = world.account
- v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.GONE
- Timelines.UserTimelineTask(account, user.id)
- .setCount(200)
- .onFail { x -> world.notifyError(R.string.notice_error_get_user_timeline) }
- .onDoneUI { tweets ->
- adapter.addAll(tweets)
- adapter.updateForce()
- v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.VISIBLE
- }
- .execute()
+ launchUi {
+ v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.GONE
+ try {
+ val tweets = world.getUserTimelineAsync(user.id).await()
+ adapter.addAll(tweets)
+ adapter.updateForce()
+ v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.VISIBLE
+ } catch (e: TwitterTaskException) {
+ world.notifyError(R.string.notice_error_get_user_timeline)
+ }
+ }
}
private val htmlDescription: String?
@@ -247,13 +248,18 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener
v.button_user_detail_follow.visibility = View.GONE
} else {
v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED
- v.textview_user_detail_followed.setText(R.string.user_detail_loading)
- Users.ShowFriendshipTask(account, user.id).onDoneUI { relationship ->
- val isFollowing = relationship.isSourceFollowingTarget
- val isFollowed = relationship.isSourceFollowedByTarget
- v.button_user_detail_follow.state = if (isFollowing) ThreeStateButton.STATE_ON else ThreeStateButton.STATE_OFF
- v.textview_user_detail_followed.setText(if (isFollowed) R.string.user_detail_followed else R.string.user_detail_not_followed)
- }.execute()
+ launchUi {
+ v.textview_user_detail_followed.setText(R.string.user_detail_loading)
+ try {
+ val relationship = world.getRelationshipAsync(user.id).await()
+ val isFollowing = relationship.isSourceFollowingTarget
+ val isFollowed = relationship.isSourceFollowedByTarget
+ v.button_user_detail_follow.state = if (isFollowing) ThreeStateButton.STATE_ON else ThreeStateButton.STATE_OFF
+ v.textview_user_detail_followed.setText(if (isFollowed) R.string.user_detail_followed else R.string.user_detail_not_followed)
+ } catch (e: TwitterTaskException) {
+ world.notifyError("Failed to fetch relationship")
+ }
+ }
}
}