Exemplo n.º 1
0
        public override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            this.Activity.ActionBar.SetTitle(Resource.String.settings);

            this.userSettings    = Locator.Current.GetService <UserSettings>();
            this.androidSettings = Locator.Current.GetService <AndroidSettings>();

            this.AddPreferencesFromResource(Resource.Layout.Settings);

            var portPref = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_port));

            portPref.EditText.InputType = InputTypes.ClassNumber;
            portPref.EditText.Events().TextChanged
            .Select(x => Int32.Parse(new string(x.Text.ToArray())))
            .Where(x => !NetworkHelpers.IsPortValid(x))
            .Subscribe(x =>
            {
                portPref.EditText.Error = this.GetString(Resource.String.preference_port_validation_error);
            });
            portPref.BindToSetting(this.userSettings, x => x.Port, x => x.Text, x => int.Parse(x.ToString()), x => x.ToString(), NetworkHelpers.IsPortValid);

            var ipAddressPref = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_ipaddress));

            ipAddressPref.EditText.InputType = InputTypes.ClassPhone;
            ipAddressPref.EditText.Events().TextChanged
            .Select(x => new string(x.Text.ToArray()))
            .Where(x => !IsValidIpAddress(x))
            .Subscribe(x =>
            {
                ipAddressPref.EditText.Error = this.GetString(Resource.String.preference_ipaddress_validation_error);
            });
            ipAddressPref.BindToSetting(this.userSettings, x => x.ServerAddress, x => x.Text, x => (string)x, x => x, IsValidIpAddress);

            var saveEnergyPref = (SwitchPreference)this.FindPreference(this.GetString(Resource.String.preference_save_energy));

            saveEnergyPref.BindToSetting(this.androidSettings, x => x.SaveEnergy, x => x.Checked, x => bool.Parse(x.ToString()));

            var passwordPreference = (EditTextPreference)this.FindPreference(this.GetString(Resource.String.preference_administrator_password));

            passwordPreference.BindToSetting(this.userSettings, x => x.AdministratorPassword, x => x.Text, x => (string)x);
            this.userSettings.WhenAnyValue(x => x.IsPremium, x => x || TrialHelpers.IsInTrialPeriod(AppConstants.TrialTime))
            .BindTo(passwordPreference, x => x.Enabled);

            Preference premiumButton = this.FindPreference(this.GetString(Resource.String.preference_purchase_premium));

            premiumButton.Events().PreferenceClick.Select(_ => this.PurchasePremium().ToObservable()
                                                          .Catch <Unit, Exception>(ex => Observable.Start(() => this.TrackInAppPurchaseException(ex))))
            .Concat()
            .Subscribe();

            Preference restorePremiumButton = this.FindPreference(this.GetString(Resource.String.preference_restore_premium));

            restorePremiumButton.Events().PreferenceClick.Select(_ => this.RestorePremium().ToObservable()
                                                                 .Catch <Unit, Exception>(ex => Observable.Start(() => this.TrackInAppPurchaseException(ex))))
            .Concat()
            .Subscribe();

            Preference versionPreference = this.FindPreference(this.GetString(Resource.String.preference_version));

            versionPreference.Summary = this.Activity.PackageManager.GetPackageInfo(this.Activity.PackageName, 0).VersionName;

            var virtualServerPreference = (SwitchPreference)this.FindPreference(this.GetString(Resource.String.preference_virtual_server));

            virtualServerPreference.Persistent = false;
            virtualServerPreference.Events().PreferenceChange
            .Subscribe(x =>
            {
                if ((bool)x.NewValue)
                {
                    NetworkMessenger.Override(new VirtualNetworkMessenger());
                }

                else
                {
                    NetworkMessenger.ResetOverride();
                }
            });

#if !DEBUG && !DEVELOPMENT
            var developmentCategory = (PreferenceCategory)this.FindPreference(this.GetString(Resource.String.preference_development));

            var screen = (PreferenceScreen)this.FindPreference(this.GetString(Resource.String.preference_main_screen));
            screen.RemovePreference(developmentCategory);
#endif
        }