Files
android-lchat/app/src/main/java/com/mattintech/lchat/ui/LobbyFragment.kt

137 lines
5.0 KiB
Kotlin
Raw Normal View History

2025-07-03 17:52:05 -04:00
package com.mattintech.lchat.ui
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Toast
import androidx.fragment.app.Fragment
2025-07-03 21:13:23 -04:00
import androidx.fragment.app.viewModels
2025-07-03 17:52:05 -04:00
import androidx.navigation.fragment.findNavController
import com.mattintech.lchat.R
import com.mattintech.lchat.databinding.FragmentLobbyBinding
2025-07-03 20:21:11 -04:00
import com.mattintech.lchat.viewmodel.LobbyEvent
import com.mattintech.lchat.viewmodel.LobbyState
import com.mattintech.lchat.viewmodel.LobbyViewModel
2025-07-03 17:52:05 -04:00
import com.mattintech.lchat.utils.LOG_PREFIX
2025-07-03 21:13:23 -04:00
import dagger.hilt.android.AndroidEntryPoint
2025-07-03 17:52:05 -04:00
2025-07-03 21:13:23 -04:00
@AndroidEntryPoint
2025-07-03 17:52:05 -04:00
class LobbyFragment : Fragment() {
companion object {
private const val TAG = LOG_PREFIX + "LobbyFragment:"
}
private var _binding: FragmentLobbyBinding? = null
private val binding get() = _binding!!
2025-07-03 21:13:23 -04:00
private val viewModel: LobbyViewModel by viewModels()
2025-07-03 17:52:05 -04:00
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View {
Log.d(TAG, "onCreateView")
_binding = FragmentLobbyBinding.inflate(inflater, container, false)
return binding.root
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
Log.d(TAG, "onViewCreated")
setupUI()
2025-07-03 20:21:11 -04:00
observeViewModel()
2025-07-03 17:52:05 -04:00
}
private fun setupUI() {
binding.modeRadioGroup.setOnCheckedChangeListener { _, checkedId ->
when (checkedId) {
R.id.hostRadio -> {
binding.roomLayout.visibility = View.VISIBLE
binding.actionButton.text = getString(R.string.start_hosting)
binding.roomsRecyclerView.visibility = View.GONE
binding.noRoomsText.visibility = View.GONE
}
R.id.clientRadio -> {
binding.roomLayout.visibility = View.GONE
binding.actionButton.text = getString(R.string.search_rooms)
binding.roomsRecyclerView.visibility = View.VISIBLE
}
}
}
binding.actionButton.setOnClickListener {
2025-07-03 20:21:11 -04:00
val userName = binding.nameInput.text?.toString()?.trim() ?: ""
2025-07-03 17:52:05 -04:00
when (binding.modeRadioGroup.checkedRadioButtonId) {
R.id.hostRadio -> {
2025-07-03 20:21:11 -04:00
val roomName = binding.roomInput.text?.toString()?.trim() ?: ""
viewModel.startHostMode(roomName, userName)
2025-07-03 17:52:05 -04:00
}
R.id.clientRadio -> {
2025-07-03 20:21:11 -04:00
viewModel.startClientMode(userName)
}
}
}
}
private fun observeViewModel() {
viewModel.state.observe(viewLifecycleOwner) { state ->
when (state) {
is LobbyState.Idle -> {
binding.noRoomsText.visibility = View.GONE
}
is LobbyState.Connecting -> {
if (binding.modeRadioGroup.checkedRadioButtonId == R.id.clientRadio) {
binding.noRoomsText.visibility = View.VISIBLE
binding.noRoomsText.text = getString(R.string.connecting)
}
}
is LobbyState.Connected -> {
if (binding.modeRadioGroup.checkedRadioButtonId == R.id.clientRadio) {
val userName = binding.nameInput.text?.toString()?.trim() ?: ""
viewModel.onConnectedToRoom(state.roomName, userName)
}
}
is LobbyState.Error -> {
binding.noRoomsText.visibility = View.VISIBLE
binding.noRoomsText.text = state.message
Toast.makeText(context, state.message, Toast.LENGTH_LONG).show()
2025-07-03 17:52:05 -04:00
}
}
}
2025-07-03 20:21:11 -04:00
viewModel.events.observe(viewLifecycleOwner) { event ->
when (event) {
is LobbyEvent.NavigateToChat -> {
navigateToChat(event.roomName, event.userName, event.isHost)
viewModel.clearEvent()
}
is LobbyEvent.ShowError -> {
Toast.makeText(context, event.message, Toast.LENGTH_SHORT).show()
viewModel.clearEvent()
2025-07-03 17:52:05 -04:00
}
2025-07-03 20:21:11 -04:00
null -> {}
2025-07-03 17:52:05 -04:00
}
}
}
private fun navigateToChat(roomName: String, userName: String, isHost: Boolean) {
Log.d(TAG, "Navigating to chat - room: $roomName, user: $userName, isHost: $isHost")
val action = LobbyFragmentDirections.actionLobbyToChat(
roomName = roomName,
userName = userName,
isHost = isHost
)
findNavController().navigate(action)
}
override fun onDestroyView() {
super.onDestroyView()
_binding = null
}
}