Ejemplo n.º 1
0
        /// <summary>
        /// Gets the manager.
        /// </summary>
        /// <returns>IManagedNotificationManager.</returns>
        public IManagedNotificationManager GetManager()
        {
            if (manager is WindowNotificationManager windowNotificationManager)
            {
                if (positionSettingsService == null)
                {
                    positionSettingsService = DIResolver.Get <INotificationPositionSettingsService>();
                }
                var selected = positionSettingsService.Get().FirstOrDefault(p => p.IsSelected);
                if (selected != null)
                {
                    switch (selected.Type)
                    {
                    case Models.Common.NotificationPosition.BottomRight:
                        windowNotificationManager.Position = NotificationPosition.BottomRight;
                        break;

                    case Models.Common.NotificationPosition.BottomLeft:
                        windowNotificationManager.Position = NotificationPosition.BottomLeft;
                        break;

                    case Models.Common.NotificationPosition.TopRight:
                        windowNotificationManager.Position = NotificationPosition.TopRight;
                        break;

                    case Models.Common.NotificationPosition.TopLeft:
                        windowNotificationManager.Position = NotificationPosition.TopLeft;
                        break;

                    default:
                        break;
                    }
                }
            }
            return(manager);
        }
        /// <summary>
        /// Called when [activated].
        /// </summary>
        /// <param name="disposables">The disposables.</param>
        protected override void OnActivated(CompositeDisposable disposables)
        {
            SetGame(gameService.GetSelected());
            SetEditor(externalEditorService.Get());
            SetNotificationPosition(positionSettingsService.Get());
            var updateSettings = updaterService.Get();

            if (updateSettings.AutoUpdates == null)
            {
                async Task showPrompt()
                {
                    var title   = localizationManager.GetResource(LocalizationResources.Options.Updates.AutoUpdatePrompts.Title);
                    var message = localizationManager.GetResource(LocalizationResources.Options.Updates.AutoUpdatePrompts.Message);

                    if (await notificationAction.ShowPromptAsync(title, title, message, NotificationType.Info))
                    {
                        updateSettings.AutoUpdates = true;
                        SaveUpdateSettings();
                        await CheckForUpdatesAsync(true);
                    }
                    else
                    {
                        updateSettings.AutoUpdates = false;
                        SaveUpdateSettings();
                    }
                }

                showPrompt().ConfigureAwait(false);
            }
            else if (updateSettings.AutoUpdates.GetValueOrDefault())
            {
                CheckForUpdatesAsync(true).ConfigureAwait(false);
            }
            SetUpdateSettings(updateSettings);

            var updateCheckAllowed = this.WhenAnyValue(p => p.CheckingForUpdates, v => !v);

            OptionsCommand = ReactiveCommand.Create(() =>
            {
                IsOpen = true;
            }).DisposeWith(disposables);

            CloseCommand = ReactiveCommand.Create(() =>
            {
                IsOpen = false;
            }).DisposeWith(disposables);

            NavigateExeCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                IsOpen     = false;
                var result = await fileDialogAction.OpenDialogAsync(NavigateExeTitle);
                IsOpen     = true;
                if (!string.IsNullOrWhiteSpace(result))
                {
                    Game.ExecutableLocation = result;
                    if (string.IsNullOrWhiteSpace(Game.LaunchArguments) || string.IsNullOrWhiteSpace(Game.UserDirectory))
                    {
                        var defaultSettings = gameService.GetDefaultGameSettings(Game);
                        if (string.IsNullOrWhiteSpace(Game.LaunchArguments))
                        {
                            Game.LaunchArguments = defaultSettings.LaunchArguments;
                        }
                        if (string.IsNullOrWhiteSpace(Game.UserDirectory))
                        {
                            Game.UserDirectory = defaultSettings.UserDirectory;
                        }
                    }
                    SaveGame();
                }
            }).DisposeWith(disposables);

            NavigateEditorCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                IsOpen     = false;
                var result = await fileDialogAction.OpenDialogAsync(NavigateEditorTitle);
                IsOpen     = true;
                if (!string.IsNullOrWhiteSpace(result))
                {
                    Editor.ExternalEditorLocation = result;
                    SaveEditor();
                }
            }).DisposeWith(disposables);

            NavigateDirectoryCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                IsOpen     = false;
                var result = await fileDialogAction.OpenFolderDialogAsync(NavigateUserDirTitle);
                IsOpen     = true;
                if (!string.IsNullOrWhiteSpace(result))
                {
                    Game.UserDirectory = result;
                    SaveGame();
                }
            }).DisposeWith(disposables);

            ResetExeCommand = ReactiveCommand.Create(() =>
            {
                var defaultSettings     = gameService.GetDefaultGameSettings(Game);
                Game.ExecutableLocation = defaultSettings.ExecutableLocation;
                if (string.IsNullOrWhiteSpace(Game.LaunchArguments))
                {
                    Game.LaunchArguments = defaultSettings.LaunchArguments;
                }
                if (string.IsNullOrWhiteSpace(Game.UserDirectory))
                {
                    Game.UserDirectory = defaultSettings.UserDirectory;
                }
                SaveGame();
            }).DisposeWith(disposables);

            ResetEditorExeCommand = ReactiveCommand.Create(() =>
            {
                Editor.ExternalEditorLocation = string.Empty;
                SaveEditor();
            }).DisposeWith(disposables);

            ResetDirectoryCommand = ReactiveCommand.Create(() =>
            {
                var defaultSettings = gameService.GetDefaultGameSettings(Game);
                Game.UserDirectory  = defaultSettings.UserDirectory;
                SaveGame();
            }).DisposeWith(disposables);

            ResetArgsCommand = ReactiveCommand.Create(() =>
            {
                Game.LaunchArguments = gameService.GetDefaultGameSettings(Game).LaunchArguments;
            }).DisposeWith(disposables);

            ResetEditorArgsCommand = ReactiveCommand.Create(() =>
            {
                Editor.ExternalEditorParameters = string.Empty;
            }).DisposeWith(disposables);

            AutoConfigureCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                IsOpen     = false;
                var result = await fileDialogAction.OpenFolderDialogAsync(NavigateGameTitle);
                IsOpen     = true;
                if (!string.IsNullOrWhiteSpace(result))
                {
                    var settings = gameService.GetGameSettingsFromJson(Game, result);
                    if (settings != null)
                    {
                        Game.UserDirectory      = settings.UserDirectory;
                        Game.ExecutableLocation = settings.ExecutableLocation;
                        Game.LaunchArguments    = settings.LaunchArguments;
                        SaveGame();
                    }
                }
            }).DisposeWith(disposables);

            CheckForUpdatesCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                await CheckForUpdatesAsync();
                IsOpen = true;
            }, updateCheckAllowed).DisposeWith(disposables);

            var  downloadingUpdates = false;
            var  installingUpdates  = false;
            long messageId          = 0;

            InstallUpdatesCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                messageId          = idGenerator.GetNextId();
                downloadingUpdates = true;
                await TriggerOverlayAsync(messageId, true, localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateDownloading));
                if (await updater.DownloadUpdateAsync())
                {
                    downloadingUpdates = false;
                    await TriggerOverlayAsync(messageId, true, localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateInstalling));
                    installingUpdates = true;
                    await updater.InstallUpdateAsync();
                    installingUpdates = false;
                    await TriggerOverlayAsync(messageId, false);
                }
                else
                {
                    downloadingUpdates = false;
                    await TriggerOverlayAsync(messageId, false);
                }
            }).DisposeWith(disposables);

            NavigateCustomDirectoryCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                IsOpen     = false;
                var result = await fileDialogAction.OpenFolderDialogAsync(NavigateUserDirTitle);
                var save   = !string.IsNullOrEmpty(result);
                if (save && !await modService.CustomModDirectoryEmptyAsync(Game.Type))
                {
                    var title   = localizationManager.GetResource(LocalizationResources.Options.Prompts.CustomModDirectory.Title);
                    var message = localizationManager.GetResource(LocalizationResources.Options.Prompts.CustomModDirectory.Message);
                    save        = await notificationAction.ShowPromptAsync(title, title, message, NotificationType.Warning);
                }
                IsOpen = true;
                if (save)
                {
                    Game.CustomModDirectory = result;
                    SaveGame();
                }
            }).DisposeWith(disposables);

            ResetCustomDirectoryCommand = ReactiveCommand.CreateFromTask(async() =>
            {
                var save = true;
                if (!await modService.CustomModDirectoryEmptyAsync(Game.Type))
                {
                    var title   = localizationManager.GetResource(LocalizationResources.Options.Prompts.CustomModDirectory.Title);
                    var message = localizationManager.GetResource(LocalizationResources.Options.Prompts.CustomModDirectory.Message);
                    save        = await notificationAction.ShowPromptAsync(title, title, message, NotificationType.Warning);
                }
                if (save)
                {
                    var defaultSettings     = gameService.GetDefaultGameSettings(Game);
                    Game.CustomModDirectory = defaultSettings.CustomModDirectory;
                    SaveGame();
                }
            }).DisposeWith(disposables);

            updater.Error.Subscribe(s =>
            {
                var title   = localizationManager.GetResource(LocalizationResources.Options.Updates.Errors.DownloadErrorTitle);
                var message = localizationManager.GetResource(LocalizationResources.Options.Updates.Errors.DownloadErrorMessage);
                logger.Error(s);
                notificationAction.ShowNotification(title, message, NotificationType.Error, 30);
                TriggerOverlay(messageId, false);
            }).DisposeWith(disposables);

            updater.Progress.Subscribe(s =>
            {
                if (downloadingUpdates)
                {
                    var message  = localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateDownloading);
                    var progress = Smart.Format(localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateDownloadProgress), new { Progress = s.ToLocalizedPercentage() });
                    TriggerOverlay(messageId, true, message, progress);
                }
                else if (installingUpdates)
                {
                    var message  = localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateInstalling);
                    var progress = Smart.Format(localizationManager.GetResource(LocalizationResources.Options.Updates.Overlay.UpdateDownloadProgress), new { Progress = s.ToLocalizedPercentage() });
                    TriggerOverlay(messageId, true, message, progress);
                }
            }).DisposeWith(disposables);

            base.OnActivated(disposables);
        }