diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index 97c91d8b..847c8020 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -140,13 +140,13 @@ True Constants.resx - - - - - - - + + + + + + + diff --git a/CampusAppWP8/CampusAppWP8/model/mensa/MenuModel.cs b/CampusAppWP8/CampusAppWP8/model/mensa/MenuModel.cs index c843c444..6c02cd64 100644 --- a/CampusAppWP8/CampusAppWP8/model/mensa/MenuModel.cs +++ b/CampusAppWP8/CampusAppWP8/model/mensa/MenuModel.cs @@ -7,7 +7,7 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Serialization; using CampusAppWP8.Resources; -using CampusAppWP8.utility; +using CampusAppWP8.Utility; namespace CampusAppWP8.model.mensa { diff --git a/CampusAppWP8/CampusAppWP8/pages/mensa/MensaFeed.cs b/CampusAppWP8/CampusAppWP8/pages/mensa/MensaFeed.cs index 59772f52..79188d74 100644 --- a/CampusAppWP8/CampusAppWP8/pages/mensa/MensaFeed.cs +++ b/CampusAppWP8/CampusAppWP8/pages/mensa/MensaFeed.cs @@ -1,5 +1,5 @@ using CampusAppWP8.model.mensa; -using CampusAppWP8.utility; +using CampusAppWP8.Utility; using System; using System.Collections.Generic; using System.Collections.ObjectModel; @@ -13,7 +13,7 @@ using Windows.Storage.FileProperties; namespace CampusAppWP8.pages.mensa { - public class MensaFeed : XMLFeed + public class MensaFeed : XmlFeed { public MensaFeed() @@ -22,13 +22,13 @@ namespace CampusAppWP8.pages.mensa } - protected override bool checkIsModelUpToDate() + protected override bool CheckIsModelUpToDate() { DateTime lastModified = Model.CreateTime; return checkIsUpToDate(lastModified); } - protected override bool checkIsFileUpToDate() + protected override bool CheckIsFileUpToDate() { DateTime lastModified = FileManager.GetFileInfo(FileName).LastWriteTime; return checkIsUpToDate(lastModified); diff --git a/CampusAppWP8/CampusAppWP8/pages/mensa/MensaPage.xaml.cs b/CampusAppWP8/CampusAppWP8/pages/mensa/MensaPage.xaml.cs index 171b6a3c..9218aa9a 100644 --- a/CampusAppWP8/CampusAppWP8/pages/mensa/MensaPage.xaml.cs +++ b/CampusAppWP8/CampusAppWP8/pages/mensa/MensaPage.xaml.cs @@ -7,7 +7,7 @@ using System.Windows.Controls; using System.Windows.Navigation; using Microsoft.Phone.Controls; using Microsoft.Phone.Shell; -using CampusAppWP8.utility; +using CampusAppWP8.Utility; using System.Xml.Serialization; using CampusAppWP8.model.mensa; using System.Xml.Linq; diff --git a/CampusAppWP8/CampusAppWP8/utility/Feed.cs b/CampusAppWP8/CampusAppWP8/utility/Feed.cs index 6bd28d8d..f75931bf 100644 --- a/CampusAppWP8/CampusAppWP8/utility/Feed.cs +++ b/CampusAppWP8/CampusAppWP8/utility/Feed.cs @@ -1,96 +1,224 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Net; -using System.IO; -using Windows.Storage; - -namespace CampusAppWP8.utility +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.IO; + using System.Linq; + using System.Net; + using System.Text; + using System.Threading.Tasks; + using Windows.Storage; + + /// + /// This a abstract Class for reading, store and deserialization Feeds from the Web. + /// + /// Type for model of the feed public abstract class Feed { #region Member - private readonly Uri _feedURL; - private readonly FeedEventHandler _eventHandler; - private readonly string _fileName; - private T _model; + + /// + /// URL of the feed + /// + private readonly Uri feedURL; + + /// + /// EventHandler of the feed + /// + private readonly FeedEventHandler eventHandler; + + /// + /// Filename for the storage-file of the feed + /// + private readonly string fileName; + + /// + /// The model of the feed + /// + private T model; #endregion #region Constructor + /// + /// Initializes a new instance of the class. + /// public Feed() { - _eventHandler = new FeedEventHandler(); + this.eventHandler = new FeedEventHandler(); } - public Feed(Uri FeedURL, string fileName) + /// + /// Initializes a new instance of the class. + /// + /// Url of the Feed + /// Name of the file, which the feed will be stored + public Feed(Uri feedURL, string fileName) { - _feedURL = FeedURL; - _eventHandler = new FeedEventHandler(); - _fileName = fileName; + this.feedURL = feedURL; + this.eventHandler = new FeedEventHandler(); + this.fileName = fileName; + } + + #endregion + + #region Proberty + + /// + /// Gets for the url of the feed + /// + public Uri FeedURL + { + get { return this.feedURL; } + } + + /// + /// Gets for the event-handler of the feed + /// + public FeedEventHandler EventHandler + { + get { return this.eventHandler; } + } + + /// + /// Gets or sets for the model of the feed + /// + public T Model + { + get + { + return this.model; + } + + set + { + if ((value == null && this.model != null) || !value.Equals(this.model)) + { + this.model = value; + } + } + } + + /// + /// Gets for the storage-file of the feed + /// + public string FileName + { + get { return this.fileName; } } #endregion #region Methods + #region public + /// + /// Method load the feed with content. At first is try to load from model, then from file and at last from the web. + /// public void LoadFeed() { - if (isModelUpToDate()) + if (this.IsModelUpToDate()) { return; } - if (isFileUpToDate()) + if (this.IsFileUpToDate()) { - loadFile(); + this.LoadFile(); } else { - downloadFeed(); + this.DownloadFeed(); } } - private void loadFile() + #endregion + + #region protected + + /// + /// The abstract method check if the content of the file is up-to-date, its has to implement by subclasses + /// + /// true if its up-to-date, otherwise false + protected abstract bool CheckIsFileUpToDate(); + + /// + /// The abstract method check if the content of the model is up-to-date, its has to implement by subclasses + /// + /// true if its up-to-date, otherwise false + protected abstract bool CheckIsModelUpToDate(); + + /// + /// The abstract method convert feed to a model, its has to implement by subclasses + /// + /// content of the feed + protected abstract void Deserialization(string feedString); + + #endregion + + #region private + + /// + /// Method load content from the file and create the model + /// + private void LoadFile() { - string feedString = createFeedString(); - createModel(feedString); + string feedString = FileManager.ReadFile(this.FileName); + this.CreateModel(feedString); } - public bool isFileUpToDate() + /// + /// Method load content from the web + /// + private void DownloadFeed() { + WebClient client = new WebClient(); + client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.DownloadCompleted); + client.DownloadStringAsync(this.FeedURL); + } - if (!FileManager.ExistsFile(FileName)) + /// + /// Method check if the content of the file is up-to-date + /// + /// true if its up-to-date, otherwise false + private bool IsFileUpToDate() + { + if (!FileManager.ExistsFile(this.FileName)) { return false; } - return checkIsFileUpToDate(); + return this.CheckIsFileUpToDate(); } - public bool isModelUpToDate() + /// + /// Method check if the content of the model is up-to-date + /// + /// true if its up-to-date, otherwise false + private bool IsModelUpToDate() { - if (Model != null) + if (this.Model != null) { - return checkIsModelUpToDate(); + return this.CheckIsModelUpToDate(); } + return false; } - protected abstract bool checkIsFileUpToDate(); - protected abstract bool checkIsModelUpToDate(); - - private void downloadFeed() - { - WebClient client = new WebClient(); - client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(downloadCompleted); - client.DownloadStringAsync(FeedURL); - } - - private void downloadCompleted(object sender, DownloadStringCompletedEventArgs e) + /// + /// Method will be execute if the download of the feed is completed and create the model + /// + /// Sender of the event + /// Arguments of the event + private void DownloadCompleted(object sender, DownloadStringCompletedEventArgs e) { Exception downloadError = e.Error; if (downloadError != null) @@ -99,61 +227,30 @@ namespace CampusAppWP8.utility } string downloadResult = e.Result; - if (downloadResult != null && !downloadResult.Equals(String.Empty)) + if (downloadResult != null && !downloadResult.Equals(string.Empty)) { - createModel(downloadResult); - FileManager.WriteFile(FileName, downloadResult); + this.CreateModel(downloadResult); + FileManager.WriteFile(this.FileName, downloadResult); } } - private void createModel(string feedString) + /// + /// Method create the model of the feed + /// + /// content of the feed + private void CreateModel(string feedString) { - if (feedString == null || feedString == String.Empty) + if (feedString == null || feedString == string.Empty) { return; } - deserialization(feedString); - EventHandler.fireFeedReadyevent(); - } - private string createFeedString() - { - return FileManager.ReadFile(FileName); - } - - protected abstract void deserialization(string feedString); - - #endregion - - #region Getter&Setter - - public Uri FeedURL - { - get { return _feedURL; } - } - - public FeedEventHandler EventHandler - { - get { return _eventHandler; } - } - - public T Model - { - get { return _model; } - set - { - if ((value == null && _model != null) || !value.Equals(_model)) - { - _model = value; - } - } - } - public string FileName - { - get { return _fileName; } + this.Deserialization(feedString); + this.EventHandler.FireFeedReadyevent(); } #endregion + #endregion } } diff --git a/CampusAppWP8/CampusAppWP8/utility/FeedEventHandler.cs b/CampusAppWP8/CampusAppWP8/utility/FeedEventHandler.cs index 09770f15..fa60c443 100644 --- a/CampusAppWP8/CampusAppWP8/utility/FeedEventHandler.cs +++ b/CampusAppWP8/CampusAppWP8/utility/FeedEventHandler.cs @@ -1,19 +1,57 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.utility +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + /// + /// This class handle the events of a feed + /// public class FeedEventHandler { - public delegate void FeedReadyHandler(); - public event FeedReadyHandler FeedIsReadyEvent; - public FeedEventHandler() { } - public void fireFeedReadyevent() + #region Constructor + + /// + /// Initializes a new instance of the class. + /// + public FeedEventHandler() { - FeedIsReadyEvent(); } + + #endregion + + #region Delegate&Events + /// + /// Delegate for the ready event + /// + public delegate void FeedReadyHandler(); + + /// + /// The ready event + /// + public event FeedReadyHandler FeedIsReadyEvent; + + #endregion + + #region Method + + /// + /// Method fire a ready event + /// + public void FireFeedReadyevent() + { + this.FeedIsReadyEvent(); + } + + #endregion } } diff --git a/CampusAppWP8/CampusAppWP8/utility/FileList.cs b/CampusAppWP8/CampusAppWP8/utility/FileList.cs index ab1fa9e9..de1aef80 100644 --- a/CampusAppWP8/CampusAppWP8/utility/FileList.cs +++ b/CampusAppWP8/CampusAppWP8/utility/FileList.cs @@ -1,13 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.utility +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + /// + /// Class contain some default names of files + /// + /// + /// This Class may be become to a resource file + /// public static class FileList { - public static string MensaXmlFile = "MesaFeed.xml"; + /// + /// Name of the file for the feed of the mensa + /// + private static readonly string MensaXmlFile = "MesaFeed.xml"; } } diff --git a/CampusAppWP8/CampusAppWP8/utility/FileManager.cs b/CampusAppWP8/CampusAppWP8/utility/FileManager.cs index 5f16e060..1f9ec797 100644 --- a/CampusAppWP8/CampusAppWP8/utility/FileManager.cs +++ b/CampusAppWP8/CampusAppWP8/utility/FileManager.cs @@ -1,76 +1,120 @@ -using System; -using System.Collections.Generic; -using System.IO; -using System.IO.IsolatedStorage; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using Windows.Storage; -using Windows.Storage.FileProperties; -using Windows.Storage.Streams; - -namespace CampusAppWP8.utility +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.IO; + using System.IO.IsolatedStorage; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using Windows.Storage; + using Windows.Storage.FileProperties; + using Windows.Storage.Streams; + + /// + /// This a static Class, which provide some method for files + /// public static class FileManager { + #region members - private static IStorageFolder localFolder = ApplicationData.Current.LocalFolder; + /// + /// Member for the local folder + /// + private static readonly IStorageFolder LocalFolder = ApplicationData.Current.LocalFolder; - public static bool isStringFileName(string fileName) - { - try - { - new FileInfo(fileName); - } - catch (Exception e) - { - Logger.logException(e); - return false; - } - return true; - } - - private static async void WriteFileAsync(string fileName, string content) - { - IStorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); - using (Stream stream = await storageFile.OpenStreamForWriteAsync()) - { - byte[] Content = Encoding.UTF8.GetBytes(content); - await stream.WriteAsync(Content, 0, Content.Length); - } - - } + #endregion + #region public + + /// + /// Method write a content to an file + /// + /// name of the file + /// content of the file public static void WriteFile(string fileName, string content) { WriteFileAsync(fileName, content); } + /// + /// Method read content from a file + /// + /// + /// Method crash sometimes by OpenStreamForReadAsync() or OpenAccessStream() + /// + /// name of the file + /// content of the file, null if file doesn't exist public static string ReadFile(string fileName) { string content = null; - using (Stream fileStream = localFolder.OpenStreamForReadAsync(fileName).Result) + + if (ExistsFile(fileName)) + { + return null; + } + + using (Stream fileStream = LocalFolder.OpenStreamForReadAsync(fileName).Result) { using (StreamReader streamReader = new StreamReader(fileStream)) { content = streamReader.ReadToEnd(); } } + return content; } + /// + /// Method return info of a file + /// + /// name of the file + /// info of the file public static FileInfo GetFileInfo(string fileName) { - FileInfo info = new FileInfo(localFolder.Path + "\\" + fileName); + FileInfo info = new FileInfo(LocalFolder.Path + "\\" + fileName); return info; } + /// + /// Method check if a file is existing + /// + /// name of the file + /// true if file exists, otherwise false public static bool ExistsFile(string fileName) - { + { return GetFileInfo(fileName).Exists; } + + #endregion + + #region private + + /// + /// Method write a content to a new file. If the file exists, it will be replaced + /// + /// + /// Method crash sometimes by OpenStreamForWriteAsync() or OpenAccessStream() + /// + /// name of the file + /// content of the file + private static async void WriteFileAsync(string fileName, string content) + { + IStorageFile storageFile = await LocalFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); + using (Stream stream = await storageFile.OpenStreamForWriteAsync()) + { + byte[] contentByte = Encoding.UTF8.GetBytes(content); + await stream.WriteAsync(contentByte, 0, contentByte.Length); + } + } + + #endregion } -} - - - +} \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/utility/Logger.cs b/CampusAppWP8/CampusAppWP8/utility/Logger.cs index 8b4c5e32..79150c18 100644 --- a/CampusAppWP8/CampusAppWP8/utility/Logger.cs +++ b/CampusAppWP8/CampusAppWP8/utility/Logger.cs @@ -1,14 +1,28 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.utility +//-------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + /// + /// This Class creates logs for the app + /// public class Logger { - public static void logException(Exception exception) + /// + /// Method log a Exception + /// + /// exception which has to log + public static void LogException(Exception exception) { Console.WriteLine(exception); } diff --git a/CampusAppWP8/CampusAppWP8/utility/URLList.cs b/CampusAppWP8/CampusAppWP8/utility/URLList.cs index ba369ce8..a49c62d9 100644 --- a/CampusAppWP8/CampusAppWP8/utility/URLList.cs +++ b/CampusAppWP8/CampusAppWP8/utility/URLList.cs @@ -1,13 +1,29 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace CampusAppWP8.utility +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility { + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + + /// + /// Class contain some default url of feeds + /// + /// + /// This Class may be become to a resource file + /// public static class URLList { - public static Uri MensaFeedURL = new Uri("http://www.studentenwerk-frankfurt.de/2011/ClassPackage/App_IKMZ_BTU/", UriKind.Absolute); + /// + /// Url for the feed of the mensa + /// + public static readonly Uri MensaFeedURL = new Uri("http://www.studentenwerk-frankfurt.de/2011/ClassPackage/App_IKMZ_BTU/", UriKind.Absolute); } } diff --git a/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs b/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs index b1d02bc6..0b09f9f4 100644 --- a/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs +++ b/CampusAppWP8/CampusAppWP8/utility/XMLFeed.cs @@ -1,26 +1,54 @@ -using CampusAppWP8.Resources; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; -using System.Xml.Linq; -using System.Xml.Serialization; +//----------------------------------------------------------------------- +// +// Company copyright tag. +// +// stubbfel +// 03.05.2013 +//---------------------------------------------------------------------- +namespace CampusAppWP8.Utility +{ + using System; + using System.Collections.Generic; + using System.Linq; + using System.Text; + using System.Threading.Tasks; + using System.Xml.Linq; + using System.Xml.Serialization; + using CampusAppWP8.Resources; -namespace CampusAppWP8.utility -{ - public abstract class XMLFeed : Feed + /// + /// This abstract Class is for Xml-feeds + /// + /// Type for model of the feed + public abstract class XmlFeed : Feed { - - public XMLFeed() + #region Constructor + + /// + /// Initializes a new instance of the class. + /// + public XmlFeed() { } - public XMLFeed(Uri feedURL,String fileName):base(feedURL,fileName) + /// + /// Initializes a new instance of the class. + /// + /// Url of the Feed + /// Name of the file, which the feed will be stored + public XmlFeed(Uri feedURL, string fileName) + : base(feedURL, fileName) { } + #endregion - protected override void deserialization(string feedString) + #region Methods + + /// + /// Method implement the deserialization a Xml-feed + /// + /// content of the feed + protected override void Deserialization(string feedString) { XmlSerializer serializer = new XmlSerializer(typeof(T)); XDocument document = XDocument.Parse(feedString); @@ -31,12 +59,13 @@ namespace CampusAppWP8.utility document = new XDocument(); document.Add(new XElement(validRootName, content)); } - T model= (T)serializer.Deserialize(document.CreateReader()); + + T model = (T)serializer.Deserialize(document.CreateReader()); if (model != null) { - this.Model = model; + this.Model = model; } - } + #endregion } }