Skip to content

Commit

Permalink
Revive discard button
Browse files Browse the repository at this point in the history
  • Loading branch information
beastoin committed Jan 6, 2025
1 parent b0c226d commit f67eb12
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 49 deletions.
106 changes: 62 additions & 44 deletions app/lib/pages/conversations/widgets/search_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,69 @@ class _SearchWidgetState extends State<SearchWidget> {
Widget build(BuildContext context) {
return Container(
padding: const EdgeInsets.fromLTRB(16, 0, 16, 0),
child: TextFormField(
controller: searchController,
focusNode: context.read<HomeProvider>().convoSearchFieldFocusNode,
onChanged: (value) {
var provider = Provider.of<ConversationProvider>(context, listen: false);
_debouncer.run(() async {
await provider.searchConversations(value);
});
setShowClearButton();
},
decoration: InputDecoration(
hintText: 'Search Conversations',
hintStyle: const TextStyle(color: Colors.white60, fontSize: 14),
filled: true,
fillColor: Colors.grey.shade900,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
child: Row(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
Expanded(
child: TextFormField(
controller: searchController,
focusNode: context.read<HomeProvider>().convoSearchFieldFocusNode,
onChanged: (value) {
var provider = Provider.of<ConversationProvider>(context, listen: false);
_debouncer.run(() async {
await provider.searchConversations(value);
});
setShowClearButton();
},
decoration: InputDecoration(
hintText: 'Search Conversations',
hintStyle: const TextStyle(color: Colors.white60, fontSize: 14),
filled: true,
fillColor: Colors.grey.shade900,
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
),
prefixIcon: const Icon(
Icons.search,
color: Colors.white60,
),
suffixIcon: showClearButton
? GestureDetector(
onTap: () async {
var provider = Provider.of<ConversationProvider>(context, listen: false);
await provider.searchConversations(""); // clear
searchController.clear();
setShowClearButton();
},
child: const Icon(
Icons.close,
color: Colors.white,
),
)
: null,
contentPadding: const EdgeInsets.symmetric(horizontal: 12),
),
style: const TextStyle(color: Colors.white),
),
),
focusedBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
),
enabledBorder: OutlineInputBorder(
borderRadius: BorderRadius.circular(16),
),
prefixIcon: const Icon(
Icons.search,
color: Colors.white60,
),
suffixIcon: showClearButton
? GestureDetector(
onTap: () async {
var provider = Provider.of<ConversationProvider>(context, listen: false);
await provider.searchConversations(""); // clear
searchController.clear();
setShowClearButton();
},
child: const Icon(
Icons.close,
color: Colors.white,
),
)
: null,
contentPadding: const EdgeInsets.symmetric(horizontal: 12),
),
style: const TextStyle(color: Colors.white),
Consumer<ConversationProvider>(
builder: (BuildContext context, ConversationProvider convoProvider, Widget? child) {
return IconButton(
onPressed: convoProvider.toggleDiscardConversations,
icon: Icon(
convoProvider.showDiscardedConversations ? Icons.filter_list_off_sharp : Icons.filter_list,
color: Colors.white,
size: 24,
),
);
}),
],
),
);
}
Expand Down
39 changes: 34 additions & 5 deletions app/lib/providers/conversation_provider.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener
Map<DateTime, List<ServerConversation>> groupedConversations = {};

bool isLoadingConversations = false;
bool hasNonDiscardedConversations = true;
bool showDiscardedConversations = false;

String previousQuery = '';
int totalSearchPages = 1;
Expand Down Expand Up @@ -75,8 +75,9 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener
previousQuery = "";
currentSearchPage = 0;
totalSearchPages = 0;
searchedConversations = conversations;
groupSearchConvosByDate();
searchedConversations = [];
groupConversationsByDate();

notifyListeners();
return;
}
Expand Down Expand Up @@ -148,6 +149,18 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener
}
}

void toggleDiscardConversations() {
showDiscardedConversations = !showDiscardedConversations;

if (previousQuery.isNotEmpty) {
groupSearchConvosByDate();
return;
}
groupConversationsByDate();

MixpanelManager().showDiscardedMemoriesToggled(showDiscardedConversations);
}

void setLoadingConversations(bool value) {
isLoadingConversations = value;
notifyListeners();
Expand All @@ -171,9 +184,25 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener
notifyListeners();
}

List<ServerConversation> _filterOutConvos(List<ServerConversation> convos) {
var havingFilters = true;
if (showDiscardedConversations) {
havingFilters = false;
}
if (!havingFilters) {
return convos;
}
return convos.where((convo) {
if (!showDiscardedConversations && (convo.discarded && !convo.isNew)) {
return false;
}
return true;
}).toList();
}

void _groupSearchConvosByDateWithoutNotify() {
groupedConversations = {};
for (var conversation in searchedConversations) {
for (var conversation in _filterOutConvos(searchedConversations)) {
var date = DateTime(conversation.createdAt.year, conversation.createdAt.month, conversation.createdAt.day);
if (!groupedConversations.containsKey(date)) {
groupedConversations[date] = [];
Expand All @@ -189,7 +218,7 @@ class ConversationProvider extends ChangeNotifier implements IWalServiceListener

void _groupConversationsByDateWithoutNotify() {
groupedConversations = {};
for (var conversation in conversations) {
for (var conversation in _filterOutConvos(conversations)) {
var date = DateTime(conversation.createdAt.year, conversation.createdAt.month, conversation.createdAt.day);
if (!groupedConversations.containsKey(date)) {
groupedConversations[date] = [];
Expand Down

0 comments on commit f67eb12

Please sign in to comment.