private static string GetJson(DataType dataType, bool createIfNotExist = false) { string s = null; switch (dataType) { case DataType.User: if (createIfNotExist && AppData.user == null) { AppData.user = new UserInfo(); } s = JsonConvert.SerializeObject(AppData.user); break; case DataType.Car: if (createIfNotExist && AppData.cars == null) { AppData.cars = new List <CarInfo>(); } s = JsonConvert.SerializeObject(AppData.cars); break; case DataType.DeviceLocation: if (createIfNotExist && AppData.deviceLocation == null) { AppData.deviceLocation = new DeviceLocationInfo(); } s = JsonConvert.SerializeObject(AppData.deviceLocation); break; default: ErrorReporter.Report($"Upload DataType \"{dataType}\" haven't been implemented!"); return(null); } return(s); }
public static async Task DownloadAsync(DataType dataType) { indexRetry :; try { var blockBlob = await GetBlockBlobForAppDataAsync(dataType); string s; if (!await blockBlob.ExistsAsync()) { s = GetJson(dataType, true); if (await App.Current.MainPage.DisplayAlert("", $"You haven't save any \"{dataType}\" data on cloud!\r\nCreate now?", "Yes", "No")) { await UploadAsync(dataType); await new JustAlert($"\"{dataType}\" data created on cloud!").Show(); } } else { s = await blockBlob.DownloadTextAsync(); } switch (dataType) { case DataType.User: AppData.user = JsonConvert.DeserializeObject <UserInfo>(s); break; case DataType.Car: AppData.cars = JsonConvert.DeserializeObject <List <CarInfo> >(s); break; case DataType.DeviceLocation: AppData.deviceLocation = JsonConvert.DeserializeObject <DeviceLocationInfo>(s); break; default: ErrorReporter.Report($"Download DataType \"{dataType}\" haven't been implemented!"); throw new OperationCanceledException(); } } catch (Microsoft.WindowsAzure.Storage.StorageException error) { if (error.Message == "An error occurred while sending the request") { if (await App.Current.MainPage.DisplayAlert("無法連線", "資料下載失敗,請將您的裝置連上網路後再重試一次", "重試", "取消")) { goto indexRetry; } else { throw new OperationCanceledException(); } } else { await new ErrorAlert("StorageException: 使用者資料下載失敗", error).Show(); throw new OperationCanceledException(); } } catch (Exception error) { await new ErrorAlert("Exception: 使用者資料下載失敗", error).Show(); throw new OperationCanceledException(); } }