Improved document load flow

This commit is contained in:
Yoshi Askharoun
2021-11-03 20:22:32 -05:00
parent 913a479cbc
commit fea0895885
3 changed files with 53 additions and 38 deletions
@@ -2,6 +2,7 @@
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
namespace ZuneUIXTools.ViewModels
@@ -28,5 +29,37 @@ namespace ZuneUIXTools.ViewModels
get => _selectedDocument;
set => SetProperty(ref _selectedDocument, value);
}
public DocumentViewModelBase LoadDocument(string fileName, string fileFormat = null)
{
if (fileFormat == null)
fileFormat = System.IO.Path.GetExtension(fileName);
DocumentViewModelBase doc = UIXDocuments.FirstOrDefault(oldDoc => fileName == oldDoc.FileName);
if (doc != null)
return doc;
if (fileFormat.EndsWith(".uix"))
doc = new UIXDocumentViewModel(fileName);
else if (fileFormat.EndsWith(".uib"))
doc = new CompiledUIXDocumentViewModel(fileName);
else
throw new ArgumentException($"\"{fileName}\" is not a known format.");
UIXDocuments.Add(doc);
return doc;
}
public void UnloadDocument(string fileName)
{
DocumentViewModelBase doc = UIXDocuments.FirstOrDefault(oldDoc => fileName == oldDoc.FileName);
if (doc != null)
UIXDocuments.Remove(doc);
}
public void UnloadDocument(DocumentViewModelBase doc)
{
UIXDocuments.Remove(doc);
}
}
}