2020-07-27 20:43:58 +00:00
|
|
|
import 'dart:io';
|
|
|
|
|
|
|
|
import 'package:flutter/cupertino.dart';
|
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
import 'package:flutter_platform_widgets/flutter_platform_widgets.dart';
|
2022-11-17 21:43:16 +00:00
|
|
|
import 'package:flutter_svg/svg.dart';
|
2020-07-27 20:43:58 +00:00
|
|
|
import 'package:mobile_nebula/components/SimplePage.dart';
|
|
|
|
import 'package:mobile_nebula/models/Site.dart';
|
2021-02-05 15:09:56 +00:00
|
|
|
import 'package:mobile_nebula/services/settings.dart';
|
2020-08-18 00:12:28 +00:00
|
|
|
import 'package:mobile_nebula/services/share.dart';
|
2020-07-27 20:43:58 +00:00
|
|
|
import 'package:mobile_nebula/services/utils.dart';
|
|
|
|
import 'package:pull_to_refresh/pull_to_refresh.dart';
|
|
|
|
|
|
|
|
class SiteLogsScreen extends StatefulWidget {
|
2022-09-21 20:27:35 +00:00
|
|
|
const SiteLogsScreen({Key? key, required this.site}) : super(key: key);
|
2020-07-27 20:43:58 +00:00
|
|
|
|
|
|
|
final Site site;
|
|
|
|
|
|
|
|
@override
|
|
|
|
_SiteLogsScreenState createState() => _SiteLogsScreenState();
|
|
|
|
}
|
|
|
|
|
|
|
|
class _SiteLogsScreenState extends State<SiteLogsScreen> {
|
|
|
|
String logs = '';
|
|
|
|
ScrollController controller = ScrollController();
|
|
|
|
RefreshController refreshController = RefreshController(initialRefresh: false);
|
|
|
|
|
2021-02-05 15:09:56 +00:00
|
|
|
var settings = Settings();
|
2020-07-27 20:43:58 +00:00
|
|
|
@override
|
|
|
|
void initState() {
|
|
|
|
loadLogs();
|
|
|
|
super.initState();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
void dispose() {
|
|
|
|
controller.dispose();
|
|
|
|
super.dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
@override
|
|
|
|
Widget build(BuildContext context) {
|
2024-09-20 18:19:23 +00:00
|
|
|
final dnIcon =
|
|
|
|
Theme.of(context).brightness == Brightness.dark ? 'images/dn-logo-dark.svg' : 'images/dn-logo-light.svg';
|
2022-11-17 21:43:16 +00:00
|
|
|
final title = Row(children: [
|
2024-09-20 18:19:23 +00:00
|
|
|
widget.site.managed
|
|
|
|
? Padding(padding: EdgeInsets.only(right: 10), child: SvgPicture.asset(dnIcon, width: 12))
|
|
|
|
: Container(),
|
2022-11-17 21:43:16 +00:00
|
|
|
Expanded(child: Text(widget.site.name, style: TextStyle(fontWeight: FontWeight.bold)))
|
|
|
|
]);
|
|
|
|
|
2020-07-27 20:43:58 +00:00
|
|
|
return SimplePage(
|
2022-11-17 21:43:16 +00:00
|
|
|
title: title,
|
2020-07-27 20:43:58 +00:00
|
|
|
scrollable: SimpleScrollable.both,
|
|
|
|
scrollController: controller,
|
|
|
|
onRefresh: () async {
|
|
|
|
await loadLogs();
|
|
|
|
refreshController.refreshCompleted();
|
|
|
|
},
|
|
|
|
onLoading: () async {
|
|
|
|
await loadLogs();
|
|
|
|
refreshController.loadComplete();
|
|
|
|
},
|
|
|
|
refreshController: refreshController,
|
2021-02-05 15:09:56 +00:00
|
|
|
child: Container(
|
|
|
|
padding: EdgeInsets.all(5),
|
|
|
|
constraints: logBoxConstraints(context),
|
2022-01-19 19:29:30 +00:00
|
|
|
child: SelectableText(logs.trim(), style: TextStyle(fontFamily: 'RobotoMono', fontSize: 14))),
|
2020-07-27 20:43:58 +00:00
|
|
|
bottomBar: _buildBottomBar(),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
Widget _buildBottomBar() {
|
|
|
|
var borderSide = BorderSide(
|
|
|
|
color: CupertinoColors.separator,
|
|
|
|
style: BorderStyle.solid,
|
|
|
|
width: 0.0,
|
|
|
|
);
|
|
|
|
|
|
|
|
var padding = Platform.isAndroid ? EdgeInsets.fromLTRB(0, 20, 0, 30) : EdgeInsets.all(10);
|
|
|
|
|
|
|
|
return Container(
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
border: Border(top: borderSide),
|
|
|
|
),
|
|
|
|
child: Row(mainAxisAlignment: MainAxisAlignment.center, children: <Widget>[
|
2024-09-20 18:19:23 +00:00
|
|
|
Expanded(child: Builder(builder: (BuildContext context) {
|
|
|
|
return PlatformIconButton(
|
|
|
|
padding: padding,
|
|
|
|
icon: Icon(context.platformIcons.share, size: 30),
|
|
|
|
onPressed: () {
|
|
|
|
Share.shareFile(context,
|
|
|
|
title: '${widget.site.name} logs',
|
|
|
|
filePath: widget.site.logFile,
|
|
|
|
filename: '${widget.site.name}.log');
|
|
|
|
},
|
|
|
|
);
|
|
|
|
})),
|
2020-07-27 20:43:58 +00:00
|
|
|
Expanded(
|
|
|
|
child: PlatformIconButton(
|
2024-09-20 18:19:23 +00:00
|
|
|
padding: padding,
|
|
|
|
icon: Icon(context.platformIcons.downArrow, size: 30),
|
|
|
|
onPressed: () async {
|
|
|
|
controller.animateTo(controller.position.maxScrollExtent,
|
|
|
|
duration: const Duration(milliseconds: 500), curve: Curves.linearToEaseOut);
|
|
|
|
},
|
2020-07-27 20:43:58 +00:00
|
|
|
)),
|
|
|
|
]));
|
|
|
|
}
|
|
|
|
|
|
|
|
loadLogs() async {
|
|
|
|
var file = File(widget.site.logFile);
|
|
|
|
try {
|
|
|
|
final v = await file.readAsString();
|
|
|
|
|
|
|
|
setState(() {
|
|
|
|
logs = v;
|
|
|
|
});
|
2022-11-17 21:43:16 +00:00
|
|
|
} on FileSystemException {
|
|
|
|
Utils.popError(context, 'Error while reading logs', 'No log file was present');
|
2020-07-27 20:43:58 +00:00
|
|
|
} catch (err) {
|
|
|
|
Utils.popError(context, 'Error while reading logs', err.toString());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
deleteLogs() async {
|
|
|
|
var file = File(widget.site.logFile);
|
|
|
|
await file.writeAsBytes([]);
|
|
|
|
await loadLogs();
|
|
|
|
}
|
2021-02-05 15:09:56 +00:00
|
|
|
|
|
|
|
logBoxConstraints(BuildContext context) {
|
|
|
|
if (settings.logWrap) {
|
|
|
|
return BoxConstraints(maxWidth: MediaQuery.of(context).size.width);
|
|
|
|
} else {
|
|
|
|
return BoxConstraints(minWidth: MediaQuery.of(context).size.width);
|
|
|
|
}
|
|
|
|
}
|
2020-07-27 20:43:58 +00:00
|
|
|
}
|