Skip to content

Commit

Permalink
Serialisation of recent files [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
RolandPheasant committed Dec 17, 2015
1 parent fd88d59 commit c72b6fe
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 32 deletions.
2 changes: 1 addition & 1 deletion Source/TailBlazer.Domain/FileHandling/IRecentFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ namespace TailBlazer.Domain.FileHandling
{
public interface IRecentFiles
{
IObservableList<FileInfo> Items { get; }
IObservableList<RecentFile> Items { get; }

void Register(FileInfo file);
}
Expand Down
40 changes: 30 additions & 10 deletions Source/TailBlazer.Domain/FileHandling/RecentFiles.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,39 @@
using System.Linq;
using System.Reactive.Disposables;
using DynamicData;
using DynamicData.Kernel;
using TailBlazer.Domain.Infrastructure;
using TailBlazer.Domain.Settings;

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
{
private const string SettingsKey = "RecentFiles";

private readonly ILogger _logger;
private readonly IDisposable _cleanUp;
private readonly ISourceCache<FileInfo,string> _files = new SourceCache<FileInfo, string>(fi=>fi.FullName);
public IObservableList<FileInfo> Items { get; }
private readonly ISourceCache<RecentFile, string> _files = new SourceCache<RecentFile, string>(fi=>fi.Name);

public IObservableList<RecentFile> Items { get; }

public RecentFiles(ILogger logger, ISettingFactory settingFactory, ISettingsStore store)
{
Expand All @@ -37,11 +55,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.FullName).HasValue)
// .ToArray();

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

Expand All @@ -58,12 +76,14 @@ public RecentFiles(ILogger logger, ISettingFactory settingFactory, ISettingsStor
public void Register(FileInfo file)
{
if (file == null) throw new ArgumentNullException(nameof(file));
_files.AddOrUpdate(file);

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

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

}

public void Dispose()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,44 @@
using System.Collections;
using System.IO;
using System.Linq;
using System.Xml.Linq;
using TailBlazer.Domain.Settings;

namespace TailBlazer.Domain.FileHandling
{
public class RecentFilesToStateConverter: IConverter<FileInfo[]>
public class RecentFilesToStateConverter: IConverter<RecentFile[]>
{
public FileInfo[] Convert(State state)
public RecentFile[] Convert(State state)
{
if (state == null || state == State.Empty)
return new FileInfo[0];

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

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

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

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

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

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

fileNodeArray.ForEach(root.Add);

return new State(1, state.Select(fi=>fi.FullName).ToDelimited(Environment.NewLine));
XDocument doc = new XDocument(root);
return new State(1, doc.ToString());
}

public FileInfo[] GetDefaultValue()
public RecentFile[] GetDefaultValue()
{
return new FileInfo[0];
return new RecentFile[0];
}
}
}
25 changes: 25 additions & 0 deletions Source/TailBlazer.Fixtures/RecentFilesToStateConverterFixture.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.IO;
using TailBlazer.Domain.FileHandling;
using Xunit;

namespace TailBlazer.Fixtures
{
public class RecentFilesToStateConverterFixture
{
[Fact]
public void ConvertFiles()
{

var files = new[]
{
new RecentFile(new FileInfo(@"C:\\File1.txt")),
new RecentFile(new FileInfo(@"C:\\File2.txt")),
};

var converter = new RecentFilesToStateConverter();

var converted = converter.Convert(files);

}
}
}
2 changes: 0 additions & 2 deletions Source/TailBlazer.Fixtures/SettingsStoreFixture.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
Expand All @@ -11,7 +10,6 @@

namespace TailBlazer.Fixtures
{

public class SettingsStoreFixture
{
[Fact]
Expand Down
1 change: 1 addition & 0 deletions Source/TailBlazer.Fixtures/TailBlazer.Fixtures.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@
<Compile Include="LogFixtures.cs" />
<Compile Include="FileNamerFixture.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
<Compile Include="RecentFilesToStateConverterFixture.cs" />
<Compile Include="SchedulerEx.cs" />
<Compile Include="IndexerFixture.cs" />
<Compile Include="SettingsStoreFixture.cs" />
Expand Down
18 changes: 9 additions & 9 deletions Source/TailBlazer/Views/WindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,15 +72,15 @@ public WindowViewModel(IObjectProvider objectProvider,

//move this out into it's own view model + create proxy so we can order
//and timestamp etc
ReadOnlyObservableCollection<FileInfo> data;
var recentLoader = recentFiles.Items
.Connect()
.ObserveOn(schedulerProvider.MainThread)
.Bind(out data)
.Subscribe();
RecentFiles = data;

_cleanUp = new CompositeDisposable(recentLoader,
//ReadOnlyObservableCollection<FileInfo> data;
//var recentLoader = recentFiles.Items
// .Connect()
// .ObserveOn(schedulerProvider.MainThread)
// .Bind(out data)
// .Subscribe();
// RecentFiles = data;

_cleanUp = new CompositeDisposable(//recentLoader,
isEmptyChecker,
fileDropped,
DropMonitor,
Expand Down

0 comments on commit c72b6fe

Please sign in to comment.