aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/dialog/MessageDetailDialogFragment.java
blob: 4b16cf494774b706ae05eb82784731efce63eca8 (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
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * 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.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ListView;
import net.lacolaco.smileessence.Application;
import net.lacolaco.smileessence.R;
import net.lacolaco.smileessence.activity.MainActivity;
import net.lacolaco.smileessence.command.Command;
import net.lacolaco.smileessence.command.CommandOpenHashtagDialog;
import net.lacolaco.smileessence.command.CommandOpenURL;
import net.lacolaco.smileessence.command.CommandOpenUserDetail;
import net.lacolaco.smileessence.data.Account;
import net.lacolaco.smileessence.entity.DirectMessage;
import net.lacolaco.smileessence.notification.Notificator;
import net.lacolaco.smileessence.twitter.task.DeleteMessageTask;
import net.lacolaco.smileessence.view.DialogHelper;
import net.lacolaco.smileessence.view.adapter.MessageListAdapter;
import net.lacolaco.smileessence.view.adapter.UnorderedCustomListAdapter;
import net.lacolaco.smileessence.viewmodel.MessageViewModel;

import java.util.ArrayList;
import java.util.List;

public class MessageDetailDialogFragment extends StackableDialogFragment implements View.OnClickListener {

    // ------------------------------ FIELDS ------------------------------

    private static final String KEY_MESSAGE_ID = "messageID";
    private DirectMessage message;

    // --------------------- GETTER / SETTER METHODS ---------------------

    public long getMessageID() {
        return getArguments().getLong(KEY_MESSAGE_ID);
    }

    public void setMessageID(long messageID) {
        Bundle args = new Bundle();
        args.putLong(KEY_MESSAGE_ID, messageID);
        setArguments(args);
    }

    // ------------------------ INTERFACE METHODS ------------------------


    // --------------------- Interface OnClickListener ---------------------

    @Override
    public void onClick(final View v) {
        switch (v.getId()) {
            case R.id.button_status_detail_reply: {
                openSendMessageDialog();
                break;
            }
            case R.id.button_status_detail_delete: {
                deleteMessage();
                break;
            }
            case R.id.button_status_detail_menu: {
                openMenu();
                break;
            }
            default: {
                dismiss();
            }
        }
    }

    // ------------------------ OVERRIDE METHODS ------------------------

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        message = DirectMessage.fetch(getMessageID());
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        if (message == null) {
            Notificator.getInstance().alert(R.string.notice_error_get_messages);
            return new DisposeDialog(getActivity());
        }

        View header = getTitleView();
        ListView listView = (ListView) header.findViewById(R.id.listview_status_detail_reply_to);
        final MessageListAdapter adapter = new MessageListAdapter(getActivity());
        listView.setAdapter(adapter);

        // TODO: 効率的な探索どうする
        DirectMessage replyTo = null;
        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) {
            listView.setVisibility(View.VISIBLE);
            adapter.addItem(new MessageViewModel(replyTo));
            adapter.updateForce();
        } else {
            listView.setVisibility(View.GONE);
        }

        return new AlertDialog.Builder(getActivity()).setView(header).create();
    }

    // -------------------------- OTHER METHODS --------------------------

    private void deleteMessage() {
        ConfirmDialogFragment.show(getActivity(), getString(R.string.dialog_confirm_commands), () -> {
            new DeleteMessageTask(Application.getInstance().getCurrentAccount(), message.getId())
                    .onDone(x -> Notificator.getInstance().publish(R.string.notice_message_delete_succeeded))
                    .onFail(x -> Notificator.getInstance().alert(R.string.notice_message_delete_failed))
                    .execute();
            dismiss();
        });
    }

    private void openSendMessageDialog() {
        SendMessageDialogFragment dialogFragment = new SendMessageDialogFragment();
        dialogFragment.setScreenName(message.getSender().getScreenName());
        DialogHelper.showDialog(getActivity(), dialogFragment);
    }

    private View getTitleView() {
        MainActivity activity = (MainActivity) getActivity();

        View view = activity.getLayoutInflater().inflate(R.layout.dialog_status_detail, null);

        MessageViewModel statusViewModel = new MessageViewModel(message);
        View messageHeader = statusViewModel.getView(activity, activity.getLayoutInflater(), view.findViewById(R.id.layout_status_header));
        messageHeader.setClickable(false);

        view.setBackgroundColor(((ColorDrawable) messageHeader.getBackground()).getColor());
        updateViewButtons(view);
        updateViewMenu(view);

        // status only parts
        view.findViewById(R.id.detail_dialog_divider_top).setVisibility(View.GONE);
        view.findViewById(R.id.button_status_detail_retweet).setVisibility(View.GONE);
        view.findViewById(R.id.button_status_detail_favorite).setVisibility(View.GONE);
        view.findViewById(R.id.image_status_detail_fav_count).setVisibility(View.GONE);
        view.findViewById(R.id.image_status_detail_rt_count).setVisibility(View.GONE);

        return view;
    }

    private void updateViewButtons(View view) {
        Account account = Application.getInstance().getCurrentAccount();

        //--- buttons
        ImageButton reply = (ImageButton) view.findViewById(R.id.button_status_detail_reply);
        reply.setOnClickListener(this);

        ImageButton delete = (ImageButton) view.findViewById(R.id.button_status_detail_delete);
        delete.setOnClickListener(this);
        delete.setVisibility(account.canDelete(message) ? View.VISIBLE : View.GONE);
    }

    private void updateViewMenu(View view) {
        MainActivity activity = ((MainActivity) getActivity());
        // -- menu dialog
        ImageButton menu = (ImageButton) view.findViewById(R.id.button_status_detail_menu);
        menu.setOnClickListener(this);

        // -- menu embedded in dialog
        View divider = view.findViewById(R.id.detail_dialog_divider_bottom);
        ListView listView = (ListView) view.findViewById(R.id.listview_status_detail_menu);
        List<Command> commands = getCommands();
        if (commands.size() > 0) {
            divider.setVisibility(View.VISIBLE);
            listView.setVisibility(View.VISIBLE);
            final UnorderedCustomListAdapter<Command> adapter = new UnorderedCustomListAdapter<>(activity);
            adapter.addItemsToBottom(commands);
            adapter.updateForce();
            listView.setAdapter(adapter);
            listView.setOnItemClickListener((parent, view1, position, id) -> {
                Command command = (Command) parent.getItemAtPosition(position);
                command.execute();
            });
        } else {
            divider.setVisibility(View.GONE);
            listView.setVisibility(View.GONE);
        }
    }

    private List<Command> getCommands() {
        Activity activity = getActivity();
        List<Command> commands = new ArrayList<>();
        // Mentions
        if (message.getSender() != message.getRecipient()) {
            commands.add(new CommandOpenUserDetail(activity, message.getRecipient().getScreenName()));
        }
        for (String screenName : message.getMentions()) {
            commands.add(new CommandOpenUserDetail(activity, screenName));
        }
        for (String hashtag : message.getHashtags()) {
            commands.add(new CommandOpenHashtagDialog(activity, hashtag));
        }
        // URL
        for (String url : message.getUrlsExpanded()) {
            commands.add(new CommandOpenURL(activity, url));
        }
        for (String url : message.getMediaUrls()) {
            commands.add(new CommandOpenURL(activity, url));
        }
        return commands;
    }

    private void openMenu() {
        MessageMenuDialogFragment fragment = new MessageMenuDialogFragment();
        fragment.setMessageID(getMessageID());
        DialogHelper.showDialog(getActivity(), fragment);
    }
}