From 323b85f9b29e7104e96521868ac80958b63ec2f9 Mon Sep 17 00:00:00 2001 From: sangeetds Date: Sun, 25 Jul 2021 08:45:29 +0530 Subject: [PATCH] Added API calls to fetch users and groups. --- .../socialbox/chat/ui/ChatSettingsActivity.kt | 10 +++-- .../com/socialbox/chat/ui/ChatViewModel.kt | 14 ++++++- .../socialbox/group/data/GroupRepository.kt | 37 +++++++++---------- .../group/data/service/GroupService.kt | 4 ++ .../com/socialbox/group/ui/AddGroupDialog.kt | 2 +- .../com/socialbox/group/ui/GroupActivity.kt | 4 +- .../com/socialbox/group/ui/GroupViewModel.kt | 11 ++++-- .../socialbox/login/data/UserRepository.kt | 18 +++++++++ .../login/data/service/UserService.kt | 4 ++ 9 files changed, 73 insertions(+), 31 deletions(-) diff --git a/app/src/main/java/com/socialbox/chat/ui/ChatSettingsActivity.kt b/app/src/main/java/com/socialbox/chat/ui/ChatSettingsActivity.kt index 495d85a..d009ad4 100644 --- a/app/src/main/java/com/socialbox/chat/ui/ChatSettingsActivity.kt +++ b/app/src/main/java/com/socialbox/chat/ui/ChatSettingsActivity.kt @@ -34,8 +34,6 @@ class ChatSettingsActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(layout.activity_chat_settings) - // group?.users ?: setOf() Todo: Fetch Users for the group - setMembersLayout() setUpMovies() setUpObservers() @@ -96,9 +94,14 @@ class ChatSettingsActivity : AppCompatActivity() { val shareIntent = Intent.createChooser(sendIntent, null) startActivity(shareIntent) }) + + chatViewModel.membersState.observe(this, Observer { + val users = it ?: return@Observer + setMembersLayout(users) + }) } - private fun setMembersLayout() { + private fun setMembersLayout(groupMembers: List = listOf()) { val memberOne: MaterialCardView = findViewById(id.memberOne) val memberTwo: MaterialCardView = findViewById(id.memberTwo) val memberThree: MaterialCardView = findViewById(id.memberThree) @@ -108,7 +111,6 @@ class ChatSettingsActivity : AppCompatActivity() { val layout = findViewById(id.membersConstrainView) val userName: MaterialTextView = findViewById(id.userName) val viewAllMembers: MaterialTextView = findViewById(id.viewAllMembers) - val groupMembers = setOf() // Todo: Fetch Users if (groupMembers.size < 6) memberSix.visibility = View.GONE if (groupMembers.size < 5) { diff --git a/app/src/main/java/com/socialbox/chat/ui/ChatViewModel.kt b/app/src/main/java/com/socialbox/chat/ui/ChatViewModel.kt index 6539ba9..f7eb3d2 100644 --- a/app/src/main/java/com/socialbox/chat/ui/ChatViewModel.kt +++ b/app/src/main/java/com/socialbox/chat/ui/ChatViewModel.kt @@ -7,12 +7,17 @@ import androidx.lifecycle.viewModelScope import com.socialbox.chat.data.model.Invite import com.socialbox.common.enums.Result.Success import com.socialbox.group.data.GroupRepository +import com.socialbox.login.data.model.User import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import javax.inject.Inject @HiltViewModel -class ChatViewModel @Inject constructor(private val groupRepository: GroupRepository) : ViewModel() { +class ChatViewModel @Inject constructor(private val groupRepository: GroupRepository) : + ViewModel() { + + private val _membersState = MutableLiveData>() + val membersState: LiveData> = _membersState private val _inviteState = MutableLiveData() val inviteState: LiveData = _inviteState @@ -23,4 +28,11 @@ class ChatViewModel @Inject constructor(private val groupRepository: GroupReposi _inviteState.value = result.data } } + + fun getMembers(groupId: Int) = viewModelScope.launch { + val result = groupRepository.getUsers(groupId) + if (result is Success) { + _membersState.value = result.data + } + } } diff --git a/app/src/main/java/com/socialbox/group/data/GroupRepository.kt b/app/src/main/java/com/socialbox/group/data/GroupRepository.kt index 804a2fa..dc258ef 100644 --- a/app/src/main/java/com/socialbox/group/data/GroupRepository.kt +++ b/app/src/main/java/com/socialbox/group/data/GroupRepository.kt @@ -16,23 +16,6 @@ class GroupRepository @Inject constructor(private val groupService: GroupService private val errorString = "Error in connecting to the server" - suspend fun getGroupsForUser(groupId: List) = - try { - Timber.i("Fetching groups for $groupId") - groupService.getGroupsForUser(groupId).run { - if (isSuccessful && body() != null) { - Timber.i("Successfully fetched groups from server.") - Success(body()!!) - } else { - Timber.e(errorBody()?.stringSuspending()) - Error(Exception(errorBody()?.stringSuspending())) - } - } - } catch (exception: SocketTimeoutException) { - Timber.e(errorString) - Error(Exception(errorString)) - } - suspend fun createGroup(group: Group) = try { val saveGroup = groupService.saveGroup(group) saveGroup.run { @@ -42,8 +25,9 @@ class GroupRepository @Inject constructor(private val groupService: GroupService Success(body()!!) } else -> { - Timber.d("Failed in saving group with error: ${errorBody()?.stringSuspending()}") - Error(Exception(errorBody()?.stringSuspending())) + val errorMessage = errorBody()?.stringSuspending() + Timber.d("Failed in saving group with error: $errorMessage") + Error(Exception(errorMessage)) } } } @@ -107,4 +91,19 @@ class GroupRepository @Inject constructor(private val groupService: GroupService Timber.d(errorString) Error(Exception("Error adding user.")) } + + suspend fun getUsers(groupId: Int) = try { + this.groupService.getUsers(groupId).run { + if (isSuccessful && body() != null) { + Timber.i("Successfully added User to Group.") + Created(body()!!) + } else { + Timber.d("Error in adding user to the group.") + Error(Exception(errorBody()?.stringSuspending())) + } + } + } catch (exception: SocketTimeoutException) { + Timber.d(errorString) + Error(Exception("Error adding user.")) + } } diff --git a/app/src/main/java/com/socialbox/group/data/service/GroupService.kt b/app/src/main/java/com/socialbox/group/data/service/GroupService.kt index 506dc08..e18d9a7 100644 --- a/app/src/main/java/com/socialbox/group/data/service/GroupService.kt +++ b/app/src/main/java/com/socialbox/group/data/service/GroupService.kt @@ -3,6 +3,7 @@ package com.socialbox.group.data.service import com.socialbox.chat.data.model.Invite import com.socialbox.group.data.model.Group import com.socialbox.group.data.model.GroupMovie +import com.socialbox.login.data.model.User import retrofit2.Response import retrofit2.http.Body import retrofit2.http.GET @@ -35,4 +36,7 @@ interface GroupService { @Path("id") groupId: Int, @Query("userId") userId: Int? ): Response + + @GET("/group/{id}/users") + suspend fun getUsers(@Path("id") groupId: Int): Response> } \ No newline at end of file diff --git a/app/src/main/java/com/socialbox/group/ui/AddGroupDialog.kt b/app/src/main/java/com/socialbox/group/ui/AddGroupDialog.kt index df04812..149a72f 100644 --- a/app/src/main/java/com/socialbox/group/ui/AddGroupDialog.kt +++ b/app/src/main/java/com/socialbox/group/ui/AddGroupDialog.kt @@ -58,7 +58,7 @@ class AddGroupDialog : com.socialbox.group.ui.BottomSheetDialog() { Timber.i("Adding Group ${newGroup.name} with id: ${newGroup.id} and userId: ${user.id}") user.groups.add(newGroup) - groupViewModel.getGroupsForUser(user.groups.map { g -> g.id!! }) + groupViewModel.getGroupsForUser(user.id!!) val intent = GroupDetailsActivity.createIntent(requireContext(), newGroup, user) startActivity(intent) dismiss() diff --git a/app/src/main/java/com/socialbox/group/ui/GroupActivity.kt b/app/src/main/java/com/socialbox/group/ui/GroupActivity.kt index bc24ebc..6f69e14 100644 --- a/app/src/main/java/com/socialbox/group/ui/GroupActivity.kt +++ b/app/src/main/java/com/socialbox/group/ui/GroupActivity.kt @@ -123,7 +123,7 @@ class GroupActivity : AppCompatActivity() { } private fun setUpObservables() { - groupViewModel.getGroupsForUser(user.groups.map { it.id!! }) + groupViewModel.getGroupsForUser(user.id!!) groupViewModel.groupListState.observe(this@GroupActivity, Observer { val result = it ?: return@Observer @@ -137,7 +137,7 @@ class GroupActivity : AppCompatActivity() { created?.let { group -> if (invitedGroupId != -1) { user.groups.add(group) - groupViewModel.getGroupsForUser(user.groups.map { g -> g.id!! }) + groupViewModel.getGroupsForUser(user.id!!) val intent = GroupDetailsActivity.createIntent(this@GroupActivity, group, user) startActivity(intent) } diff --git a/app/src/main/java/com/socialbox/group/ui/GroupViewModel.kt b/app/src/main/java/com/socialbox/group/ui/GroupViewModel.kt index 1b7746a..72f1739 100644 --- a/app/src/main/java/com/socialbox/group/ui/GroupViewModel.kt +++ b/app/src/main/java/com/socialbox/group/ui/GroupViewModel.kt @@ -7,17 +7,20 @@ import androidx.lifecycle.viewModelScope import com.socialbox.common.enums.Result.Created import com.socialbox.common.enums.Result.Error import com.socialbox.common.enums.Result.Success -import com.socialbox.login.ui.LoginResult import com.socialbox.group.data.GroupRepository import com.socialbox.group.data.model.Group import com.socialbox.group.data.model.GroupMovie +import com.socialbox.login.data.UserRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.launch import timber.log.Timber import javax.inject.Inject @HiltViewModel -class GroupViewModel @Inject constructor(private val groupRepository: GroupRepository) : +class GroupViewModel @Inject constructor( + private val groupRepository: GroupRepository, + private val userRepository: UserRepository +) : ViewModel() { private val cachedGroups: MutableList = mutableListOf() @@ -27,8 +30,8 @@ class GroupViewModel @Inject constructor(private val groupRepository: GroupRepos private val _groupState = MutableLiveData() val groupState: LiveData = _groupState - fun getGroupsForUser(groupId: List) = viewModelScope.launch { - val groups = groupRepository.getGroupsForUser(groupId) + fun getGroupsForUser(userId: Int) = viewModelScope.launch { + val groups = userRepository.getGroupsForUser(userId) _groupListState.value = when (groups) { is Success -> { cachedGroups.clear() diff --git a/app/src/main/java/com/socialbox/login/data/UserRepository.kt b/app/src/main/java/com/socialbox/login/data/UserRepository.kt index 6d09249..99a4106 100644 --- a/app/src/main/java/com/socialbox/login/data/UserRepository.kt +++ b/app/src/main/java/com/socialbox/login/data/UserRepository.kt @@ -79,4 +79,22 @@ class UserRepository @Inject constructor(private val userService: UserService) { Timber.e("Error while fetching in with error: $exception") Error(Exception("Server Down. Please try again.")) } + + suspend fun getGroupsForUser(userId: Int) = + try { + Timber.i("Fetching groups for $userId") + userService.getGroupsForUser(userId).run { + if (isSuccessful && body() != null) { + Timber.i("Successfully fetched groups from server.") + Success(body()!!) + } else { + val message = errorBody()?.stringSuspending() + Timber.e(message) + Error(Exception(message)) + } + } + } catch (exception: SocketTimeoutException) { + Timber.e("Error while fetching in with error: $exception") + Error(Exception("Server Down. Please try again.")) + } } diff --git a/app/src/main/java/com/socialbox/login/data/service/UserService.kt b/app/src/main/java/com/socialbox/login/data/service/UserService.kt index 0198b39..75489b7 100644 --- a/app/src/main/java/com/socialbox/login/data/service/UserService.kt +++ b/app/src/main/java/com/socialbox/login/data/service/UserService.kt @@ -1,5 +1,6 @@ package com.socialbox.login.data.service +import com.socialbox.group.data.model.Group import com.socialbox.login.data.model.User import com.socialbox.movie.data.model.UserMovie import retrofit2.Response @@ -18,4 +19,7 @@ interface UserService { @POST("/user/{id}/settings") suspend fun updateSettings(@Body user: User, @Path("id") id: Int): Response + + @GET("/user/{id}/groups") + suspend fun getGroupsForUser(@Path("id") userId: Int): Response> } \ No newline at end of file