Skip to content

Commit

Permalink
kinda working log viewer search and text select
Browse files Browse the repository at this point in the history
  • Loading branch information
hagaygo committed Feb 2, 2023
1 parent 9af5aa7 commit a16609d
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 31 deletions.
11 changes: 7 additions & 4 deletions lib/Dialog/Dialogs.dart
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,8 @@ class Dialogs {
});
}

static void showPage(BuildContext context, String title, Widget widget, {List<Widget> actions}) {
static void showPage(BuildContext context, String title, Widget widget,
{bool useListView = true, List<Widget> actions}) {
Navigator.push(
context,
MaterialPageRoute(
Expand All @@ -96,9 +97,11 @@ class Dialogs {
title: Text(title),
),
body: Center(
child: ListView(
children: [widget],
),
child: useListView
? ListView(
children: [widget],
)
: widget,
),
)));
}
Expand Down
25 changes: 16 additions & 9 deletions lib/Page/Form/deviceActionForm.dart
Original file line number Diff line number Diff line change
Expand Up @@ -132,16 +132,23 @@ class DeviceActionFormState extends State<DeviceActionForm> {
}

void showLogPage(String title, LogViewerForm logView) {
Dialogs.showPage(context, title, logView, actions: <Widget>[
Dialogs.showPage(context, title, logView, useListView: false, actions: <Widget>[
IconButton(
icon: Icon(
Icons.refresh,
color: Colors.white,
),
onPressed: () {
logView.refresh();
},
)
onPressed: () {
// var lst = logView.getCurrentLines();
// var idx = lst.indexOf("[ 0.004845] Zone ranges:");
// var scrollX = (idx * 12).toDouble();
// logView.scroll(scrollX, 0);
},
icon: Icon(Icons.search)),
IconButton(
icon: Icon(
Icons.refresh,
color: Colors.white,
),
onPressed: () {
logView.refresh();
})
]);
}

Expand Down
106 changes: 89 additions & 17 deletions lib/Page/Form/logViewerForm.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

// ignore: must_be_immutable
Expand All @@ -15,29 +14,59 @@ class LogViewerForm extends StatefulWidget {
return _state;
}

List<String> getCurrentLines() {
return _state._currentLines;
}

void scroll(double y, double x) {
_state.verticalScrollController.animateTo(y, duration: Duration(milliseconds: 500), curve: Curves.ease);
}

void refresh() {
_state.refresh();
}
}

class LogViewerFormState extends State<LogViewerForm> {
List<int> _foundTextLines = [];

Future<List<Widget>> getLines() async {
var lines = await getContent();
if (_currentLines == null) _currentLines = await getContent();
List<Widget> lst = [];
for (var line in lines.reversed) {
lst.add(Container(padding: EdgeInsets.all(2), child: Text(line.trim(), textScaleFactor: 0.8)));
}

var textSpans = <TextSpan>[];
int counter = 0;
for (var line in _currentLines) {
if (_searchText != null && _searchText.length > 0 && line.contains(_searchText)) {
textSpans.add(TextSpan(text: line.substring(0, line.indexOf(_searchText))));
var ts = TextSpan(text: _searchText, style: TextStyle(backgroundColor: Colors.yellow));
textSpans.add(ts);
textSpans.add(TextSpan(text: line.substring(line.indexOf(_searchText) + _searchText.length)));
_foundTextLines.add(counter);
} else
textSpans.add(TextSpan(text: line));
textSpans.add(TextSpan(text: "\n"));
counter++;
}
lst.add(Container(
padding: EdgeInsets.all(2),
child: SelectableText.rich(
TextSpan(children: textSpans),
textScaleFactor: 0.8,
)));
return lst;
}

final Future<List<String>> Function() getContent;
List<String> _currentLines;
String _searchText;

LogViewerFormState(this.getContent) {
refresh();
}

void refresh() {
_currentLines = null;
if (_initialLinesLoaded) {
setState(() {
_initialLinesLoaded = false;
Expand All @@ -57,19 +86,62 @@ class LogViewerFormState extends State<LogViewerForm> {
});
}

ScrollController verticalScrollController = ScrollController();
ScrollController horizontalScrollController = ScrollController();
String _lastSearchText;
int _lastFoundIndex = 0;

final searchTextController = TextEditingController();
@override
Widget build(BuildContext context) {
return SingleChildScrollView(
scrollDirection: Axis.vertical,
child: Container(
padding: EdgeInsets.all(5),
child: _initialLinesLoaded
? SingleChildScrollView(
scrollDirection: Axis.horizontal,
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: _lines))
: Container(
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [Text("Loading...")]),
),
));
return Column(
children: [
Container(
margin: EdgeInsets.all(2),
child: Row(
children: [
Text("Search Text"),
Expanded(
child: Container(
padding: EdgeInsets.all(10),
child:
TextField(controller: searchTextController, decoration: InputDecoration(isDense: true)))),
IconButton(
onPressed: () {
setState(() {
_searchText = searchTextController.text;
getLines().then((l) {
_lines = l;
if (_foundTextLines.length > 0) {
verticalScrollController.jumpTo((_foundTextLines[0] * 12).toDouble());
}
});
});
},
icon: Icon(Icons.search))
],
)),
Expanded(
child: SingleChildScrollView(
controller: verticalScrollController,
scrollDirection: Axis.vertical,
child: Column(
children: [
Container(
padding: EdgeInsets.all(5),
child: _initialLinesLoaded
? SingleChildScrollView(
controller: horizontalScrollController,
scrollDirection: Axis.horizontal,
child: Column(crossAxisAlignment: CrossAxisAlignment.start, children: _lines))
: Container(
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: [Text("Loading...")]),
),
),
],
)),
),
],
);
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ dependencies:
http: ^0.13.4
package_info_plus: ^3.0.1
feature_discovery: ^0.14.1
provider: ^6.0.0
provider: ^6.0.0

dev_dependencies:
flutter_test:
Expand Down

0 comments on commit a16609d

Please sign in to comment.