2024-06-03 18:02:05 +08:00
|
|
|
import 'dart:async';
|
|
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:fl_clash/common/common.dart';
|
2024-05-20 15:15:09 +08:00
|
|
|
import 'package:fl_clash/enum/enum.dart';
|
|
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:provider/provider.dart';
|
|
|
|
|
import '../models/models.dart';
|
|
|
|
|
import '../widgets/widgets.dart';
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
class LogsFragment extends StatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
const LogsFragment({super.key});
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
@override
|
|
|
|
|
State<LogsFragment> createState() => _LogsFragmentState();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class _LogsFragmentState extends State<LogsFragment> {
|
|
|
|
|
final logsNotifier = ValueNotifier<List<Log>>([]);
|
|
|
|
|
Timer? timer;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
|
|
|
|
logsNotifier.value = context.read<AppState>().logs;
|
|
|
|
|
if (timer != null) {
|
|
|
|
|
timer?.cancel();
|
|
|
|
|
timer = null;
|
|
|
|
|
}
|
|
|
|
|
timer = Timer.periodic(const Duration(milliseconds: 200), (timer) {
|
|
|
|
|
logsNotifier.value = globalState.appController.appState.logs;
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void dispose() {
|
|
|
|
|
super.dispose();
|
|
|
|
|
timer?.cancel();
|
|
|
|
|
timer = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_initActions() {
|
2024-04-30 23:38:49 +08:00
|
|
|
WidgetsBinding.instance.addPostFrameCallback((timeStamp) {
|
|
|
|
|
final commonScaffoldState =
|
|
|
|
|
context.findAncestorStateOfType<CommonScaffoldState>();
|
|
|
|
|
commonScaffoldState?.actions = [
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
showSearch(
|
|
|
|
|
context: context,
|
|
|
|
|
delegate: LogsSearchDelegate(
|
2024-06-03 18:02:05 +08:00
|
|
|
logs: logsNotifier.value.reversed.toList(),
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
icon: const Icon(Icons.search),
|
2024-06-08 15:04:18 +08:00
|
|
|
),
|
|
|
|
|
const SizedBox(
|
|
|
|
|
width: 8,
|
2024-04-30 23:38:49 +08:00
|
|
|
)
|
|
|
|
|
];
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_buildList() {
|
2024-06-03 18:02:05 +08:00
|
|
|
return ValueListenableBuilder<List<Log>>(
|
|
|
|
|
valueListenable: logsNotifier,
|
2024-04-30 23:38:49 +08:00
|
|
|
builder: (_, List<Log> logs, __) {
|
|
|
|
|
if (logs.isEmpty) {
|
|
|
|
|
return NullStatus(
|
|
|
|
|
label: appLocalizations.nullLogsDesc,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
logs = logs.reversed.toList();
|
|
|
|
|
return ListView.separated(
|
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
|
itemCount: logs.length,
|
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
|
final log = logs[index];
|
|
|
|
|
return LogItem(
|
|
|
|
|
log: log,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
separatorBuilder: (BuildContext context, int index) {
|
|
|
|
|
return const Divider(
|
|
|
|
|
height: 0,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Selector<AppState, bool?>(
|
2024-06-03 18:02:05 +08:00
|
|
|
selector: (_, appState) =>
|
|
|
|
|
appState.currentLabel == 'logs' ||
|
|
|
|
|
appState.viewMode == ViewMode.mobile &&
|
|
|
|
|
appState.currentLabel == "tools",
|
2024-04-30 23:38:49 +08:00
|
|
|
builder: (_, isCurrent, child) {
|
|
|
|
|
if (isCurrent == null || isCurrent) {
|
2024-06-03 18:02:05 +08:00
|
|
|
_initActions();
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
return child!;
|
|
|
|
|
},
|
|
|
|
|
child: _buildList(),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class LogsSearchDelegate extends SearchDelegate {
|
|
|
|
|
List<Log> logs = [];
|
|
|
|
|
|
|
|
|
|
LogsSearchDelegate({
|
|
|
|
|
required this.logs,
|
|
|
|
|
});
|
|
|
|
|
|
2024-05-20 15:15:09 +08:00
|
|
|
List<Log> get _results {
|
|
|
|
|
final lowQuery = query.toLowerCase();
|
|
|
|
|
return logs
|
|
|
|
|
.where(
|
|
|
|
|
(log) =>
|
|
|
|
|
(log.payload?.toLowerCase().contains(lowQuery) ?? false) ||
|
|
|
|
|
log.logLevel.name.contains(lowQuery),
|
|
|
|
|
)
|
|
|
|
|
.toList();
|
|
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
List<Widget>? buildActions(BuildContext context) {
|
|
|
|
|
return [
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
if (query.isEmpty) {
|
|
|
|
|
close(context, null);
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
query = '';
|
|
|
|
|
},
|
|
|
|
|
icon: const Icon(Icons.clear),
|
|
|
|
|
),
|
2024-06-08 15:04:18 +08:00
|
|
|
const SizedBox(
|
|
|
|
|
width: 8,
|
|
|
|
|
)
|
2024-04-30 23:38:49 +08:00
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget? buildLeading(BuildContext context) {
|
|
|
|
|
return IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
close(context, null);
|
|
|
|
|
},
|
|
|
|
|
icon: const Icon(Icons.arrow_back),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget buildResults(BuildContext context) {
|
|
|
|
|
return buildSuggestions(context);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget buildSuggestions(BuildContext context) {
|
|
|
|
|
return ListView.separated(
|
|
|
|
|
physics: const AlwaysScrollableScrollPhysics(),
|
|
|
|
|
itemCount: _results.length,
|
|
|
|
|
itemBuilder: (BuildContext context, int index) {
|
|
|
|
|
final log = _results[index];
|
|
|
|
|
return LogItem(
|
2024-05-20 15:15:09 +08:00
|
|
|
key: ValueKey(log.dateTime),
|
2024-04-30 23:38:49 +08:00
|
|
|
log: log,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
separatorBuilder: (BuildContext context, int index) {
|
|
|
|
|
return const Divider(
|
|
|
|
|
height: 0,
|
|
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class LogItem extends StatelessWidget {
|
|
|
|
|
final Log log;
|
|
|
|
|
|
|
|
|
|
const LogItem({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.log,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return ListTile(
|
|
|
|
|
title: SelectableText(log.payload ?? ''),
|
|
|
|
|
subtitle: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
|
|
|
|
Padding(
|
|
|
|
|
padding: const EdgeInsets.only(
|
|
|
|
|
top: 8,
|
|
|
|
|
),
|
|
|
|
|
child: SelectableText(
|
|
|
|
|
"${log.dateTime}",
|
|
|
|
|
style: context.textTheme.bodySmall
|
|
|
|
|
?.copyWith(color: context.colorScheme.primary),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
Container(
|
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
vertical: 8,
|
|
|
|
|
),
|
|
|
|
|
child: CommonChip(
|
|
|
|
|
label: log.logLevel.name,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|