1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
|
package net.lacolaco.smileessence.view.page
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.page_fragment_tweet.view.*
import net.lacolaco.smileessence.R
import net.lacolaco.smileessence.activity.MainActivity
import net.lacolaco.smileessence.data.PageInfo
import net.lacolaco.smileessence.entity.Tweet
import net.lacolaco.smileessence.twitter.*
import net.lacolaco.smileessence.util.*
import net.lacolaco.smileessence.view.Partials
import net.lacolaco.smileessence.view.PopupMenu
import net.lacolaco.smileessence.view.confirm
class TweetPageFragment : PageFragment<PageInfo.TweetPageInfo>() {
private lateinit var tweet: Tweet
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val id = pageInfo.statusId
var found = Tweet.cached(id)
if (found == null) {
world.notifyError("Tweet id=$id not found")
found = Tweet.placeHolder
}
tweet = found
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
return inflater.inflate(R.layout.page_fragment_tweet, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val statusHeader = Partials.getTweetView(tweet, world, activity!!, view.detail_current_status)
statusHeader.isClickable = false
// XXX
view.button_status_detail_favorite.isActivated = world.id in tweet.favoriters
view.button_status_detail_retweet.isActivated = world.id in tweet.retweets
updateViewButtons(view)
setupViewMenu(view)
if (tweet.inReplyToStatusId != null) {
view.tweet_in_reply_to.visibility = View.VISIBLE
view.detail_dialog_divider_top.visibility = View.VISIBLE
val cached = Tweet.cached(tweet.inReplyToStatusId!!)
if (cached != null)
Partials.getTweetView(cached, world, activity!!, view.tweet_in_reply_to)
else {
Partials.getTweetView(Tweet.placeHolder, world, activity!!, view.tweet_in_reply_to)
launchUi {
try {
val tweet = world.getTweetAsync(tweet.inReplyToStatusId!!, false).await()
Partials.getTweetView(tweet, world, activity!!, view.tweet_in_reply_to)
} catch (e: TwitterTaskException) {
}
}
}
} else {
view.tweet_in_reply_to.visibility = View.GONE
view.detail_dialog_divider_top.visibility = View.GONE
}
}
private fun updateViewButtons(view: View) {
view.button_status_detail_reply.setOnClickListener {
val originalTweet = tweet.originalTweet
val builder = StringBuilder()
builder.append("@${originalTweet.user.screenName} ")
originalTweet.entities.mentions
.filter { it != world.user.screenName }
.forEach { builder.append("@$it ") }
val text = builder.toString()
val selStart = originalTweet.user.screenName.length + 2 // "@" and " "
(activity as MainActivity).openPostPageAndReplyTo(originalTweet, text)
}
view.button_status_detail_retweet.setOnClickListener {
confirm(R.string.dialog_confirm_commands) {
if (world.id in tweet.retweets) {
launchUi {
try {
world.deleteTweetAsync(tweet.retweets[world.id]!!).await()
world.notify(R.string.notice_status_delete_succeeded)
updateViewButtons(view)
} catch (e: TwitterTaskException) {
world.notifyError(R.string.notice_status_delete_failed)
}
}
} else {
launchUi {
try {
world.retweetAsync(tweet.id).await()
world.notify(R.string.notice_retweet_succeeded)
updateViewButtons(view)
} catch (e: TwitterTaskException) {
world.notifyError(R.string.notice_retweet_failed)
}
}
}
}
}
view.button_status_detail_favorite.setOnClickListener {
val favoriting = world.id !in tweet.favoriters
launchUi {
try {
if (favoriting) {
world.favoriteAsync(tweet.id).await()
world.notify(R.string.notice_favorite_succeeded)
} else {
world.unfavoriteAsync(tweet.id).await()
world.notify(R.string.notice_unfavorite_succeeded)
}
} catch (e: TwitterTaskException) {
if (favoriting)
world.notifyError(R.string.notice_favorite_failed)
else
world.notifyError(R.string.notice_unfavorite_failed)
}
}
}
if (tweet.originalTweet.user === world.user)
view.button_status_detail_delete.visibility = View.VISIBLE
else
view.button_status_detail_delete.visibility = View.GONE
view.button_status_detail_delete.setOnClickListener {
confirm(R.string.dialog_confirm_commands) {
launchBg {
try {
world.deleteTweetAsync(tweet.originalTweet.id).await()
world.notify(R.string.notice_status_delete_succeeded)
} catch (e: TwitterTaskException) {
world.notifyError(R.string.notice_status_delete_failed)
}
}
(activity as MainActivity).closeCurrentEphemeralPage()
}
}
view.button_status_detail_menu.setOnClickListener {
val popup = PopupMenu(activity!!, view.button_status_detail_menu)
popup.add(R.string.command_status_add_to_reply) {
val text = String.format("@%s ", tweet.originalTweet.user.screenName)
(activity as MainActivity).openPostPageAndAppendText(text)
world.notify(R.string.notice_add_to_reply)
}
popup.add(R.string.command_status_open_in_browser) {
browse("https://twitter.com/${tweet.originalTweet.user.screenName}/status/${tweet.originalTweet.id}")
}
popup.add(R.string.command_status_copy_text_to_clipboard) {
SystemServiceHelper.copyToClipboard(activity!!, "tweet text", tweet.originalTweet.text)
world.notify(R.string.notice_copy_clipboard)
}
popup.add(R.string.command_status_copy_url_to_clipboard) {
SystemServiceHelper.copyToClipboard(activity!!, "tweet url", "https://twitter.com/${tweet.originalTweet.user.screenName}/status/${tweet.originalTweet.id}")
world.notify(R.string.notice_copy_clipboard)
}
popup.show()
}
}
private fun setupViewMenu(view: View) {
val addUserItem = { screenName: String, title: String ->
view.embedded_menu_items.add("@$screenName", title) {
val ref = ref(activity as MainActivity)
launchUi {
try {
val user = world.getUserAsync(screenName).await()
(activity as MainActivity).addEphemeralUserPage(user.id)
} catch (e: TwitterTaskException) {
world.notifyError(R.string.notice_error_show_user)
}
}
}
}
if (tweet.user !== tweet.originalTweet.user)
addUserItem(tweet.user.screenName, "RT by")
for (screenName in tweet.entities.mentions.distinct().minus(tweet.user.screenName))
addUserItem(screenName, "Mention")
for (hashtag in tweet.entities.hashtags)
view.embedded_menu_items.add("#$hashtag", "Hashtag") {
// XXX
(activity as MainActivity).openPostPageAndAppendText(" #$hashtag")
}
for (url in tweet.entities.urlsExpanded)
view.embedded_menu_items.add(url, "URL") {
activity!!.browse(url)
}
for (url in tweet.entities.mediaUrls)
view.embedded_menu_items.add(url, "Media") {
activity!!.browse(url)
}
if (view.embedded_menu_items.childCount == 0) {
view.detail_dialog_divider_bottom.visibility = View.GONE
view.embedded_menu_items.visibility = View.GONE
}
}
}
|