Skip to content

Commit 50c2d22

Browse files
committed
Improve dialog handling to allow the update of existing sound sources
1 parent bc8b1b3 commit 50c2d22

File tree

6 files changed

+95
-93
lines changed

6 files changed

+95
-93
lines changed

lib/feature/sound/card/small_file_card.dart

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
1+
import 'package:async_redux/async_redux.dart';
12
import 'package:flutter/material.dart';
3+
import 'package:stelaris/api/model/sound/sound_file_source.dart';
4+
import 'package:stelaris/api/state/actions/sound/sound_file_actions.dart';
25
import 'package:stelaris/feature/sound/card/folder_icon.dart';
36
import 'package:stelaris/feature/sound/modal/sound_file_modal.dart';
47

58
class SmallFileCard extends StatelessWidget {
6-
const SmallFileCard({super.key});
9+
10+
const SmallFileCard({required this.fileSource, super.key});
11+
12+
final SoundFileSource fileSource;
713

814
@override
915
Widget build(BuildContext context) {
@@ -19,23 +25,14 @@ class SmallFileCard extends StatelessWidget {
1925
showDialog(
2026
context: context,
2127
builder: (context) => SoundFileModal(
28+
initialData: fileSource,
2229
create: false,
23-
onSave: ({
24-
required name,
25-
required volume,
26-
required pitch,
27-
required weight,
28-
required stream,
29-
required attenuationDistance,
30-
required preload,
31-
required type,
32-
}) {
33-
// Handle save logic here
34-
},
30+
onSave: (soundFile) =>
31+
context.dispatch(SoundFileLinkAction(soundFile)),
3532
),
3633
);
3734
},
38-
child: const FolderIcon()
35+
child: const FolderIcon(),
3936
),
4037
);
4138
}

lib/feature/sound/card/sound_card_button.dart

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
1+
import 'package:async_redux/async_redux.dart';
12
import 'package:flutter/material.dart';
3+
import 'package:stelaris/api/model/sound/sound_file_source.dart';
4+
import 'package:stelaris/api/state/actions/sound/sound_file_actions.dart';
25
import 'package:stelaris/feature/sound/modal/sound_file_modal.dart';
36

47
/// The [SoundCardButton] is a widget that displays a specific button on a sound card which triggers a dialog to view sound file details.
58
/// It is styled with the theme's secondary container colors and has a rounded rectangle shape.
69
class SoundCardButton extends StatelessWidget {
7-
const SoundCardButton({super.key});
10+
const SoundCardButton({required this.source, super.key});
11+
12+
final SoundFileSource source;
813

914
@override
1015
Widget build(BuildContext context) {
@@ -20,20 +25,9 @@ class SoundCardButton extends StatelessWidget {
2025
showDialog(
2126
context: context,
2227
builder: (context) => SoundFileModal(
28+
initialData: source,
2329
create: false,
24-
onSave:
25-
({
26-
required name,
27-
required volume,
28-
required pitch,
29-
required weight,
30-
required stream,
31-
required attenuationDistance,
32-
required preload,
33-
required type,
34-
}) {
35-
// Handle save logic here
36-
},
30+
onSave: (soundFile) => context.dispatch(SoundFileLinkAction(soundFile))
3731
),
3832
);
3933
},

lib/feature/sound/card/sound_file_card.dart

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,18 +8,19 @@ class SoundFileCard extends StatelessWidget {
88

99
static const double _fullCardMinWidth = 230;
1010

11-
const SoundFileCard({required this.eventModel, super.key});
11+
const SoundFileCard({required this.source, super.key});
1212

13-
final SoundFileSource eventModel;
13+
final SoundFileSource source;
1414

1515
@override
1616
Widget build(BuildContext context) {
17+
debugPrint('Build file card with model ' + source.toString());
1718
final theme = Theme.of(context);
1819
return LayoutBuilder(
1920
builder: (context, constraints) {
2021
// If the card is too narrow, show only the icon centered in a card
2122
if (constraints.maxWidth < _fullCardMinWidth) {
22-
return const SmallFileCard();
23+
return SmallFileCard(fileSource: source);
2324
}
2425
// Otherwise, show the full card layout
2526
return Card(
@@ -37,22 +38,22 @@ class SoundFileCard extends StatelessWidget {
3738
crossAxisAlignment: CrossAxisAlignment.start,
3839
children: [
3940
Text(
40-
eventModel.name,
41+
source.name,
4142
style: theme.textTheme.titleMedium?.copyWith(
4243
color: theme.colorScheme.onSurface,
4344
),
4445
),
4546
const SizedBox(height: 4),
4647
Text(
47-
eventModel.name,
48+
source.name,
4849
style: theme.textTheme.bodySmall?.copyWith(
4950
color: theme.colorScheme.onSurfaceVariant,
5051
),
5152
),
5253
],
5354
),
5455
),
55-
const SoundCardButton()
56+
SoundCardButton(source: source)
5657
],
5758
),
5859
),

lib/feature/sound/modal/section/string_field_section.dart

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,16 @@
11
import 'package:flutter/material.dart';
22
import 'package:stelaris/feature/sound/modal/section/base_section.dart';
3+
import 'package:stelaris/util/typedefs.dart';
34

45
class StringInputSection extends StatefulWidget {
5-
const StringInputSection({super.key});
6+
const StringInputSection({
7+
required this.onUpdate,
8+
required this.initialValue,
9+
super.key,
10+
});
11+
12+
final String initialValue;
13+
final ValueUpdate<String> onUpdate;
614

715
@override
816
State<StringInputSection> createState() => _StringInputSectionState();
@@ -15,22 +23,24 @@ class _StringInputSectionState extends State<StringInputSection> {
1523
@override
1624
void initState() {
1725
super.initState();
18-
_controller = TextEditingController();
26+
_controller = TextEditingController(text: widget.initialValue);
1927
}
2028

2129
@override
2230
Widget build(BuildContext context) {
2331
final colorScheme = Theme.of(context).colorScheme;
2432
final outlineBorder = OutlineInputBorder(
2533
borderRadius: _borderRadius,
26-
borderSide: BorderSide(
27-
color: colorScheme.outline,
28-
),
34+
borderSide: BorderSide(color: colorScheme.outline),
2935
);
3036
return BaseSection(
3137
title: 'Name',
3238
child: TextField(
3339
controller: _controller,
40+
onChanged: (value) {
41+
if (value.isNotEmpty && value.trim().isEmpty) return;
42+
widget.onUpdate(value);
43+
},
3444
decoration: InputDecoration(
3545
hintText: 'Enter your sound name',
3646
hintStyle: TextStyle(color: colorScheme.onSurfaceVariant),

lib/feature/sound/modal/sound_file_modal.dart

Lines changed: 52 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import 'package:flutter/material.dart';
2+
import 'package:stelaris/api/model/sound/sound_file_source.dart';
23
import 'package:stelaris/feature/base/button/cancel_button.dart';
34
import 'package:stelaris/feature/sound/modal/section/base_section.dart';
45
import 'package:stelaris/feature/sound/modal/type/integer_fields_section.dart';
@@ -8,38 +9,54 @@ import 'package:stelaris/feature/sound/modal/type/volume_section.dart';
89
import 'section/string_field_section.dart';
910

1011
class SoundFileModal extends StatefulWidget {
11-
final void Function({
12-
required String name,
13-
required double volume,
14-
required double pitch,
15-
required int weight,
16-
required bool stream,
17-
required int attenuationDistance,
18-
required bool preload,
19-
required String type,
20-
})?
21-
onSave;
2212

23-
final bool create;
13+
const SoundFileModal({
14+
required this.create,
15+
required this.onSave,
16+
this.initialData,
17+
super.key,
18+
});
2419

25-
const SoundFileModal({required this.create, required this.onSave, super.key});
20+
final void Function(SoundFileSource soundFile)? onSave;
21+
final bool create;
22+
final SoundFileSource? initialData;
2623

2724
@override
2825
State<SoundFileModal> createState() => _SoundFileModalState();
2926
}
3027

28+
3129
class _SoundFileModalState extends State<SoundFileModal> {
32-
String _name = '';
33-
double _volume = 1;
34-
double _pitch = 1;
35-
int _weight = 1;
36-
bool _stream = true;
37-
int _attenuationDistance = 16;
38-
bool _preload = false;
39-
String _type = 'file';
30+
late String _name;
31+
late double _volume;
32+
late double _pitch;
33+
late int _weight;
34+
late bool _stream;
35+
late int _attenuationDistance;
36+
late bool _preload;
37+
late String _type;
4038

4139
final _formKey = GlobalKey<FormState>();
4240

41+
@override
42+
void initState() {
43+
super.initState();
44+
debugPrint('Read data from');
45+
if (widget.initialData != null) {
46+
debugPrint('Exsts');
47+
debugPrint(widget.initialData!.toString());
48+
}
49+
final data = widget.initialData;
50+
_name = data?.name ?? '';
51+
_volume = data?.volume ?? 1;
52+
_pitch = data?.pitch ?? 1;
53+
_weight = data?.weight ?? 1;
54+
_stream = true;
55+
_attenuationDistance = data?.attenuationDistance ?? 16;
56+
_preload = data?.preload ?? false;
57+
_type = data?.type ?? 'file';
58+
}
59+
4360
@override
4461
Widget build(BuildContext context) {
4562
final media = MediaQuery.of(context);
@@ -67,7 +84,10 @@ class _SoundFileModalState extends State<SoundFileModal> {
6784
style: Theme.of(context).textTheme.titleLarge,
6885
),
6986
const SizedBox(height: 24),
70-
const StringInputSection(),
87+
StringInputSection(
88+
initialValue: _name,
89+
onUpdate: (value) => _name = value,
90+
),
7191
const SizedBox(height: 16),
7292
VolumeSection(
7393
initialPitch: _pitch,
@@ -133,15 +153,17 @@ class _SoundFileModalState extends State<SoundFileModal> {
133153
onPressed: () {
134154
if (_formKey.currentState?.validate() ?? false) {
135155
widget.onSave?.call(
136-
name: _name,
137-
volume: _volume,
138-
pitch: _pitch,
139-
weight: _weight,
140-
stream: _stream,
141-
attenuationDistance: _attenuationDistance,
142-
preload: _preload,
143-
type: _type,
156+
SoundFileSource(
157+
name: _name,
158+
volume: _volume,
159+
pitch: _pitch,
160+
weight: _weight,
161+
attenuationDistance: _attenuationDistance,
162+
preload: _preload,
163+
type: _type,
164+
),
144165
);
166+
145167
Navigator.pop(context);
146168
}
147169
},

lib/feature/sound/sound_file_entries.dart

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -78,29 +78,7 @@ class _SoundFileEntriesState extends State<SoundFileEntryPage> {
7878
builder: (context) =>
7979
SoundFileModal(
8080
create: true,
81-
onSave:
82-
({
83-
required name,
84-
required volume,
85-
required pitch,
86-
required weight,
87-
required stream,
88-
required attenuationDistance,
89-
required preload,
90-
required type,
91-
}) {
92-
final SoundFileSource fileSource = SoundFileSource(
93-
name: 'Test',
94-
volume: volume,
95-
pitch: pitch,
96-
attenuationDistance: attenuationDistance,
97-
preload: preload,
98-
type: type,
99-
weight: weight,
100-
);
101-
context.dispatch(SoundFileLinkAction(fileSource));
102-
// Handle save logic here
103-
},
81+
onSave: (soundFile) => context.dispatch(SoundFileLinkAction(soundFile))
10482
),
10583
);
10684
}
@@ -123,7 +101,7 @@ class _SoundFileEntriesState extends State<SoundFileEntryPage> {
123101
minWidth: 220,
124102
maxWidth: 400,
125103
),
126-
child: SoundFileCard(eventModel: files.items[index]),
104+
child: SoundFileCard(source: files.items[index]),
127105
);
128106
},
129107
);

0 commit comments

Comments
 (0)