/// <summary> /// Constructs a Layout instance from the given JSON data. /// </summary> /// <param name="headers"></param> /// <param name="content"></param> /// <param name="layoutRetrievedTime"></param> /// <returns></returns> public static Layout FromJson(string headers, JsonObject content, DateTimeOffset layoutRetrievedTime) { Layout layout = null; try { layout = new Layout(); if (!string.IsNullOrEmpty(headers)) { try { layout.ResolveMaxAge(headers, layoutRetrievedTime); } catch (Exception) { layout.ValidTill = DateTimeOffset.MaxValue; } } else { layout.ValidTill = DateTimeOffset.MaxValue; } layout.ResolveAccountBeaconId1s(content); layout.ResolveActions(content); } catch (Exception ex) { System.Diagnostics.Debug.WriteLine("Layout.FromJson(): Failed to parse: " + ex.ToString()); } return layout; }
internal async Task<bool> InternalVerifyLayoutAsync(bool forceUpdate) { if (forceUpdate || !CheckLayoutValidity()) { if (!forceUpdate) { // Check local storage first _layout = await LoadLayoutFromLocalStorageAsync(); } if (forceUpdate || !CheckLayoutValidity()) { // Make sure that the existing layout (even if old) is not set to null in case // we fail to load the fresh one from the web. Layout freshLayout = await RetrieveLayoutAsync(); if (freshLayout != null) { _layout = freshLayout; } } } return CheckLayoutValidity(); }
/// <summary> /// Creates a hash string based on the beacon ID1s in the given layout. /// </summary> /// <param name="layout">The layout containing the beacon ID1s.</param> /// <returns>A hash string of the beacon ID1s or null in case of an error.</returns> public static string CreateHashOfBeaconId1sInLayout(Layout layout) { string hash = null; if (layout != null) { IList<string> beaconId1s = layout.AccountBeaconId1s; if (beaconId1s.Count > 0) { hash = beaconId1s[0]; string currentUuid = string.Empty; for (int i = 1; i < beaconId1s.Count; ++i) { currentUuid = beaconId1s[i]; for (int j = 0; j < currentUuid.Length; ++j) { if (hash.Length < j + 1) { hash += currentUuid[j]; } else { char combinationChar = (char)(((int)hash[j] + (int)currentUuid[j]) / 2 + 1); if (j == 0) { hash = combinationChar + hash.Substring(j + 1); } else if (hash.Length > j + 1) { hash = hash.Substring(0, j) + combinationChar + hash.Substring(j + 1); } else { hash = hash.Substring(0, j) + combinationChar; } } } } } } return hash; }
/// <summary> /// Invalidates both the current and cached layout. /// </summary> public IAsyncAction InvalidateLayoutAsync() { Func<Task> action = async () => { _layout = null; _localSettings.Values[KeyLayoutHeaders] = null; _localSettings.Values[KeyLayoutRetrievedTime] = null; try { var contentFile = await ApplicationData.Current.LocalFolder.TryGetItemAsync(KeyLayoutContent); if (contentFile != null) { await contentFile.DeleteAsync(); } } catch (Exception) { } }; return action().AsAsyncAction(); }