Files
umbrix/lib/features/logs/view/logs_page.dart

195 lines
6.7 KiB
Dart
Raw Normal View History

2023-07-06 17:18:41 +03:30
import 'package:flutter/material.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
2023-07-06 17:18:41 +03:30
import 'package:fpdart/fpdart.dart';
import 'package:gap/gap.dart';
import 'package:hiddify/core/core_providers.dart';
2023-09-01 15:00:41 +03:30
import 'package:hiddify/core/prefs/prefs.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/domain/failures.dart';
import 'package:hiddify/domain/singbox/singbox.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/features/logs/notifier/notifier.dart';
2023-08-24 16:18:05 +03:30
import 'package:hiddify/services/service_providers.dart';
2023-07-06 17:18:41 +03:30
import 'package:hiddify/utils/utils.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
2023-09-07 01:56:59 +03:30
2023-08-24 22:19:22 +03:30
class LogsPage extends HookConsumerWidget with PresLogger {
2023-07-06 17:18:41 +03:30
const LogsPage({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
final t = ref.watch(translationsProvider);
final state = ref.watch(logsNotifierProvider);
2023-07-06 17:18:41 +03:30
final notifier = ref.watch(logsNotifierProvider.notifier);
2023-09-07 13:43:46 +03:30
final debug = ref.watch(debugModeNotifierProvider);
2023-08-24 22:19:22 +03:30
final filesEditor = ref.watch(filesEditorServiceProvider);
2023-08-24 16:18:05 +03:30
final filterController = useTextEditingController(text: state.filter);
2023-08-24 16:18:05 +03:30
final List<PopupMenuEntry> popupButtons = debug || PlatformUtils.isDesktop
? [
PopupMenuItem(
2023-09-07 01:56:59 +03:30
child: Text(t.logs.shareCoreLogs),
2023-08-24 16:18:05 +03:30
onTap: () async {
2023-08-25 17:58:04 +03:30
await UriUtils.tryShareOrLaunchFile(
Uri.parse(filesEditor.coreLogsPath),
fileOrDir: filesEditor.logsDir.uri,
2023-08-24 22:19:22 +03:30
);
2023-08-24 16:18:05 +03:30
},
),
PopupMenuItem(
2023-09-07 01:56:59 +03:30
child: Text(t.logs.shareAppLogs),
2023-08-24 16:18:05 +03:30
onTap: () async {
2023-08-25 17:58:04 +03:30
await UriUtils.tryShareOrLaunchFile(
Uri.parse(filesEditor.appLogsPath),
fileOrDir: filesEditor.logsDir.uri,
2023-08-24 22:19:22 +03:30
);
2023-08-24 16:18:05 +03:30
},
),
]
: [];
return Scaffold(
appBar: AppBar(
// TODO: fix height
toolbarHeight: 90,
title: Text(t.logs.pageTitle),
actions: [
if (state.paused)
IconButton(
onPressed: notifier.resume,
icon: const Icon(Icons.play_arrow),
tooltip: t.logs.resumeTooltip,
)
else
IconButton(
onPressed: notifier.pause,
icon: const Icon(Icons.pause),
tooltip: t.logs.pauseTooltip,
),
IconButton(
onPressed: notifier.clear,
icon: const Icon(Icons.clear_all),
tooltip: t.logs.clearTooltip,
),
if (popupButtons.isNotEmpty)
PopupMenuButton(
itemBuilder: (context) {
return popupButtons;
},
),
],
bottom: PreferredSize(
preferredSize: const Size.fromHeight(36),
child: Padding(
padding: const EdgeInsets.symmetric(horizontal: 16, vertical: 8),
child: Row(
children: [
Flexible(
child: TextFormField(
controller: filterController,
onChanged: notifier.filterMessage,
decoration: InputDecoration(
isDense: true,
hintText: t.logs.filterHint,
),
),
2023-08-24 16:18:05 +03:30
),
const Gap(16),
DropdownButton<Option<LogLevel>>(
value: optionOf(state.levelFilter),
onChanged: (v) {
if (v == null) return;
notifier.filterLevel(v.toNullable());
},
padding: const EdgeInsets.symmetric(horizontal: 8),
borderRadius: BorderRadius.circular(4),
items: [
DropdownMenuItem(
value: none(),
child: Text(t.logs.allLevelsFilter),
2023-07-06 17:18:41 +03:30
),
...LogLevel.choices.map(
(e) => DropdownMenuItem(
value: some(e),
child: Text(e.name),
),
2023-07-06 17:18:41 +03:30
),
],
),
],
2023-07-06 17:18:41 +03:30
),
),
),
),
body: switch (state.logs) {
AsyncData(value: final logs) => SelectionArea(
child: ListView.builder(
itemCount: logs.length,
reverse: true,
itemBuilder: (context, index) {
final log = logs[index];
return Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.symmetric(
horizontal: 16,
vertical: 4,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
if (log.level != null)
Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
log.level!.name.toUpperCase(),
style: Theme.of(context)
.textTheme
.labelMedium
?.copyWith(color: log.level!.color),
),
if (log.time != null)
Text(
log.time!.toString(),
style:
Theme.of(context).textTheme.labelSmall,
),
],
),
Text(
log.message,
style: Theme.of(context).textTheme.bodySmall,
),
],
),
2023-07-06 17:18:41 +03:30
),
if (index != 0)
const Divider(
indent: 16,
endIndent: 16,
height: 4,
),
],
);
},
),
2023-07-06 17:18:41 +03:30
),
AsyncError(:final error) => CustomScrollView(
2023-07-06 17:18:41 +03:30
slivers: [
2023-10-04 18:06:48 +03:30
SliverErrorBodyPlaceholder(t.presentShortError(error)),
2023-07-06 17:18:41 +03:30
],
),
_ => const CustomScrollView(
2023-07-06 17:18:41 +03:30
slivers: [
SliverLoadingBodyPlaceholder(),
2023-07-06 17:18:41 +03:30
],
),
},
);
2023-07-06 17:18:41 +03:30
}
}