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
+38 -13
View File
@@ -59,10 +59,17 @@ namespace WPImageCaching
try
{
var response = (HttpWebResponse)transfer.WebRequest.EndGetResponse(result);
string newKey = transfer.WebRequest.RequestUri.ToString();
//Bild wurde nicht geändert seit dem letzten Aufruf
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(() =>
{
using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication())
@@ -80,22 +87,40 @@ namespace WPImageCaching
}
lock (ImageCache._lock)
{
string newKey = transfer.WebRequest.RequestUri.ToString();
if (ImageCache.imageCache.ContainsKey(newKey))
{
//Setzen des Bildes
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
using (IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream fs = ifs.OpenFile(transfer.Item.LocalFilename, FileMode.Open))
bool isExpired = (DateTime.Compare(DateTime.Now, ImageCache.imageCache[newKey].Expiration) >= 0);
if (!isExpired)
{
//Setzen des Bildes
Deployment.Current.Dispatcher.BeginInvoke(
() =>
{
transfer.Image.SetSource(fs);
}
}
});
return;
using (
IsolatedStorageFile ifs = IsolatedStorageFile.GetUserStoreForApplication())
{
using (
IsolatedStorageFileStream fs = ifs.OpenFile(
transfer.Item.LocalFilename, FileMode.Open))
{
transfer.Image.SetSource(fs);
}
}
});
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?
if (response.Headers["ETag"] != null)
+47 -5
View File
@@ -16,6 +16,7 @@ using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Phone;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Net.NetworkInformation;
using Microsoft.Phone.Shell;
using MyFriendsAround.WP7.Model;
using MyFriendsAround.WP7.ViewModel;
@@ -53,6 +54,21 @@ namespace MyFriendsAround.WP7
//register ViewModelLocator
Container.Instance.RegisterInstance(typeof(ViewModelLocator), "ViewModelLocator");
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
private void Application_Launching(object sender, LaunchingEventArgs e)
{
#if GPS_EMULATOR
IsolatedStorageExplorer.Explorer.Start("localhost");
#endif
DispatcherHelper.Initialize();
LoadModel();
}
@@ -76,6 +95,9 @@ namespace MyFriendsAround.WP7
// This code will not execute when the application is first launched
private void Application_Activated(object sender, ActivatedEventArgs e)
{
#if GPS_EMULATOR
IsolatedStorageExplorer.Explorer.RestoreFromTombstone();
#endif
DispatcherHelper.Initialize();
LoadModel();
}
@@ -104,15 +126,16 @@ namespace MyFriendsAround.WP7
MainViewModel mainModel = this.RetrieveFromIsolatedStorage<MainViewModel>();
if (mainModel != null)
{
mainModel.IsBusy = false;
mainModel.GpsLocation = Location.Unknown;
mainModel.GpsStatus = GeoPositionStatus.NoData;
Container.Instance.RegisterInstance<MainViewModel>(mainModel, Constants.VM_MAIN);
}
else
{
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>();
if (settingsModel != null)
@@ -138,7 +161,14 @@ namespace MyFriendsAround.WP7
void RootFrame_NavigationFailed(object sender, NavigationFailedEventArgs e)
{
//LittleWatson.ReportException(e.Exception, string.Empty);
ShowException(e.Exception);
if (e.Exception is WebException)
{
MessageBox.Show(e.Exception.Message);
}
else
{
ShowException(e.Exception);
}
if (System.Diagnostics.Debugger.IsAttached)
{
@@ -150,8 +180,20 @@ namespace MyFriendsAround.WP7
// Code to execute on Unhandled Exceptions
private void Application_UnhandledException(object sender, ApplicationUnhandledExceptionEventArgs e)
{
#region Error Logging
//LittleWatson.ReportException(e.ExceptionObject, string.Empty);
ShowException(e.ExceptionObject);
if (e.ExceptionObject is WebException)
{
MessageBox.Show(e.ExceptionObject.Message);
}
else
{
ShowException(e.ExceptionObject);
}
#endregion
if (System.Diagnostics.Debugger.IsAttached)
{
@@ -102,6 +102,9 @@
<Reference Include="ICSharpCode.SharpZipLib.WindowsPhone">
<HintPath>..\Libs\Hammock-Binaries\.NET 4.0\Windows Phone 7\ICSharpCode.SharpZipLib.WindowsPhone.dll</HintPath>
</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">
<HintPath>..\packages\Hammock.1.2.1\lib\sl4\Microsoft.CSharp.dll</HintPath>
</Reference>
@@ -52,7 +52,7 @@ namespace MyFriendsAround.WP7.Service
}
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
{
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)
{
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
{
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)
{
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
{
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,29 +677,34 @@ namespace MyFriendsAround.WP7.ViewModel
if (e.Location != Location.Unknown)
{
GpsLocation = e.Location;
if (LastBoundRect != null && LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
.5,
.5)))
{
ObservableCollection<PushPinModel> _mynewlocation = new ObservableCollection<PushPinModel>();
_mynewlocation.Add(new PushPinModel()
{
Location = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
PinUserName = "Me"
});
MyLocationPushPins = _mynewlocation;
}
else
{
MyLocationPushPins = new ObservableCollection<PushPinModel>();
}
RefreshMyLocationPushPins();
}
System.Diagnostics.Debug.WriteLine("watcher_PositionChanged + " + DateTime.Now.Second);
}
public void RefreshMyLocationPushPins()
{
if (LastBoundRect != null && LastBoundRect.Intersects(new LocationRect(
new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
.5,
.5)))
{
ObservableCollection<PushPinModel> _mynewlocation = new ObservableCollection<PushPinModel>();
_mynewlocation.Add(new PushPinModel()
{
Location = new GeoCoordinate(GpsLocation.Latitude, GpsLocation.Longitude),
PinUserName = "Me"
});
MyLocationPushPins = _mynewlocation;
}
else
{
MyLocationPushPins = new ObservableCollection<PushPinModel>();
}
}
private void ShowMyLocation()
{
//
@@ -936,8 +941,15 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
IsBusy = false;
var exception = new ExceptionPrompt();
exception.Show(args.Error);
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt();
exception.Show(args.Error);
}
}
);
}
@@ -950,8 +962,15 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
IsBusy = false;
var exception = new ExceptionPrompt();
exception.Show(args.Error);
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt();
exception.Show(args.Error);
}
});
}
else
@@ -997,8 +1016,15 @@ namespace MyFriendsAround.WP7.ViewModel
DispatcherHelper.CheckBeginInvokeOnUI(() =>
{
IsBusy = false;
var exception = new ExceptionPrompt();
exception.Show(args.Error);
if (args.Error is WebException)
{
MessageBox.Show(args.Error.Message);
}
else
{
var exception = new ExceptionPrompt();
exception.Show(args.Error);
}
});
}
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|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.Deploy.0 = 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.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|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.Deploy.0 = 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.Build.0 = GPS_EMULATOR|Any CPU