/// <summary> /// Creates new empty LibraryViewModel using given ManagerViewModel, Notifier, and imageGrid element /// </summary> /// <param name="manager">ManagerViewModel to be linked to this library</param> /// <param name="notifier">Notifier used to send toast notifs (from main VM)</param> /// <param name="imageGrid">ListView element used to display image grid on front-end</param> public LibraryViewModel(ManagerViewModel manager, Notifier notifier, ListView imageGrid) { // Create new empty library list LibraryList = new ObservableCollection <WBImage>(); // This library will be empty (it's new) IsEmpty = true; // Link the manager and set the notifier and ImageGrid Manager = manager; _notifier = notifier; ImageGrid = imageGrid; // Create SortTypes and SortDirections SortTypes = new List <string>() { "Brightness", "Date Added", "Enabled" }; SortDirections = new List <string>() { "Descending", "Ascending" }; // Create commands CreateCommands(); }
/// <summary> /// Creates new MainViewModel; attempts to pull Library and Manager from file and creates default /// ones if either fails /// </summary> /// <param name="mainWindow">Main display window</param> /// <param name="startingMinimized">True when starting minimized (ie from Windows startup), false /// otherwise; will only show window after startup if this is set true</param> /// <param name="imageGrid">Front-end ListView element used for displaying images on main window</param> public MainViewModel(MainWindow mainWindow, bool startingMinimized, ListView imageGrid) { // Create notifier for sending toast notifs CreateNotifier(); // Only show window if not starting minimized if (!startingMinimized) { mainWindow.Show(); } // Try to pull library from last lib file LibraryViewModel libraryFromFile = GetLibraryFromLastLibFile(startingMinimized); // If successful, use the library from file if (libraryFromFile != null) { Library = libraryFromFile; Library.ImageGrid = imageGrid; } // Otherwise create new empty library else { Library = new LibraryViewModel(Manager, _notifier, imageGrid); } // Try to pull new manager from settings file ManagerViewModel managerFromFile = GetManagerFromSettingsFile(startingMinimized); // If successful, use the manager from file if (managerFromFile != null) { Manager = managerFromFile; } // If unsuccessful (e.g. no settings file exists), just create new manager with default settings else { Manager = new ManagerViewModel(Library); } // Link library and manager after creation Library.Manager = Manager; Manager.Library = Library; // Check and update before displaying Manager.CheckAndUpdate(); // Store window and image grid element _window = mainWindow; _imageGrid = imageGrid; CreateCommands(); }
/// <summary> /// Attempts to pull manager settings from AppData file and returns ManagerViewModel with those /// settings if successful /// </summary> /// <param name="startingMinimized">True if starting app minimized, false otherwise; if true, will /// display success/failure toast notifs on loading settings file</param> /// <returns>ManagerViewModel with settings from AppData file if successful; null if unsuccessful</returns> public ManagerViewModel GetManagerFromSettingsFile(bool startingMinimized) { // Pull AppData directory (will differ depending on user + drive) string wallBriteAppDataDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\WallBrite"; // Check if there is settings file in the AppData directory; if not then skip loading settings file if (File.Exists(wallBriteAppDataDirectory + "\\Settings.json")) { // If file exists, try to open it try { ManagerViewModel newManager; // Open the file and create newManager using setings from it using (FileStream fileStream = File.OpenRead(wallBriteAppDataDirectory + "\\Settings.json")) newManager = new ManagerViewModel(Library, GetManagerSettingsFromStream(fileStream)); // Show success toast notif if not starting minimized if (!startingMinimized) { _notifier.ShowInformation("Successfully loaded last used WallBrite settings"); } return(newManager); } // If file fails to open, send failure toast notif and return null catch (IOException) { // Show failure toast notif not starting minimized if (!startingMinimized) { _notifier.ShowError("Failed to load last used WallBrite settings: file corrupted or in use by another program"); } return(null); } } // If file doesn't exist, return null return(null); }
/// <summary> /// Creates new LibraryViewModel using given libraryList, ManagerViewModel, Notifier, and imageGrid element /// </summary> /// <param name="libraryList">Array of WBImages to be represented by this library</param> /// <param name="manager">ManagerViewModel to be linked to this library</param> /// <param name="notifier">otifier used to send toast notifs (from main VM)</param> /// /// <param name="imageGrid">ListView element used to display image grid on front-end</param> public LibraryViewModel(WBImage[] libraryList, ManagerViewModel manager, Notifier notifier, ListView imageGrid) { // Create library list from given array LibraryList = new ObservableCollection <WBImage>(libraryList); // Set empty fla true if this library is empty; otherwise false if (LibraryList.Count < 1) { IsEmpty = true; } else { IsEmpty = false; } // Link the manager and set the notifier and ImageGrid Manager = manager; _notifier = notifier; ImageGrid = imageGrid; // Create SortTypes and SortDirections SortTypes = new List <string>() { "Brightness", "Date Added", "Enabled" }; SortDirections = new List <string>() { "Descending", "Ascending" }; // Set default sort type and direction SortType = "Date Added"; SortDirection = "Ascending"; // Create commands CreateCommands(); }