-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathdialog.dart
324 lines (308 loc) · 11 KB
/
dialog.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
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
import 'package:file_selector/file_selector.dart' as fs;
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:go_router/go_router.dart';
import 'package:intl/intl.dart';
import 'package:lw_file_system/lw_file_system.dart';
import 'package:material_leap/material_leap.dart';
import 'package:phosphor_flutter/phosphor_flutter.dart';
import 'package:setonix/api/open.dart';
import 'package:setonix/api/save.dart';
import 'package:setonix/bloc/world/bloc.dart';
import 'package:setonix/bloc/world/state.dart';
import 'package:setonix/services/file_system.dart';
import 'package:setonix/widgets/search.dart';
import 'package:setonix_api/setonix_api.dart';
part 'installed.dart';
part 'world.dart';
part 'editor.dart';
class PacksDialog extends StatefulWidget {
final WorldBloc? bloc;
const PacksDialog({
super.key,
this.bloc,
});
@override
State<PacksDialog> createState() => _PacksDialogState();
}
typedef SelectFunction = Future<void> Function(
Widget title,
List<Widget> details,
List<Widget> actions,
String id,
);
class _PacksDialogState extends State<PacksDialog>
with TickerProviderStateMixin {
// It needs to be here to be able to reload it when clicking on import
Future<Iterable<(SetonixFile, SetonixData, DataMetadata)>>? _packsFuture;
({
Widget title,
List<Widget> details,
List<Widget> actions,
String id
})? _selected;
late final AnimationController _controller = AnimationController(
vsync: this,
duration: const Duration(milliseconds: 100),
);
late final TabController _tabController;
late final SetonixFileSystem _fileSystem = context.read<SetonixFileSystem>();
final TextEditingController _searchController = TextEditingController();
@override
void initState() {
super.initState();
_packsFuture = _getPacks();
_tabController = TabController(length: 3, vsync: this);
_controller.addStatusListener((status) {
if (status == AnimationStatus.dismissed) {
setState(() {
_selected = null;
});
}
});
}
Future<List<(SetonixFile, SetonixData, DataMetadata)>> _getPacks() async {
final packs = <(SetonixFile, SetonixData, DataMetadata)>[];
for (final pack in await _fileSystem.getPacks()) {
final data = pack.load();
final dataMeta =
await _fileSystem.dataInfoSystem.getFile(pack.identifier) ??
DataMetadata(addedAt: DateTime.now());
packs.add((pack, data, dataMeta));
}
return packs;
}
void _reloadPacks() {
if (mounted) {
setState(() {
_packsFuture = _getPacks();
});
}
}
@override
void dispose() {
_tabController.dispose();
super.dispose();
}
bool get isWorldLoaded => widget.bloc != null;
void _unselect() {
_controller.reverse();
}
Future<void> _select(bool isMobile, Widget title, List<Widget> details,
List<Widget> actions, String id) async {
_controller.forward();
setState(() {
_selected = (
title: title,
details: details,
actions: actions,
id: id,
);
});
if (isMobile) {
await showLeapBottomSheet(
context: context,
childrenBuilder: (context) => [
...details,
const SizedBox(height: 16),
...actions,
],
titleBuilder: (context) => title,
);
if (mounted) {
_unselect();
}
}
}
@override
Widget build(BuildContext context) {
final currentSize = MediaQuery.sizeOf(context).width;
final isMobile = currentSize < LeapBreakpoints.medium;
Future<void> select(Widget title, List<Widget> details,
List<Widget> actions, String id) =>
_select(isMobile, title, details, actions, id);
return ResponsiveAlertDialog(
title: Text(AppLocalizations.of(context).packs),
constraints: const BoxConstraints(
maxWidth: LeapBreakpoints.expanded,
maxHeight: 700,
),
content: Column(
mainAxisSize: MainAxisSize.min,
children: [
TabSearchView(
tabController: _tabController,
searchController: _searchController,
onTabTap: (_) => _unselect(),
tabs: [
if (isWorldLoaded)
HorizontalTab(
icon: const PhosphorIcon(PhosphorIconsLight.play),
label: Text(AppLocalizations.of(context).game),
),
HorizontalTab(
icon: const PhosphorIcon(PhosphorIconsLight.folder),
label: Text(AppLocalizations.of(context).installed),
),
HorizontalTab(
icon: const PhosphorIcon(PhosphorIconsLight.globe),
label: Text(AppLocalizations.of(context).browse),
),
if (!isWorldLoaded)
HorizontalTab(
icon: const PhosphorIcon(PhosphorIconsLight.notePencil),
label: Text(AppLocalizations.of(context).editor),
),
],
),
const SizedBox(height: 8),
Expanded(
child: FutureBuilder<
Iterable<(SetonixFile, SetonixData, DataMetadata)>>(
future: _packsFuture,
builder: (context, snapshot) {
final packs = snapshot.data ?? [];
if (snapshot.hasError) {
return Center(
child: Column(
children: [
Text(AppLocalizations.of(context).error,
style: Theme.of(context).textTheme.headlineMedium),
const SizedBox(height: 8),
Text(snapshot.error.toString()),
],
),
);
}
final view = ListenableBuilder(
listenable: _searchController,
builder: (context, _) {
final query = _searchController.text.toLowerCase();
final filtered = packs
.where((entry) =>
entry.$2
.getMetadata()
?.name
.toLowerCase()
.contains(query) ??
entry.$1.identifier.toLowerCase().contains(query))
.where(
(e) => widget.bloc == null || e.$3.manuallyAdded)
.toList();
final bloc = widget.bloc;
final selected = _selected?.id;
return TabBarView(
controller: _tabController,
children: [
if (bloc != null)
_WorldPacksView(
bloc: bloc,
query: query,
),
_InstalledPacksView(
filtered: filtered,
bloc: bloc,
onSelected: select,
onReload: _reloadPacks,
onUnselect: _unselect,
selected: selected,
),
Center(
child:
Text(AppLocalizations.of(context).comingSoon),
),
if (bloc == null)
_EditorPacksView(
onReload: _reloadPacks,
),
],
);
});
if (isMobile) {
return view;
}
return Row(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Expanded(
child: view,
),
SizeTransition(
sizeFactor: CurvedAnimation(
parent: _controller,
curve: Curves.fastOutSlowIn,
),
axis: Axis.horizontal,
child: SizedBox(
width: 300,
child: Card(child: Builder(
builder: (context) {
final selected = _selected;
if (selected == null) {
return const SizedBox();
}
return Padding(
padding: const EdgeInsets.only(
left: 8,
right: 8,
bottom: 8,
),
child: Column(
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Header(
title: selected.title,
actions: [
IconButton.outlined(
icon: const Icon(PhosphorIconsLight.x),
onPressed: _unselect,
),
],
),
Expanded(
child: ListView(children: selected.details),
),
Column(
crossAxisAlignment:
CrossAxisAlignment.stretch,
children: selected.actions,
),
],
),
);
},
)),
),
),
],
);
},
),
),
],
),
leading: IconButton.outlined(
icon: const Icon(PhosphorIconsLight.x),
onPressed: () => Navigator.of(context).pop(),
),
headerActions: [
/*IconButton(
icon: Icon(_gridView
? PhosphorIconsLight.list
: PhosphorIconsLight.gridFour),
onPressed: () => setState(() => _gridView = !_gridView),
),
const SizedBox(height: 32, child: VerticalDivider()),*/
IconButton(
tooltip: AppLocalizations.of(context).import,
onPressed: () => importFile(
context,
context.read<SetonixFileSystem>(),
).then((_) => _reloadPacks()),
icon: const Icon(PhosphorIconsLight.arrowSquareIn),
),
],
);
}
}