jamesmontemagno / SettingsPlugin

Read and Write Settings Plugin for Xamarin and Windows
MIT License
324 stars 80 forks source link

Implement FileName for specifying file/suite/container name. #19

Closed jamesmontemagno closed 8 years ago

jamesmontemagno commented 8 years ago

Feature Request:

This feature would enable any developer to specify an additional parameter on each method in the Settings plugin to specify a name for the platform specific file. If it is null or empty string, then use default. This would fix #3 and #17.

iOS

File name would be used to specify the SuiteName:

 NSUserDefaults GetUserDefaults(string fileName = null) =>
            string.IsNullOrWhiteSpace(fileName) ?
            NSUserDefaults.StandardUserDefaults :
            new NSUserDefaults(fileName, NSUserDefaultsType.SuiteName);

Android

This would be used to specify the shared preference name instead of using the default shared preferences. The FileCreationMode will be set to private because all other ones have been deprecated at this point according to documentation.

Implementation:

    ISharedPreferences GetSharedPrefence(string fileName) =>
            string.IsNullOrWhiteSpace(fileName) ?
            PreferenceManager.GetDefaultSharedPreferences(Application.Context) :
            Application.Context.GetSharedPreferences(fileName, FileCreationMode.Private);

UWP and Win RT

These use ApplicationDataContainers. By default we use the root, but the LocalSettings can actually have buckets of additional containers. So we can attempt to create additional containers and return it if necessary:

ApplicationDataContainer GetAppSettings(string fileName = null)
        {
            if (string.IsNullOrWhiteSpace(fileName))
                return ApplicationData.Current.LocalSettings;

            if (!ApplicationData.Current.LocalSettings.Containers.ContainsKey(fileName))
                ApplicationData.Current.LocalSettings.CreateContainer(fileName, ApplicationDataCreateDisposition.Always);

            return ApplicationData.Current.LocalSettings.Containers[fileName];
        }

.NET 4.5/WP Silverlight

These use isolated storage, so no implementation to be used.

jamesmontemagno commented 8 years ago

Available for testing on: 2.6.0-beta9