/// <summary> /// Read the key in the storage helper instance and get the value. /// </summary> /// <typeparam name="TKey">The type of key used to lookup the object.</typeparam> /// <typeparam name="TValue">The type of object value expected.</typeparam> /// <param name="storageHelper">The storage helper instance fo read from.</param> /// <param name="key">The key of the target object.</param> /// <returns>The value of the target object</returns> /// <exception cref="KeyNotFoundException">Throws when the key is not found in storage.</exception> public static TValue?Read <TKey, TValue>(this ISettingsStorageHelper <TKey> storageHelper, TKey key) where TKey : notnull { if (storageHelper.TryRead <TValue>(key, out TValue? value)) { return(value); } else { ThrowKeyNotFoundException(key); return(default);
/// <summary> /// Attempts to read the provided key and return the value. /// If the key is not found, the fallback value will be used instead. /// </summary> /// <typeparam name="TKey">The type of key used to lookup the object.</typeparam> /// <typeparam name="TValue">The type of object value expected.</typeparam> /// <param name="storageHelper">The storage helper instance fo read from.</param> /// <param name="key">The key of the target object.</param> /// <param name="fallback">An alternative value returned if the read fails.</param> /// <returns>The value of the target object, or the fallback value.</returns> public static TValue?GetValueOrDefault <TKey, TValue>(this ISettingsStorageHelper <TKey> storageHelper, TKey key, TValue?fallback = default) where TKey : notnull { if (storageHelper.TryRead <TValue>(key, out TValue? storedValue)) { return(storedValue); } else { return(fallback); } }
private async Task InitRoamingSettingsHelperAsync() { switch (ProviderManager.Instance?.GlobalProvider?.State) { case ProviderState.SignedIn: if (_remoteFileStorage == null) { _remoteFileStorage = await OneDriveStorageHelper.CreateForCurrentUserAsync(_serializer); } if (_remoteSettingsStorage == null) { _remoteSettingsStorage = await UserExtensionStorageHelper.CreateForCurrentUserAsync("ContosoNotes.json", _serializer); } break; case ProviderState.SignedOut: _remoteFileStorage = null; _remoteSettingsStorage = null; break; } }
private async Task <NotePageModel> GetCurrentNotePage(ISettingsStorageHelper <string> settingsStorage, IFileStorageHelper fileStorage, NotesListModel notesList) { if (fileStorage == null || settingsStorage == null) { return(null); } if (settingsStorage.TryRead <string>("currentNotePageId", out string currentNotePageId) && currentNotePageId != null) { foreach (var notesListItem in notesList.Items) { if (currentNotePageId == notesListItem.NotePageId) { string notePageFileName = GetNotePageFileName(notesListItem); return(await fileStorage.ReadFileAsync <NotePageModel>(notePageFileName)); } } } return(null); }