forked from raj-ship-it/Orate-for-ineuron
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HomeFragment.kt
299 lines (256 loc) · 9.06 KB
/
HomeFragment.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
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package live.hms.app2.ui.home
import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.MenuItem
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.activity.OnBackPressedCallback
import androidx.appcompat.app.AlertDialog
import androidx.core.widget.addTextChangedListener
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.navigation.fragment.findNavController
import live.hms.app2.BuildConfig
import live.hms.app2.R
import live.hms.app2.api.Status
import live.hms.app2.databinding.FragmentHomeBinding
import live.hms.app2.model.RoomDetails
import live.hms.app2.ui.meeting.LEAVE_INFORMATION_PERSON
import live.hms.app2.ui.meeting.LEAVE_INFORMATION_REASON
import live.hms.app2.ui.meeting.LEAVE_INFROMATION_WAS_END_ROOM
import live.hms.app2.ui.meeting.MeetingActivity
import live.hms.app2.ui.settings.SettingsMode
import live.hms.app2.ui.settings.SettingsStore
import live.hms.app2.util.*
import live.hms.app2.util.NameUtils.isValidUserName
class HomeFragment : Fragment() {
companion object {
private const val TAG = "HomeFragment"
}
private var binding by viewLifecycle<FragmentHomeBinding>()
private val homeViewModel: HomeViewModel by viewModels()
private lateinit var settings: SettingsStore
override fun onResume() {
super.onResume()
val data = requireActivity().intent.data
Log.v(TAG, "onResume: Trying to update $data into EditTextMeetingUrl")
data?.let {
if (it.toString().isNotEmpty()) {
val url = it.toString()
requireActivity().intent.data = null
if (saveTokenEndpointUrlIfValid(url) && isValidUserName(
binding.containerName,
binding.editTextName
)
) {
joinRoom()
}
}
}
val person = requireActivity().intent.getStringExtra(LEAVE_INFORMATION_PERSON)
val reason = requireActivity().intent.getStringExtra(LEAVE_INFORMATION_REASON)
val roomWasEnded = requireActivity().intent.getBooleanExtra(LEAVE_INFROMATION_WAS_END_ROOM, false)
if(person != null && reason != null){
requireActivity().intent.removeExtra(LEAVE_INFORMATION_PERSON)
requireActivity().intent.removeExtra(LEAVE_INFORMATION_REASON)
requireActivity().intent.removeExtra(LEAVE_INFROMATION_WAS_END_ROOM)
createForceLeaveDialog(person, reason, roomWasEnded)
}
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_settings -> {
findNavController().navigate(
HomeFragmentDirections.actionHomeFragmentToSettingsFragment(SettingsMode.HOME)
)
}
R.id.action_email_logs -> {
requireContext().startActivity(
EmailUtils.getNonFatalLogIntent(requireContext())
)
}
}
return false
}
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View {
binding = FragmentHomeBinding.inflate(inflater, container, false)
settings = SettingsStore(requireContext())
setHasOptionsMenu(true)
observeLiveData()
initEditTextViews()
initConnectButton()
initOnBackPress()
hideProgressBar()
return binding.root
}
private fun initOnBackPress() {
requireActivity().apply {
onBackPressedDispatcher.addCallback(
this@HomeFragment.viewLifecycleOwner,
object : OnBackPressedCallback(true) {
override fun handleOnBackPressed() {
Log.v(TAG, "initOnBackPress -> handleOnBackPressed")
finish()
}
})
}
}
@SuppressLint("SetTextI18n")
private fun updateProgressBarUI() {
val headingPrefix = "Fetching Token"
binding.progressBar.heading.text = "$headingPrefix ${getUsername()}..."
val descriptionDefaults = if (settings.publishVideo && settings.publishAudio) {
"Video and microphone will be turned on by default.\n"
} else if (settings.publishVideo && !settings.publishVideo) {
"Only audio will be turned on by default\n"
} else if (!settings.publishVideo && settings.publishVideo) {
"Only video will be turned on by default\n"
} else {
"Video and microphone will be turned off by default.\n"
}
val descriptionSetting = "You can change the defaults in the app settings."
binding.progressBar.description.text = descriptionDefaults + descriptionSetting
}
private fun showProgressBar() {
binding.buttonJoinMeeting.visibility = View.GONE
binding.containerCardJoin.visibility = View.GONE
binding.containerCardName.visibility = View.GONE
binding.progressBar.root.visibility = View.VISIBLE
binding.containerMeetingUrl.isEnabled = false
}
private fun hideProgressBar() {
binding.buttonJoinMeeting.visibility = View.VISIBLE
binding.containerCardJoin.visibility = View.VISIBLE
binding.containerCardName.visibility = View.VISIBLE
binding.progressBar.root.visibility = View.GONE
binding.containerMeetingUrl.isEnabled = true
}
private fun getUsername() = binding.editTextName.text.toString()
private fun joinRoom() {
homeViewModel.sendAuthTokenRequest(settings.lastUsedMeetingUrl)
}
private fun observeLiveData() {
homeViewModel.authTokenResponse.observe(viewLifecycleOwner) { response ->
when (response.status) {
Status.LOADING -> {
updateProgressBarUI()
showProgressBar()
}
Status.SUCCESS -> {
// No need to hide progress bar here, as we directly move to
// the next page
val data = response.data!!
val roomDetails = RoomDetails(
env = settings.environment,
url = settings.lastUsedMeetingUrl,
username = getUsername(),
authToken = data.token
)
Log.i(TAG, "Auth Token: ${roomDetails.authToken}")
// Start the meeting activity
startMeetingActivity(roomDetails)
requireActivity().finish()
}
Status.ERROR -> {
hideProgressBar()
Log.e(TAG, "observeLiveData: $response")
Toast.makeText(
requireContext(),
response.message,
Toast.LENGTH_LONG
).show()
}
}
}
}
private fun startMeetingActivity(roomDetails: RoomDetails) {
Intent(requireContext(), MeetingActivity::class.java).apply {
putExtra(ROOM_DETAILS, roomDetails)
startActivity(this)
}
}
private fun saveTokenEndpointUrlIfValid(url: String): Boolean {
if (url.isValidMeetingUrl()) {
settings.lastUsedMeetingUrl = url
binding.editTextMeetingUrl.setText(url)
settings.environment = url.getInitEndpointEnvironment()
return true
}
return false
}
private fun initEditTextViews() {
// Load the data if saved earlier (easy debugging)
binding.editTextName.setText(settings.username)
binding.editTextMeetingUrl.setText(settings.lastUsedMeetingUrl)
mapOf(
binding.editTextName to binding.containerName,
binding.editTextMeetingUrl to binding.containerMeetingUrl
).forEach {
it.key.addTextChangedListener { text ->
if (text.toString().isNotEmpty()) it.value.error = null
}
}
}
private fun initConnectButton() {
binding.buttonJoinMeeting.setOnClickListener {
try {
val input = binding.editTextMeetingUrl.text.toString()
if (saveTokenEndpointUrlIfValid(input) && isValidUserName(
binding.containerName,
binding.editTextName
)
) {
joinRoom()
settings.username = binding.editTextName.text.toString()
} else if (REGEX_MEETING_CODE.matches(input) && isValidUserName(
binding.containerName,
binding.editTextName
)
) {
var subdomain = BuildConfig.TOKEN_ENDPOINT.toSubdomain()
if (BuildConfig.INTERNAL) {
val env = when (settings.environment) {
ENV_PROD -> "prod2"
else -> "qa2"
}
subdomain = "$env.100ms.live"
}
val url = "https://$subdomain/meeting/$input"
saveTokenEndpointUrlIfValid(url)
joinRoom()
} else {
binding.containerMeetingUrl.error = "Invalid Meeting URL"
}
} catch (e: Exception) {
binding.containerMeetingUrl.error = e.message
}
}
}
private fun createForceLeaveDialog(removedBy : String, reason : String, wasRoomEnded : Boolean) {
val message = if(wasRoomEnded) {
"The room was ended by ${removedBy}.\nThe reason was $reason."
} else {
"You were removed from the room by ${removedBy}.\nThe reason was: $reason."
}
val title = if(wasRoomEnded) {
"Room Ended"
} else {
"Removed from the room"
}
val builder = AlertDialog.Builder(requireContext())
.setMessage(message)
.setTitle(title)
.setCancelable(false)
builder.setPositiveButton(R.string.ok) { dialog, _ ->
dialog.dismiss()
}
builder.create().apply { show() }
}
}