Skip to content

Commit

Permalink
Recent Files
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandPheasant committed Dec 17, 2015
1 parent c72b6fe commit d1cdff9
Show file tree
Hide file tree
Showing 21 changed files with 561 additions and 120 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@

namespace TailBlazer.Domain.FileHandling
{
public interface IRecentFiles
public interface IRecentFileCollection
{
IObservableList<RecentFile> Items { get; }

void Register(FileInfo file);
void Add(RecentFile file);

void Remove(RecentFile file);
}
}
65 changes: 65 additions & 0 deletions Source/TailBlazer.Domain/FileHandling/RecentFile.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
using System;
using System.IO;

namespace TailBlazer.Domain.FileHandling
{
public class RecentFile : IEquatable<RecentFile>
{
public DateTime Timestamp { get; }
public string Name { get; }

public RecentFile(FileInfo fileInfo)
{
Name = fileInfo.FullName;
Timestamp = DateTime.Now;
}

public RecentFile(DateTime timestamp, string name)
{
Timestamp = timestamp;
Name = name;
}

#region Equality

public bool Equals(RecentFile other)
{
if (ReferenceEquals(null, other)) return false;
if (ReferenceEquals(this, other)) return true;
return Timestamp.Equals(other.Timestamp) && string.Equals(Name, other.Name);
}

public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((RecentFile) obj);
}

public override int GetHashCode()
{
unchecked
{
return (Timestamp.GetHashCode()*397) ^ (Name != null ? Name.GetHashCode() : 0);
}
}

public static bool operator ==(RecentFile left, RecentFile right)
{
return Equals(left, right);
}

public static bool operator !=(RecentFile left, RecentFile right)
{
return !Equals(left, right);
}

#endregion

public override string ToString()
{
return $"{Name} ({Timestamp})";
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,26 +8,7 @@

namespace TailBlazer.Domain.FileHandling
{

public class RecentFile
{
public DateTime Timestamp { get; }
public string Name { get; }

public RecentFile(FileInfo fileInfo)
{
Name = fileInfo.FullName;
Timestamp = DateTime.Now;
}

public RecentFile(DateTime timestamp, string name)
{
Timestamp = timestamp;
Name = name;
}
}

public class RecentFiles : IRecentFiles, IDisposable
public class RecentFileCollection : IRecentFileCollection, IDisposable
{
private const string SettingsKey = "RecentFiles";

Expand All @@ -37,7 +18,7 @@ public class RecentFiles : IRecentFiles, IDisposable

public IObservableList<RecentFile> Items { get; }

public RecentFiles(ILogger logger, ISettingFactory settingFactory, ISettingsStore store)
public RecentFileCollection(ILogger logger, ISettingFactory settingFactory, ISettingsStore store)
{
if (logger == null) throw new ArgumentNullException(nameof(logger));
if (store == null) throw new ArgumentNullException(nameof(store));
Expand All @@ -55,11 +36,11 @@ public RecentFiles(ILogger logger, ISettingFactory settingFactory, ISettingsStor
_files.Edit(innerCache =>
{
//all files are loaded when state changes, so only add new ones
//var newItems = files
// .Where(f => !innerCache.Lookup(f.FullName).HasValue)
// .ToArray();
var newItems = files
.Where(f => !innerCache.Lookup(f.Name).HasValue)
.ToArray();

//innerCache.AddOrUpdate(newItems);
innerCache.AddOrUpdate(newItems);
});
});

Expand All @@ -73,16 +54,16 @@ public RecentFiles(ILogger logger, ISettingFactory settingFactory, ISettingsStor
_cleanUp = new CompositeDisposable(settingsWriter, loader, _files,Items);
}

public void Register(FileInfo file)
public void Add(RecentFile file)
{
if (file == null) throw new ArgumentNullException(nameof(file));

_files.AddOrUpdate(new RecentFile(file));
_files.AddOrUpdate(file);
}

public void Remove(FileInfo file)
public void Remove(RecentFile file)
{
_files.Remove(file.Name);
_files.Remove(file);

}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,61 @@
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using TailBlazer.Domain.Settings;

namespace TailBlazer.Domain.FileHandling
{
public class RecentFilesToStateConverter: IConverter<RecentFile[]>
{
public RecentFile[] Convert(State state)
private static class Structure
{
public const string Root = "Files";
public const string File = "File";
public const string Name = "Name";
public const string Date = "Date";
}


public RecentFile[] Convert(State state)
{
if (state == null || state == State.Empty)
return new RecentFile[0];

return null;
// return state.Value.FromDelimited(s => new RecentFile(s),Environment.NewLine).ToArray();

//previous format
if (state.Version== 1)
return state.Value.FromDelimited(s => new RecentFile(new FileInfo(s)), Environment.NewLine).ToArray();

//v2 format is xml format
XDocument doc = XDocument.Parse(state.Value);

var root = doc.ElementOrThrow(Structure.Root);

var files = root.Elements(Structure.File)
.Select(element =>
{
var name = element.Attribute(Structure.Name).Value;
var dateTime = element.Attribute(Structure.Date).Value;
return new RecentFile(DateTime.Parse(dateTime),name);
}).ToArray();
return files;
}

public State Convert(RecentFile[] files)
{
if (files == null || !files.Any())
return State.Empty;

var root = new XElement(new XElement("Files", new XAttribute("Version",1)));
var root = new XElement(new XElement(Structure.Root));

var fileNodeArray = files.Select(f => new XElement("File",
new XAttribute("Name", f.Name),
new XAttribute("Date", f.Timestamp)));
var fileNodeArray = files.Select(f => new XElement(Structure.File,
new XAttribute(Structure.Name, f.Name),
new XAttribute(Structure.Date, f.Timestamp)));

fileNodeArray.ForEach(root.Add);

XDocument doc = new XDocument(root);
return new State(1, doc.ToString());
return new State(2, doc.ToString());
}

public RecentFile[] GetDefaultValue()
Expand Down
36 changes: 17 additions & 19 deletions Source/TailBlazer.Domain/Settings/SettingsStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,14 @@ public class FileSettingsStore : ISettingsStore
private readonly ILogger _logger;

private string Location { get; }

private static class Structure
{
public const string Root = "Setting";
public const string Version = "Version";
public const string State = "State";
}

public FileSettingsStore(ILogger logger)
{
_logger = logger;
Expand All @@ -28,23 +36,14 @@ public void Save(string key, State state)

_logger.Info($"Creating setting for {key}");

var writer = new StringWriter();
using (var xmlWriter = new XmlTextWriter(writer))
{
using (xmlWriter.WriteElement("Setting"))
{
xmlWriter.WriteAttributeString("Version",state.Version.ToString());
using (xmlWriter.WriteElement("State"))
{
xmlWriter.WriteString(state.Value);
}
}
xmlWriter.Close();
}

var formatted = XDocument.Parse(writer.ToString());
_logger.Info($"Writing value {formatted.ToString()}");
File.WriteAllText(file, formatted.ToString());
var root = new XElement(new XElement(Structure.Root, new XAttribute(Structure.Version,state.Version)));
var stateElement = new XElement(Structure.State, state.Value);
root.Add(stateElement);
var doc = new XDocument(root);
var fileText = doc.ToString();

_logger.Info($"Writing value {fileText}");
File.WriteAllText(file, fileText);

}

Expand All @@ -57,10 +56,9 @@ public State Load(string key)

if (!info.Exists || info.Length == 0) return State.Empty;



var doc = XDocument.Load(file);
var root = doc.Element("Setting");
var root = doc.ElementOrThrow("Setting");
var versionString = root.AttributeOrThrow("Version");
var version = int.Parse(versionString);
var state = root.ElementOrThrow("State");
Expand Down
17 changes: 16 additions & 1 deletion Source/TailBlazer.Domain/Settings/XmlEx.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,20 @@ public static IDisposable WriteElement(this XmlTextWriter source, string element
return Disposable.Create(source.WriteEndElement);
}

public static XElement ElementOrThrow(this XDocument source, string elementName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
if (elementName == null) throw new ArgumentNullException(nameof(elementName));

var element = source.Element(elementName);

if (element == null)
throw new ArgumentNullException($"{elementName} does not exist");


return element;
}

public static string ElementOrThrow(this XElement source, string elementName)
{
if (source == null) throw new ArgumentNullException(nameof(source));
Expand All @@ -40,8 +54,9 @@ public static string AttributeOrThrow(this XElement source, string elementName)
if (element == null)
throw new ArgumentNullException($"{elementName} does not exist");


return element.Value;
}


}
}
5 changes: 3 additions & 2 deletions Source/TailBlazer.Domain/TailBlazer.Domain.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,9 @@
<Compile Include="FileHandling\LineReaderEx.cs" />
<Compile Include="FileHandling\LinesChangedReason.cs" />
<Compile Include="FileHandling\Page.cs" />
<Compile Include="FileHandling\RecentFiles.cs" />
<Compile Include="FileHandling\IRecentFiles.cs" />
<Compile Include="FileHandling\RecentFile.cs" />
<Compile Include="FileHandling\RecentFileCollection.cs" />
<Compile Include="FileHandling\IRecentFileCollection.cs" />
<Compile Include="FileHandling\RecentFilesToStateConverter.cs" />
<Compile Include="FileHandling\ScrollReason.cs" />
<Compile Include="FileHandling\ScrollRequest.cs" />
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.IO;
using FluentAssertions;
using TailBlazer.Domain.FileHandling;
using Xunit;

Expand All @@ -7,7 +8,7 @@ namespace TailBlazer.Fixtures
public class RecentFilesToStateConverterFixture
{
[Fact]
public void ConvertFiles()
public void TwoWayConversion()
{

var files = new[]
Expand All @@ -17,9 +18,9 @@ public void ConvertFiles()
};

var converter = new RecentFilesToStateConverter();

var converted = converter.Convert(files);

var state = converter.Convert(files);
var restored = converter.Convert(state);
restored.ShouldAllBeEquivalentTo(files);
}
}
}
2 changes: 1 addition & 1 deletion Source/TailBlazer.Fixtures/SettingsStoreFixture.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void WriteState()
[Fact]
public void WriteComplexState()
{
var state = new State(1, "<<something weird<>");
var state = new State(1, "<<something weird<> which breaks xml {}");

var store = new FileSettingsStore(new NullLogger());
store.Save("wierdfile", state);
Expand Down
1 change: 0 additions & 1 deletion Source/TailBlazer/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Threading.Tasks;
using System.Windows;
Expand Down
2 changes: 1 addition & 1 deletion Source/TailBlazer/Infrastucture/AppConventions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public void Process(Type type, Registry registry)
// Only work on concrete types
if (!type.IsConcrete() || type.IsGenericType) return;

// Register against all the interfaces implemented
// Add against all the interfaces implemented
// by this concrete class
type.GetInterfaces()
.Where(@interface => @interface.Name == $"I{type.Name}" )
Expand Down
Loading

0 comments on commit d1cdff9

Please sign in to comment.