|
using System;
using System.IO;
using System.IO.IsolatedStorage;
using System.Net;
using System.Runtime.Serialization;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Resources;
using System.Windows.Shapes;
using System.Linq;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Serialization;
using TBBT.Core;
using System.Collections.Generic;
namespace TBBT.Data
{
public class QuoteRepository
{
private const string Quote_FILE_PATH = @"DB\Quotes.xml";
public static void SaveQuotes(List<Quote> quotes)
{
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(Quote_FILE_PATH, UriKind.Relative));
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
string directoryName = System.IO.Path.GetDirectoryName(Quote_FILE_PATH);
if (!string.IsNullOrEmpty(directoryName) && !isolatedStorage.DirectoryExists(directoryName))
{
isolatedStorage.CreateDirectory(directoryName);
}
isolatedStorage.DeleteFile(Quote_FILE_PATH);
Serialize(isolatedStorage, Quote_FILE_PATH, quotes, typeof(List<Quote>));
}
}
public static List<Quote> RetrieveQuotes()
{
List<Quote> quotes = null;
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(Quote_FILE_PATH, UriKind.Relative));
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(Quote_FILE_PATH))
{
quotes = (List<Quote>)Deserialize(isolatedStorage, Quote_FILE_PATH, typeof(List<Quote>));
}
}
return quotes;
}
public static bool PreviousQuotesExists()
{
bool exists = false;
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(Quote_FILE_PATH, UriKind.Relative));
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
exists = isolatedStorage.FileExists(Quote_FILE_PATH);
}
return exists;
}
public static bool DeletePreviousQuotes()
{
bool exists = false;
StreamResourceInfo streamResourceInfo = Application.GetResourceStream(new Uri(Quote_FILE_PATH, UriKind.Relative));
using (IsolatedStorageFile isolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
if (isolatedStorage.FileExists(Quote_FILE_PATH))
{
try
{
isolatedStorage.DeleteFile(Quote_FILE_PATH);
}
catch
{
}
}
}
return exists;
}
public static void SearchQuotes(string searchText, Action<int> onProgressChanged, Action<Quote> onNewQuotesFound, Action onSearchCompleted)
{
searchText = searchText.ToLower();
List<Quote> quotes = new List<Quote>();
var episodes = EpisodeRepository.RetrieveEpisodeList();
var episodeCount = episodes.Count();
var currentEpisodePosition = 0;
foreach (var episode in episodes)
{
currentEpisodePosition++;
onProgressChanged((100 * currentEpisodePosition) / episodeCount);
if (EpisodeRepository.EpisodeTranscriptExists(episode.Season, episode.Number))
{
var episodeTranscript = EpisodeRepository.RetrieveEpisodeTranscript(episode.Season, episode.Number);
var matches = episodeTranscript.Quotes.Where(x => x.Character.ToLower().Contains(searchText.ToLower()) ||
x.Speech.ToLower().Contains(searchText.ToLower()));
foreach (var match in matches)
{
onNewQuotesFound(match);
}
}
}
onProgressChanged(100);
onSearchCompleted();
}
public static void Serialize(IsolatedStorageFile isolatedStorage, string path, object obj, Type type)
{
using (var file = isolatedStorage.OpenFile(path, FileMode.Create, FileAccess.Write))
{
DataContractSerializer serializer = new DataContractSerializer(type);
serializer.WriteObject(file, obj);
}
}
public static object Deserialize(IsolatedStorageFile isolatedStorage, string path, Type type)
{
object obj = null;
using (var file = isolatedStorage.OpenFile(path, FileMode.Open, FileAccess.Read))
{
DataContractSerializer serializer = new DataContractSerializer(type);
obj = serializer.ReadObject(file);
}
return obj;
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.