-
Notifications
You must be signed in to change notification settings - Fork 5
/
ChatFragment.kt
204 lines (179 loc) · 7.8 KB
/
ChatFragment.kt
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
package com.majeur.psclient.ui
import android.app.AlertDialog
import android.content.Context
import android.content.DialogInterface
import android.os.Bundle
import android.text.Spanned
import android.text.method.LinkMovementMethod
import android.view.*
import android.view.inputmethod.EditorInfo
import android.view.inputmethod.InputMethodManager
import android.widget.ArrayAdapter
import android.widget.TextView
import com.majeur.psclient.R
import com.majeur.psclient.databinding.FragmentChatBinding
import com.majeur.psclient.io.AssetLoader
import com.majeur.psclient.io.GlideHelper
import com.majeur.psclient.model.ChatRoomInfo
import com.majeur.psclient.service.ShowdownService
import com.majeur.psclient.service.observer.ChatRoomMessageObserver
import com.majeur.psclient.util.Callback
import com.majeur.psclient.util.Utils
import com.majeur.psclient.util.html.Html
import com.majeur.psclient.util.toId
class ChatFragment : BaseFragment(), ChatRoomMessageObserver.UiCallbacks {
private val observer get() = service!!.chatMessageObserver
private lateinit var inputMethodManager: InputMethodManager
private lateinit var glideHelper: GlideHelper
private lateinit var assetLoader: AssetLoader
private var _binding: FragmentChatBinding? = null
private val binding get() = _binding!!
private var _observedRoomId: String? = null
var observedRoomId: String?
get() = _observedRoomId
set(observedRoomId) {
_observedRoomId = observedRoomId
observer.observedRoomId = observedRoomId
}
override fun onAttach(context: Context) {
super.onAttach(context)
inputMethodManager = context.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
glideHelper = mainActivity.glideHelper
assetLoader = mainActivity.assetLoader
}
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
_binding = FragmentChatBinding.inflate(inflater, container, false)
return binding.root
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
service?.chatMessageObserver?.uiCallbacks = null
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding.chatLog.apply {
movementMethod = LinkMovementMethod()
animate().duration = 200
}
binding.joinButton.setOnClickListener {
if (service?.isConnected != true) return@setOnClickListener
if (observer.roomJoined) service?.sendRoomCommand(observedRoomId, "leave")
else service?.sendGlobalCommand("cmd", "rooms")
}
binding.usersCount.setOnClickListener { v: View ->
val adapter = ArrayAdapter(requireActivity(), android.R.layout.simple_list_item_1, observer.users)
AlertDialog.Builder(requireActivity())
.setTitle("Users")
.setAdapter(adapter) { dialog: DialogInterface, pos: Int ->
service?.sendGlobalCommand("cmd userdetails", adapter.getItem(pos)!!.toId())
dialog.dismiss()
}
.setNegativeButton("Close", null)
.show()
}
binding.sendButton.setOnClickListener { sendMessageIfAny() }
binding.messageInput.setOnEditorActionListener { _: TextView?, actionId: Int, _: KeyEvent? ->
if (actionId == EditorInfo.IME_ACTION_SEND) {
sendMessageIfAny()
return@setOnEditorActionListener true
}
false
}
setUiState(roomJoined = false)
}
private fun setUiState(roomJoined: Boolean) {
if (roomJoined) {
binding.apply {
messageInput.isEnabled = true
messageInput.requestFocus()
sendButton.isEnabled = true
sendButton.drawable.alpha = 255
joinButton.setImageResource(R.drawable.ic_exit)
chatLog.gravity = Gravity.START
chatLog.setText("", TextView.BufferType.EDITABLE)
}
} else {
binding.apply {
roomTitle.text = "—"
usersCount.text = "-\nusers"
messageInput.text.clear()
messageInput.clearFocus()
messageInput.isEnabled = false
sendButton.isEnabled = false
sendButton.drawable.alpha = 128
joinButton.setImageResource(R.drawable.ic_enter)
joinButton.requestFocus() // Remove focus from message input widget
chatLog.animate().alpha(0f).withEndAction {
chatLog.text = "\n\n\n\n\n\n\n\n\n\nTap the join button to join a room"
chatLog.gravity = Gravity.CENTER_HORIZONTAL
chatLog.animate().alpha(1f).withEndAction(null).start()
}.start()
}
inputMethodManager.hideSoftInputFromWindow(binding.messageInput.windowToken, 0)
}
}
private fun sendMessageIfAny() {
val message = binding.messageInput.text.toString()
if (message.isEmpty()) return
service?.sendRoomMessage(observedRoomId, message)
binding.messageInput.text.clear()
}
override fun onServiceBound(service: ShowdownService) {
super.onServiceBound(service)
service.chatMessageObserver.uiCallbacks = this
}
override fun onServiceWillUnbound(service: ShowdownService) {
super.onServiceWillUnbound(service)
service.chatMessageObserver.uiCallbacks = null
}
fun onAvailableRoomsChanged(officialRooms: List<ChatRoomInfo>, chatRooms: List<ChatRoomInfo>) {
if (requireFragmentManager().findFragmentByTag(JoinChatRoomDialog.FRAGMENT_TAG) == null)
JoinChatRoomDialog.newInstance(officialRooms, chatRooms)
.show(requireFragmentManager(), JoinChatRoomDialog.FRAGMENT_TAG)
}
private fun notifyNewMessageReceived() {
mainActivity.showBadge(id)
}
private fun postFullScroll() {
binding.chatLogContainer.post { binding.chatLogContainer.fullScroll(View.FOCUS_DOWN) }
}
override fun onRoomInit() {
setUiState(roomJoined = true)
}
override fun onRoomDeInit() {
setUiState(roomJoined = false)
}
override fun onPrintText(text: CharSequence) {
val fullScrolled = Utils.fullScrolled(binding.chatLogContainer)
if (binding.chatLog.length() > 0) binding.chatLog.append("\n")
binding.chatLog.append(text)
notifyNewMessageReceived()
if (fullScrolled) postFullScroll()
}
override fun onPrintHtml(html: String) {
val mark = Any()
val l = binding.chatLog.length()
binding.chatLog.append("\u200C")
binding.chatLog.editableText.setSpan(mark, l, l + 1, Spanned.SPAN_MARK_MARK)
Html.fromHtml(html,
Html.FROM_HTML_MODE_COMPACT,
glideHelper.getHtmlImageGetter(assetLoader, binding.chatLog.width),
Callback { spanned: Spanned? ->
val at = binding.chatLog.editableText.getSpanStart(mark)
if (at == -1) return@Callback // Check if text has been cleared
val fullScrolled = Utils.fullScrolled(binding.chatLogContainer)
binding.chatLog.editableText
.insert(at, "\n")
.insert(at + 1, spanned)
notifyNewMessageReceived()
if (fullScrolled) postFullScroll()
})
}
override fun onRoomTitleChanged(title: String) {
binding.roomTitle.text = title
}
override fun onUpdateUsers(users: List<String>) {
binding.usersCount.text = "${users.size} users"
}
}