@@ -7,7 +7,6 @@ import 'package:stelaris/api/service/client/sound_client_api.dart';
77import 'package:stelaris/api/state/app_state.dart' ;
88
99class 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
3536class 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
95144class 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