extend Utilty

This commit is contained in:
stubbfel
2013-06-03 18:41:10 +02:00
parent 9444c1a2eb
commit 75a30578ed
10 changed files with 204 additions and 50 deletions

View File

@@ -98,7 +98,6 @@
<Compile Include="MainPage.xaml.cs">
<DependentUpon>MainPage.xaml</DependentUpon>
</Compile>
<Compile Include="model\departments\ChairModel.cs" />
<Compile Include="model\departments\DepartmentModel.cs" />
<Compile Include="model\departments\DepartmentViewModel.cs" />
@@ -119,8 +118,7 @@
<Compile Include="pages\mensa\MensaFeed.cs" />
<Compile Include="pages\mensa\MensaPage.xaml.cs">
<DependentUpon>MensaPage.xaml</DependentUpon>
</Compile>
</Compile>
<Compile Include="pages\news\NewsPage.xaml.cs">
<DependentUpon>NewsPage.xaml</DependentUpon>
</Compile>
@@ -147,6 +145,7 @@
</Compile>
<Compile Include="utility\Feed.cs" />
<Compile Include="utility\FeedEventHandler.cs" />
<Compile Include="utility\FileList.cs" />
<Compile Include="utility\FileManager.cs" />
<Compile Include="utility\Logger.cs" />
<Compile Include="utility\URLList.cs" />
@@ -170,7 +169,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</DesignData>
<Page Include="pages\campusmap\CampusMapPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -187,7 +185,6 @@
<Generator>MSBuild:Compile</Generator>
<SubType>Designer</SubType>
</Page>
<Page Include="pages\news\NewsPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -196,7 +193,6 @@
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
</Page>
<Page Include="pages\StartPage.xaml">
<SubType>Designer</SubType>
<Generator>MSBuild:Compile</Generator>
@@ -207,6 +203,7 @@
</Page>
</ItemGroup>
<ItemGroup>
<None Include="CampusAppWP8_2013_06_03_18_32_48.sap" />
<None Include="packages.config" />
<None Include="Properties\AppManifest.xml" />
<None Include="Properties\WMAppManifest.xml">
@@ -255,9 +252,7 @@
<HintPath>..\packages\WPtoolkit.4.2012.10.30\lib\wp8\Microsoft.Phone.Controls.Toolkit.dll</HintPath>
</Reference>
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).$(TargetFrameworkVersion).Overrides.targets" />
<Import Project="$(MSBuildExtensionsPath)\Microsoft\$(TargetFrameworkIdentifier)\$(TargetFrameworkVersion)\Microsoft.$(TargetFrameworkIdentifier).CSharp.targets" />
<!-- To modify your build process, add your task inside one of the targets below and uncomment it.

View File

@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<ProfilerSession Type="Monitoring" Name="CampusAppWP83916386620">
<TargetApplication Name="CampusAppWP8_Debug_AnyCPU" SourceProject="CampusAppWP8.csproj" Type="WindowsPhone">
<ApplicationBinary Path="Bin\Debug\CampusAppWP8_Debug_AnyCPU.xap" Timestamp="22.05.2013 10:04:47" />
<ApplicationIconPath Path="" />
<SymbolPaths>
<Path Value="Bin\Debug\" />
</SymbolPaths>
</TargetApplication>
<Settings>
<Probe Name="Visual">
<CollectElementLevelCosting Enabled="True" />
<CollectSnapshots Enabled="True" Interval="1000" Resampling="2" />
<CollectMediaEvents Enabled="False" />
<CollectCacheDetails Enabled="False" />
</Probe>
<Probe Name="Code">
<SampleCallStack Enabled="True" Depth="4294967295" Interval="10" />
<CollectPerformanceCounters Enabled="False" />
</Probe>
<Probe Name="Memory">
<MemoryCallStack Enabled="True" Depth="8" />
<ObjectTree Enabled="True" Interval="5" />
</Probe>
</Settings>
<Data>
<Logs>
<File Type="NativeFormat" Path="PerfLogs\CampusAppWP83916386620\tmpDB0E.spl" />
<File Type="SRUMData" Path="PerfLogs\CampusAppWP83916386620\tmpDB1F.srum" />
</Logs>
</Data>
<TargetDeviceSettings OSVersion="8.0.9903" AvailablePhysicalMemoryAtStartup="0" ProcessorArchitectureType="Intel" ProcessorInformation="X86" IsEmulator="True" />
</ProfilerSession>

View File

@@ -13,7 +13,7 @@
<Capability Name="ID_CAP_ISV_CAMERA" />
</Capabilities>
<Tasks>
<DefaultTask Name="_default" NavigationPage="pages/news/NewsPage.xaml" />
<DefaultTask Name="_default" NavigationPage="pages/mensa/MensaPage.xaml" />
</Tasks>
<Tokens>
<PrimaryToken TokenID="CampusAppWP8Token" TaskName="_default">

View File

@@ -14,8 +14,15 @@ namespace CampusAppWP8.model.mensa
[XmlArray("BTU")]
[XmlArrayItem("Tagesmenu")]
public ObservableCollection<MenuModel> menus { get; set; }
private readonly DateTime _createTime;
public DateTime CreateTime { get { return _createTime; } }
public MenuWeekModel()
{
_createTime = DateTime.Now;
}
}
}

View File

@@ -2,21 +2,61 @@
using CampusAppWP8.utility;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;
using System.Xml.Serialization;
using Windows.Storage;
using Windows.Storage.FileProperties;
namespace CampusAppWP8.pages.mensa
{
public class MensaFeed : XMLFeed<MenuWeekModel>
{
public MensaFeed(): base(URLList.MensaFeedURL, "MesaFeed.xml")
public MensaFeed()
: base(URLList.MensaFeedURL, FileList.MensaXmlFile)
{
}
protected override bool checkIsModelUpToDate()
{
DateTime lastModified = Model.CreateTime;
return checkIsUpToDate(lastModified);
}
protected override bool checkIsFileUpToDate()
{
DateTime lastModified = FileManager.GetFileInfo(FileName).LastWriteTime;
return checkIsUpToDate(lastModified);
}
public ObservableCollection<MenuModel> menus { get; set; }
private DateTime calcNewMenuWeekDay()
{
DateTime now = DateTime.Now;
while (now.DayOfWeek != DayOfWeek.Monday)
{
now = now.Subtract(new TimeSpan(1, 0, 0, 0));
}
DateTime monday = new DateTime(now.Year, now.Month, now.Day);
return monday;
}
private bool checkIsUpToDate(DateTime lastModified)
{
int diff = lastModified.CompareTo(calcNewMenuWeekDay());
if (diff < 0)
{
return false;
}
return true;
}
public System.Collections.ObjectModel.ObservableCollection<MenuModel> menus { get; set; }
}
}

View File

@@ -16,8 +16,8 @@ namespace CampusAppWP8.pages.mensa
{
public partial class MensaPage : PhoneApplicationPage
{
private MensaFeed feed { get; set; }
private MensaFeed feed;
private int selectedIndex;
public MensaPage()
@@ -26,12 +26,20 @@ namespace CampusAppWP8.pages.mensa
InitializeFeed();
}
protected override void OnNavigatedTo(NavigationEventArgs e)
{
base.OnNavigatedTo(e);
if (feed == null)
{
InitializeFeed();
}
feed.LoadFeed();
}
private void InitializeFeed()
{
feed = new MensaFeed();
feed.EventHandler.FeedIsReadyEvent += new FeedEventHandler.FeedReadyHandler(feedIsReady);
feed.LoadFeed();
calcSelectedIndex();
}

View File

@@ -36,11 +36,53 @@ namespace CampusAppWP8.utility
#endregion
#region Methods
public void LoadFeed()
{
downloadFeed();
if (isModelUpToDate())
{
return;
}
if (isFileUpToDate())
{
loadFile();
}
else
{
downloadFeed();
}
}
private void loadFile()
{
string feedString = createFeedString();
createModel(feedString);
}
public bool isFileUpToDate()
{
if (!FileManager.ExistsFile(FileName))
{
return false;
}
return checkIsFileUpToDate();
}
public bool isModelUpToDate()
{
if (Model != null)
{
return checkIsModelUpToDate();
}
return false;
}
protected abstract bool checkIsFileUpToDate();
protected abstract bool checkIsModelUpToDate();
private void downloadFeed()
{
WebClient client = new WebClient();
@@ -53,7 +95,6 @@ namespace CampusAppWP8.utility
Exception downloadError = e.Error;
if (downloadError != null)
{
Console.Out.WriteLineAsync(downloadError.Message);
return;
}
@@ -71,21 +112,13 @@ namespace CampusAppWP8.utility
{
return;
}
if (!FileManager.isStringFileName(feedString))
{
deserialization(feedString);
EventHandler.fireFeedReadyevent();
}
else
{
throw new NotImplementedException();
}
}
private string createFeedString(string feedFileName)
private string createFeedString()
{
throw new NotImplementedException();
return FileManager.ReadFile(FileName);
}
protected abstract void deserialization(string feedString);

View File

@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace CampusAppWP8.utility
{
public static class FileList
{
public static string MensaXmlFile = "MesaFeed.xml";
}
}

View File

@@ -1,51 +1,76 @@
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
{
public class FileManager
public static class FileManager
{
private static IStorageFolder localFolder = ApplicationData.Current.LocalFolder;
public static bool isStringFileName(string fileName)
{
try
{
validFileNameTry(fileName);
new FileInfo(fileName);
}
catch (Exception e)
{
validFileNameCatch(e);
Logger.logException(e);
return false;
}
return true;
}
private static void validFileNameTry(string fileName)
private static async void WriteFileAsync(string fileName, string content)
{
new System.IO.FileInfo(fileName);
}
private static void validFileNameCatch(Exception e)
{
Logger.logException(e);
}
public static async void WriteFile(string fileName, string content)
{
byte[] data = Encoding.UTF8.GetBytes(content);
StorageFolder folder = ApplicationData.Current.LocalFolder;
StorageFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream s = await file.OpenStreamForWriteAsync())
IStorageFile storageFile = await localFolder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting);
using (Stream stream = await storageFile.OpenStreamForWriteAsync())
{
await s.WriteAsync(data, 0, data.Length);
byte[] Content = Encoding.UTF8.GetBytes(content);
await stream.WriteAsync(Content, 0, Content.Length);
}
}
public static void WriteFile(string fileName, string content)
{
WriteFileAsync(fileName, content);
}
public static string ReadFile(string fileName)
{
string content = null;
using (Stream fileStream = localFolder.OpenStreamForReadAsync(fileName).Result)
{
using (StreamReader streamReader = new StreamReader(fileStream))
{
content = streamReader.ReadToEnd();
}
}
return content;
}
public static FileInfo GetFileInfo(string fileName)
{
FileInfo info = new FileInfo(localFolder.Path + "\\" + fileName);
return info;
}
public static bool ExistsFile(string fileName)
{
return GetFileInfo(fileName).Exists;
}
}
}
}

View File

@@ -9,7 +9,7 @@ using System.Xml.Serialization;
namespace CampusAppWP8.utility
{
public class XMLFeed<T> : Feed<T>
public abstract class XMLFeed<T> : Feed<T>
{
public XMLFeed()