Skip to content

Commit 032b537

Browse files
committed
Add action filter chips to history screen
1 parent 90fee4c commit 032b537

2 files changed

Lines changed: 65 additions & 38 deletions

File tree

app/src/main/java/com/hush/app/ui/screens/history/HistoryScreen.kt

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ fun HistoryScreen(
3434
) {
3535
val searchQuery by viewModel.searchQuery.collectAsState()
3636
val historyLogs by viewModel.historyLogs.collectAsState()
37-
37+
val selectedFilter by viewModel.selectedFilter.collectAsState()
3838
var selectedLog by remember { mutableStateOf<NotificationEvent?>(null) }
3939
val timeFormatter = remember {
4040
DateTimeFormatter.ofPattern("hh:mm a").withZone(ZoneId.systemDefault())
@@ -75,7 +75,41 @@ fun HistoryScreen(
7575
)
7676

7777
Spacer(modifier = Modifier.height(12.dp))
78+
Row(
79+
horizontalArrangement = Arrangement.spacedBy(8.dp)
80+
) {
7881

82+
FilterChip(
83+
selected = selectedFilter == RuleAction.BLOCK,
84+
onClick = {
85+
viewModel.toggleFilter(RuleAction.BLOCK)
86+
},
87+
label = {
88+
Text("Blocked")
89+
}
90+
)
91+
92+
FilterChip(
93+
selected = selectedFilter == RuleAction.MUTE,
94+
onClick = {
95+
viewModel.toggleFilter(RuleAction.MUTE)
96+
},
97+
label = {
98+
Text("Muted")
99+
}
100+
)
101+
102+
FilterChip(
103+
selected = selectedFilter == RuleAction.ALLOW,
104+
onClick = {
105+
viewModel.toggleFilter(RuleAction.ALLOW)
106+
},
107+
label = {
108+
Text("Delivered")
109+
}
110+
)
111+
}
112+
Spacer(modifier = Modifier.height(12.dp))
79113
// ── Logs List ──
80114
LazyColumn(
81115
modifier = Modifier

app/src/main/java/com/hush/app/ui/screens/history/HistoryViewModel.kt

Lines changed: 30 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -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

Comments
 (0)