private long NSDateToUnixEpoch(NSDate date) { NSCalendar cal = NSCalendar.CurrentCalendar; int year = (int)cal.GetComponentFromDate(NSCalendarUnit.Year, date); int month = (int)cal.GetComponentFromDate(NSCalendarUnit.Month, date); int day = (int)cal.GetComponentFromDate(NSCalendarUnit.Day, date); int hour = (int)cal.GetComponentFromDate(NSCalendarUnit.Hour, date); int minute = (int)cal.GetComponentFromDate(NSCalendarUnit.Minute, date); int second = (int)cal.GetComponentFromDate(NSCalendarUnit.Second, date); int nanosecond = (int)cal.GetComponentFromDate(NSCalendarUnit.Nanosecond, date); int millisecond = (nanosecond / 1000000); var local = new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Local); DateTime utc = local.ToUniversalTime(); return((long)(utc - UnixEpoch).TotalMilliseconds); }
public static DateTime NSDateToDateTime(this NSDate nsDate) { if (nsDate == null) { return(new DateTime()); // ? } NSCalendar cal = NSCalendar.CurrentCalendar; int year = (int)cal.GetComponentFromDate(NSCalendarUnit.Year, nsDate); int month = (int)cal.GetComponentFromDate(NSCalendarUnit.Month, nsDate); int day = (int)cal.GetComponentFromDate(NSCalendarUnit.Day, nsDate); int hour = (int)cal.GetComponentFromDate(NSCalendarUnit.Hour, nsDate); int minute = (int)cal.GetComponentFromDate(NSCalendarUnit.Minute, nsDate); int second = (int)cal.GetComponentFromDate(NSCalendarUnit.Second, nsDate); int nanosecond = (int)cal.GetComponentFromDate(NSCalendarUnit.Nanosecond, nsDate); int millisecond = (nanosecond / 1000000); return(new DateTime(year, month, day, hour, minute, second, millisecond, DateTimeKind.Local)); }
public override void ViewDidLoad() { base.ViewDidLoad (); //scrollView = stackView.Superview as UIScrollView; //scrollView.KeyboardDismissMode = UIScrollViewKeyboardDismissMode.OnDrag; //scrollView.ScrollEnabled = true; // Set up Navigation Bar var saveButton = new UIBarButtonItem (UIBarButtonSystemItem.Save, save); var cancelButton = new UIBarButtonItem (UIBarButtonSystemItem.Cancel, cancel); NavigationItem.Title = "New Medication:"; NavigationItem.RightBarButtonItem = saveButton; NavigationItem.HidesBackButton = true; NavigationItem.LeftBarButtonItem = cancelButton; //NSNotificationCenter.DefaultCenter.AddObserver (this, new ObjCRuntime.Selector("keyboardDidAppear:"), UIKeyboard.DidShowNotification, null); //NSNotificationCenter.DefaultCenter.AddObserver (this, new ObjCRuntime.Selector ("keyboardWillDissapear:"), UIKeyboard.WillHideNotification, null); // Set up the date formatter dateFormat = new NSDateFormatter(); dateFormat.DateStyle = NSDateFormatterStyle.None; dateFormat.TimeStyle = NSDateFormatterStyle.Short; // Set up new pet form nameField = new UITextField(); nameField.Text = "Medication Name"; nameField.BorderStyle = UITextBorderStyle.RoundedRect; nameField.ReturnKeyType = UIReturnKeyType.Done; medTypeLabel = new UILabel (); medTypeLabel.Text = "Type of Medication"; medTypeButton = new UIButton (UIButtonType.RoundedRect); medTypeButton.AddTarget (editMedType, UIControlEvent.TouchUpInside); medTypeButton.SetTitle ("Pill", UIControlState.Normal); medTypeButton.TitleLabel.Font = medTypeButton.TitleLabel.Font.WithSize (medTypeLabel.Font.PointSize); medTypePicker = new UIPickerView (); medTypePicker.Delegate = new MedTypePickerDelegate (this); medTypePicker.DataSource = new MedTypePickerDataSource (); freqLabel = new UILabel (); freqLabel.Text = "Frequency"; freqTextField = new UITextField (); freqTextField.Text = "1"; freqTextField.KeyboardType = UIKeyboardType.NumberPad; freqTextField.BorderStyle = UITextBorderStyle.RoundedRect; freqTextField.Enabled = false; freqTextField.AddTarget (freqTextFieldChanged, UIControlEvent.EditingDidEnd | UIControlEvent.EditingDidEndOnExit); freqStepper = new UIStepper (); freqStepper.Value = 1; freqStepper.MinimumValue = 1; freqStepper.Enabled = false; freqStepper.AddTarget (freqStepperIncremented, UIControlEvent.ValueChanged); UIStackView freqStackView = new UIStackView (new UIView[] { freqTextField, freqStepper }); freqStackView.Spacing = 8; freqStackView.Axis = UILayoutConstraintAxis.Horizontal; freqButton = new UIButton (UIButtonType.RoundedRect); freqButton.SetTitle ("Daily", UIControlState.Normal); freqButton.TitleLabel.Font = freqButton.TitleLabel.Font.WithSize (medTypeLabel.Font.PointSize); freqButton.AddTarget (editFrequency, UIControlEvent.TouchUpInside); freqPicker = new UIPickerView (); freqPicker.Delegate = new MedFreqPickerDelegate (this); freqPicker.DataSource = new MedFreqPickerDataSource (); timePicker = new UIDatePicker (); gregorian = new NSCalendar (NSCalendarType.Gregorian); timePicker.Date = gregorian.DateBySettingsHour (9, 0, 0, NSDate.Now, NSCalendarOptions.MatchNextTime); timePicker.Mode = UIDatePickerMode.Time; timePicker.AddTarget (timePickerChanged, UIControlEvent.AllEvents); UILabel timeLabel = new UILabel (); timeLabel.Text = "Time"; timeButtons = new UIButton[1]; timeDates = new NSDate[1]; timeDates [0] = gregorian.DateBySettingsHour (9, 0, 0, NSDate.Now, NSCalendarOptions.MatchNextTime); timeButtons [0] = new UIButton (UIButtonType.RoundedRect); timeButtons [0].SetTitle ("9:00 AM", UIControlState.Normal); timeButtons [0].AddTarget (openTimePicker, UIControlEvent.TouchUpInside); timeStack = new UIStackView (); timeStack.Alignment = UIStackViewAlignment.Leading; timeStack.Distribution = UIStackViewDistribution.FillProportionally; timeStack.Spacing = 5; timeStack.Axis = UILayoutConstraintAxis.Vertical; timeStack.AddArrangedSubview (timeLabel); foreach (var button in timeButtons) { timeStack.AddArrangedSubview (button); } timeStack.AddArrangedSubview (timePicker); timePicker.Hidden = true; dayLabel = new UILabel (); dayLabel.Text = "Day"; dayLabel.Hidden = true; dayButton = new UIButton (UIButtonType.RoundedRect); var day = gregorian.GetComponentFromDate (NSCalendarUnit.Weekday, NSDate.Now); dayButton.SetTitle (gregorian.WeekdaySymbols[day], UIControlState.Normal); dayButton.AddTarget (openDayPicker, UIControlEvent.TouchUpInside); dayButton.Hidden = true; dayPicker = new UIDatePicker (); dayPicker.Mode = UIDatePickerMode.Date; dayPicker.MinimumDate = NSDate.Now; dayPicker.MaximumDate = NSDate.Now.AddSeconds (604800); dayPicker.AddTarget (dayPickerChanged, UIControlEvent.AllEvents); dayPicker.Hidden = true; prescriptionLengthLabel = new UILabel (); prescriptionLengthLabel.Text = "Prescription Length"; prescriptionLengthTextField = new UITextField (); prescriptionLengthTextField.Text = "20"; prescriptionLengthTextField.AddTarget (prescriptionTextFieldChanged, UIControlEvent.EditingDidEnd | UIControlEvent.EditingDidEndOnExit); prescriptionLengthTextField.KeyboardType = UIKeyboardType.NumberPad; prescriptionLengthTextField.BorderStyle = UITextBorderStyle.RoundedRect; prescriptionLengthStepper = new UIStepper (); prescriptionLengthStepper.Value = 20; prescriptionLengthStepper.MinimumValue = 1; prescriptionLengthStepper.AddTarget (prescriptionStepperIncremented, UIControlEvent.ValueChanged); prescriptionDayLabel = new UILabel (); prescriptionDayLabel.Text = "Days"; UIStackView prescriptionStackView = new UIStackView (new UIView[] { prescriptionLengthTextField, prescriptionLengthStepper, prescriptionDayLabel }); prescriptionStackView.Axis = UILayoutConstraintAxis.Horizontal; prescriptionStackView.Alignment = UIStackViewAlignment.Leading; prescriptionStackView.Distribution = UIStackViewDistribution.FillProportionally; prescriptionStackView.Spacing = 5; refillsLabel = new UILabel (); refillsLabel.Text = "Refills Remaining"; refillsTextField = new UITextField (); refillsTextField.Text = "0"; refillsTextField.KeyboardType = UIKeyboardType.NumberPad; refillsTextField.BorderStyle = UITextBorderStyle.RoundedRect; refillsTextField.AddTarget (refillsTextFieldChanged, UIControlEvent.EditingDidEnd | UIControlEvent.EditingDidEndOnExit); refillsStepper = new UIStepper (); refillsStepper.Value = 0; refillsStepper.MinimumValue = 0; refillsStepper.AddTarget (refillsStepperIncremented, UIControlEvent.ValueChanged); UIStackView refillsStackView = new UIStackView (new UIView[] { refillsTextField, refillsStepper }); refillsStackView.Spacing = 5; refillsStackView.Alignment = UIStackViewAlignment.Leading; refillsStackView.Distribution = UIStackViewDistribution.FillProportionally; refillsStackView.Axis = UILayoutConstraintAxis.Horizontal; pharmacyTextView = new UITextView (); pharmacyTextView.Text = "Pharmacy Address"; pharmacyTextView.ScrollEnabled = false; pharmacyTextView.BackgroundColor = UIColor.LightGray; pharmacyTextField = new UITextField (); pharmacyTextField.Text = "Pharmacy Address"; pharmacyTextField.BorderStyle = UITextBorderStyle.RoundedRect; // Sets up the stackview stackView.Spacing = 5; stackView.Alignment = UIStackViewAlignment.Leading; stackView.Distribution = UIStackViewDistribution.EqualSpacing; stackView.AddArrangedSubview (nameField); UIView spaceView = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView.AddConstraint (NSLayoutConstraint.Create (spaceView, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView); stackView.AddArrangedSubview (medTypeLabel); stackView.AddArrangedSubview (medTypeButton); stackView.AddArrangedSubview (medTypePicker); medTypePicker.Hidden = true; UIView spaceView2 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView2.AddConstraint (NSLayoutConstraint.Create (spaceView2, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView2); stackView.AddArrangedSubview (freqLabel); stackView.AddArrangedSubview (freqStackView); stackView.AddArrangedSubview (freqButton); stackView.AddArrangedSubview (freqPicker); freqPicker.Hidden = true; UIView spaceView3 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView3.AddConstraint (NSLayoutConstraint.Create (spaceView3, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView3); stackView.AddArrangedSubview (timeStack); stackView.AddArrangedSubview (dayLabel); stackView.AddArrangedSubview (dayButton); stackView.AddArrangedSubview (dayPicker); UIView spaceView4 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView4.AddConstraint (NSLayoutConstraint.Create (spaceView4, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView4); stackView.AddArrangedSubview (prescriptionLengthLabel); stackView.AddArrangedSubview (prescriptionStackView); UIView spaceView5 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView5.AddConstraint (NSLayoutConstraint.Create (spaceView5, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView5); stackView.AddArrangedSubview (refillsLabel); stackView.AddArrangedSubview (refillsStackView); UIView spaceView6 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView6.AddConstraint (NSLayoutConstraint.Create (spaceView6, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView6); stackView.AddArrangedSubview (pharmacyTextField); UIView spaceView7 = new UIView (CoreGraphics.CGRect.FromLTRB (0, 0, 50, SECTION_SPACER)); spaceView7.AddConstraint (NSLayoutConstraint.Create (spaceView7, NSLayoutAttribute.Height, NSLayoutRelation.Equal, null, NSLayoutAttribute.NoAttribute, 1, SECTION_SPACER)); stackView.AddArrangedSubview (spaceView7); }