Esempio n. 1
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_trip);

            // Initialise the action bar
            SetSupportActionBar(FindViewById <Toolbar>(Resource.Id.toolbar));

            // Create a StorageMechanism instance
            PersistentStorage.StorageMechanism = new StorageMechanism(this);

            // Link a custom adapter to the 'from' station selecter
            fromView           = FindViewById <StationAutoComplete>(Resource.Id.autoCompleteTextViewFrom);
            fromView.Adapter   = new StationAdapter(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
            fromView.InputType = Android.Text.InputTypes.TextFlagNoSuggestions | Android.Text.InputTypes.TextVariationVisiblePassword;

            // Link a custom adapter to the 'to' station selecter
            toView           = FindViewById <StationAutoComplete>(Resource.Id.autoCompleteTextViewTo);
            toView.Adapter   = new StationAdapter(this, Android.Resource.Layout.SimpleSpinnerDropDownItem);
            toView.InputType = Android.Text.InputTypes.TextFlagNoSuggestions | Android.Text.InputTypes.TextVariationVisiblePassword;

            // Validate the station names when the text is changed
            fromView.TextChanged += ValidateStationFields;
            toView.TextChanged   += ValidateStationFields;

            // Disable the add button by default
            addButton         = FindViewById <Button>(Resource.Id.buttonAdd);
            addButton.Enabled = false;

            // Validate the station names again when the add button is pressed
            addButton.Click += AddButtonClicked;

            // Terminate the activity if the cancel button is clicked
            FindViewById <Button>(Resource.Id.buttonCancel).Click += (sender, e) => {
                SetResult(Result.Canceled);
                Finish();
            };

            // Read in the fixed (and static) list of stations (aynchronously)
            StationStorage.ReadStations(Assets);
        }
Esempio n. 2
0
        /// <summary>
        /// Validate that the from and to station fileds contain valid stations names, are not the same, and
        /// do not represent a valid trip
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void AddButtonClicked(object sender, System.EventArgs e)
        {
            bool   validTrip    = true;
            string errorMessage = "";

            // Check for valid station names
            if (Array.IndexOf(StationStorage.StationNames, fromView.Text) < 0)
            {
                errorMessage += string.Format("'{0}' is not a valid station name\n", fromView.Text);
                validTrip     = false;
            }

            if (Array.IndexOf(StationStorage.StationNames, toView.Text) < 0)
            {
                errorMessage += string.Format("'{0}' is not a valid station name\n", toView.Text);
                validTrip     = false;
            }

            if (validTrip == true)
            {
                // Make sure they are not the same
                if (fromView.Text == toView.Text)
                {
                    errorMessage += "Station names cannot be the same\n";
                    validTrip     = false;
                }
            }

            if (validTrip == true)
            {
                // Check for a duplicate trip
                if (TrainTrips.IsDuplicateTrip(fromView.Text, toView.Text) == true)
                {
                    errorMessage += string.Format("A trip from {0} to {1} already exists\n", fromView.Text, toView.Text);
                    validTrip     = false;
                }
            }

            if (validTrip == false)
            {
                // Display the reason why this trip cannot be added
                new AlertDialog.Builder(this)
                .SetTitle("Cannot add trip")
                .SetMessage(errorMessage)
                .SetPositiveButton("OK", (EventHandler <DialogClickEventArgs>)null)
                .Create()
                .Show();
            }
            else
            {
                // Add the trip and close this activity
                TrainTrips.AddTrip(new TrainTrip {
                    From   = fromView.Text, To = toView.Text, FromCode = StationStorage.GetCode(fromView.Text),
                    ToCode = StationStorage.GetCode(toView.Text)
                });
                RecentStations.AddStation(fromView.Text);
                RecentStations.AddStation(toView.Text);

                SetResult(Result.Ok);
                Finish();
            }
        }