Skip to content

Commit

Permalink
Added API calls to fetch users and groups.
Browse files Browse the repository at this point in the history
  • Loading branch information
sangeetds committed Jul 25, 2021
1 parent ef714fc commit 323b85f
Show file tree
Hide file tree
Showing 9 changed files with 73 additions and 31 deletions.
10 changes: 6 additions & 4 deletions app/src/main/java/com/socialbox/chat/ui/ChatSettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<User> = listOf()) {
val memberOne: MaterialCardView = findViewById(id.memberOne)
val memberTwo: MaterialCardView = findViewById(id.memberTwo)
val memberThree: MaterialCardView = findViewById(id.memberThree)
Expand All @@ -108,7 +111,6 @@ class ChatSettingsActivity : AppCompatActivity() {
val layout = findViewById<ConstraintLayout>(id.membersConstrainView)
val userName: MaterialTextView = findViewById(id.userName)
val viewAllMembers: MaterialTextView = findViewById(id.viewAllMembers)
val groupMembers = setOf<User>() // Todo: Fetch Users

if (groupMembers.size < 6) memberSix.visibility = View.GONE
if (groupMembers.size < 5) {
Expand Down
14 changes: 13 additions & 1 deletion app/src/main/java/com/socialbox/chat/ui/ChatViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<User>>()
val membersState: LiveData<List<User>> = _membersState

private val _inviteState = MutableLiveData<Invite>()
val inviteState: LiveData<Invite> = _inviteState
Expand All @@ -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
}
}
}
37 changes: 18 additions & 19 deletions app/src/main/java/com/socialbox/group/data/GroupRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int>) =
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 {
Expand All @@ -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))
}
}
}
Expand Down Expand Up @@ -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."))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -35,4 +36,7 @@ interface GroupService {
@Path("id") groupId: Int,
@Query("userId") userId: Int?
): Response<Group>

@GET("/group/{id}/users")
suspend fun getUsers(@Path("id") groupId: Int): Response<List<User>>
}
2 changes: 1 addition & 1 deletion app/src/main/java/com/socialbox/group/ui/AddGroupDialog.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
4 changes: 2 additions & 2 deletions app/src/main/java/com/socialbox/group/ui/GroupActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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)
}
Expand Down
11 changes: 7 additions & 4 deletions app/src/main/java/com/socialbox/group/ui/GroupViewModel.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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<Group> = mutableListOf()
Expand All @@ -27,8 +30,8 @@ class GroupViewModel @Inject constructor(private val groupRepository: GroupRepos
private val _groupState = MutableLiveData<Group>()
val groupState: LiveData<Group> = _groupState

fun getGroupsForUser(groupId: List<Int>) = 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()
Expand Down
18 changes: 18 additions & 0 deletions app/src/main/java/com/socialbox/login/data/UserRepository.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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."))
}
}
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -18,4 +19,7 @@ interface UserService {

@POST("/user/{id}/settings")
suspend fun updateSettings(@Body user: User, @Path("id") id: Int): Response<User>

@GET("/user/{id}/groups")
suspend fun getGroupsForUser(@Path("id") userId: Int): Response<List<Group>>
}

0 comments on commit 323b85f

Please sign in to comment.