From d8a2c1c87f7d32bfe91216adaaa6ad6b43ee1419 Mon Sep 17 00:00:00 2001 From: Kazuki Yamaguchi Date: Wed, 11 Oct 2017 15:44:45 +0900 Subject: unyap --- .../java/net/lacolaco/smileessence/entity/Tweet.kt | 44 +++--- .../java/net/lacolaco/smileessence/entity/User.kt | 10 ++ .../view/dialog/StatusDetailDialogFragment.kt | 4 +- .../view/dialog/TalkChainDialogFragment.kt | 4 +- .../view/dialog/UserDetailDialogFragment.kt | 168 +++++++++------------ build.gradle | 2 +- 6 files changed, 112 insertions(+), 120 deletions(-) diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.kt b/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.kt index d34675af..fbb45641 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.kt +++ b/app/src/main/java/net/lacolaco/smileessence/entity/Tweet.kt @@ -11,20 +11,32 @@ class Tweet private constructor(st: twitter4j.Status, myUserId: Long) : EntitySu val user: User = User.fromTwitter(st.user) val createdAt: Date = st.createdAt val source: String = st.source - val retweetedTweet: Tweet? = if (st.isRetweet) Tweet.fromTwitter(st.retweetedStatus, myUserId) else null + val retweetedTweet: Tweet? = + if (st.isRetweet) Tweet.fromTwitter(st.retweetedStatus, myUserId) + else null val isRetweet = retweetedTweet != null val originalTweet = retweetedTweet ?: this - val text: String = if (isRetweet) originalTweet.text else extractText(st, false) - val inReplyToStatusId: Long = if (isRetweet) originalTweet.inReplyToStatusId else st.inReplyToStatusId + val text: String = retweetedTweet?.text ?: extractText(st, false) + val inReplyToStatusId: Long? = + if (isRetweet) retweetedTweet!!.inReplyToStatusId + else if (st.inReplyToStatusId != -1L) st.inReplyToStatusId + else null var favoriteCount: Int = st.favoriteCount - get() = if (isRetweet) originalTweet.favoriteCount else field + get() = retweetedTweet?.favoriteCount ?: field private set var retweetCount: Int = st.retweetCount - get() = if (isRetweet) originalTweet.retweetCount else field + get() = retweetedTweet?.retweetCount ?: field private set private val favoriters: MutableSet = Collections.newSetFromMap(ConcurrentHashMap()) private val retweets: MutableMap = ConcurrentHashMap() + // Override EntitySupport + override val mentions: List = retweetedTweet?.mentions ?: super.mentions + override val hashtags: List = retweetedTweet?.hashtags ?: super.hashtags + override val mediaUrls: List = retweetedTweet?.mediaUrls ?: super.mediaUrls + override val urlsExpanded: List = retweetedTweet?.urlsExpanded ?: super.urlsExpanded + override val symbols: List = retweetedTweet?.symbols ?: super.symbols + init { update(st, myUserId) } @@ -33,16 +45,13 @@ class Tweet private constructor(st: twitter4j.Status, myUserId: Long) : EntitySu User.fromTwitter(status.user) if (retweetedTweet != null) { Tweet.fromTwitter(status.retweetedStatus, myUserId) - if (status.isFavorited) { + if (status.isFavorited) retweetedTweet.favoriters.add(myUserId) - } - if (status.currentUserRetweetId > 0) { + if (status.currentUserRetweetId > 0) retweetedTweet.retweets.put(myUserId, status.currentUserRetweetId) - } } else { favoriteCount = status.favoriteCount retweetCount = status.retweetCount - if (status.isFavorited) favoriters.add(myUserId) else @@ -81,22 +90,17 @@ class Tweet private constructor(st: twitter4j.Status, myUserId: Long) : EntitySu for (url in urlsExpanded) { val uri = Uri.parse(url) if ("twitter.com" == uri.host) { - val arr = uri.toString().split("/".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray() - if (arr.size > 2 && "status" == arr[arr.size - 2]) { - list.add(arr[arr.size - 1].split("\\?".toRegex()).dropLastWhile({ it.isEmpty() }).toTypedArray()[0].toLong()) + val segments = uri.pathSegments + if (segments.size >= 3 && segments[1] == "status") { + val idish = segments[2].toLongOrNull() + if (idish != null) + list.add(idish) } } } list } - // override EntitySupport - override val mentions: List = retweetedTweet?.mentions ?: super.mentions - override val hashtags: List = retweetedTweet?.hashtags ?: super.hashtags - override val mediaUrls: List = retweetedTweet?.mediaUrls ?: super.mediaUrls - override val urlsExpanded: List = retweetedTweet?.urlsExpanded ?: super.urlsExpanded - override val symbols: List = retweetedTweet?.symbols ?: super.symbols - companion object { private val storage = HashMap() diff --git a/app/src/main/java/net/lacolaco/smileessence/entity/User.kt b/app/src/main/java/net/lacolaco/smileessence/entity/User.kt index 8dd05d3d..c1afafbe 100644 --- a/app/src/main/java/net/lacolaco/smileessence/entity/User.kt +++ b/app/src/main/java/net/lacolaco/smileessence/entity/User.kt @@ -76,6 +76,16 @@ class User private constructor(override val id: Long, screenName: String) : IdOb val formattedName: String get() = screenName + " / " + name + // XXX + val decoratedDescription: String + get() { + var html = description + html = html.replace("https?://[\\w/:%#$&?()~.=+-]+".toRegex(), "$0") + html = html.replace("@([a-zA-Z0-9_]+)".toRegex(), "$0") + html = html.replace("\r\n".toRegex(), "
") + return html + } + companion object { private val storage = HashMap() diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt index d2784cdf..3e9918a2 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/StatusDetailDialogFragment.kt @@ -53,13 +53,13 @@ class StatusDetailDialogFragment : StackableDialogFragment() { val adapter = TimelineAdapter(activity, world) view.listview_status_detail_reply_to.adapter = adapter - if (tweet.inReplyToStatusId != -1L) { + if (tweet.inReplyToStatusId != null) { view.detail_dialog_divider_top.visibility = View.VISIBLE view.listview_status_detail_reply_to.visibility = View.VISIBLE bg { val inreply = try { - world.getTweetAsync(tweet.inReplyToStatusId, false).await() + world.getTweetAsync(tweet.inReplyToStatusId!!, false).await() } catch (e: Exception) { return@bg } diff --git a/app/src/main/java/net/lacolaco/smileessence/view/dialog/TalkChainDialogFragment.kt b/app/src/main/java/net/lacolaco/smileessence/view/dialog/TalkChainDialogFragment.kt index 2584c15d..617a3d45 100644 --- a/app/src/main/java/net/lacolaco/smileessence/view/dialog/TalkChainDialogFragment.kt +++ b/app/src/main/java/net/lacolaco/smileessence/view/dialog/TalkChainDialogFragment.kt @@ -25,8 +25,8 @@ class TalkChainDialogFragment : StackableDialogFragment() { view.listview_dialog_talk_list.adapter = adapter launchBg { - var id = arguments.getLong(KEY_STATUS_ID) - while (id != -1L) { + var id: Long? = arguments.getLong(KEY_STATUS_ID) + while (id != null) { val tweet = try { world.getTweetAsync(id, false).await() } catch (e: TwitterTaskException) { 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 acfca4ec..211700aa 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 @@ -22,93 +22,80 @@ import net.lacolaco.smileessence.view.ThreeStateButton import net.lacolaco.smileessence.view.adapter.TimelineAdapter import org.jetbrains.anko.browse -class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener { +class UserDetailDialogFragment : StackableDialogFragment() { private val adapter by lazy { TimelineAdapter(activity, world) } private lateinit var user: User - override fun onClick(v: View) { - when (v.id) { - R.id.imageview_user_detail_menu -> { - val v = view - val builder = AlertDialog.Builder(activity) - builder.setTitle("@" + user.screenName) - .setItems(R.array.user_commands) { dialog, which -> - // XXX - UIHandler().postDelayed({ - if (this@UserDetailDialogFragment.isAdded) { + private fun showPopupMenu(v: View) { + val builder = AlertDialog.Builder(activity) + builder.setTitle("@" + user.screenName) + .setItems(R.array.user_commands) { _, which -> + when (which) { + 0 -> { + val text = String.format("@%s ", user.screenName) + world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() + world.notify(R.string.notice_add_to_reply) + } + 1 -> DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user)) + 2 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { + launchUi { + try { + world.blockAsync(user.id).await() + world.notify(R.string.notice_block_succeeded) updateRelationship(v) + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_block_failed) } - }, 1000) - when (which) { - 0 -> { - val text = String.format("@%s ", user.screenName) - world.postState.beginTransaction().insertText(0, text).moveCursor(text.length).commit() - world.notify(R.string.notice_add_to_reply) - } - 1 -> DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user)) - 2 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { - 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 -> 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)) { - 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() } } - val dialog = builder.create() - dialog.show() - } - R.id.imageview_user_detail_icon -> browse(user.profileImageUrl) - R.id.textview_user_detail_screenname, R.id.textview_user_detail_tweet_count -> browse(user.userHomeURL) - R.id.textview_user_detail_friend_count -> browse(String.format("%s/following", user.userHomeURL)) - R.id.textview_user_detail_follower_count -> browse(String.format("%s/followers", user.userHomeURL)) - R.id.textview_user_detail_favorite_count -> browse(String.format("%s/favorites", user.userHomeURL)) - R.id.button_user_detail_follow -> { - val v = view - ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { - val isFollowing = v.button_user_detail_follow.state == ThreeStateButton.STATE_ON - v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED - 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) + 3 -> launchBg { + try { + world.unblockAsync(user.id).await() + world.notify(R.string.notice_unblock_succeeded) + updateRelationship(v) + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_unblock_failed) } - updateRelationship(v) - } catch (e: TwitterTaskException) { - world.notifyError(if (isFollowing) - R.string.notice_unfollow_failed - else - R.string.notice_follow_failed) } + 4 -> ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { + launchBg { + try { + world.reportSpamAsync(user.id).await() + world.notify(R.string.notice_r4s_succeeded) + updateRelationship(v) + } catch (e: TwitterTaskException) { + world.notifyError(R.string.notice_r4s_failed) + } + } + } + 5 -> browse(user.aclogTimelineURL) + else -> throw IllegalStateException() } } + val dialog = builder.create() + dialog.show() + } + + private fun toggleFollowing(v: View) { + ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) { + val isFollowing = v.button_user_detail_follow.state == ThreeStateButton.STATE_ON + v.button_user_detail_follow.state = ThreeStateButton.STATE_LOCKED + 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) + } } } } @@ -122,15 +109,15 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener user = found val v = activity.layoutInflater.inflate(R.layout.dialog_user_detail, null) - v.imageview_user_detail_menu.setOnClickListener(this) - v.textview_user_detail_screenname.setOnClickListener(this) + v.imageview_user_detail_menu.setOnClickListener { showPopupMenu(v) } + v.textview_user_detail_screenname.setOnClickListener { browse(user.userHomeURL) } v.textview_user_detail_description.movementMethod = LinkMovementMethod.getInstance() - v.textview_user_detail_tweet_count.setOnClickListener(this) - v.textview_user_detail_friend_count.setOnClickListener(this) - v.textview_user_detail_follower_count.setOnClickListener(this) - v.textview_user_detail_favorite_count.setOnClickListener(this) - v.imageview_user_detail_icon.setOnClickListener(this) - v.button_user_detail_follow.setOnClickListener(this) + v.textview_user_detail_tweet_count.setOnClickListener { browse(user.userHomeURL) } + v.textview_user_detail_friend_count.setOnClickListener { browse(String.format("%s/following", user.userHomeURL)) } + v.textview_user_detail_follower_count.setOnClickListener { browse(String.format("%s/followers", user.userHomeURL)) } + v.textview_user_detail_favorite_count.setOnClickListener { browse(String.format("%s/favorites", user.userHomeURL)) } + v.imageview_user_detail_icon.setOnClickListener { browse(user.profileImageUrl) } + v.button_user_detail_follow.setOnClickListener { toggleFollowing(v) } val refreshLayout = v.user_detail_refresh_layout refreshLayout.setOnRefreshListener { launchUi { @@ -161,7 +148,7 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener updateUserDataBasic(v) updateUserDataDetail(v) - v.listview_user_detail_timeline.setAdapter(adapter) + v.listview_user_detail_timeline.adapter = adapter executeUserTimelineTask(v, adapter) updateRelationship(v) @@ -185,15 +172,6 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener } } - private val htmlDescription: String? - get() { - var html = user.description - html = html.replace("https?://[\\w/:%#$&?()~.=+-]+".toRegex(), "$0") - html = html.replace("@([a-zA-Z0-9_]+)".toRegex(), "$0") - html = html.replace("\r\n".toRegex(), "
") - return html - } - private fun updateUserDataBasic(v: View) { v.textview_user_detail_name.text = user.name v.textview_user_detail_screenname.text = user.screenName @@ -214,7 +192,7 @@ class UserDetailDialogFragment : StackableDialogFragment(), View.OnClickListener v.textview_user_detail_url.text = user.url v.textview_user_detail_url.visibility = View.VISIBLE } - v.textview_user_detail_description.text = Html.fromHtml(htmlDescription) + v.textview_user_detail_description.text = Html.fromHtml(user.decoratedDescription) v.textview_user_detail_tweet_count.text = user.statusesCount.toString() v.textview_user_detail_friend_count.text = user.friendsCount.toString() diff --git a/build.gradle b/build.gradle index 75125581..b234098e 100644 --- a/build.gradle +++ b/build.gradle @@ -1,5 +1,5 @@ buildscript { - ext.kotlin_version = '1.1.3-2' + ext.kotlin_version = '1.1.51' repositories { google() jcenter() -- cgit v1.2.3