1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
|
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:path/path.dart' as p;
import '../services/log_service.dart';
import '../services/preferences.dart';
final _displayFormat = DateFormat('yyyy-MM-dd HH:mm:ss');
class EntryBrowserScreen extends StatefulWidget {
const EntryBrowserScreen({super.key});
@override
State<EntryBrowserScreen> createState() => _EntryBrowserScreenState();
}
class _EntryBrowserScreenState extends State<EntryBrowserScreen> {
final PreferencesService _prefs = PreferencesService();
Future<List<LogEntry>>? _future;
String _dir = '';
@override
void initState() {
super.initState();
_refresh();
}
void _refresh() {
setState(() {
_future = _load();
});
}
Future<List<LogEntry>> _load() async {
_dir = await _prefs.directory();
return listEntries(_dir);
}
Future<void> _open(LogEntry entry) async {
final content = await entry.file.readAsString();
if (!mounted) return;
await Navigator.of(context).push(
MaterialPageRoute(
builder: (_) => _EntryDetailScreen(entry: entry, content: content),
),
);
}
Future<void> _confirmDelete(LogEntry entry) async {
final ok = await showDialog<bool>(
context: context,
builder: (ctx) => AlertDialog(
title: const Text('Delete entry?'),
content: Text(p.basename(entry.file.path)),
actions: [
TextButton(onPressed: () => Navigator.pop(ctx, false), child: const Text('Cancel')),
FilledButton(onPressed: () => Navigator.pop(ctx, true), child: const Text('Delete')),
],
),
);
if (ok == true) {
await deleteEntry(entry.file);
_refresh();
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Entries'),
actions: [
IconButton(
tooltip: 'Refresh',
icon: const Icon(Icons.refresh),
onPressed: _refresh,
),
],
),
body: FutureBuilder<List<LogEntry>>(
future: _future,
builder: (ctx, snap) {
if (snap.connectionState != ConnectionState.done) {
return const Center(child: CircularProgressIndicator());
}
if (snap.hasError) {
return Center(child: Text('Error: ${snap.error}'));
}
final entries = snap.data ?? const [];
if (entries.isEmpty) {
return Center(
child: Padding(
padding: const EdgeInsets.all(24),
child: Text(
'No entries in $_dir',
textAlign: TextAlign.center,
),
),
);
}
return RefreshIndicator(
onRefresh: () async => _refresh(),
child: ListView.separated(
itemCount: entries.length,
separatorBuilder: (_, _) => const Divider(height: 1),
itemBuilder: (_, i) => _EntryTile(
entry: entries[i],
onTap: () => _open(entries[i]),
onLongPress: () => _confirmDelete(entries[i]),
),
),
);
},
),
);
}
}
class _EntryTile extends StatelessWidget {
const _EntryTile({required this.entry, required this.onTap, required this.onLongPress});
final LogEntry entry;
final VoidCallback onTap;
final VoidCallback onLongPress;
@override
Widget build(BuildContext context) {
return ListTile(
title: Text(_displayFormat.format(entry.timestamp)),
subtitle: FutureBuilder<String>(
future: _firstLine(entry.file),
builder: (_, snap) => Text(
snap.data ?? '',
maxLines: 1,
overflow: TextOverflow.ellipsis,
),
),
onTap: onTap,
onLongPress: onLongPress,
);
}
Future<String> _firstLine(File f) async {
try {
final content = await f.readAsString();
final i = content.indexOf('\n');
return i < 0 ? content : content.substring(0, i);
} catch (_) {
return '';
}
}
}
class _EntryDetailScreen extends StatelessWidget {
const _EntryDetailScreen({required this.entry, required this.content});
final LogEntry entry;
final String content;
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text(p.basename(entry.file.path))),
body: SingleChildScrollView(
padding: const EdgeInsets.all(12),
child: SelectableText(content),
),
);
}
}
|