PowerToys/WinAlfred/Helper/SelectedRecords.cs

74 lines
2.1 KiB
C#
Raw Normal View History

2014-01-08 23:21:37 +08:00
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Windows.Documents;
using WinAlfred.Plugin;
2014-01-07 23:27:05 +08:00
namespace WinAlfred.Helper
{
public class SelectedRecords
{
private int hasAddedCount = 0;
2014-01-08 23:21:37 +08:00
private Dictionary<string, int> dict = new Dictionary<string, int>();
private string filePath = Directory.GetCurrentDirectory() + "\\selectedRecords.dat";
2014-01-07 23:27:05 +08:00
public void LoadSelectedRecords()
{
2014-01-08 23:21:37 +08:00
if (File.Exists(filePath))
{
FileStream fileStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
BinaryFormatter b = new BinaryFormatter();
dict = (Dictionary<string, int>)b.Deserialize(fileStream);
fileStream.Close();
}
if (dict.Count > 1000)
{
List<string> onlyOnceKeys = (from c in dict where c.Value == 1 select c.Key).ToList();
foreach (string onlyOnceKey in onlyOnceKeys)
{
dict.Remove(onlyOnceKey);
}
}
2014-01-07 23:27:05 +08:00
}
public void AddSelect(Result result)
{
hasAddedCount++;
if (hasAddedCount == 10)
{
SaveSelectedRecords();
hasAddedCount = 0;
}
2014-01-08 23:21:37 +08:00
if (dict.ContainsKey(result.ToString()))
{
dict[result.ToString()] += 1;
}
else
{
dict.Add(result.ToString(), 1);
}
2014-01-07 23:27:05 +08:00
}
public int GetSelectedCount(Result result)
{
2014-01-08 23:21:37 +08:00
if (dict.ContainsKey(result.ToString()))
{
return dict[result.ToString()];
}
2014-01-07 23:27:05 +08:00
return 0;
}
2014-01-08 23:21:37 +08:00
private void SaveSelectedRecords()
2014-01-07 23:27:05 +08:00
{
2014-01-08 23:21:37 +08:00
FileStream fileStream = new FileStream("selectedRecords.dat", FileMode.Create);
BinaryFormatter b = new BinaryFormatter();
b.Serialize(fileStream, dict);
fileStream.Close();
2014-01-07 23:27:05 +08:00
}
}
}