|
1 | 1 | using Avalonia;
|
2 | 2 | using Avalonia.Platform;
|
3 | 3 | using Avalonia.Platform.Storage;
|
| 4 | +using DynamicData; |
4 | 5 | using NuGet.Versioning;
|
5 | 6 | using OpenLoco.Common.Logging;
|
6 | 7 | using OpenLoco.Dat;
|
@@ -41,7 +42,7 @@ public class MainWindowViewModel : ViewModelBase
|
41 | 42 | [Reactive]
|
42 | 43 | public TabViewPageViewModel CurrentTabModel { get; set; } = new();
|
43 | 44 |
|
44 |
| - public ObservableCollection<MenuItemViewModel> ObjDataItems { get; } |
| 45 | + public ObservableCollection<MenuItemViewModel> ObjDataItems { get; init; } = []; |
45 | 46 |
|
46 | 47 | public ObservableCollection<LogLine> Logs => Model.LoggerObservableLogs;
|
47 | 48 |
|
@@ -103,12 +104,7 @@ public MainWindowViewModel()
|
103 | 104 | _ = CurrentTabModel.WhenAnyValue(o => o.SelectedDocument)
|
104 | 105 | .Subscribe((x) => FolderTreeViewModel.CurrentlySelectedObject = x?.CurrentFile);
|
105 | 106 |
|
106 |
| - ObjDataItems = new ObservableCollection<MenuItemViewModel>(Model.Settings.ObjDataDirectories |
107 |
| - .Select(x => new MenuItemViewModel( |
108 |
| - x, |
109 |
| - ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = x)))); |
110 |
| - ObjDataItems.Insert(0, new MenuItemViewModel("Add new folder", ReactiveCommand.Create(SelectNewFolder))); |
111 |
| - ObjDataItems.Insert(1, new MenuItemViewModel("-", ReactiveCommand.Create(() => { }))); |
| 107 | + PopulateObjDataMenu(); |
112 | 108 |
|
113 | 109 | OpenSingleObject = ReactiveCommand.Create(LoadSingleObject);
|
114 | 110 | OpenDownloadFolder = ReactiveCommand.Create(() => PlatformSpecific.FolderOpenInDesktop(Model.Settings.DownloadFolder, Model.Logger));
|
@@ -169,6 +165,36 @@ public MainWindowViewModel()
|
169 | 165 | #endregion
|
170 | 166 | }
|
171 | 167 |
|
| 168 | + void PopulateObjDataMenu() |
| 169 | + { |
| 170 | + ObjDataItems.Clear(); |
| 171 | + |
| 172 | + ObjDataItems.Add(new MenuItemViewModel("Add new folder", ReactiveCommand.Create(SelectNewFolder))); |
| 173 | + ObjDataItems.Add(new MenuItemViewModel("-", ReactiveCommand.Create(() => { }))); |
| 174 | + |
| 175 | + if (Directory.Exists(Model.Settings.AppDataObjDataFolder)) |
| 176 | + { |
| 177 | + ObjDataItems.Add(new MenuItemViewModel($"[{nameof(GameObjDataFolder.AppData)}] {Model.Settings.AppDataObjDataFolder}", ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = Model.Settings.AppDataObjDataFolder))); |
| 178 | + } |
| 179 | + if (Directory.Exists(Model.Settings.LocomotionObjDataFolder)) |
| 180 | + { |
| 181 | + ObjDataItems.Add(new MenuItemViewModel($"[{nameof(GameObjDataFolder.Locomotion)}] {Model.Settings.LocomotionObjDataFolder}", ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = Model.Settings.LocomotionObjDataFolder))); |
| 182 | + } |
| 183 | + if (Directory.Exists(Model.Settings.OpenLocoObjDataFolder)) |
| 184 | + { |
| 185 | + ObjDataItems.Add(new MenuItemViewModel($"[{nameof(GameObjDataFolder.OpenLoco)}] {Model.Settings.OpenLocoObjDataFolder}", ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = Model.Settings.OpenLocoObjDataFolder))); |
| 186 | + } |
| 187 | + |
| 188 | + ObjDataItems.Add(new MenuItemViewModel("-", ReactiveCommand.Create(() => { }))); |
| 189 | + |
| 190 | + // add the rest |
| 191 | + ObjDataItems.AddRange( |
| 192 | + Model.Settings.ObjDataDirectories |
| 193 | + .Select(x => new MenuItemViewModel( |
| 194 | + x, |
| 195 | + ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = x)))); |
| 196 | + } |
| 197 | + |
172 | 198 | public static async Task<FileSystemItemBase?> GetFileSystemItemFromUser(IReadOnlyList<FilePickerFileType> filetypes)
|
173 | 199 | {
|
174 | 200 | var openFile = await PlatformSpecific.OpenFilePicker(filetypes);
|
@@ -362,16 +388,40 @@ public async Task SelectNewFolder()
|
362 | 388 | }
|
363 | 389 |
|
364 | 390 | var dirPath = dir.Path.LocalPath;
|
365 |
| - if (Directory.Exists(dirPath) && !Model.Settings.ObjDataDirectories.Contains(dirPath)) |
366 |
| - { |
367 |
| - FolderTreeViewModel.CurrentLocalDirectory = dirPath; // this will cause the reindexing |
368 |
| - var menuItem = new MenuItemViewModel( |
369 |
| - dirPath, |
370 |
| - ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = dirPath) |
371 |
| - /*ReactiveCommand.Create(() => ObjDataItems.RemoveAt(ObjDataItems.Count))*/); |
372 | 391 |
|
373 |
| - ObjDataItems.Add(menuItem); |
| 392 | + if (!Directory.Exists(dirPath)) |
| 393 | + { |
| 394 | + Model.Logger.Warning("Directory doesn't exist"); |
| 395 | + return; |
| 396 | + } |
| 397 | + if (Model.Settings.ObjDataDirectories.Contains(dirPath)) |
| 398 | + { |
| 399 | + Model.Logger.Warning("Object directory is already in the list"); |
| 400 | + return; |
374 | 401 | }
|
| 402 | + if (Model.Settings.AppDataObjDataFolder != dirPath) |
| 403 | + { |
| 404 | + Model.Logger.Warning("No need to add - this is the predefined AppData folder"); |
| 405 | + return; |
| 406 | + } |
| 407 | + if (Model.Settings.LocomotionObjDataFolder != dirPath) |
| 408 | + { |
| 409 | + Model.Logger.Warning("No need to add - this is the predefined Locomotion ObjData folder"); |
| 410 | + return; |
| 411 | + } |
| 412 | + if (Model.Settings.OpenLocoObjDataFolder != dirPath) |
| 413 | + { |
| 414 | + Model.Logger.Warning("No need to add - this is the predefined OpenLoco object folder"); |
| 415 | + return; |
| 416 | + } |
| 417 | + |
| 418 | + FolderTreeViewModel.CurrentLocalDirectory = dirPath; // this will cause the reindexing |
| 419 | + var menuItem = new MenuItemViewModel( |
| 420 | + dirPath, |
| 421 | + ReactiveCommand.Create(() => FolderTreeViewModel.CurrentLocalDirectory = dirPath) |
| 422 | + /*ReactiveCommand.Create(() => ObjDataItems.RemoveAt(ObjDataItems.Count))*/); |
| 423 | + |
| 424 | + ObjDataItems.Add(menuItem); |
375 | 425 | }
|
376 | 426 |
|
377 | 427 | public static bool IsDarkTheme
|
|
0 commit comments