package net.lacolaco.smileessence.view.dialog import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import kotlinx.android.synthetic.main.dialog_status_detail.view.* import net.lacolaco.smileessence.R import net.lacolaco.smileessence.activity.MainActivity import net.lacolaco.smileessence.entity.Tweet import net.lacolaco.smileessence.twitter.TwitterTaskException import net.lacolaco.smileessence.twitter.task.* import net.lacolaco.smileessence.util.* import net.lacolaco.smileessence.view.DialogHelper import net.lacolaco.smileessence.view.Partials import net.lacolaco.smileessence.view.PopupMenu import net.lacolaco.smileessence.view.adapter.TimelineAdapter import net.lacolaco.smileessence.view.confirm class StatusDetailDialogFragment : StackableDialogFragment() { private lateinit var tweet: Tweet override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) val id = arguments.getLong(KEY_STATUS_ID) val found = Tweet.fetch(id) if (found == null) { world.notifyError("Tweet id=$id not found") dismiss() return } tweet = found } override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { val view = inflater.inflate(R.layout.dialog_status_detail, container, false) 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) val adapter = TimelineAdapter(activity, world) view.listview_status_detail_reply_to.adapter = adapter 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() } catch (e: Exception) { return@bg } adapter.add(inreply) adapter.update() } } else { view.detail_dialog_divider_top.visibility = View.GONE view.listview_status_detail_reply_to.visibility = View.GONE } return view } private fun updateViewButtons(view: View) { view.button_status_detail_reply.setOnClickListener { val originalTweet = tweet.originalTweet val builder = StringBuilder() builder.append("@${originalTweet.user.screenName} ") for (screenName in originalTweet.entities.mentions) { if (screenName != world.user.screenName) builder.append("@$screenName ") } 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) } } dismiss() } } 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 users = mutableListOf() if (tweet.user !== tweet.originalTweet.user) users += tweet.user.screenName users += tweet.entities.mentions for (screenName in users.distinct()) view.embedded_menu_items.add("@$screenName") { val ref = ref(activity as MainActivity) launchUi { try { val user = world.getUserAsync(screenName).await() DialogHelper.showDialog(ref.get(), UserDetailDialogFragment.newInstance(user)) } catch (e: TwitterTaskException) { world.notifyError(R.string.notice_error_show_user) } } } for (hashtag in tweet.entities.hashtags) view.embedded_menu_items.add("#$hashtag") { // XXX (activity as MainActivity).openPostPageAndAppendText(" #$hashtag") } for (url in tweet.entities.urlsExpanded + tweet.entities.mediaUrls) view.embedded_menu_items.add(url) { 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 } } companion object { private val KEY_STATUS_ID = "status_id" fun newInstance(tweet: Tweet): StatusDetailDialogFragment { val obj = StatusDetailDialogFragment() val args = Bundle() args.putLong(KEY_STATUS_ID, tweet.id) obj.arguments = args return obj } } }