aboutsummaryrefslogtreecommitdiffstats
path: root/app/src/main/java/net/lacolaco/smileessence/view/dialog/UserDetailDialogFragment.kt
blob: 275f4a9c9a28fb25565bf7034d29d486abe0027b (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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
package net.lacolaco.smileessence.view.dialog

import android.os.Bundle
import android.text.Html
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import com.bumptech.glide.Glide
import com.omadahealth.github.swipyrefreshlayout.library.SwipyRefreshLayoutDirection
import kotlinx.android.synthetic.main.dialog_user_detail.*
import kotlinx.android.synthetic.main.dialog_user_detail.view.*
import net.lacolaco.smileessence.R
import net.lacolaco.smileessence.activity.MainActivity
import net.lacolaco.smileessence.entity.User
import net.lacolaco.smileessence.twitter.TwitterTaskException
import net.lacolaco.smileessence.twitter.task.*
import net.lacolaco.smileessence.util.browse
import net.lacolaco.smileessence.util.launchUi
import net.lacolaco.smileessence.view.DialogHelper
import net.lacolaco.smileessence.view.PopupMenu
import net.lacolaco.smileessence.view.adapter.TimelineAdapter
import net.lacolaco.smileessence.view.confirm

class UserDetailDialogFragment : StackableDialogFragment() {
    private val adapter by lazy { TimelineAdapter(activity, world) }
    private lateinit var user: User

    private fun showPopupMenu() {
        val popup = PopupMenu(activity, imageview_user_detail_menu)
        popup.add(R.string.command_user_add_to_reply) {
            (activity as MainActivity).openPostPageAndReplyTo(user)
            world.notify(R.string.notice_add_to_reply)
        }
        popup.add(R.string.command_user_send_message) {
            DialogHelper.showDialog(activity, SendMessageDialogFragment.newInstance(user))
        }
        popup.add(R.string.command_user_block) {
            confirm(R.string.dialog_confirm_commands) {
                launchUi {
                    try {
                        world.blockAsync(user.id).await()
                        world.notify(R.string.notice_block_succeeded)
                        updateRelationship()
                    } catch (e: TwitterTaskException) {
                        world.notifyError(R.string.notice_block_failed)
                    }
                }
            }
        }
        popup.add(R.string.command_user_unblock) {
            launchUi {
                try {
                    world.unblockAsync(user.id).await()
                    world.notify(R.string.notice_unblock_succeeded)
                    updateRelationship()
                } catch (e: TwitterTaskException) {
                    world.notifyError(R.string.notice_unblock_failed)
                }
            }
        }
        popup.add(R.string.command_user_r4s) {
            confirm(R.string.dialog_confirm_commands) {
                launchUi {
                    try {
                        world.reportSpamAsync(user.id).await()
                        world.notify(R.string.notice_r4s_succeeded)
                        updateRelationship()
                    } catch (e: TwitterTaskException) {
                        world.notifyError(R.string.notice_r4s_failed)
                    }
                }
            }
        }
        popup.show()
    }

    private fun toggleFollowing(v: View) {
        assert(v.toggle_follow.isEnabled)
        confirm(R.string.dialog_confirm_commands) {
            val isFollowing = toggle_follow.isActivated
            toggle_follow.isEnabled = false
            toggle_follow.text = "..."
            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()
                } catch (e: TwitterTaskException) {
                    world.notifyError(if (isFollowing)
                        R.string.notice_unfollow_failed
                    else
                        R.string.notice_follow_failed)
                }
            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val found = User.fetch(arguments.getLong(KEY_USER_ID))
        if (found == null) {
            world.notify(R.string.notice_error_show_user)
            dismiss()
            return
        }
        user = found
    }

    override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
        val v = inflater.inflate(R.layout.dialog_user_detail, container, false)
        v.imageview_user_detail_menu.setOnClickListener { showPopupMenu() }
        v.textview_user_detail_screenname.setOnClickListener { browse(user.userHomeURL) }
        v.textview_user_detail_description.movementMethod = LinkMovementMethod.getInstance()
        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.toggle_follow.setOnClickListener { toggleFollowing(v) }
        val refreshLayout = v.user_detail_refresh_layout
        refreshLayout.setOnRefreshListener {
            launchUi {
                try {
                    if (it == SwipyRefreshLayoutDirection.TOP) {
                        val tweets = world.getUserTimelineAsync(user.id, sinceId = adapter.topID).await()
                        adapter.addAll(tweets)
                        updateListView(v, true)
                    } else {
                        val tweets = world.getUserTimelineAsync(user.id, maxId = adapter.lastID - 1).await()
                        adapter.addAll(tweets)
                        updateListView(v, false)
                    }
                } catch (e: TwitterTaskException) {
                    world.notifyError(R.string.notice_error_get_user_timeline)
                }
                refreshLayout.isRefreshing = false
            }
        }

        v.tabhost.setup()
        val tab1 = v.tabhost.newTabSpec("tab1").setContent(R.id.tab1).setIndicator(getString(R.string.user_detail_tab_info))
        v.tabhost.addTab(tab1)
        val tab2 = v.tabhost.newTabSpec("tab2").setContent(R.id.user_detail_refresh_layout).setIndicator(getString(R.string.user_detail_tab_timeline))
        v.tabhost.addTab(tab2)
        v.tabhost.currentTab = 0

        updateUserDataBasic(v)
        updateUserDataDetail(v)

        v.listview_user_detail_timeline.adapter = adapter
        executeUserTimelineTask(v, adapter)

        return v
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        updateRelationship()
    }

    private fun executeUserTimelineTask(v: View, adapter: TimelineAdapter) {
        launchUi {
            v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.GONE
            try {
                val tweets = world.getUserTimelineAsync(user.id).await()
                adapter.addAll(tweets)
                adapter.updateForce()
                v.tabhost.tabWidget.getChildTabViewAt(1).visibility = View.VISIBLE
            } catch (e: TwitterTaskException) {
                world.notifyError(R.string.notice_error_get_user_timeline)
            }
        }
    }

    private fun updateUserDataBasic(v: View) {
        v.textview_user_detail_name.text = user.name
        v.textview_user_detail_screenname.text = user.screenName
        v.texttview_user_detail_protected.visibility = if (user.isProtected) View.VISIBLE else View.GONE
        Glide.with(this).load(user.profileImageUrl).into(v.imageview_user_detail_icon)
    }

    private fun updateUserDataDetail(v: View) {
        if (TextUtils.isEmpty(user.location)) {
            v.textview_user_detail_locate.visibility = View.GONE
        } else {
            v.textview_user_detail_locate.text = user.location
            v.textview_user_detail_locate.visibility = View.VISIBLE
        }
        if (TextUtils.isEmpty(user.url)) {
            v.textview_user_detail_url.visibility = View.GONE
        } else {
            v.textview_user_detail_url.text = user.url
            v.textview_user_detail_url.visibility = View.VISIBLE
        }
        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()
        v.textview_user_detail_follower_count.text = user.followersCount.toString()
        v.textview_user_detail_favorite_count.text = user.favoritesCount.toString()

        Glide.with(this).load(user.profileBannerUrl).into(v.imageview_user_detail_header)
    }

    private fun updateListView(v: View, addedToTop: Boolean) {
        val absListView = v.listview_user_detail_timeline
        val before = adapter.count
        adapter.notifyDataSetChanged() // synchronized call (not adapter#updateForce())
        val after = adapter.count
        val increments = after - before
        if (increments > 0) {
            if (addedToTop) {
                absListView.setSelection(increments + 1)
                absListView.smoothScrollToPositionFromTop(increments, 0)
                absListView.setSelection(increments)
            } else {
                absListView.smoothScrollToPositionFromTop(before, 0)
            }
        }
    }

    private fun updateRelationship() {
        if (user === world.user) {
            textview_user_detail_followed.setText(R.string.user_detail_followed_is_me)
            toggle_follow.visibility = View.GONE
        } else {
            toggle_follow.isEnabled = false
            toggle_follow.text = "..."
            launchUi {
                textview_user_detail_followed.setText(R.string.user_detail_loading)
                try {
                    val relationship = world.getRelationshipAsync(user.id).await()
                    val isFollowing = relationship.isSourceFollowingTarget
                    val isFollowed = relationship.isSourceFollowedByTarget
                    toggle_follow.isEnabled = true
                    toggle_follow.isActivated = isFollowing
                    toggle_follow.text = if (isFollowing) "Unfollow" else "Follow"
                    textview_user_detail_followed.setText(if (isFollowed) R.string.user_detail_followed else R.string.user_detail_not_followed)
                } catch (e: TwitterTaskException) {
                    world.notifyError("Failed to fetch relationship")
                }
            }
        }
    }

    companion object {
        private val KEY_USER_ID = "userID"

        fun newInstance(user: User): UserDetailDialogFragment {
            val obj = UserDetailDialogFragment()
            val bundle = Bundle()
            bundle.putLong(KEY_USER_ID, user.id)
            obj.arguments = bundle
            return obj
        }
    }
}