Skip to content

Commit

Permalink
Pass whole state (#30)
Browse files Browse the repository at this point in the history
* Data -> State

* set whole State not just Pages

* stop listening for "Created" events as they're always followed up by "Changed" events

* adjust namespace

* disable configWatcher when saving to prevent infinite loop when adding pages

* for now, pass whole state to SelectScreen to receive updates
  • Loading branch information
hbnrmx authored Jan 11, 2020
1 parent 1234a26 commit de7e9d4
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 39 deletions.
7 changes: 6 additions & 1 deletion SeeSharp/Models/Data.cs → SeeSharp/Models/State.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

namespace SeeSharp.Models
{
public class Data
public class State
{
public State()
{
Pages = new Bindable<SortedList<BindablePage>>();
}

[JsonProperty("default_speed")]
public float DefaultSpeed;

Expand Down
10 changes: 5 additions & 5 deletions SeeSharp/Screens/Select/SelectScreen.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ namespace SeeSharp.Screens.Select
public class SelectScreen : Screen
{
public Action Save;
private readonly Bindable<SortedList<BindablePage>> _pages;
private readonly Bindable<State> _state;
private readonly List<MenuItem> _menuItems = new List<MenuItem>();
private readonly BasicScrollContainer scroll;
private readonly AddPagesContainer right;
private readonly AddPagesContainer center;
private readonly FillFlowContainer<MenuItem> fillFlow;

public SelectScreen(Bindable<SortedList<BindablePage>> pages)
public SelectScreen(Bindable<State> state)
{
_pages = pages;
_state = state;
RelativeSizeAxes = Axes.Both;
InternalChildren = new Drawable[]
{
Expand Down Expand Up @@ -56,14 +56,14 @@ public SelectScreen(Bindable<SortedList<BindablePage>> pages)
center = new AddPagesContainer()
};

_pages.BindValueChanged(_ => Scheduler.AddOnce(load), true);
_state.BindValueChanged(_ => Scheduler.AddOnce(load), true);
}

private void load()
{
_menuItems.Clear();

foreach (var page in _pages.Value)
foreach (var page in _state.Value.Pages.Value)
{
_menuItems.Add(new MenuItem(page) {PageSelected = pageSelected});
}
Expand Down
9 changes: 5 additions & 4 deletions SeeSharp/SeeSharp.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace SeeSharp
{
public class SeeSharp : Game
{
private readonly Bindable<SortedList<BindablePage>> _pages = new Bindable<SortedList<BindablePage>>();
private readonly Bindable<State> _state = new Bindable<State>(new State());

private string _pagesPath;

[BackgroundDependencyLoader]
Expand All @@ -29,10 +30,10 @@ private void load(SeeSharpStorage storage)
var basePath = storage.GetFullPath(string.Empty);
var pageStorage = storage.GetStorageForDirectory("pages");
_pagesPath = pageStorage.GetFullPath(string.Empty);

var syncManager = new SyncManager(basePath, _pagesPath, _pages);

var selectScreen = new SelectScreen(_pages)
var syncManager = new SyncManager(basePath, _pagesPath, _state);

var selectScreen = new SelectScreen(_state)
{
Save = () => { syncManager.Save(); }
};
Expand Down
18 changes: 11 additions & 7 deletions SeeSharp/Sync/FileWatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,16 @@
using System.IO;
using System.Threading;

namespace SeeSharp
namespace SeeSharp.Sync
{
public class FileWatcher
{
public Action OnChange;
private readonly FileSystemWatcher _watcher;

public FileWatcher(string path, string filter)
{
var watcher = new FileSystemWatcher
_watcher = new FileSystemWatcher
{
Path = path,
Filter = filter,
Expand All @@ -22,18 +23,21 @@ public FileWatcher(string path, string filter)
| NotifyFilters.FileName
};

watcher.Created += Update;
watcher.Deleted += Update;
watcher.Changed += Update;
watcher.Renamed += Update;
_watcher.Deleted += Update;
_watcher.Changed += Update;
_watcher.Renamed += Update;
}

private void Update(object _, FileSystemEventArgs __)
{
//give some time for the external writing process to unlock the file
//TODO: find more robust solution later.
Thread.Sleep(2000);
OnChange?.Invoke();
}

public void Disable() => _watcher.EnableRaisingEvents = false;

public void Enable() => _watcher.EnableRaisingEvents = true;
}
}
43 changes: 21 additions & 22 deletions SeeSharp/Sync/SyncManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,50 +14,49 @@ public class SyncManager : IConfigManager
private readonly string _basePath;
private readonly string _pagesPath;
private string configPath() => Path.Combine(_basePath, "pages.json");
private readonly Bindable<SortedList<BindablePage>> _pages;
private readonly Bindable<State> _state;

public SyncManager(string basePath, string pagesPath, Bindable<SortedList<BindablePage>> pages)
public SyncManager(string basePath, string pagesPath, Bindable<State> state)
{
_basePath = basePath;
_pagesPath = pagesPath;
_pages = pages;
_state = state;

Load();
Save();

new FileWatcher(basePath, "pages.json")
var configWatcher = new FileWatcher(basePath, "pages.json")
{
OnChange = () => {
Load();
Save();
}
OnChange = Load
};

new FileWatcher(_pagesPath, "*.*")
{
OnChange = () =>
{
OnChange = () => {
Load();

configWatcher.Disable();
Save();
configWatcher.Enable();
}
};
}

public void Load()
{
Data data;
State state;

try
{
data = LoadFromConfig<Data>(configPath());
state = LoadFromConfig<State>(configPath());
}
catch (IOException e)
{
Logger.Error(e, "failed to retrieve config. Supplying standard config instead.");
data = new Data();
state = new State();
}

var registeredFileNames = data.Pages.Value.Select(p => p.Value.Name);
var registeredFileNames = state.Pages.Value.Select(p => p.Value.Name);

//add pages which have not been registered yet.
var newItems = new DirectoryInfo(_pagesPath)
Expand All @@ -71,28 +70,28 @@ public void Load()
.Select(file => new Page
{
Name = file.Name,
Speed = data.DefaultSpeed,
Zoom = data.DefaultZoom,
Speed = state.DefaultSpeed,
Zoom = state.DefaultZoom,
Bars = new SortedList<float>()
})
.Select(page => new BindablePage(page));

data.Pages.Value.AddRange(newItems);
state.Pages.Value.AddRange(newItems);

//remove pages which cannot be found in the folder anymore.
var oldItems = data.Pages.Value
var oldItems = state.Pages.Value
.Where(page => !File.Exists(Path.Combine(_pagesPath, page.Value.Name)));

data.Pages.Value.RemoveAll(item => oldItems.Contains(item));
state.Pages.Value.RemoveAll(item => oldItems.Contains(item));

//set new Value
_pages.Value = data.Pages.Value;
_state.Value = state;
}

public bool Save()
{
var data = new Data {Pages = _pages};
return SaveToConfig(configPath(), data);
var state = _state;
return SaveToConfig(configPath(), state);
}

private T LoadFromConfig<T>(string path)
Expand Down

0 comments on commit de7e9d4

Please sign in to comment.