20
20
using NexusMods . MnemonicDB . Abstractions . ElementComparers ;
21
21
using NexusMods . MnemonicDB . Abstractions . TxFunctions ;
22
22
using ObservableCollections ;
23
+ using OneOf ;
23
24
using R3 ;
24
25
using ReactiveUI ;
25
26
using ReactiveUI . Fody . Helpers ;
@@ -150,38 +151,21 @@ public LoadoutViewModel(IWindowManager windowManager, IServiceProvider servicePr
150
151
Adapter . Activate ( ) . AddTo ( disposables ) ;
151
152
152
153
Adapter . MessageSubject . SubscribeAwait ( async ( message , _ ) =>
153
- {
154
- var toggleableItems = message . Ids
155
- . Select ( loadoutItemId => LoadoutItem . Load ( connection . Db , loadoutItemId ) )
156
- // Exclude collection required items
157
- . Where ( item => ! ( NexusCollectionItemLoadoutGroup . IsRequired . TryGetValue ( item , out var isRequired ) && isRequired ) )
158
- // Exclude items that are part of a collection that is disabled
159
- . Where ( item => ! ( item . Parent . TryGetAsCollectionGroup ( out var collectionGroup )
160
- && collectionGroup . AsLoadoutItemGroup ( ) . AsLoadoutItem ( ) . IsDisabled )
161
- )
162
- . ToArray ( ) ;
163
-
164
- if ( toggleableItems . Length == 0 ) return ;
165
-
166
- // We only enable if all items are disabled, otherwise we disable
167
- var shouldEnable = toggleableItems . All ( loadoutItem => loadoutItem . IsDisabled ) ;
168
-
169
- using var tx = connection . BeginTransaction ( ) ;
154
+ {
155
+ // Toggle item state
156
+ if ( message . IsT0 ) {
157
+ await ToggleItemEnabledState ( message . AsT0 . Ids , connection ) ;
158
+ return ;
159
+ }
170
160
171
- foreach ( var id in toggleableItems )
161
+ // Open collection
162
+ if ( message . IsT1 )
172
163
{
173
- if ( shouldEnable )
174
- {
175
- tx . Retract ( id , LoadoutItem . Disabled , Null . Instance ) ;
176
- }
177
- else
178
- {
179
- tx . Add ( id , LoadoutItem . Disabled , Null . Instance ) ;
180
- }
164
+ var data = message . AsT1 ;
165
+ OpenItemCollectionPage ( data . Ids , data . NavigationInformation , loadoutId , GetWorkspaceController ( ) , connection ) ;
166
+ return ;
181
167
}
182
168
183
- await tx . Commit ( ) ;
184
-
185
169
} , awaitOperation : AwaitOperation . Parallel , configureAwait : false ) . AddTo ( disposables ) ;
186
170
187
171
// Compute the target group for the ViewFilesCommand
@@ -208,6 +192,87 @@ public LoadoutViewModel(IWindowManager windowManager, IServiceProvider servicePr
208
192
} ) ;
209
193
}
210
194
195
+ internal static async Task ToggleItemEnabledState ( LoadoutItemId [ ] ids , IConnection connection )
196
+ {
197
+ var toggleableItems = ids
198
+ . Select ( loadoutItemId => LoadoutItem . Load ( connection . Db , loadoutItemId ) )
199
+ // Exclude collection required items
200
+ . Where ( item => ! ( NexusCollectionItemLoadoutGroup . IsRequired . TryGetValue ( item , out var isRequired ) && isRequired ) )
201
+ // Exclude items that are part of a collection that is disabled
202
+ . Where ( item => ! ( item . Parent . TryGetAsCollectionGroup ( out var collectionGroup )
203
+ && collectionGroup . AsLoadoutItemGroup ( ) . AsLoadoutItem ( ) . IsDisabled )
204
+ )
205
+ . ToArray ( ) ;
206
+
207
+ if ( toggleableItems . Length == 0 ) return ;
208
+
209
+ // We only enable if all items are disabled, otherwise we disable
210
+ var shouldEnable = toggleableItems . All ( loadoutItem => loadoutItem . IsDisabled ) ;
211
+
212
+ using var tx = connection . BeginTransaction ( ) ;
213
+
214
+ foreach ( var id in toggleableItems )
215
+ {
216
+ if ( shouldEnable )
217
+ {
218
+ tx . Retract ( id , LoadoutItem . Disabled , Null . Instance ) ;
219
+ }
220
+ else
221
+ {
222
+ tx . Add ( id , LoadoutItem . Disabled , Null . Instance ) ;
223
+ }
224
+ }
225
+
226
+ await tx . Commit ( ) ;
227
+ }
228
+
229
+ internal static void OpenItemCollectionPage (
230
+ LoadoutItemId [ ] ids ,
231
+ NavigationInformation navInfo ,
232
+ LoadoutId loadoutId ,
233
+ IWorkspaceController workspaceController ,
234
+ IConnection connection )
235
+ {
236
+ if ( ids . Length == 0 ) return ;
237
+
238
+ // Open the collection page for the first item
239
+ var firstItemId = ids . First ( ) ;
240
+
241
+ var parentGroup = LoadoutItem . Load ( connection . Db , firstItemId ) . Parent ;
242
+ if ( ! parentGroup . TryGetAsCollectionGroup ( out var collectionGroup ) ) return ;
243
+
244
+ if ( collectionGroup . TryGetAsNexusCollectionLoadoutGroup ( out var nexusCollectionGroup ) )
245
+ {
246
+ var nexusCollPageData = new PageData
247
+ {
248
+ FactoryId = CollectionLoadoutPageFactory . StaticId ,
249
+ Context = new CollectionLoadoutPageContext
250
+ {
251
+ LoadoutId = loadoutId ,
252
+ GroupId = nexusCollectionGroup . Id ,
253
+ } ,
254
+ } ;
255
+ var nexusPageBehavior = workspaceController . GetOpenPageBehavior ( nexusCollPageData , navInfo ) ;
256
+ workspaceController . OpenPage ( workspaceController . ActiveWorkspaceId , nexusCollPageData , nexusPageBehavior ) ;
257
+
258
+ return ;
259
+ }
260
+
261
+ var pageData = new PageData
262
+ {
263
+ FactoryId = LoadoutPageFactory . StaticId ,
264
+ Context = new LoadoutPageContext
265
+ {
266
+ LoadoutId = loadoutId ,
267
+ GroupScope = collectionGroup . AsLoadoutItemGroup ( ) . LoadoutItemGroupId ,
268
+ } ,
269
+ } ;
270
+ var behavior = workspaceController . GetOpenPageBehavior ( pageData , navInfo ) ;
271
+ workspaceController . OpenPage ( workspaceController . ActiveWorkspaceId , pageData , behavior ) ;
272
+
273
+ return ;
274
+ }
275
+
211
276
private static async Task ToggleCollectionGroup ( Optional < LoadoutItemGroupId > collectionGroupId , bool shouldEnable , IConnection connection )
212
277
{
213
278
if ( ! collectionGroupId . HasValue ) return ;
@@ -229,13 +294,16 @@ private static IEnumerable<LoadoutItemId> GetLoadoutItemIds(CompositeItemModel<E
229
294
}
230
295
}
231
296
232
- public readonly record struct ToggleEnableState ( LoadoutItemId [ ] Ids , bool ShouldEnable ) ;
297
+ public readonly record struct ToggleEnableStateMessage ( LoadoutItemId [ ] Ids ) ;
298
+
299
+ public readonly record struct OpenCollectionMessage ( LoadoutItemId [ ] Ids , NavigationInformation NavigationInformation ) ;
300
+
233
301
234
302
public class LoadoutTreeDataGridAdapter :
235
303
TreeDataGridAdapter < CompositeItemModel < EntityId > , EntityId > ,
236
- ITreeDataGirdMessageAdapter < ToggleEnableState >
304
+ ITreeDataGirdMessageAdapter < OneOf < ToggleEnableStateMessage , OpenCollectionMessage > >
237
305
{
238
- public Subject < ToggleEnableState > MessageSubject { get ; } = new ( ) ;
306
+ public Subject < OneOf < ToggleEnableStateMessage , OpenCollectionMessage > > MessageSubject { get ; } = new ( ) ;
239
307
240
308
private readonly ILoadoutDataProvider [ ] _loadoutDataProviders ;
241
309
private readonly LoadoutFilter _loadoutFilter ;
@@ -261,11 +329,45 @@ protected override void BeforeModelActivationHook(CompositeItemModel<EntityId> m
261
329
factory : static ( self , itemModel , component ) => component . CommandToggle . Subscribe ( ( self , itemModel , component ) , static ( _ , tuple ) =>
262
330
{
263
331
var ( self , itemModel , component ) = tuple ;
264
- var isEnabled = component . Value . Value ;
265
332
var ids = GetLoadoutItemIds ( itemModel ) . ToArray ( ) ;
266
- var shouldEnable = ! isEnabled ?? false ;
267
333
268
- self . MessageSubject . OnNext ( new ToggleEnableState ( ids , shouldEnable ) ) ;
334
+ self . MessageSubject . OnNext ( new ToggleEnableStateMessage ( ids ) ) ;
335
+ } )
336
+ ) ;
337
+
338
+ model . SubscribeToComponentAndTrack < LoadoutComponents . ParentCollectionDisabled , LoadoutTreeDataGridAdapter > (
339
+ key : LoadoutColumns . EnabledState . ParentCollectionDisabledComponentKey ,
340
+ state : this ,
341
+ factory : static ( self , itemModel , component ) => component . ButtonCommand . Subscribe ( ( self , itemModel , component ) , static ( navInfo , tuple ) =>
342
+ {
343
+ var ( self , itemModel , component ) = tuple ;
344
+ var ids = GetLoadoutItemIds ( itemModel ) . ToArray ( ) ;
345
+
346
+ self . MessageSubject . OnNext ( new OpenCollectionMessage ( ids , navInfo ) ) ;
347
+ } )
348
+ ) ;
349
+
350
+ model . SubscribeToComponentAndTrack < LoadoutComponents . LockedEnabledState , LoadoutTreeDataGridAdapter > (
351
+ key : LoadoutColumns . EnabledState . LockedEnabledStateComponentKey ,
352
+ state : this ,
353
+ factory : static ( self , itemModel , component ) => component . ButtonCommand . Subscribe ( ( self , itemModel , component ) , static ( navInfo , tuple ) =>
354
+ {
355
+ var ( self , itemModel , component ) = tuple ;
356
+ var ids = GetLoadoutItemIds ( itemModel ) . ToArray ( ) ;
357
+
358
+ self . MessageSubject . OnNext ( new OpenCollectionMessage ( ids , navInfo ) ) ;
359
+ } )
360
+ ) ;
361
+
362
+ model . SubscribeToComponentAndTrack < LoadoutComponents . MixLockedAndParentDisabled , LoadoutTreeDataGridAdapter > (
363
+ key : LoadoutColumns . EnabledState . MixLockedAndParentDisabledComponentKey ,
364
+ state : this ,
365
+ factory : static ( self , itemModel , component ) => component . ButtonCommand . Subscribe ( ( self , itemModel , component ) , static ( navInfo , tuple ) =>
366
+ {
367
+ var ( self , itemModel , component ) = tuple ;
368
+ var ids = GetLoadoutItemIds ( itemModel ) . ToArray ( ) ;
369
+
370
+ self . MessageSubject . OnNext ( new OpenCollectionMessage ( ids , navInfo ) ) ;
269
371
} )
270
372
) ;
271
373
}
0 commit comments