RecentFiles - put jsonignore on derived properties, and clean up some things

This commit is contained in:
adelikat 2014-05-03 01:33:12 +00:00
parent 0a017f7f27
commit c0b8647e69
1 changed files with 45 additions and 50 deletions

View File

@ -2,17 +2,14 @@
using System.Collections; using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Newtonsoft.Json;
namespace BizHawk.Client.Common namespace BizHawk.Client.Common
{ {
[Newtonsoft.Json.JsonObject] [JsonObject]
public class RecentFiles : IEnumerable public class RecentFiles : IEnumerable
{ {
public int MAX_RECENT_FILES { get; private set; } // Maximum number of files private List<string> recentlist;
public List<string> recentlist { get; private set; } // List of recent files
public bool AutoLoad = false;
public RecentFiles() : this(8) { } public RecentFiles() : this(8) { }
public RecentFiles(int max) public RecentFiles(int max)
{ {
@ -20,6 +17,43 @@ namespace BizHawk.Client.Common
MAX_RECENT_FILES = max; MAX_RECENT_FILES = max;
} }
public int MAX_RECENT_FILES { get; private set; }
public bool AutoLoad { get; set; }
[JsonIgnore]
public bool Empty
{
get { return !recentlist.Any(); }
}
[JsonIgnore]
public int Count
{
get { return recentlist.Count; }
}
[JsonIgnore]
public string MostRecent
{
get
{
return recentlist.Any() ? recentlist[0] : string.Empty;
}
}
public string this[int index]
{
get
{
if (recentlist.Any())
{
return recentlist[index];
}
return string.Empty;
}
}
public IEnumerator<string> GetEnumerator() public IEnumerator<string> GetEnumerator()
{ {
return recentlist.GetEnumerator(); return recentlist.GetEnumerator();
@ -35,39 +69,23 @@ namespace BizHawk.Client.Common
recentlist.Clear(); recentlist.Clear();
} }
public bool Empty
{
get { return !recentlist.Any(); }
}
public int Count
{
get { return recentlist.Count; }
}
public void Add(string newFile) public void Add(string newFile)
{ {
for (int i = 0; i < recentlist.Count; i++) Remove(newFile);
{
if (String.Compare(newFile, recentlist[i], StringComparison.CurrentCultureIgnoreCase) == 0)
{
recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
}
}
recentlist.Insert(0, newFile); recentlist.Insert(0, newFile);
if (recentlist.Count > MAX_RECENT_FILES) if (recentlist.Count > MAX_RECENT_FILES)
{ {
recentlist.Remove(recentlist[recentlist.Count - 1]); recentlist.Remove(recentlist.Last());
} }
} }
public bool Remove(string newFile) public bool Remove(string newFile)
{ {
var removed = false; var removed = false;
for (int i = 0; i < recentlist.Count; i++) foreach (var recent in recentlist.ToList())
{ {
if (String.Compare(newFile, recentlist[i], StringComparison.CurrentCultureIgnoreCase) == 0) if (string.Compare(newFile, recent, StringComparison.CurrentCultureIgnoreCase) == 0)
{ {
recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place recentlist.Remove(newFile); // intentionally keeps iterating after this to remove duplicate instances, though those should never exist in the first place
removed = true; removed = true;
@ -82,32 +100,9 @@ namespace BizHawk.Client.Common
return recentlist.Select(t => t.Substring(0, length)).ToList(); return recentlist.Select(t => t.Substring(0, length)).ToList();
} }
public string this[int index]
{
get
{
if (recentlist.Any())
{
return recentlist[index];
}
else
{
return String.Empty;
}
}
}
public void ToggleAutoLoad() public void ToggleAutoLoad()
{ {
AutoLoad ^= true; AutoLoad ^= true;
} }
public string MostRecent
{
get
{
return recentlist.Any() ? recentlist[0] : string.Empty;
}
}
} }
} }