Skip to content

Commit

Permalink
Update ChattyAppViewModel to fetch rooms post connection
Browse files Browse the repository at this point in the history
+ Fetch and update the list of rooms for the authenticated user after the socket connection is successfully established.
+ Fix the static homescreen message - Show a different message while the server is booting up.
  • Loading branch information
aqib-m31 committed Aug 27, 2024
1 parent 86eba68 commit 6e213a2
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 33 deletions.
3 changes: 2 additions & 1 deletion app/src/main/java/com/aqib/chatty/ui/ChattyApp.kt
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ fun ChattyApp() {
isJoined = viewModel::isJoined,
onShare = { subject: String, roomId: String ->
shareRoomId(context = context, subject = subject, roomId = roomId)
}
},
message = userState.value.homeScreenMessage
)
}
composable(route = ChattyAppScreen.CreateRoom.name) {
Expand Down
70 changes: 40 additions & 30 deletions app/src/main/java/com/aqib/chatty/ui/ChattyAppViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ data class UserState(
val joinedRoom: Boolean = false,
val joinedRooms: UserRooms = UserRooms(own = emptyList(), others = emptyList()),
val errors: List<String> = emptyList(),
val message: String = ""
val message: String = "",
val homeScreenMessage: String = ""
)

/**
Expand Down Expand Up @@ -119,39 +120,47 @@ class ChattyAppViewModel(
_userState.update { currentState ->
currentState.copy(
username = username,
homeScreenMessage = "Establishing connection! It may take up to a minute!"
)
}
}
}
}

if (_userState.value.authenticated) {
try {
val rooms = userRepository.getRooms(_userState.value.accessToken, username)
_userState.update { currentState ->
currentState.copy(
joinedRooms = rooms
)
}
} catch (e: retrofit2.HttpException) {
Log.d("INIT RETRIEVE ROOMS", "${e.message}")
if (e.code() in 400..499) {
_userState.update { currentState ->
currentState.copy(
message = "FORBIDDEN",
authenticated = false
)
}
} else {
_userState.update { currentState ->
currentState.copy(
message = "SERVER ERROR"
)
}
}
} catch (e: IOException) {
Log.d("INIT RETRIEVE ROOMS", e.message.toString())
} catch (e: Exception) {
Log.d("INIT RETRIEVE ROOMS", e.message.toString())
/**
* Fetches the list of rooms for the authenticated user and updates the user state with the retrieved rooms.
* !! Called after the socket connection is successfully established.
*/
private fun fetchRooms() {
viewModelScope.launch {
try {
val rooms = userRepository.getRooms(_userState.value.accessToken, _userState.value.username)
_userState.update { currentState ->
currentState.copy(
joinedRooms = rooms,
homeScreenMessage = if (rooms.own.isEmpty() && rooms.others.isEmpty()) "You haven't joined any rooms!" else "",
)
}
} catch (e: retrofit2.HttpException) {
Log.d("INIT RETRIEVE ROOMS", "${e.message}")
if (e.code() in 400..499) {
_userState.update { currentState ->
currentState.copy(
message = "FORBIDDEN",
authenticated = false
)
}
} else {
_userState.update { currentState ->
currentState.copy(
message = "SERVER ERROR"
)
}
}
} catch (e: IOException) {
Log.d("INIT RETRIEVE ROOMS", e.message.toString())
} catch (e: Exception) {
Log.d("INIT RETRIEVE ROOMS", e.message.toString())
}
}
}
Expand Down Expand Up @@ -262,12 +271,13 @@ class ChattyAppViewModel(
socket?.connect()
setup()
_userState.update { currentState ->
currentState.copy(message = "Connecting... May be the server is booting up!")
currentState.copy(message = "Connecting... Server is booting up! It'll take upto a minute!")
}
socket?.on(Socket.EVENT_CONNECT) {
_userState.update { currentState ->
currentState.copy(message = "Connected Successfully!")
}
fetchRooms()
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions app/src/main/java/com/aqib/chatty/ui/screens/HomeScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ fun HomeScreen(
onDeleteRoom: (String) -> Unit,
isJoined: (String) -> Boolean,
onShare: (String, String) -> Unit,
message: String,
modifier: Modifier = Modifier
) {
Column(
Expand Down Expand Up @@ -121,7 +122,7 @@ fun HomeScreen(
.weight(1f)
.padding(vertical = dimensionResource(id = R.dimen.padding_medium))
) {
Text(text = "You haven't joined any rooms.")
Text(text = message)
}
} else {
LazyColumn(
Expand Down Expand Up @@ -268,7 +269,8 @@ fun HomeScreenPreview() {
onJoinRoom = {},
onDeleteRoom = {},
isJoined = { _ -> false },
onShare = { _, _ -> {} }
onShare = { _, _ -> {} },
message = ""
)
}
}

0 comments on commit 6e213a2

Please sign in to comment.