163 lines
5.3 KiB
C#
163 lines
5.3 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.ObjectModel;
|
|
using System.Collections.Specialized;
|
|
using System.Windows.Input;
|
|
using RGBController2.Commands;
|
|
using RGBController2.ViewModels.Tabs;
|
|
using RGBController2.ViewModels.Dialogs;
|
|
using System.ComponentModel;
|
|
using System.Windows;
|
|
using System.Xml;
|
|
|
|
namespace RGBController2.ViewModels
|
|
{
|
|
class MainViewModel
|
|
{
|
|
private readonly ObservableCollection<Tab> _tabs;
|
|
public ICommand NewTabCommand { get; }
|
|
public static ICollection<Tab> Tabs { get; set; }
|
|
|
|
public MainViewModel()
|
|
{
|
|
NewTabCommand = new ActionCommand(p => NewTab());
|
|
|
|
_tabs = new ObservableCollection<Tab>();
|
|
_tabs.CollectionChanged += Tabs_CollectionChanged;
|
|
|
|
Tabs = _tabs;
|
|
|
|
// Check here that the config file exists
|
|
|
|
// Load the config file
|
|
using (XmlReader reader = XmlReader.Create("config.xml"))
|
|
{
|
|
string name = "";
|
|
string type = "";
|
|
while (reader.Read())
|
|
{
|
|
if (reader.IsStartElement())
|
|
{
|
|
//return only when you have START tag
|
|
switch (reader.Name.ToString())
|
|
{
|
|
case "name":
|
|
name = reader.ReadString();
|
|
break;
|
|
case "type":
|
|
type = reader.ReadString();
|
|
break;
|
|
}
|
|
}
|
|
// Create the tab here
|
|
if (name != "" && type != "")
|
|
{
|
|
switch(type)
|
|
{
|
|
case "arduino":
|
|
_tabs.Add(new ArduinoTab(name));
|
|
break;
|
|
case "cue":
|
|
_tabs.Add(new CueDeviceTab(name));
|
|
break;
|
|
}
|
|
|
|
name = "";
|
|
type = "";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
private void Tabs_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
|
|
{
|
|
ITab tab;
|
|
|
|
switch (e.Action)
|
|
{
|
|
case NotifyCollectionChangedAction.Add:
|
|
tab = (ITab) e.NewItems[0];
|
|
tab.CloseRequested += OnTabCloseRequested;
|
|
break;
|
|
case NotifyCollectionChangedAction.Remove:
|
|
tab = (ITab) e.OldItems[0];
|
|
tab.CloseRequested -= OnTabCloseRequested;
|
|
break;
|
|
}
|
|
|
|
}
|
|
|
|
private void OnTabCloseRequested(object sender, EventArgs e)
|
|
{
|
|
Tabs.Remove((Tab) sender);
|
|
}
|
|
|
|
private void NewTab()
|
|
{
|
|
// Open the add tab dialog
|
|
var dialog = new NewTabDialogViewModel();
|
|
dialog.ShowDialogWindow();
|
|
|
|
// Use the results from the dialog when creating the new tab
|
|
if (dialog.Success)
|
|
{
|
|
switch(dialog.SelectedDeviceType)
|
|
{
|
|
case 0: // Arduino
|
|
Tabs.Add(new ArduinoTab(dialog.DeviceName));
|
|
break;
|
|
case 1:
|
|
Tabs.Add(new CueDeviceTab(dialog.DeviceName));
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// This function is called when the application is closed.
|
|
/// This allows the configuration to be saved to disk.
|
|
/// </summary>
|
|
/// <param name="sender"></param>
|
|
/// <param name="e"></param>
|
|
public void OnWindowClosing(object sender, CancelEventArgs e)
|
|
{
|
|
// ToDo - Save the config to disk
|
|
// This may need to be saved somwhere better, for now we will just save it
|
|
// with the executable
|
|
XmlWriter xmlWriter = XmlWriter.Create("config.xml");
|
|
xmlWriter.WriteStartDocument();
|
|
|
|
xmlWriter.WriteStartElement("tabs");
|
|
xmlWriter.WriteAttributeString("count", Tabs.Count.ToString());
|
|
foreach (var tab in Tabs)
|
|
{
|
|
xmlWriter.WriteStartElement("tab");
|
|
xmlWriter.WriteStartElement("name");
|
|
xmlWriter.WriteString(tab.Name);
|
|
xmlWriter.WriteEndElement();
|
|
|
|
switch (tab.TabType)
|
|
{
|
|
case Tab.tabType.Arduino:
|
|
xmlWriter.WriteStartElement("type");
|
|
xmlWriter.WriteString("arduino");
|
|
xmlWriter.WriteEndElement();
|
|
break;
|
|
case Tab.tabType.CUE:
|
|
xmlWriter.WriteStartElement("type");
|
|
xmlWriter.WriteString("cue");
|
|
xmlWriter.WriteEndElement();
|
|
break;
|
|
}
|
|
xmlWriter.WriteEndElement();
|
|
}
|
|
|
|
xmlWriter.WriteEndDocument();
|
|
xmlWriter.Close();
|
|
|
|
|
|
}
|
|
|
|
}
|
|
}
|