Skip to content

Commit 9ae16b9

Browse files
committed
Add api and redux action to delete a sound file source
1 parent 3828995 commit 9ae16b9

File tree

2 files changed

+104
-26
lines changed

2 files changed

+104
-26
lines changed

lib/api/service/client/sound_client_api.dart

Lines changed: 39 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,11 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
1616
/// Serialization and deserialization for [SoundEventModel] are handled
1717
/// by [SoundEventModel.fromJson] and [SoundEventModel.toJson] respectively.
1818
SoundClientApi({required super.apiClient})
19-
: super(
20-
endpoint: 'sound',
21-
fromJson: SoundEventModel.fromJson,
22-
toJson: (p0) => p0.toJson(),
23-
);
19+
: super(
20+
endpoint: 'sound',
21+
fromJson: SoundEventModel.fromJson,
22+
toJson: (p0) => p0.toJson(),
23+
);
2424

2525
/// Fetches a paginated list of [SoundFileSource] objects associated with a specific sound event.
2626
///
@@ -33,10 +33,10 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
3333
/// Returns a [Future] that completes with a [PaginatedResult] containing
3434
/// a list of [SoundFileSource] and pagination details.
3535
Future<PaginatedResult<SoundFileSource>> getFiles(
36-
String id, [
37-
int page = 1,
38-
int items = 20,
39-
]) async {
36+
String id, [
37+
int page = 1,
38+
int items = 20,
39+
]) async {
4040
final baseUri = Uri.parse(apiClient.baseUrl);
4141
final uri = baseUri.replace(
4242
path: '${baseUri.path}/$endpoint/$id/sources',
@@ -49,7 +49,8 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
4949
final result = await apiClient.dio.getUri(uri);
5050
return PaginatedResult.fromJson(
5151
result.data,
52-
(jsonSource) => SoundFileSource.fromJson(jsonSource as Map<String, dynamic>),
52+
(jsonSource) =>
53+
SoundFileSource.fromJson(jsonSource as Map<String, dynamic>),
5354
);
5455
}
5556

@@ -58,7 +59,10 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
5859
/// The request is the updated entry from the backend
5960
/// - [modelId]: The unique id of the sound event
6061
/// - [fileToLink]: The [SoundFileSource] which should be linked
61-
Future<SoundFileSource> linkFile(String modelId, SoundFileSource fileToLink) async {
62+
Future<SoundFileSource> linkFile(
63+
String modelId,
64+
SoundFileSource fileToLink,
65+
) async {
6266
final baseUri = Uri.parse(apiClient.baseUrl);
6367
final uri = baseUri.replace(
6468
path: '${baseUri.path}/$endpoint/$modelId/sources',
@@ -67,12 +71,35 @@ class SoundClientApi extends BaseApi<SoundEventModel> {
6771
return SoundFileSource.fromJson(result.data);
6872
}
6973

70-
Future<SoundFileSource> updateFile(String modelId, SoundFileSource file) async {
74+
Future<SoundFileSource> updateFile(
75+
String modelId,
76+
SoundFileSource file,
77+
) async {
7178
final baseUri = Uri.parse(apiClient.baseUrl);
7279
final uri = baseUri.replace(
7380
path: '${baseUri.path}/$endpoint/$modelId/sources/update',
7481
);
7582
final result = await apiClient.dio.postUri(uri, data: file.toJson());
7683
return SoundFileSource.fromJson(result.data);
7784
}
85+
86+
/// Deletes a [SoundFileSource] associated with a specific sound event.
87+
///
88+
/// The request is made to the `/sound/{id}/sources/delete` endpoint.
89+
///
90+
/// - [modelId]: The unique identifier of the sound event.
91+
/// - [file]: The [SoundFileSource] to be deleted.
92+
///
93+
/// Returns a [Future] that completes with the deleted [SoundFileSource].
94+
Future<SoundFileSource> deleteFile(
95+
String modelId,
96+
SoundFileSource file,
97+
) async {
98+
final baseUri = Uri.parse(apiClient.baseUrl);
99+
final uri = baseUri.replace(
100+
path: '${baseUri.path}/$endpoint/$modelId/sources/delete',
101+
);
102+
final result = await apiClient.dio.deleteUri(uri, data: file.toJson());
103+
return SoundFileSource.fromJson(result.data);
104+
}
78105
}

lib/api/state/actions/sound/sound_file_actions.dart

Lines changed: 65 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ import 'package:stelaris/api/service/client/sound_client_api.dart';
77
import 'package:stelaris/api/state/app_state.dart';
88

99
class InitSoundFileAction extends ReduxAction<AppState> {
10-
1110
@override
1211
Future<AppState?> reduce() async {
1312
if (state.selectedSoundEvent == null) return null;
@@ -17,7 +16,9 @@ class InitSoundFileAction extends ReduxAction<AppState> {
1716
if (model.files.items.isNotEmpty) return null;
1817

1918
final soundClient = ApiService().soundApi as SoundClientApi;
20-
final PaginatedResult<SoundFileSource> files = await soundClient.getFiles(model.id!);
19+
final PaginatedResult<SoundFileSource> files = await soundClient.getFiles(
20+
model.id!,
21+
);
2122
return state.copyWith(
2223
selectedSoundEvent: model.copyWith(
2324
files: model.files.copyWith(
@@ -33,7 +34,6 @@ class InitSoundFileAction extends ReduxAction<AppState> {
3334
}
3435

3536
class SoundFileLinkAction extends ReduxAction<AppState> {
36-
3737
final SoundFileSource source;
3838

3939
SoundFileLinkAction(this.source);
@@ -43,20 +43,28 @@ class SoundFileLinkAction extends ReduxAction<AppState> {
4343
if (state.selectedSoundEvent == null) return null;
4444

4545
final SoundEventModel soundModel = state.selectedSoundEvent!;
46-
final List<SoundFileSource> list = List.of(soundModel.files.items, growable: true);
46+
final List<SoundFileSource> list = List.of(
47+
soundModel.files.items,
48+
growable: true,
49+
);
4750
final SoundClientApi soundApi = ApiService().soundApi as SoundClientApi;
48-
final SoundFileSource linkedFile = await soundApi.linkFile(soundModel.id!, source);
51+
final SoundFileSource linkedFile = await soundApi.linkFile(
52+
soundModel.id!,
53+
source,
54+
);
4955

5056
list.add(linkedFile);
5157

5258
// Create a new PaginatedResult with the new list and updated counts
53-
final PaginatedResult<SoundFileSource> updatedSources = soundModel.files.copyWith(
59+
final PaginatedResult<SoundFileSource>
60+
updatedSources = soundModel.files.copyWith(
5461
items: list,
5562
totalItems: soundModel.files.totalItems + 1, // Adjust counts as necessary
5663
// Potentially update totalPages if this new item pushes it to a new page
5764
);
58-
return state.copyWith(selectedSoundEvent: soundModel.copyWith(files: updatedSources));
59-
65+
return state.copyWith(
66+
selectedSoundEvent: soundModel.copyWith(files: updatedSources),
67+
);
6068
}
6169
}
6270

@@ -73,7 +81,10 @@ class SoundFileUpdateAction extends ReduxAction<AppState> {
7381
final SoundClientApi soundApi = ApiService().soundApi as SoundClientApi;
7482

7583
// Call the API to update the file
76-
final SoundFileSource updatedFile = await soundApi.updateFile(soundModel.id!, soundFile);
84+
final SoundFileSource updatedFile = await soundApi.updateFile(
85+
soundModel.id!,
86+
soundFile,
87+
);
7788

7889
// Create a new list with the updated item
7990
final List<SoundFileSource> list = List.of(soundModel.files.items);
@@ -83,14 +94,52 @@ class SoundFileUpdateAction extends ReduxAction<AppState> {
8394
}
8495

8596
// Create a new PaginatedResult with the updated list
86-
final PaginatedResult<SoundFileSource> updatedSources = soundModel.files.copyWith(
87-
items: list,
88-
);
97+
final PaginatedResult<SoundFileSource> updatedSources = soundModel.files
98+
.copyWith(items: list);
8999

90-
return state.copyWith(selectedSoundEvent: soundModel.copyWith(files: updatedSources));
100+
return state.copyWith(
101+
selectedSoundEvent: soundModel.copyWith(files: updatedSources),
102+
);
91103
}
92104
}
93105

106+
class SoundFileSourceDeleteAction extends ReduxAction<AppState> {
107+
final SoundFileSource soundFile;
108+
109+
SoundFileSourceDeleteAction(this.soundFile);
110+
111+
@override
112+
Future<AppState?> reduce() async {
113+
if (state.selectedSoundEvent == null) return null;
114+
115+
final SoundEventModel model = state.selectedSoundEvent!;
116+
final soundClient = ApiService().soundApi as SoundClientApi;
117+
118+
final SoundFileSource deletedFile = await soundClient.deleteFile(
119+
model.id!,
120+
soundFile,
121+
);
122+
123+
final PaginatedResult<SoundFileSource> entries = model.files;
124+
125+
final List<SoundFileSource> updatedFiles = List.of(
126+
entries.items,
127+
growable: true,
128+
);
129+
updatedFiles.removeWhere((file) => file.id == deletedFile.id);
130+
131+
final updatedSoundEvent = model.copyWith(
132+
files: entries.copyWith(
133+
items: updatedFiles,
134+
currentPage: entries.currentPage,
135+
totalPages: entries.totalPages,
136+
totalItems: entries.totalItems - 1,
137+
),
138+
);
139+
140+
return state.copyWith(selectedSoundEvent: updatedSoundEvent);
141+
}
142+
}
94143

95144
class LoadMoreSoundFiles extends ReduxAction<AppState> {
96145
LoadMoreSoundFiles({required this.pageToLoad, this.pageSize = 1});
@@ -143,6 +192,8 @@ class SetSelectedSoundLoading extends ReduxAction<AppState> {
143192
AppState? reduce() {
144193
final sel = state.selectedSoundEvent;
145194
if (sel == null) return null;
146-
return state.copyWith(selectedSoundEvent: sel.copyWith(isLoading: isLoading));
195+
return state.copyWith(
196+
selectedSoundEvent: sel.copyWith(isLoading: isLoading),
197+
);
147198
}
148199
}

0 commit comments

Comments
 (0)