-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy patheditor.dart
232 lines (225 loc) · 9.38 KB
/
editor.dart
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
part of 'dialog.dart';
class _EditorPacksView extends StatefulWidget {
final VoidCallback onReload;
const _EditorPacksView({
required this.onReload,
});
@override
State<_EditorPacksView> createState() => _EditorPacksViewState();
}
class _EditorPacksViewState extends State<_EditorPacksView> {
late final SetonixFileSystem _fileSystem = context.read<SetonixFileSystem>();
Future<List<FileSystemFile<SetonixData>>>? _packs;
@override
void initState() {
super.initState();
_packs = _fileSystem.editorSystem.getFiles();
}
void _reloadPacks() {
setState(() {
_packs = _fileSystem.editorSystem.getFiles();
});
}
@override
Widget build(BuildContext context) {
return Stack(
children: [
FutureBuilder<List<FileSystemFile<SetonixData>>>(
future: _packs,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return const Center(child: CircularProgressIndicator());
}
if (snapshot.hasError) {
return Center(
child: Text(snapshot.error.toString()),
);
}
final files = snapshot.data ?? [];
if (files.isEmpty) {
return Center(
child: Text(AppLocalizations.of(context).noData),
);
}
return ListView.builder(
itemCount: files.length,
itemBuilder: (context, index) {
final file = files[index];
final data = file.data!;
final metadata = data.getMetadataOrDefault();
return ContextRegion(
builder: (context, widget, controller) {
return ListTile(
title: Text(metadata.name),
subtitle: Text(file.identifier),
onTap: () => GoRouter.of(context)
.goNamed('editor', pathParameters: {
'name': file.pathWithoutLeadingSlash,
}),
trailing: widget,
);
},
menuChildren: [
MenuItemButton(
leadingIcon: const Icon(PhosphorIconsLight.download),
child: Text(AppLocalizations.of(context).install),
onPressed: () async {
await importFileData(context, _fileSystem,
SetonixFile(data.exportAsBytes()));
widget.onReload();
},
),
MenuItemButton(
leadingIcon: const Icon(PhosphorIconsLight.export),
child: Text(AppLocalizations.of(context).export),
onPressed: () async {
exportData(
context,
data,
metadata.name,
);
},
),
MenuItemButton(
leadingIcon: const Icon(PhosphorIconsLight.trash),
child: Text(AppLocalizations.of(context).delete),
onPressed: () async {
final result = await showDialog<bool>(
context: context,
builder: (context) => AlertDialog(
title:
Text(AppLocalizations.of(context).removePack),
content: Text(AppLocalizations.of(context)
.removePackMessage(file.identifier)),
actions: [
TextButton(
onPressed: () =>
Navigator.of(context).pop(false),
child:
Text(AppLocalizations.of(context).cancel),
),
TextButton(
onPressed: () =>
Navigator.of(context).pop(true),
child:
Text(AppLocalizations.of(context).remove),
),
],
),
);
if (!(result ?? false)) return;
await _fileSystem.editorSystem
.deleteFile(file.identifier);
_reloadPacks();
},
),
],
);
},
);
}),
Align(
alignment: Alignment.bottomRight,
child: FloatingActionButton.extended(
onPressed: () => showLeapBottomSheet(
context: context,
titleBuilder: (context) => Text(AppLocalizations.of(context).add),
childrenBuilder: (context) => [
ListTile(
title: Text(LeapLocalizations.of(context).create),
leading: const Icon(PhosphorIconsLight.plusCircle),
onTap: () async {
final name = await showDialog(
context: context, builder: (context) => NameDialog());
if (name == null) return;
await _fileSystem.editorSystem.createFile(
name,
SetonixData.empty().setMetadata(FileMetadata(
name: name,
type: FileType.pack,
)));
_reloadPacks();
},
),
ListTile(
title: Text(AppLocalizations.of(context).installed),
leading: const Icon(PhosphorIconsLight.download),
onTap: () async {
final packs = await _fileSystem.packSystem.getFiles();
if (packs.isEmpty || !context.mounted) {
return;
}
showDialog(
context: context,
builder: (context) => AlertDialog(
title: Text(AppLocalizations.of(context).import),
scrollable: true,
content: Column(
mainAxisSize: MainAxisSize.min,
children: packs.map((pack) {
final file = pack.data!;
final data = file.load();
final metadata = data.getMetadataOrDefault();
return ListTile(
title: Text(metadata.name),
subtitle: Text(pack.identifier),
onTap: () async {
await _fileSystem.editorSystem.createFile(
metadata.name,
SetonixData.fromData(data.exportAsBytes()));
_reloadPacks();
},
);
}).toList(),
),
actions: [
TextButton(
onPressed: () => Navigator.of(context).pop(false),
child: Text(AppLocalizations.of(context).cancel),
),
],
),
);
},
),
ListTile(
title: Text(AppLocalizations.of(context).import),
leading: const Icon(PhosphorIconsLight.arrowSquareIn),
onTap: () async {
final result = await fs.openFile(
acceptedTypeGroups: [
fs.XTypeGroup(
label: AppLocalizations.of(context).packs,
extensions: const ['stnx'],
uniformTypeIdentifiers: const [
'dev.linwood.setonix.pack'
],
mimeTypes: const [
'application/octet-stream',
'application/zip'
],
)
],
);
if (result == null) return;
final bytes = await result.readAsBytes();
final data = SetonixFile(bytes).load();
final metadata = data.getMetadataOrDefault();
if (metadata.type != FileType.pack) {
return;
}
await _fileSystem.editorSystem
.createFile(metadata.name, data);
_reloadPacks();
},
),
],
),
label: Text(AppLocalizations.of(context).add),
icon: const Icon(PhosphorIconsLight.plus),
),
),
],
);
}
}