コード例 #1
0
        private bool IsDataValid(out TimeSpan duration, out ZoneData zoneData)
        {
            duration = TimeSpan.Zero;
            zoneData = new ZoneData();

            // duration
            if (txtDuration.Text.Split(':').Length == 2)
            {
                txtDuration.Text = "0:" + txtDuration.Text;
            }
            if (txtDuration.Text.Split(':').Length == 3)
            {
                if (!TimeSpan.TryParse(txtDuration.Text, out duration))
                {
                    MessageBox.Show("Please enter a valid duration", "Invalid Duration", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return(false);
                }
            }
            else
            {
                MessageBox.Show("Please enter a valid duration", "Invalid Duration", MessageBoxButtons.OK,
                                MessageBoxIcon.Information);
                return(false);
            }

            // distance
            if (comSport.SelectedIndex == (int)Common.Sport.Running ||
                comSport.SelectedIndex == (int)Common.Sport.Cycling)
            {
                double foo;
                if (!double.TryParse(txtDistance.Text, out foo))
                {
                    MessageBox.Show("Please enter the distance", "No distance", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return(false);
                }
            }

            // zone data
            if (txtZone1.Text != "" || txtZone2.Text != "" || txtZone3.Text != "" || txtZone4.Text != "" || txtZone5.Text != "")
            {
                // ensure TS format is fine
                if (txtZone1.Text.Split(':').Length == 2)
                {
                    txtZone1.Text = "0:" + txtZone1.Text;
                }
                if (txtZone2.Text.Split(':').Length == 2)
                {
                    txtZone2.Text = "0:" + txtZone2.Text;
                }
                if (txtZone3.Text.Split(':').Length == 2)
                {
                    txtZone3.Text = "0:" + txtZone3.Text;
                }
                if (txtZone4.Text.Split(':').Length == 2)
                {
                    txtZone4.Text = "0:" + txtZone4.Text;
                }
                if (txtZone5.Text.Split(':').Length == 2)
                {
                    txtZone5.Text = "0:" + txtZone5.Text;
                }

                if (txtZone1.Text == "")
                {
                    txtZone1.Text = "0:0:0";
                }
                if (txtZone2.Text == "")
                {
                    txtZone2.Text = "0:0:0";
                }
                if (txtZone3.Text == "")
                {
                    txtZone3.Text = "0:0:0";
                }
                if (txtZone4.Text == "")
                {
                    txtZone4.Text = "0:0:0";
                }
                if (txtZone5.Text == "")
                {
                    txtZone5.Text = "0:0:0";
                }

                if (!ZoneData.TryParse(txtZone5.Text + '_' + txtZone4.Text + '_' + txtZone3.Text + '_' +
                                       txtZone2.Text + '_' + txtZone1.Text, out
                                       zoneData))
                {
                    MessageBox.Show("Please enter valid zone data", "Invalid Zone Data", MessageBoxButtons.OK,
                                    MessageBoxIcon.Information);
                    return(false);
                }

                var diff = zoneData.Duration.CompareTo(duration) <= 0 ? zoneData.Duration.TotalSeconds / duration.TotalSeconds : duration.TotalSeconds / zoneData.Duration.TotalSeconds;
                if (diff > 1 + Common.SignificancePercentage || diff < 1 - Common.SignificancePercentage)
                {
                    var res = MessageBox.Show("Difference between sum of zone data and duration is too big (" + Math.Round((1 - diff) * 100, 2) + "%). Do you want to normalize?", "Too big difference", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Information);

                    if (res == DialogResult.Cancel)
                    {
                        return(false);
                    }

                    if (res == DialogResult.Yes)
                    {
                        zoneData.Normailze(duration);

                        diff = zoneData.Duration.CompareTo(duration) <= 0 ? zoneData.Duration.TotalSeconds / duration.TotalSeconds : duration.TotalSeconds / zoneData.Duration.TotalSeconds;

                        MessageBox.Show("New difference after normalizing: " + Math.Round((1 - diff) * 100, 2) + "%. Times:\n" + zoneData.ToString().Replace('_', '\t'), "Results of Normalization", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    }
                }
            }

            return(true);
        }