aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.kt
blob: 8315ca9f3889afc3679e49adee242939606cc7c4 (plain)
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
/*
 * The MIT License (MIT)
 *
 * Copyright (c) 2012-2015 lacolaco.net
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 */

package net.lacolaco.smileessence.view.dialog

import android.app.AlertDialog
import android.app.Dialog
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.View
import android.view.ViewGroup
import kotlinx.android.synthetic.main.dialog_status_detail.view.*
import kotlinx.android.synthetic.main.menu_item_simple_text.view.*
import net.lacolaco.smileessence.R
import net.lacolaco.smileessence.activity.MainActivity
import net.lacolaco.smileessence.command.Command
import net.lacolaco.smileessence.command.CommandAddHashtag
import net.lacolaco.smileessence.command.CommandOpenURL
import net.lacolaco.smileessence.command.CommandOpenUserDetail
import net.lacolaco.smileessence.entity.DirectMessage
import net.lacolaco.smileessence.preference.UserPreferenceHelper
import net.lacolaco.smileessence.twitter.task.Messages
import net.lacolaco.smileessence.util.SystemServiceHelper
import net.lacolaco.smileessence.view.DialogHelper
import net.lacolaco.smileessence.view.Partials
import net.lacolaco.smileessence.view.adapter.CustomListAdapter
import net.lacolaco.smileessence.view.adapter.MessageListAdapter
import net.lacolaco.smileessence.view.adapter.UnorderedCustomListAdapter

import java.util.ArrayList

class MessageDetailDialogFragment : StackableDialogFragment() {
    private lateinit var message: DirectMessage

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
        val found = DirectMessage.fetch(arguments.getLong(KEY_MESSAGE_ID))
        if (found == null) {
            world.notifyError(R.string.notice_error_get_messages)
            return DisposeDialog(activity)
        }
        message = found

        val view = activity.layoutInflater.inflate(R.layout.dialog_status_detail, null)

        val messageHeader = Partials.getDirectMessageView(message, world, activity, view.layout_status_header)
        messageHeader.isClickable = false

        view.setBackgroundColor((messageHeader.background as ColorDrawable).color)
        //--- buttons
        view.button_status_detail_reply.setOnClickListener {
            DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(message.sender))
        }
        view.button_status_detail_delete.setOnClickListener {
            ConfirmDialogFragment.show(activity, getString(R.string.dialog_confirm_commands)) {
                Messages.DestroyTask(world.account, message.id)
                        .onDone { x -> world.notify(R.string.notice_message_delete_succeeded) }
                        .onFail { x -> world.notifyError(R.string.notice_message_delete_failed) }
                        .execute()
                dismiss()
            }
        }
        // -- menu dialog
        view.button_status_detail_menu.setOnClickListener {
            val builder = AlertDialog.Builder(activity)
            builder.setTitle("@" + message.sender.screenName + ": " + message.text)
                    .setItems(R.array.message_commands) { dialog, which ->
                        when (which) {
                            0 -> {
                                SystemServiceHelper.copyToClipboard(activity, "message text", message.text)
                                world.notify(R.string.notice_copy_clipboard)
                            }
                            else -> throw IllegalStateException()
                        }
                    }
            val dialog = builder.create()
            dialog.show()
        }

        // -- menu embedded in dialog
        val activity = activity as MainActivity
        val commands = ArrayList<Command>()
        if (message.sender !== message.recipient) {
            commands.add(CommandOpenUserDetail(activity, message.recipient.screenName))
        }
        for (screenName in message.mentions) {
            commands.add(CommandOpenUserDetail(activity, screenName))
        }
        for (hashtag in message.hashtags) {
            commands.add(CommandAddHashtag(activity, hashtag))
        }
        for (url in message.urlsExpanded) {
            commands.add(CommandOpenURL(activity, url))
        }
        for (url in message.mediaUrls) {
            commands.add(CommandOpenURL(activity, url))
        }
        if (commands.size > 0) {
            view.detail_dialog_divider_bottom.visibility = View.VISIBLE
            view.listview_status_detail_menu.visibility = View.VISIBLE
            val adapter = object : UnorderedCustomListAdapter<Command>(commands) {
                override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
                    val itemView = convertView ?: activity.layoutInflater.inflate(R.layout.menu_item_simple_text, parent, false)
                    itemView.list_item_textview.textSize = UserPreferenceHelper.instance.textSize.toFloat()
                    itemView.list_item_textview.text = getItem(position).text
                    return itemView
                }
            }
            view.listview_status_detail_menu.adapter = adapter
            view.listview_status_detail_menu.setOnItemClickListener { parent, view1, position, id ->
                val command = parent.getItemAtPosition(position) as Command
                command.execute()
            }
        } else {
            view.detail_dialog_divider_bottom.visibility = View.GONE
            view.listview_status_detail_menu.visibility = View.GONE
        }
        view.detail_dialog_divider_top.visibility = View.GONE
        view.button_status_detail_retweet.visibility = View.GONE
        view.button_status_detail_favorite.visibility = View.GONE
        view.image_status_detail_fav_count.visibility = View.GONE
        view.image_status_detail_rt_count.visibility = View.GONE

        val adapter = MessageListAdapter(activity, world)
        view.listview_status_detail_reply_to.adapter = adapter

        // TODO: 効率的な探索どうする
        val replyTo: DirectMessage? = null
        // FIXME
        // for (DirectMessage mes : DirectMessage.cached()) {
        //     if (message.getId() > mes.getId() &&
        //             message.getRecipient() == mes.getSender() &&
        //             message.getSender() == mes.getRecipient() &&
        //             (replyTo == null || replyTo.getId() < mes.getId())) {
        //         replyTo = mes;
        //     }
        // }
        if (replyTo != null) {
            view.listview_status_detail_reply_to.visibility = View.VISIBLE
            adapter.add(replyTo)
            adapter.updateForce()
        } else {
            view.listview_status_detail_reply_to.visibility = View.GONE
        }

        return AlertDialog.Builder(activity).setView(view).create()
    }

    companion object {
        private val KEY_MESSAGE_ID = "message_id"

        fun newInstance(message: DirectMessage): MessageDetailDialogFragment {
            val obj = MessageDetailDialogFragment()
            val args = Bundle()
            args.putLong(KEY_MESSAGE_ID, message.id)
            obj.arguments = args
            return obj
        }
    }
}