diff --git a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj index a4942f33..dec8a31a 100644 --- a/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj +++ b/CampusAppWP8/CampusAppWP8/CampusAppWP8.csproj @@ -441,7 +441,7 @@ - ..\packages\WPtoolkit.4.2012.10.30\lib\wp8\Microsoft.Phone.Controls.Toolkit.dll + ..\packages\WPToolkit.4.2013.06.11\lib\wp8\Microsoft.Phone.Controls.Toolkit.dll diff --git a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs index 9e5f85af..ba793fda 100644 --- a/CampusAppWP8/CampusAppWP8/Model/MainModel.cs +++ b/CampusAppWP8/CampusAppWP8/Model/MainModel.cs @@ -116,6 +116,11 @@ namespace CampusAppWP8 /// public delegate void OnSaved(); + /// + /// Delegate of the OnFailed(File/Web) callback function. + /// + public delegate void OnFailed(); + /// /// Delegate of the IsModelUpToDate callback function. /// @@ -151,6 +156,22 @@ namespace CampusAppWP8 /// public event OnSaved onSaved = null; + /// + /// Callback pointer, called after failed file loading. + /// + public event OnFailed onFailedFile = null; + + /// + /// Callback pointer, called after failed web loading. + /// + public event OnFailed onFailedWeb = null; + + /// + /// Callback pointer, called after failed file or web loading, if there + /// is no specialized onFailed callback set. + /// + public event OnFailed onFailed = null; + /// /// Callback pointer, for checking if file is up to date at loading. /// @@ -294,7 +315,18 @@ namespace CampusAppWP8 { string data = this.file.ReadFile(); - if (data != null && !data.Equals(string.Empty)) + if (data == null) + { + if (this.onFailedFile != null) + { + this.onFailedFile(); + } + else if (this.onFailed != null) + { + this.onFailed(); + } + } + else if (!data.Equals(string.Empty)) { this.DeserializeModel(Encoding.UTF8.GetBytes(data)); } @@ -486,6 +518,15 @@ namespace CampusAppWP8 Exception downloadError = e.Error; if (downloadError != null) { + if (this.onFailedWeb != null) + { + this.onFailedWeb(); + } + else if (this.onFailed != null) + { + this.onFailed(); + } + return; } diff --git a/CampusAppWP8/CampusAppWP8/Utility/File.cs b/CampusAppWP8/CampusAppWP8/Utility/File.cs index 6b6349a5..ae66a2e5 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/File.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/File.cs @@ -220,20 +220,48 @@ namespace CampusAppWP8.Utility /// data array private async void WriteAsync(byte[] data) { + Logger.LogMsg("writeasync file: " + this.filename); + StorageFile file = null; + Stream s = null; + + // try opening the file + while (file == null) + { + try + { + file = await File.LocalFolder.CreateFileAsync(this.filename, CreationCollisionOption.ReplaceExisting); + } + catch (Exception) + { + System.Threading.Thread.Sleep(10); + } + } + + // try to get a stream on the file + while (s == null) + { + try + { + s = await file.OpenStreamForWriteAsync(); + } + catch (Exception) + { + System.Threading.Thread.Sleep(10); + } + } + + // writing data try { - var file = await File.LocalFolder.CreateFileAsync(this.filename, CreationCollisionOption.ReplaceExisting); - - using (var s = await file.OpenStreamForWriteAsync()) - { - await s.WriteAsync(data, 0, data.Length); - } + await s.WriteAsync(data, 0, data.Length); + await s.FlushAsync(); } catch (Exception e) { - // log write Error Logger.LogException(e); } + + s.Dispose(); } } } \ No newline at end of file diff --git a/CampusAppWP8/CampusAppWP8/Utility/Logger.cs b/CampusAppWP8/CampusAppWP8/Utility/Logger.cs index 69fa2898..6b8d3d33 100644 --- a/CampusAppWP8/CampusAppWP8/Utility/Logger.cs +++ b/CampusAppWP8/CampusAppWP8/Utility/Logger.cs @@ -22,5 +22,14 @@ namespace CampusAppWP8.Utility { Console.WriteLine(exception); } + + /// + /// Log a message. + /// + /// to be logged message + public static void LogMsg(string msg) + { + Console.WriteLine(msg); + } } } diff --git a/CampusAppWP8/CampusAppWP8/packages.config b/CampusAppWP8/CampusAppWP8/packages.config index a6c4d07d..f3d7e2f0 100644 --- a/CampusAppWP8/CampusAppWP8/packages.config +++ b/CampusAppWP8/CampusAppWP8/packages.config @@ -1,4 +1,4 @@  - + \ No newline at end of file