Skip to content

Commit df3ca5d

Browse files
committed
Refactor: Make HideChannel option stateless
The `HideChannel` option in `ChatInfoItem` has been changed from a `Stateful` option to a regular `Option`. This simplifies the logic in `ChatInfoFragment` and `GroupChatInfoFragment` by removing the need to manually update the option's checked state when a modal is dismissed. The click handling for the `HideChannel` option has been updated to reflect this change. It now checks the current `isChecked` state of the option to determine whether to trigger `HideChannelClick` or `UnhideChannelClick`.
1 parent 2d9787a commit df3ca5d

File tree

4 files changed

+34
-46
lines changed

4 files changed

+34
-46
lines changed

stream-chat-android-ui-components-sample/src/main/kotlin/io/getstream/chat/ui/sample/feature/chat/info/ChatInfoExtensions.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ internal fun List<ChannelInfoViewState.Content.Option>.toChannelInfoItems(
5858
)
5959

6060
is ChannelInfoViewState.Content.Option.HideChannel -> add(
61-
ChatInfoItem.Option.Stateful.HideChannel(
61+
ChatInfoItem.Option.HideChannel(
6262
textResId = if (isGroupChannel) {
6363
R.string.stream_ui_channel_info_option_hide_group
6464
} else {

stream-chat-android-ui-components-sample/src/main/kotlin/io/getstream/chat/ui/sample/feature/chat/info/ChatInfoFragment.kt

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,7 @@ class ChatInfoFragment : Fragment() {
9393
when (event) {
9494
is ChannelInfoViewEvent.Error -> showError(event, isGroupChannel = false)
9595
is ChannelInfoViewEvent.Navigation -> onNavigationEvent(event)
96-
is ChannelInfoViewEvent.Modal -> showModal(event, viewModel, isGroupChannel = false) { modal ->
97-
if (modal is ChannelInfoViewEvent.HideChannelModal) {
98-
// If the HideChannel modal was dismissed,
99-
// we need to revert the state of the HideChannel option to unchecked,
100-
// as it was checked when the modal was shown.
101-
binding.optionsRecyclerView.adapter?.notifyDataSetChanged()
102-
}
103-
}
96+
is ChannelInfoViewEvent.Modal -> showModal(event, viewModel, isGroupChannel = false)
10497
}
10598
}
10699
}
@@ -141,13 +134,6 @@ class ChatInfoFragment : Fragment() {
141134
} else {
142135
ChannelInfoViewAction.UnmuteChannelClick
143136
}
144-
145-
is ChatInfoItem.Option.Stateful.HideChannel ->
146-
if (isChecked) {
147-
ChannelInfoViewAction.HideChannelClick
148-
} else {
149-
ChannelInfoViewAction.UnhideChannelClick
150-
}
151137
},
152138
)
153139
}
@@ -176,10 +162,16 @@ class ChatInfoFragment : Fragment() {
176162
is ChatInfoItem.Option.DeleteChannel ->
177163
viewModel.onViewAction(ChannelInfoViewAction.DeleteChannelClick)
178164

165+
is ChatInfoItem.Option.HideChannel -> viewModel.onViewAction(
166+
if (option.isChecked) {
167+
ChannelInfoViewAction.UnhideChannelClick
168+
} else {
169+
ChannelInfoViewAction.HideChannelClick
170+
},
171+
)
172+
179173
// Already handled
180-
is ChatInfoItem.Option.Stateful.MuteChannel,
181-
is ChatInfoItem.Option.Stateful.HideChannel,
182-
-> Unit
174+
is ChatInfoItem.Option.Stateful.MuteChannel -> Unit
183175
}
184176
}
185177
}

stream-chat-android-ui-components-sample/src/main/kotlin/io/getstream/chat/ui/sample/feature/chat/info/ChatInfoItem.kt

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,19 @@ sealed class ChatInfoItem {
101101
override val showRightArrow: Boolean = false
102102
}
103103

104+
data class HideChannel(
105+
@StringRes override val textResId: Int,
106+
val isChecked: Boolean,
107+
) : Option() {
108+
override val iconResId: Int
109+
get() = R.drawable.stream_ic_hide
110+
111+
override val showRightArrow: Boolean = false
112+
113+
override val checkedState: Boolean
114+
get() = isChecked
115+
}
116+
104117
sealed class Stateful : Option() {
105118
abstract val isChecked: Boolean
106119

@@ -111,14 +124,6 @@ sealed class ChatInfoItem {
111124
override val iconResId: Int
112125
get() = R.drawable.ic_mute
113126
}
114-
115-
data class HideChannel(
116-
@StringRes override val textResId: Int,
117-
override var isChecked: Boolean,
118-
) : Stateful() {
119-
override val iconResId: Int
120-
get() = R.drawable.stream_ic_hide
121-
}
122127
}
123128
}
124129
}

stream-chat-android-ui-components-sample/src/main/kotlin/io/getstream/chat/ui/sample/feature/chat/info/group/GroupChatInfoFragment.kt

Lines changed: 10 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -106,14 +106,7 @@ class GroupChatInfoFragment : Fragment() {
106106
when (event) {
107107
is ChannelInfoViewEvent.Error -> showError(event, isGroupChannel = true)
108108
is ChannelInfoViewEvent.Navigation -> onNavigationEvent(event)
109-
is ChannelInfoViewEvent.Modal -> showModal(event, viewModel, isGroupChannel = true) { modal ->
110-
if (modal is ChannelInfoViewEvent.HideChannelModal) {
111-
// If the HideChannel modal was dismissed,
112-
// we need to revert the state of the HideChannel option to unchecked,
113-
// as it was checked when the modal was shown.
114-
binding.optionsRecyclerView.adapter?.notifyDataSetChanged()
115-
}
116-
}
109+
is ChannelInfoViewEvent.Modal -> showModal(event, viewModel, isGroupChannel = true)
117110
}
118111
}
119112
viewModel.state.observe(viewLifecycleOwner) { state ->
@@ -183,13 +176,6 @@ class GroupChatInfoFragment : Fragment() {
183176
} else {
184177
ChannelInfoViewAction.UnmuteChannelClick
185178
}
186-
187-
is ChatInfoItem.Option.Stateful.HideChannel ->
188-
if (isChecked) {
189-
ChannelInfoViewAction.HideChannelClick
190-
} else {
191-
ChannelInfoViewAction.UnhideChannelClick
192-
}
193179
},
194180
)
195181
}
@@ -200,7 +186,6 @@ class GroupChatInfoFragment : Fragment() {
200186
ChatInfoItem.Option.SharedMedia -> findNavController().navigateSafely(
201187
GroupChatInfoFragmentDirections.actionGroupChatInfoFragmentToChatInfoSharedMediaFragment(args.cid),
202188
)
203-
204189
ChatInfoItem.Option.SharedFiles -> findNavController().navigateSafely(
205190
GroupChatInfoFragmentDirections.actionGroupChatInfoFragmentToChatInfoSharedFilesFragment(args.cid),
206191
)
@@ -211,13 +196,19 @@ class GroupChatInfoFragment : Fragment() {
211196
is ChatInfoItem.Option.DeleteChannel ->
212197
viewModel.onViewAction(ChannelInfoViewAction.DeleteChannelClick)
213198

199+
is ChatInfoItem.Option.HideChannel -> viewModel.onViewAction(
200+
if (option.isChecked) {
201+
ChannelInfoViewAction.UnhideChannelClick
202+
} else {
203+
ChannelInfoViewAction.HideChannelClick
204+
},
205+
)
206+
214207
// Not applicable in this UI
215208
ChatInfoItem.Option.SharedGroups -> Unit
216209

217210
// Already handled
218-
is ChatInfoItem.Option.Stateful.MuteChannel,
219-
is ChatInfoItem.Option.Stateful.HideChannel,
220-
-> Unit
211+
is ChatInfoItem.Option.Stateful.MuteChannel -> Unit
221212
}
222213
}
223214
adapter.setMemberClickListener { viewModel.onViewAction(ChannelInfoViewAction.MemberClick(it)) }

0 commit comments

Comments
 (0)