Improve images caching

Improve error reporting
Implemented IsolatedStorageExplorer feature
This commit is contained in:
2011-04-13 06:52:25 +03:00
parent a297460f37
commit 72c1bbdcfc
7 changed files with 144 additions and 50 deletions
@@ -59,10 +59,17 @@ namespace WPImageCaching
try try
{ {
var response = (HttpWebResponse)transfer.WebRequest.EndGetResponse(result); var response = (HttpWebResponse)transfer.WebRequest.EndGetResponse(result);
string newKey = transfer.WebRequest.RequestUri.ToString();
//Bild wurde nicht geändert seit dem letzten Aufruf //Bild wurde nicht geändert seit dem letzten Aufruf
if (response.StatusCode == HttpStatusCode.NotModified) if (response.StatusCode == HttpStatusCode.NotModified)
{ {
if (ImageCache.imageCache.ContainsKey(newKey))
{
ImageCache.imageCache[newKey].Expiration = (response.Headers["Expires"] != null) ? DateTime.Parse(response.Headers["Expires"]) : DateTime.Now.AddDays(EXPIRATIONDAYS);
//save updated
ImageCache.SaveCachedImageInfo();
}
Deployment.Current.Dispatcher.BeginInvoke(() => Deployment.Current.Dispatcher.BeginInvoke(() =>
{ {
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
@@ -80,16 +87,21 @@ namespace WPImageCaching
} }
lock (ImageCache._lock) lock (ImageCache._lock)
{ {
string newKey = transfer.WebRequest.RequestUri.ToString();
if (ImageCache.imageCache.ContainsKey(newKey)) if (ImageCache.imageCache.ContainsKey(newKey))
{
bool isExpired = (DateTime.Compare(DateTime.Now, ImageCache.imageCache[newKey].Expiration) >= 0);
if (!isExpired)
{ {
//Setzen des Bildes //Setzen des Bildes
Deployment.Current.Dispatcher.BeginInvoke( Deployment.Current.Dispatcher.BeginInvoke(
() => () =>
{ {
using (IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication()) using (
IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication())
{ {
using (IsolatedStorageFileStream fs = ifs.OpenFile(transfer.Item.LocalFilename, FileMode.Open)) using (
IsolatedStorageFileStream fs = ifs.OpenFile(
transfer.Item.LocalFilename, FileMode.Open))
{ {
transfer.Image.SetSource(fs); transfer.Image.SetSource(fs);
} }
@@ -97,6 +109,19 @@ namespace WPImageCaching
}); });
return; return;
} }
else
{
if (IsolatedStorageFile.GetUserStoreForApplication().FileExists(ImageCache.imageCache[newKey].LocalFilename))
{
//remove old image
IsolatedStorageFile.GetUserStoreForApplication().DeleteFile(ImageCache.imageCache[newKey].LocalFilename);
//
ImageCache.imageCache.Remove(newKey);
//Speichern der Bildinformationen
ImageCache.SaveCachedImageInfo();
}
}
}
//Hat das Bild eine neue ID? //Hat das Bild eine neue ID?
if (response.Headers["ETag"] != null) if (response.Headers["ETag"] != null)
{ {
+45 -3
View File
@@ -16,6 +16,7 @@ using System.Windows.Navigation;
using System.Windows.Shapes; using System.Windows.Shapes;
using Microsoft.Phone; using Microsoft.Phone;
using Microsoft.Phone.Controls; using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Shell; using Microsoft.Phone.Shell;
using MyFriendsAround.WP7.Model; using MyFriendsAround.WP7.Model;
using MyFriendsAround.WP7.ViewModel; using MyFriendsAround.WP7.ViewModel;
@@ -53,6 +54,21 @@ namespace MyFriendsAround.WP7
//register ViewModelLocator //register ViewModelLocator
Container.Instance.RegisterInstance(typeof(ViewModelLocator), "ViewModelLocator"); Container.Instance.RegisterInstance(typeof(ViewModelLocator), "ViewModelLocator");
Container.Instance.RegisterInstance<ILocationService>(new LocationService(), "LocationService"); Container.Instance.RegisterInstance<ILocationService>(new LocationService(), "LocationService");
NetworkDetector.Instance.OnNetworkOFF += new EventHandler<NetworkAvailableEventArgs>(Instance_OnNetworkOFF);
NetworkDetector.Instance.OnNetworkON += new EventHandler<NetworkAvailableEventArgs>(Instance_OnNetworkON);
}
void Instance_OnNetworkON(object sender, NetworkAvailableEventArgs e)
{
ViewModelLocator locator = Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator");
locator.Main.IsBusy = true;
}
void Instance_OnNetworkOFF(object sender, NetworkAvailableEventArgs e)
{
ViewModelLocator locator = Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator");
locator.Main.IsBusy = false;
} }
@@ -68,6 +84,9 @@ namespace MyFriendsAround.WP7
// This code will not execute when the application is reactivated // This code will not execute when the application is reactivated
private void Application_Launching(object sender, LaunchingEventArgs e) private void Application_Launching(object sender, LaunchingEventArgs e)
{ {
#if GPS_EMULATOR
IsolatedStorageExplorer.Explorer.Start("localhost");
#endif
DispatcherHelper.Initialize(); DispatcherHelper.Initialize();
LoadModel(); LoadModel();
} }
@@ -76,6 +95,9 @@ namespace MyFriendsAround.WP7
// This code will not execute when the application is first launched // This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e) private void Application_Activated(object sender, ActivatedEventArgs e)
{ {
#if GPS_EMULATOR
IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
#endif
DispatcherHelper.Initialize(); DispatcherHelper.Initialize();
LoadModel(); LoadModel();
} }
@@ -104,15 +126,16 @@ namespace MyFriendsAround.WP7
MainViewModel mainModel = this.RetrieveFromIsolatedStorage<MainViewModel>(); MainViewModel mainModel = this.RetrieveFromIsolatedStorage<MainViewModel>();
if (mainModel != null) if (mainModel != null)
{ {
mainModel.IsBusy = false;
mainModel.GpsLocation = Location.Unknown;
mainModel.GpsStatus = GeoPositionStatus.NoData;
Container.Instance.RegisterInstance<MainViewModel>(mainModel, Constants.VM_MAIN); Container.Instance.RegisterInstance<MainViewModel>(mainModel, Constants.VM_MAIN);
} }
else else
{ {
Container.Instance.RegisterInstance<MainViewModel>(new MainViewModel(), Constants.VM_MAIN); Container.Instance.RegisterInstance<MainViewModel>(new MainViewModel(), Constants.VM_MAIN);
} }
Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator").Main.GpsLocation = Location.Unknown;
Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator").Main.GpsStatus = GeoPositionStatus.NoData;
Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator").Main.RefreshMyLocationPushPins();
Container.Instance.Resolve<ViewModelLocator>("ViewModelLocator").Main.IsBusy = false; //(NetworkDetector.Instance.GetCurrentNetworkType() == NetworkInterfaceType.None) || !NetworkDetector.Instance.GetZuneStatus();
// //
SettingsViewModel settingsModel = this.RetrieveFromIsolatedStorage<SettingsViewModel>(); SettingsViewModel settingsModel = this.RetrieveFromIsolatedStorage<SettingsViewModel>();
if (settingsModel != null) if (settingsModel != null)
@@ -138,7 +161,14 @@ namespace MyFriendsAround.WP7
void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e) void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{ {
//LittleWatson.ReportException(e.Exception, string.Empty); //LittleWatson.ReportException(e.Exception, string.Empty);
if (e.Exception is WebException)
{
MessageBox.Show(e.Exception.Message);
}
else
{
ShowException(e.Exception); ShowException(e.Exception);
}
if (System.Diagnostics.Debugger.IsAttached) if (System.Diagnostics.Debugger.IsAttached)
{ {
@@ -150,8 +180,20 @@ namespace MyFriendsAround.WP7
// Code to execute on Unhandled Exceptions // Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e) private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{ {
#region Error Logging
//LittleWatson.ReportException(e.ExceptionObject, string.Empty); //LittleWatson.ReportException(e.ExceptionObject, string.Empty);
if (e.ExceptionObject is WebException)
{
MessageBox.Show(e.ExceptionObject.Message);
}
else
{
ShowException(e.ExceptionObject); ShowException(e.ExceptionObject);
}
#endregion
if (System.Diagnostics.Debugger.IsAttached) if (System.Diagnostics.Debugger.IsAttached)
{ {
@@ -102,6 +102,9 @@
<Reference Include="ICSharpCode.SharpZipLib.WindowsPhone"> <Reference Include="ICSharpCode.SharpZipLib.WindowsPhone">
<HintPath>..\Libs\Hammock-Binaries\.NET 4.0\Windows Phone 7\ICSharpCode.SharpZipLib.WindowsPhone.dll</HintPath> <HintPath>..\Libs\Hammock-Binaries\.NET 4.0\Windows Phone 7\ICSharpCode.SharpZipLib.WindowsPhone.dll</HintPath>
</Reference> </Reference>
<Reference Include="IsolatedStorageExplorer, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\Libs\WP7 Isolated Storage Explorer\Library\IsolatedStorageExplorer.dll</HintPath>
</Reference>
<Reference Include="Microsoft.CSharp"> <Reference Include="Microsoft.CSharp">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\Microsoft.CSharp.dll</HintPath> <HintPath>..\packages\Hammock.1.2.1\lib\sl4\Microsoft.CSharp.dll</HintPath>
</Reference> </Reference>
@@ -52,7 +52,7 @@ namespace MyFriendsAround.WP7.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
friendscallback.Invoke(null, new FriendsListEventArgs() { Friends = null, Error = ex }); friendscallback.Invoke(null, new FriendsListEventArgs() { Friends = null, Error = new WebException("Communication Error!", ex) });
} }
} }
@@ -66,7 +66,7 @@ namespace MyFriendsAround.WP7.Service
} }
else else
{ {
friendscallback.Invoke(null, new FriendsListEventArgs() { Friends = null, Error = new Exception("Communication Error!") }); friendscallback.Invoke(null, new FriendsListEventArgs() { Friends = null, Error = new WebException("Communication Error!") });
} }
} }
@@ -102,7 +102,7 @@ namespace MyFriendsAround.WP7.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
publishlocationcallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = ex }); publishlocationcallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new WebException("Communication Error!", ex) });
} }
} }
@@ -116,7 +116,7 @@ namespace MyFriendsAround.WP7.Service
} }
else else
{ {
publishlocationcallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new Exception("Communication Error!")}); publishlocationcallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new WebException("Communication Error!") });
} }
} }
@@ -155,7 +155,7 @@ namespace MyFriendsAround.WP7.Service
} }
catch (Exception ex) catch (Exception ex)
{ {
publishmypicturecallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = ex }); publishmypicturecallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new WebException("Communication Error!", ex) });
} }
} }
@@ -169,7 +169,7 @@ namespace MyFriendsAround.WP7.Service
} }
else else
{ {
publishmypicturecallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new Exception("Communication Error!") }); publishmypicturecallback.Invoke(null, new PublishLocationEventArgs() { IsSuccess = false, Error = new WebException("Communication Error!") });
} }
} }
@@ -677,7 +677,14 @@ namespace MyFriendsAround.WP7.ViewModel
if (e.Location != Location.Unknown) if (e.Location != Location.Unknown)
{ {
GpsLocation = e.Location; GpsLocation = e.Location;
RefreshMyLocationPushPins();
}
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
}
public void RefreshMyLocationPushPins()
{
if (LastBoundRect != null && LastBoundRect.Intersects(new LocationRect( if (LastBoundRect != null && LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude), new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
.5, .5,
@@ -697,8 +704,6 @@ namespace MyFriendsAround.WP7.ViewModel
} }
} }
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
}
private void ShowMyLocation() private void ShowMyLocation()
{ {
@@ -936,9 +941,16 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() => DispatcherHelper.CheckBeginInvokeOnUI(() =>
{ {
IsBusy = false; IsBusy = false;
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt(); var exception = new ExceptionPrompt();
exception.Show(args.Error); exception.Show(args.Error);
} }
}
); );
} }
} }
@@ -950,8 +962,15 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() => DispatcherHelper.CheckBeginInvokeOnUI(() =>
{ {
IsBusy = false; IsBusy = false;
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt(); var exception = new ExceptionPrompt();
exception.Show(args.Error); exception.Show(args.Error);
}
}); });
} }
else else
@@ -997,8 +1016,15 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() => DispatcherHelper.CheckBeginInvokeOnUI(() =>
{ {
IsBusy = false; IsBusy = false;
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt(); var exception = new ExceptionPrompt();
exception.Show(args.Error); exception.Show(args.Error);
}
}); });
} }
else else
-2
View File
@@ -113,7 +113,6 @@ Global
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Any CPU.Build.0 = Debug|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|Mixed Platforms.Deploy.0 = Debug|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|x86.ActiveCfg = Debug|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.Debug|x86.ActiveCfg = Debug|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Any CPU.ActiveCfg = GPS_EMULATOR|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Any CPU.ActiveCfg = GPS_EMULATOR|Any CPU
{67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Any CPU.Build.0 = GPS_EMULATOR|Any CPU {67CBC824-A49E-4E9B-A947-360F3DFE65C3}.GPS_EMULATOR|Any CPU.Build.0 = GPS_EMULATOR|Any CPU
@@ -227,7 +226,6 @@ Global
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Any CPU.Deploy.0 = Debug|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Mixed Platforms.ActiveCfg = Debug|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Mixed Platforms.Build.0 = Debug|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|Mixed Platforms.Deploy.0 = Debug|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|x86.ActiveCfg = Debug|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.Debug|x86.ActiveCfg = Debug|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Any CPU.ActiveCfg = GPS_EMULATOR|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Any CPU.ActiveCfg = GPS_EMULATOR|Any CPU
{BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Any CPU.Build.0 = GPS_EMULATOR|Any CPU {BF7316A8-A2C5-4176-8D7F-672AD12F474D}.GPS_EMULATOR|Any CPU.Build.0 = GPS_EMULATOR|Any CPU