Skip to content
Snippets Groups Projects
Commit b223e8ab authored by Taras's avatar Taras
Browse files

Add loading for follow/unfollow on user fragment

parent 663d4ff4
No related branches found
No related tags found
No related merge requests found
......@@ -120,7 +120,7 @@ class UserDialogFragment : BaseFullscreenDialogFragment(DialogFragmentUserBindin
}
}
viewModel.userLiveData.observeData(this) { setupUserInfo(it) }
viewModel.timelineLiveDataLiveData.observeData(this) {
viewModel.usersTimelinesLiveData.observeData(this) {
usersCirclesAdapter.submitList(it)
}
viewModel.requestFollowLiveData.observeResponse(this,
......
package org.futo.circles.core.feature.user
import androidx.lifecycle.MediatorLiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.SavedStateHandle
import androidx.lifecycle.ViewModel
......@@ -15,7 +16,9 @@ import org.futo.circles.core.extensions.launchUi
import org.futo.circles.core.feature.room.RoomRelationsBuilder
import org.futo.circles.core.feature.room.invite.ManageInviteRequestsDataSource
import org.futo.circles.core.feature.workspace.SharedCircleDataSource
import org.futo.circles.core.model.TimelineHeaderItem
import org.futo.circles.core.model.TimelineListItem
import org.futo.circles.core.model.TimelineRoomListItem
import org.futo.circles.core.provider.MatrixSessionProvider
import org.matrix.android.sdk.api.session.getRoom
import javax.inject.Inject
......@@ -34,7 +37,7 @@ class UserViewModel @Inject constructor(
private val profileRoomId = sharedCircleDataSource.getSharedCirclesSpaceId() ?: ""
val userLiveData = userDataSource.userLiveData
val timelineLiveDataLiveData = MutableLiveData<List<TimelineListItem>>()
val requestFollowLiveData = SingleEventLiveData<Response<Unit?>>()
val inviteToConnectLiveData = SingleEventLiveData<Response<Unit?>>()
val ignoreUserLiveData = SingleEventLiveData<Response<Unit?>>()
......@@ -44,6 +47,29 @@ class UserViewModel @Inject constructor(
it.firstOrNull { it.userId == userId } != null
}
private val timelineLiveData = MutableLiveData<List<TimelineListItem>>()
private val loadingItemsIdsList = MutableLiveData<Set<String>>(emptySet())
val usersTimelinesLiveData = MediatorLiveData<List<TimelineListItem>>().also {
it.addSource(loadingItemsIdsList) { loadingItemsValue ->
val currentList = it.value ?: emptyList()
it.postValue(
currentList.map { item ->
when (item) {
is TimelineRoomListItem -> item.copy(
isLoading = loadingItemsValue.contains(item.id)
)
is TimelineHeaderItem -> item
}
}
)
}
it.addSource(timelineLiveData) { value ->
it.postValue(value)
}
}
init {
getUsersTimelines()
}
......@@ -51,16 +77,18 @@ class UserViewModel @Inject constructor(
private fun getUsersTimelines() {
launchUi {
userDataSource.getTimelinesFlow().collectLatest {
timelineLiveDataLiveData.postValue(it)
timelineLiveData.postValue(it)
}
}
}
fun requestFollowTimeline(timelineId: String) {
launchBg {
toggleItemLoading(timelineId)
val result = createResult {
MatrixSessionProvider.currentSession?.roomService()?.knock(timelineId)
}
toggleItemLoading(timelineId)
requestFollowLiveData.postValue(result)
}
}
......@@ -68,8 +96,10 @@ class UserViewModel @Inject constructor(
fun unFollowTimeline(timelineId: String) {
launchBg {
createResult {
toggleItemLoading(timelineId)
roomRelationsBuilder.removeFromAllParents(timelineId)
MatrixSessionProvider.currentSession?.roomService()?.leaveRoom(timelineId)
toggleItemLoading(timelineId)
}
}
}
......@@ -109,4 +139,13 @@ class UserViewModel @Inject constructor(
}
}
private fun toggleItemLoading(id: String) {
val currentSet = loadingItemsIdsList.value?.toMutableSet() ?: return
val newLoadingSet = currentSet.apply {
if (this.contains(id)) remove(id)
else add(id)
}
loadingItemsIdsList.postValue(newLoadingSet)
}
}
\ No newline at end of file
......@@ -39,8 +39,9 @@ class UsersTimelineRoomViewHolder(
with(binding) {
tvTimelineName.text = data.info.title
ivTimelineImage.loadRoomProfileIcon(data.info.avatarUrl, data.info.title)
btnFollow.setIsVisible(!data.isJoined)
btnUnFollow.setIsVisible(data.isJoined)
vLoading.setIsVisible(data.isLoading)
btnFollow.setIsVisible(!data.isJoined && !data.isLoading)
btnUnFollow.setIsVisible(data.isJoined && !data.isLoading)
}
}
}
......
......@@ -23,7 +23,8 @@ data class TimelineHeaderItem(
data class TimelineRoomListItem(
override val id: String,
val info: RoomInfo,
val isJoined: Boolean
val isJoined: Boolean,
val isLoading: Boolean = false
) : TimelineListItem()
fun RoomSummary.toTimelineRoomListItem() = TimelineRoomListItem(
......
......@@ -62,4 +62,14 @@
app:layout_constraintTop_toTopOf="parent"
tools:visibility="visible" />
<ProgressBar
android:id="@+id/vLoading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment