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';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:fl_clash/providers/providers.dart';
|
2024-05-20 15:15:09 +08:00
|
|
|
import 'package:fl_clash/state.dart';
|
2024-04-30 23:38:49 +08:00
|
|
|
import 'package:flutter/material.dart';
|
2025-02-09 18:39:38 +08:00
|
|
|
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
2025-02-03 23:32:00 +08:00
|
|
|
|
2024-04-30 23:38:49 +08:00
|
|
|
import '../models/models.dart';
|
|
|
|
|
import '../widgets/widgets.dart';
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
double _preOffset = 0;
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class LogsFragment extends ConsumerStatefulWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
const LogsFragment({super.key});
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
@override
|
2025-02-09 18:39:38 +08:00
|
|
|
ConsumerState<LogsFragment> createState() => _LogsFragmentState();
|
2024-06-03 18:02:05 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-09 18:39:38 +08:00
|
|
|
class _LogsFragmentState extends ConsumerState<LogsFragment> with PageMixin {
|
2025-02-03 23:32:00 +08:00
|
|
|
final _logsStateNotifier = ValueNotifier<LogsState>(LogsState());
|
|
|
|
|
final _scrollController = ScrollController(
|
|
|
|
|
initialScrollOffset: _preOffset != 0 ? _preOffset : double.maxFinite,
|
2024-06-19 13:13:31 +08:00
|
|
|
);
|
2025-02-03 23:32:00 +08:00
|
|
|
double _currentMaxWidth = 0;
|
2025-03-12 17:15:31 +08:00
|
|
|
final GlobalKey<CacheItemExtentListViewState> _key = GlobalKey();
|
2024-06-19 13:13:31 +08:00
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
List<Log> _logs = [];
|
2024-06-03 18:02:05 +08:00
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
void initState() {
|
|
|
|
|
super.initState();
|
2025-02-03 23:32:00 +08:00
|
|
|
_logsStateNotifier.value = _logsStateNotifier.value.copyWith(
|
2025-02-09 18:39:38 +08:00
|
|
|
logs: globalState.appState.logs.list,
|
|
|
|
|
);
|
|
|
|
|
ref.listenManual(
|
|
|
|
|
logsProvider.select((state) => state.list),
|
|
|
|
|
(prev, next) {
|
|
|
|
|
if (prev != next) {
|
|
|
|
|
final isEquality = logListEquality.equals(prev, next);
|
|
|
|
|
if (!isEquality) {
|
|
|
|
|
_logs = next;
|
|
|
|
|
updateLogsThrottler();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fireImmediately: true,
|
|
|
|
|
);
|
|
|
|
|
ref.listenManual(
|
|
|
|
|
isCurrentPageProvider(
|
|
|
|
|
PageLabel.logs,
|
|
|
|
|
handler: (pageLabel, viewMode) =>
|
|
|
|
|
pageLabel == PageLabel.tools && viewMode == ViewMode.mobile,
|
|
|
|
|
),
|
|
|
|
|
(prev, next) {
|
|
|
|
|
if (prev != next && next == true) {
|
|
|
|
|
initPageState();
|
|
|
|
|
}
|
|
|
|
|
},
|
|
|
|
|
fireImmediately: true,
|
2025-02-03 23:32:00 +08:00
|
|
|
);
|
2024-06-03 18:02:05 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
@override
|
|
|
|
|
List<Widget> get actions => [
|
|
|
|
|
IconButton(
|
|
|
|
|
onPressed: () {
|
|
|
|
|
_handleExport();
|
|
|
|
|
},
|
|
|
|
|
icon: const Icon(
|
|
|
|
|
Icons.file_download_outlined,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
];
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
get onSearch => (value) {
|
|
|
|
|
_logsStateNotifier.value = _logsStateNotifier.value.copyWith(
|
|
|
|
|
query: value,
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
get onKeywordsUpdate => (keywords) {
|
|
|
|
|
_logsStateNotifier.value =
|
|
|
|
|
_logsStateNotifier.value.copyWith(keywords: keywords);
|
|
|
|
|
};
|
|
|
|
|
|
2024-06-03 18:02:05 +08:00
|
|
|
@override
|
|
|
|
|
void dispose() {
|
2025-02-03 23:32:00 +08:00
|
|
|
_logsStateNotifier.dispose();
|
|
|
|
|
_scrollController.dispose();
|
2024-12-09 01:40:39 +08:00
|
|
|
super.dispose();
|
2024-06-03 18:02:05 +08:00
|
|
|
}
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
_handleTryClearCache(double maxWidth) {
|
|
|
|
|
if (_currentMaxWidth != maxWidth) {
|
|
|
|
|
_currentMaxWidth = maxWidth;
|
2025-03-12 17:15:31 +08:00
|
|
|
_key.currentState?.clearCache();
|
2025-02-03 23:32:00 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-09-20 14:32:57 +08:00
|
|
|
_handleExport() async {
|
|
|
|
|
final commonScaffoldState = context.commonScaffoldState;
|
|
|
|
|
final res = await commonScaffoldState?.loadingRun<bool>(
|
|
|
|
|
() async {
|
|
|
|
|
return await globalState.appController.exportLogs();
|
|
|
|
|
},
|
|
|
|
|
title: appLocalizations.exportLogs,
|
|
|
|
|
);
|
|
|
|
|
if (res != true) return;
|
|
|
|
|
globalState.showMessage(
|
|
|
|
|
title: appLocalizations.tip,
|
|
|
|
|
message: TextSpan(text: appLocalizations.exportSuccess),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
double _getItemHeight(Log log) {
|
|
|
|
|
final measure = globalState.measure;
|
|
|
|
|
final bodySmallHeight = measure.bodySmallHeight;
|
|
|
|
|
final bodyMediumHeight = measure.bodyMediumHeight;
|
2025-03-12 17:15:31 +08:00
|
|
|
final height = globalState.measure
|
|
|
|
|
.computeTextSize(
|
|
|
|
|
Text(
|
|
|
|
|
log.payload ?? "",
|
|
|
|
|
style: globalState.appController.context.textTheme.bodyLarge,
|
|
|
|
|
),
|
|
|
|
|
maxWidth: _currentMaxWidth,
|
|
|
|
|
)
|
|
|
|
|
.height;
|
2025-02-03 23:32:00 +08:00
|
|
|
return height + bodySmallHeight + 8 + bodyMediumHeight + 40;
|
2024-05-20 15:15:09 +08:00
|
|
|
}
|
2024-04-30 23:38:49 +08:00
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
updateLogsThrottler() {
|
|
|
|
|
throttler.call("logs", () {
|
|
|
|
|
final isEquality = logListEquality.equals(
|
|
|
|
|
_logs,
|
|
|
|
|
_logsStateNotifier.value.logs,
|
|
|
|
|
);
|
|
|
|
|
if (isEquality) {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
WidgetsBinding.instance.addPostFrameCallback((_) {
|
|
|
|
|
_logsStateNotifier.value = _logsStateNotifier.value.copyWith(
|
|
|
|
|
logs: _logs,
|
|
|
|
|
);
|
|
|
|
|
});
|
|
|
|
|
}, duration: commonDuration);
|
2024-04-30 23:38:49 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@override
|
2025-02-03 23:32:00 +08:00
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return LayoutBuilder(
|
|
|
|
|
builder: (_, constraints) {
|
|
|
|
|
_handleTryClearCache(constraints.maxWidth - 40);
|
2025-02-09 18:39:38 +08:00
|
|
|
return Align(
|
|
|
|
|
alignment: Alignment.topCenter,
|
|
|
|
|
child: ValueListenableBuilder<LogsState>(
|
|
|
|
|
valueListenable: _logsStateNotifier,
|
|
|
|
|
builder: (_, state, __) {
|
|
|
|
|
final logs = state.list;
|
|
|
|
|
if (logs.isEmpty) {
|
|
|
|
|
return NullStatus(
|
|
|
|
|
label: appLocalizations.nullLogsDesc,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
final items = logs
|
|
|
|
|
.map<Widget>(
|
|
|
|
|
(log) => LogItem(
|
|
|
|
|
key: Key(log.dateTime.toString()),
|
|
|
|
|
log: log,
|
|
|
|
|
onClick: (value) {
|
|
|
|
|
context.commonScaffoldState?.addKeyword(value);
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
.separated(
|
|
|
|
|
const Divider(
|
|
|
|
|
height: 0,
|
2025-02-03 23:32:00 +08:00
|
|
|
),
|
2025-02-09 18:39:38 +08:00
|
|
|
)
|
|
|
|
|
.toList();
|
|
|
|
|
return NotificationListener<ScrollEndNotification>(
|
|
|
|
|
onNotification: (details) {
|
|
|
|
|
_preOffset = details.metrics.pixels;
|
|
|
|
|
return false;
|
2024-06-19 13:13:31 +08:00
|
|
|
},
|
2025-02-09 18:39:38 +08:00
|
|
|
child: CommonScrollBar(
|
|
|
|
|
controller: _scrollController,
|
2025-03-12 17:15:31 +08:00
|
|
|
child: CacheItemExtentListView(
|
|
|
|
|
key: _key,
|
2025-02-09 18:39:38 +08:00
|
|
|
reverse: true,
|
|
|
|
|
shrinkWrap: true,
|
|
|
|
|
physics: NextClampingScrollPhysics(),
|
|
|
|
|
controller: _scrollController,
|
|
|
|
|
itemBuilder: (_, index) {
|
|
|
|
|
return items[index];
|
|
|
|
|
},
|
2025-03-12 17:15:31 +08:00
|
|
|
itemExtentBuilder: (index) {
|
2025-02-09 18:39:38 +08:00
|
|
|
final item = items[index];
|
|
|
|
|
if (item.runtimeType == Divider) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
final log = logs[(index / 2).floor()];
|
|
|
|
|
return _getItemHeight(log);
|
|
|
|
|
},
|
|
|
|
|
itemCount: items.length,
|
2025-03-12 17:15:31 +08:00
|
|
|
keyBuilder: (int index) {
|
|
|
|
|
final item = items[index];
|
|
|
|
|
if (item.runtimeType == Divider) {
|
|
|
|
|
return "divider";
|
|
|
|
|
}
|
|
|
|
|
final log = logs[(index / 2).floor()];
|
|
|
|
|
return log.payload ?? "";
|
|
|
|
|
},
|
2025-02-09 18:39:38 +08:00
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
},
|
2025-02-03 23:32:00 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
);
|
|
|
|
|
},
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-02-03 23:32:00 +08:00
|
|
|
class LogItem extends StatelessWidget {
|
2024-04-30 23:38:49 +08:00
|
|
|
final Log log;
|
2024-06-19 13:13:31 +08:00
|
|
|
final Function(String)? onClick;
|
2024-04-30 23:38:49 +08:00
|
|
|
|
|
|
|
|
const LogItem({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.log,
|
2024-06-19 13:13:31 +08:00
|
|
|
this.onClick,
|
2024-04-30 23:38:49 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2024-07-07 10:02:10 +08:00
|
|
|
return ListItem(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: 16,
|
|
|
|
|
vertical: 4,
|
|
|
|
|
),
|
2024-09-26 14:29:04 +08:00
|
|
|
title: SelectableText(
|
|
|
|
|
log.payload ?? '',
|
2025-02-03 23:32:00 +08:00
|
|
|
style: context.textTheme.bodyLarge,
|
2024-09-26 14:29:04 +08:00
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
subtitle: Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.start,
|
|
|
|
|
children: [
|
2024-07-07 10:02:10 +08:00
|
|
|
SelectableText(
|
|
|
|
|
"${log.dateTime}",
|
2025-02-03 23:32:00 +08:00
|
|
|
style: context.textTheme.bodySmall?.copyWith(
|
|
|
|
|
color: context.colorScheme.primary,
|
|
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
),
|
2024-09-20 14:32:57 +08:00
|
|
|
const SizedBox(
|
|
|
|
|
height: 8,
|
|
|
|
|
),
|
2024-04-30 23:38:49 +08:00
|
|
|
Container(
|
|
|
|
|
alignment: Alignment.centerLeft,
|
|
|
|
|
child: CommonChip(
|
2024-06-19 13:13:31 +08:00
|
|
|
onPressed: () {
|
2025-02-03 23:32:00 +08:00
|
|
|
if (onClick == null) return;
|
|
|
|
|
onClick!(log.logLevel.name);
|
2024-06-19 13:13:31 +08:00
|
|
|
},
|
2024-04-30 23:38:49 +08:00
|
|
|
label: log.logLevel.name,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|