From c7635be2345f4696e53a202fefe158c35613eb02 Mon Sep 17 00:00:00 2001 From: Taras Smakula <tarassmakula@gmail.com> Date: Wed, 20 Dec 2023 17:55:36 +0200 Subject: [PATCH] Create separate screen for ignored users --- .../ignored/IgnoredUsersDialogFragment.kt | 56 +++++++++++++++++++ .../feature/ignored/IgnoredUsersViewModel.kt | 31 ++++++++++ .../ignored/list/IgnoredUsersAdapter.kt | 24 ++++++++ .../ignored/list/IgnoredUsersViewHolder.kt | 26 +++++++++ .../layout/dialog_fragment_ignored_users.xml | 40 +++++++++++++ 5 files changed, 177 insertions(+) create mode 100644 app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersDialogFragment.kt create mode 100644 app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersViewModel.kt create mode 100644 app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersAdapter.kt create mode 100644 app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersViewHolder.kt create mode 100644 app/src/main/res/layout/dialog_fragment_ignored_users.xml diff --git a/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersDialogFragment.kt b/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersDialogFragment.kt new file mode 100644 index 000000000..52ff95be2 --- /dev/null +++ b/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersDialogFragment.kt @@ -0,0 +1,56 @@ +package org.futo.circles.feature.ignored + +import android.os.Bundle +import android.view.View +import androidx.fragment.app.viewModels +import androidx.recyclerview.widget.DividerItemDecoration +import dagger.hilt.android.AndroidEntryPoint +import org.futo.circles.R +import org.futo.circles.core.base.fragment.BaseFullscreenDialogFragment +import org.futo.circles.core.extensions.observeData +import org.futo.circles.core.extensions.observeResponse +import org.futo.circles.core.view.EmptyTabPlaceholderView +import org.futo.circles.databinding.DialogFragmentIgnoredUsersBinding +import org.futo.circles.feature.ignored.list.IgnoredUsersAdapter + +@AndroidEntryPoint +class IgnoredUsersDialogFragment : + BaseFullscreenDialogFragment(DialogFragmentIgnoredUsersBinding::inflate) { + + private val viewModel by viewModels<IgnoredUsersViewModel>() + + private val binding by lazy { + getBinding() as DialogFragmentIgnoredUsersBinding + } + + private val usersAdapter by lazy { + IgnoredUsersAdapter( + onUnIgnore = { userId -> viewModel.unIgnoreUser(userId) } + ) + } + + override fun onViewCreated(view: View, savedInstanceState: Bundle?) { + super.onViewCreated(view, savedInstanceState) + setupViews() + setupObservers() + } + + private fun setupViews() { + binding.rvIgnoredUsers.apply { + setEmptyView(EmptyTabPlaceholderView(requireContext()).apply { + setText(getString(R.string.ignored_users_empty_message)) + setArrowVisible(false) + }) + adapter = usersAdapter + addItemDecoration(DividerItemDecoration(context, DividerItemDecoration.VERTICAL)) + } + } + + private fun setupObservers() { + viewModel.ignoredUsersLiveData.observeData(this) { + usersAdapter.submitList(it) + } + viewModel.unIgnoreUserLiveData.observeResponse(this) + } + +} diff --git a/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersViewModel.kt b/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersViewModel.kt new file mode 100644 index 000000000..b4ae32aa4 --- /dev/null +++ b/app/src/main/java/org/futo/circles/feature/ignored/IgnoredUsersViewModel.kt @@ -0,0 +1,31 @@ +package org.futo.circles.feature.ignored + +import androidx.lifecycle.ViewModel +import androidx.lifecycle.map +import dagger.hilt.android.lifecycle.HiltViewModel +import org.futo.circles.core.base.SingleEventLiveData +import org.futo.circles.core.extensions.Response +import org.futo.circles.core.extensions.launchBg +import org.futo.circles.core.mapping.toCirclesUserSummary +import org.futo.circles.core.provider.MatrixSessionProvider +import org.futo.circles.feature.people.UserOptionsDataSource +import javax.inject.Inject + +@HiltViewModel +class IgnoredUsersViewModel @Inject constructor( + private val userOptionsDataSource: UserOptionsDataSource +) : ViewModel() { + + val unIgnoreUserLiveData = SingleEventLiveData<Response<Unit?>>() + + val ignoredUsersLiveData = + MatrixSessionProvider.getSessionOrThrow().userService().getIgnoredUsersLive() + .map { it.map { it.toCirclesUserSummary() } } + + fun unIgnoreUser(userId: String) { + launchBg { + unIgnoreUserLiveData.postValue(userOptionsDataSource.unIgnoreSender(userId)) + } + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersAdapter.kt b/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersAdapter.kt new file mode 100644 index 000000000..bf83de3a0 --- /dev/null +++ b/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersAdapter.kt @@ -0,0 +1,24 @@ +package org.futo.circles.feature.ignored.list + +import android.view.ViewGroup +import org.futo.circles.core.base.list.BaseRvAdapter +import org.futo.circles.core.model.CirclesUserSummary +import org.futo.circles.model.PeopleUserListItem + +class IgnoredUsersAdapter( + private val onUnIgnore: (String) -> Unit, +) : BaseRvAdapter<CirclesUserSummary, IgnoredUsersViewHolder>(DefaultIdEntityCallback()) { + + + override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): IgnoredUsersViewHolder { + return IgnoredUsersViewHolder( + parent, + onUnIgnore = { position -> onUnIgnore(getItem(position).id) } + ) + } + + override fun onBindViewHolder(holder: IgnoredUsersViewHolder, position: Int) { + holder.bind(getItem(position)) + } + +} \ No newline at end of file diff --git a/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersViewHolder.kt b/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersViewHolder.kt new file mode 100644 index 000000000..bbded269a --- /dev/null +++ b/app/src/main/java/org/futo/circles/feature/ignored/list/IgnoredUsersViewHolder.kt @@ -0,0 +1,26 @@ +package org.futo.circles.feature.ignored.list + +import android.view.ViewGroup +import androidx.recyclerview.widget.RecyclerView +import org.futo.circles.core.base.list.ViewBindingHolder +import org.futo.circles.core.extensions.onClick +import org.futo.circles.core.model.CirclesUserSummary +import org.futo.circles.databinding.ListItemPeopleIgnoredBinding + +class IgnoredUsersViewHolder( + parent: ViewGroup, + private val onUnIgnore: (Int) -> Unit +) : RecyclerView.ViewHolder(inflate(parent, ListItemPeopleIgnoredBinding::inflate)) { + + private companion object : ViewBindingHolder + + private val binding = baseBinding as ListItemPeopleIgnoredBinding + + init { + onClick(binding.btnUnIgnore) { position -> onUnIgnore(position) } + } + + fun bind(data: CirclesUserSummary) { + binding.userItem.bind(data) + } +} \ No newline at end of file diff --git a/app/src/main/res/layout/dialog_fragment_ignored_users.xml b/app/src/main/res/layout/dialog_fragment_ignored_users.xml new file mode 100644 index 000000000..474900d1a --- /dev/null +++ b/app/src/main/res/layout/dialog_fragment_ignored_users.xml @@ -0,0 +1,40 @@ +<?xml version="1.0" encoding="utf-8"?> +<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" + xmlns:app="http://schemas.android.com/apk/res-auto" + android:layout_width="match_parent" + android:layout_height="match_parent"> + + + <com.google.android.material.appbar.MaterialToolbar + android:id="@+id/toolbar" + android:layout_width="0dp" + android:layout_height="?attr/actionBarSize" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toTopOf="parent" + app:navigationIcon="?attr/homeAsUpIndicator" + app:title="@string/ignored_users" + app:titleCentered="true" /> + + <View + android:id="@+id/toolbarDivider" + android:layout_width="0dp" + android:layout_height="@dimen/divider_height" + android:background="@color/divider_color" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/toolbar" /> + + + <org.futo.circles.core.view.LoadingRecyclerView + android:id="@+id/rvIgnoredUsers" + android:layout_width="0dp" + android:layout_height="0dp" + app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" + app:layout_constraintBottom_toBottomOf="parent" + app:layout_constraintEnd_toEndOf="parent" + app:layout_constraintStart_toStartOf="parent" + app:layout_constraintTop_toBottomOf="@id/toolbarDivider" /> + + +</androidx.constraintlayout.widget.ConstraintLayout> \ No newline at end of file -- GitLab