Example #1
0
        /// <summary>
        /// Creates a new RoamingSettingsHelper instance for the currently signed in user.
        /// </summary>
        /// <param name="dataStore">Which specific data store is being used.</param>
        /// <param name="autoSync">Whether the values should immediately sync or not.</param>
        /// <param name="serializer">An object serializer for serialization of objects in the data store.</param>
        /// <returns>A new instance of the RoamingSettingsHelper configured for the current user.</returns>
        public static async Task <RoamingSettingsHelper> CreateForCurrentUser(RoamingDataStore dataStore = RoamingDataStore.UserExtensions, bool autoSync = true, IObjectSerializer serializer = null)
        {
            var provider = ProviderManager.Instance.GlobalProvider;

            if (provider == null || provider.State != ProviderState.SignedIn)
            {
                throw new InvalidOperationException("The GlobalProvider must be set and signed in to create a new RoamingSettingsHelper for the current user.");
            }

            var me = await provider.Graph.Me.Request().GetAsync();

            return(new RoamingSettingsHelper(me.Id, dataStore, autoSync, serializer));
        }
Example #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="RoamingSettingsHelper"/> class.
        /// </summary>
        /// <param name="userId">The id of the target Graph User.</param>
        /// <param name="dataStore">Which specific data store is being used.</param>
        /// <param name="autoSync">Whether the values should immediately sync or not.</param>
        /// <param name="serializer">An object serializer for serialization of objects in the data store.</param>
        public RoamingSettingsHelper(string userId, RoamingDataStore dataStore = RoamingDataStore.UserExtensions, bool autoSync = true, IObjectSerializer serializer = null)
        {
            if (serializer == null)
            {
                serializer = new JsonObjectSerializer();
            }

            // TODO: Infuse unique identifier from Graph registration into the storage name.
            string dataStoreName = "communityToolkit.roamingSettings";

            switch (dataStore)
            {
            case RoamingDataStore.UserExtensions:
                DataStore = new UserExtensionDataStore(dataStoreName, userId, serializer);
                break;

            case RoamingDataStore.OneDrive:
                DataStore = new OneDriveDataStore(dataStoreName + ".json", serializer);
                break;

            default:
                throw new ArgumentOutOfRangeException(nameof(dataStore));
            }

            if (autoSync)
            {
                try
                {
                    DataStore.Sync();
                }
                catch
                {
                    // Sync may fail if the storage container does not yet exist.
                }
            }
        }