From 59c2f866bcf71f87a09ab6135518208357064b12 Mon Sep 17 00:00:00 2001 From: Vishesh Handa Date: Tue, 19 May 2020 22:51:28 +0200 Subject: [PATCH] DebugScreen: Allow filtering the logs based on their level --- lib/screens/debug_screen.dart | 103 ++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/lib/screens/debug_screen.dart b/lib/screens/debug_screen.dart index b93b2210..20cfd98f 100644 --- a/lib/screens/debug_screen.dart +++ b/lib/screens/debug_screen.dart @@ -9,6 +9,7 @@ class DebugScreen extends StatefulWidget { class _DebugScreenState extends State { ScrollController _controller = ScrollController(); + String filterLevel = 'v'; @override void initState() { @@ -45,6 +46,10 @@ class _DebugScreenState extends State { }, ), actions: [ + IconButton( + icon: const Icon(Icons.filter_list), + onPressed: _showFilterSelection, + ), IconButton( icon: const Icon(Icons.arrow_upward), onPressed: _scrollToTop, @@ -67,9 +72,37 @@ class _DebugScreenState extends State { ); } + bool _shouldDisplay(LogMessage msg) { + if (filterLevel == null || filterLevel.isEmpty) { + return true; + } + + if (filterLevel == 'v') { + return true; + } + if (filterLevel == 'd' && msg.l == 'v') { + return false; + } + if (filterLevel == 'i' && (msg.l == 'v' || msg.l == 'd')) { + return false; + } + if (filterLevel == 'w' && (msg.l == 'v' || msg.l == 'd' || msg.l == 'i')) { + return false; + } + if (filterLevel == 'e' && + (msg.l == 'v' || msg.l == 'd' || msg.l == 'i' || msg.l == 'w')) { + return false; + } + return true; + } + Iterable _fetchLogWidgets() sync* { var prevDate = ""; for (var msg in Log.fetchLogs()) { + if (!_shouldDisplay(msg)) { + continue; + } + var dt = DateTime.fromMillisecondsSinceEpoch(msg.t); var date = dt.toIso8601String().substring(0, 10); if (date != prevDate) { @@ -154,4 +187,74 @@ class _DebugScreenState extends State { padding: const EdgeInsets.all(8.0), ); } + + void _showFilterSelection() async { + var dialog = AlertDialog( + title: const Text("Purchase Failed"), + content: Column( + children: [ + FilterListTile('Error', 'e', filterLevel == 'e'), + FilterListTile('Warning', 'w', filterLevel == 'w'), + FilterListTile('Info', 'i', filterLevel == 'i'), + FilterListTile('Debug', 'd', filterLevel == 'd'), + FilterListTile('Verbose', 'v', filterLevel == 'v'), + ], + mainAxisSize: MainAxisSize.min, + ), + ); + var l = await showDialog(context: context, builder: (context) => dialog); + setState(() { + filterLevel = l; + }); + } +} + +class FilterListTile extends StatelessWidget { + final String publicLevel; + final String internalLevel; + final bool selected; + + FilterListTile(this.publicLevel, this.internalLevel, this.selected); + + @override + Widget build(BuildContext context) { + return ListTile( + leading: _getIcon(context), + title: Text(publicLevel), + onTap: () { + Navigator.pop(context, internalLevel); + }, + selected: selected, + ); + } + + Icon _getIcon(BuildContext context) { + var theme = Theme.of(context); + var color = theme.textTheme.headline6.color; + if (selected) { + switch (theme.brightness) { + case Brightness.light: + color = theme.primaryColor; + break; + case Brightness.dark: + color = theme.accentColor; + break; + } + } + + switch (internalLevel) { + case 'e': + return Icon(Icons.error, color: color); + case 'w': + return Icon(Icons.warning, color: color); + case 'i': + return Icon(Icons.info, color: color); + case 'd': + return Icon(Icons.bug_report, color: color); + case 'v': + return Icon(Icons.all_inclusive, color: color); + } + + return Icon(Icons.all_inclusive, color: color); + } }