@@ -19,52 +19,45 @@ class HistoryViewModel @Inject constructor(
1919 private val _searchQuery = MutableStateFlow (" " )
2020 val searchQuery: StateFlow <String > = _searchQuery .asStateFlow()
2121
22- private val _selectedTab = MutableStateFlow ( " All " ) // "All", "BLOCK", "MUTE", "ALLOW"
23- val selectedTab : StateFlow <String > = _selectedTab .asStateFlow()
22+ private val _selectedFilter = MutableStateFlow < RuleAction ?>( null )
23+ val selectedFilter : StateFlow <RuleAction ? > = _selectedFilter .asStateFlow()
2424
25- val historyLogs: StateFlow <List <NotificationEvent >> = combine(
26- _searchQuery ,
27- _selectedTab
28- ) { query, tab ->
29- Pair (query, tab)
30- }.flatMapLatest { (query, tab) ->
31- val baseFlow = if (query.isBlank()) {
32- if (tab == " All" ) {
33- historyRepository.getAllLogs()
34- } else {
35- val action = runCatching { RuleAction .valueOf(tab) }.getOrNull()
36- if (action != null ) {
37- historyRepository.getLogsByAction(action)
38- } else {
25+ val historyLogs: StateFlow <List <NotificationEvent >> =
26+ combine(
27+ _searchQuery ,
28+ _selectedFilter
29+ ) { query, filter ->
30+ query to filter
31+ }.flatMapLatest { (query, filter) ->
32+
33+ val flow =
34+ if (query.isBlank()) {
3935 historyRepository.getAllLogs()
40- }
41- }
42- } else {
43- historyRepository.searchLogs(query).map { list ->
44- if (tab == " All" ) {
45- list
4636 } else {
47- val action = runCatching { RuleAction .valueOf(tab) }.getOrNull()
48- if (action != null ) {
49- list.filter { it.actionTaken == action }
50- } else {
51- list
52- }
37+ historyRepository.searchLogs(query)
5338 }
39+
40+ flow.map { logs ->
41+ filter?.let { action ->
42+ logs.filter { it.actionTaken == action }
43+ } ? : logs
5444 }
55- }
56- baseFlow
57- }.stateIn(
58- scope = viewModelScope,
59- started = SharingStarted .WhileSubscribed (5000 ),
60- initialValue = emptyList()
61- )
45+ }.stateIn(
46+ scope = viewModelScope,
47+ started = SharingStarted .WhileSubscribed (5000 ),
48+ initialValue = emptyList()
49+ )
6250
6351 fun setSearchQuery (query : String ) {
6452 _searchQuery .value = query
6553 }
6654
67- fun setSelectedTab (tab : String ) {
68- _selectedTab .value = tab
55+ fun toggleFilter (filter : RuleAction ) {
56+ _selectedFilter .value =
57+ if (_selectedFilter .value == filter) {
58+ null
59+ } else {
60+ filter
61+ }
6962 }
7063}
0 commit comments