Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/no ref/search #7

Merged
merged 3 commits into from
Jun 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/src/delegates/delegates.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export 'record_search_delegate.dart';
119 changes: 119 additions & 0 deletions lib/src/delegates/record_search_delegate.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
import 'dart:io';

import 'package:flutter/material.dart';
import 'package:logman/logman.dart';
import 'package:logman/src/presentation/presentation.dart';

class RecordSearchDelegate extends SearchDelegate<LogmanRecord> {
final List<LogmanRecord> records;

RecordSearchDelegate({
super.searchFieldLabel,
super.searchFieldStyle,
super.searchFieldDecorationTheme,
super.keyboardType,
super.textInputAction,
required this.records,
});

@override
List<Widget>? buildActions(BuildContext context) {
return [
// Clear search query button
IconButton(
onPressed: () => query = '',
icon: const Icon(Icons.clear_rounded),
),
];
}

@override
Widget? buildLeading(BuildContext context) {
// Back button
return IconButton(
onPressed: () => Navigator.pop(context),
icon: Icon(
Platform.isIOS ? Icons.arrow_back_ios_new : Icons.arrow_back,
size: 16,
),
);
}

@override
Widget buildResults(BuildContext context) {
final searchRecords = _likeSearch(records);

return _searchRecordListView(searchRecords);
}

@override
Widget buildSuggestions(BuildContext context) {
final searchRecords = _likeSearch(records);

if (searchRecords.isEmpty) {
return const Center(
child: Text.rich(
TextSpan(
text: 'No record(s) found',
children: [
TextSpan(text: ' 🫣', style: TextStyle(fontSize: 25)),
],
),
),
);
}

return _searchRecordListView(searchRecords);
}

Widget _searchRecordListView(List<LogmanRecord> searchRecords) {
return ListView.separated(
itemCount: searchRecords.length,
itemBuilder: (context, index) {
final record = searchRecords[index];
if (record is SimpleLogmanRecord) {
return SimpleRecordItem(record: record);
}

if (record is NavigationLogmanRecord) {
return NavigationRecordItem(record: record);
}

if (record is NetworkLogmanRecord) {
return NetworkRecordItem(record: record);
}

return const SizedBox.shrink();
},
separatorBuilder: (context, index) => const CustomDivider(),
);
}

// Function to perform a "like" search on all logman records
List<LogmanRecord> _likeSearch(List<LogmanRecord> records) {
// Convert search query to lowercase for case-insensitive search
final String lowerSearchQuery = query.toLowerCase();

return records.where((record) {
if (record is SimpleLogmanRecord) {
return record.message.toLowerCase().contains(lowerSearchQuery);
}

if (record is NavigationLogmanRecord) {
return record.routeName.toLowerCase().contains(lowerSearchQuery) ||
record.action.name.toLowerCase().contains(lowerSearchQuery);
}

if (record is NetworkLogmanRecord) {
return record.request.url.toLowerCase().contains(lowerSearchQuery) ||
Uri.parse(record.request.url)
.path
.toLowerCase()
.contains(lowerSearchQuery) ||
record.request.method.toLowerCase().contains(lowerSearchQuery);
}

return false;
}).toList();
}
}
26 changes: 13 additions & 13 deletions lib/src/presentation/pages/logman_dashboard_page.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:flutter/material.dart';
import 'package:logman/logman.dart';
import 'package:logman/src/delegates/delegates.dart';
import 'package:logman/src/presentation/notifier/notifier.dart';
import 'package:logman/src/presentation/presentation.dart';

Expand Down Expand Up @@ -71,8 +72,8 @@ class _LogmanDashboardPageState extends State<LogmanDashboardPage>
controller: _tabController,
isScrollable: true,
tabAlignment: TabAlignment.center,
onTap: (value) =>
setState(() => currentIndex = value), // Update currentIndex value
onTap: (value) => setState(() => currentIndex = value),
// Update currentIndex value
tabs: [
const Tab(text: 'All'),
const Tab(text: 'Logs'),
Expand All @@ -82,20 +83,19 @@ class _LogmanDashboardPageState extends State<LogmanDashboardPage>
],
),
actions: [
IconButton(
onPressed: () => showSearch(
context: context,
delegate: RecordSearchDelegate(
records: widget.logman.records.value,
),
),
icon: const Icon(Icons.search_rounded),
),
// Only show these action widgets when network tab is active
Visibility(
visible: currentIndex == 2,
child: Row(
mainAxisSize: MainAxisSize.min,
children: [
// todo implement search functionality
/*IconButton(
onPressed: () {},
icon: const Icon(Icons.search_rounded),
),*/
_NetworkFilterButton(recordsNotifier: _recordNotifier),
],
),
child: _NetworkFilterButton(recordsNotifier: _recordNotifier),
),
IconButton(
onPressed: () {
Expand Down