# MystBin ! - ongoing.cs using System; using System.CodeDom.Compiler; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.IO.Packaging; using System.Linq; using System.Runtime.Serialization; using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; using System.Windows.Forms; using System.Windows.Media.Media3D; namespace NEA___Fitness_Tracker { public partial class ongoing : Form { public string username; private int hours = 0; private int minutes = 0; private int seconds = 0; public int WorkoutID; public string workout_name; public string exercise_name = ""; public string added_exercise = ""; string _sSqlString; public List exercises = new List(); List> exerciseData = new List>(); public ongoing(string username2) { InitializeComponent(); username = username2; vScrollBar1.Value = flowLayoutPanel1.VerticalScroll.Value; vScrollBar1.Minimum = flowLayoutPanel1.VerticalScroll.Minimum; vScrollBar1.Maximum = flowLayoutPanel1.VerticalScroll.Maximum; DurationTimer.Start(); } private void ongoing_Load(object sender, EventArgs e) { } private void vScrollBar1_Scroll(object sender, ScrollEventArgs e) { flowLayoutPanel1.VerticalScroll.Value = vScrollBar1.Value; } private bool DataEntryCheck(List weights_lifted, List reps_completed) { try { for(int i = 0; i weights_lifted, List reps_completed) { foreach(object weight in weights_lifted) { if (weight.ToString() != "") { try { int.Parse(weight.ToString()); } catch (Exception e) { return false; } } } foreach (object reps in reps_completed) { if (reps.ToString() != "") { try { int.Parse(reps.ToString()); } catch (Exception e) { return false; } } } return true; } private int TotalSetsCompleted(List weights_lifted) { int count = 0; foreach(object item in weights_lifted) { if (item.ToString() != "") { count++; } } return count; } private int total_weight(List weights_lifted) { int total_weight = 0; foreach(object item in weights_lifted) { try { total_weight += int.Parse(item.ToString()); } catch(Exception e) { continue; } } return total_weight; } private double calc_1repmax(List weights_lifted, List reps_completed) { List weights_lifted_int = new List(); double onerepmax = 0; double multiplier = 1; foreach(object item in weights_lifted) { try { weights_lifted_int.Add(int.Parse(item.ToString())); } catch(Exception E) { continue; } } int highest_weight = weights_lifted_int.Max(); int position = weights_lifted_int.FindIndex(a => a==highest_weight); int reps_completed_for_set = int.Parse((reps_completed[position]).ToString()); if (reps_completed_for_set == 1) { onerepmax = highest_weight; } else if(reps_completed_for_set > 1 && reps_completed_for_set <= 10) { multiplier = (0.0278 + ((0.002) * reps_completed_for_set)); } else if(reps_completed_for_set > 10) { multiplier = (0.0278 + ((0.002) * 10)); } onerepmax = (highest_weight / ((1.0278 - (multiplier * reps_completed_for_set)))); return onerepmax; } private string ConvertToString(List> dataValues) { string final = "["; bool first = true; foreach(var data in dataValues) { if (!first) { final += ","; } final += "{"; foreach(var data2 in data) { final += $"\"{data2.Key}\": \"{data2.Value}\","; } if (data.Count > 0) { final = final.Remove(final.Length - 1); } final += "},"; } if(dataValues.Count > 0) { final = final.Remove(final.Length - 1); } final += "]"; return final; } private async void button5_Click(object sender, EventArgs e) { DateTime today = DateTime.Today; string date_today = today.ToString("dd/MM/yy"); string duration = label2.Text; bool complete = false; foreach (System.Windows.Forms.Control flowpanel in this.Controls) { if(flowpanel is System.Windows.Forms.FlowLayoutPanel) { int count = 0; List> data = DatabaseUtils.ReadData("SELECT * FROM HistoricalWorkouts"); if (data.Count == 0) { WorkoutID = 0; } else { data = DatabaseUtils.ReadData("SELECT MAX(WorkoutID) FROM HistoricalWorkouts"); WorkoutID = Convert.ToInt32(data[0][0]) + 1; } workout_name = (Controls.Find("workoutName", true).FirstOrDefault() as System.Windows.Forms.TextBox).Text; _sSqlString = "INSERT INTO HistoricalWorkouts(WorkoutID, Username, WorkoutName, DateOf, Duration) " + "Values('" + WorkoutID + "','" + username + "', '" + workout_name + "', '" + date_today + "', '" + duration + "')"; DatabaseUtils.ExecuteSqlNonQuery(_sSqlString); foreach (System.Windows.Forms.Control usercontrol in flowpanel.Controls) { count += 1; } if (count == 0) { warning.Text = "No workouts added detected, workout is being cancelled"; warning.Location = new System.Drawing.Point(23, 551); warning.Font = new Font("Bauhaus 93", 9); warning.Visible = true; await Task.Delay(3000); warning.Visible = false; this.Close(); } else { foreach (System.Windows.Forms.Control usercontrol in flowpanel.Controls) { List weights_lifted = new List(); List reps_completed = new List(); int count3 = 1; bool continue1 = true; while(continue1 == true) { string name1 = "KG" + count3.ToString(); System.Windows.Forms.TextBox KG_find = usercontrol.Controls.Find(name1, true).FirstOrDefault() as System.Windows.Forms.TextBox; if(KG_find == null){ continue1 = false; } else { weights_lifted.Add(KG_find.Text); } count3++; } for(int i = 0; i exerciseinfo = new Dictionary() { {"Exercise Name",exercise_name }, {"Total Sets",totalsets }, {"Reps", reps_completed }, {"Weights", weights_lifted }, {"Total Weight", total_weightcalc }, {"1REPMAX", onerepmax } }; exerciseData.Add(exerciseinfo); List> reps_completed2 = new List>(); Dictionary reps_completed3 = new Dictionary(); for(int i = 0; i> weights_lifted2 = new List>(); Dictionary weights_lifted3 = new Dictionary(); for (int i = 0; i < reps_completed.Count; i++) { string name = "Set" + (i + 1).ToString(); weights_lifted3.Add(name, weights_lifted[i]); } weights_lifted2.Add(weights_lifted3); _sSqlString = "INSERT INTO ExerciseInfo(WorkoutID, ExcName, TotalSets, Reps, Weights, TotalWeight, [1REPMAX]) " + "VALUES('" + WorkoutID + "', '" + exercise_name + "', '" + totalsets + "', '" + ConvertToString(reps_completed2) + "', '" + ConvertToString(weights_lifted2) + "', '" + total_weightcalc + "', '" + onerepmax + "')"; DatabaseUtils.ExecuteSqlNonQuery(_sSqlString); _sSqlString = $"UPDATE UserProfile SET TotalWorkouts = TotalWorkouts + 1 WHERE username = '{username}';"; DatabaseUtils.ExecuteSqlNonQuery(_sSqlString); complete = true; } } } } } } if(complete == true) { warning.Text = "Workout successfully saved"; warning.Location = new System.Drawing.Point(25, 551); warning.Font = new Font("Bauhaus 93", 15); warning.Visible = true; await Task.Delay(3000); warning.Visible = false; } this.Close(); } private void button4_Click(object sender, EventArgs e) { var confirmation = new confirmation(username); confirmation.Show(); } private void button3_Click(object sender, EventArgs e) { addexercise add = new addexercise(username); add.DataTransferred += ongoing_datareceived; add.Show(); } private void UserControl_DeleteClicked(object sender, string exercise) { exercises.Remove(exercise); } private async void ongoing_datareceived(object sender, string data) { added_exercise = data; int index = exercises.IndexOf(added_exercise); if (index == -1) { ongoing_control control = new ongoing_control(added_exercise); control.DeleteClicked += UserControl_DeleteClicked; control.Name = added_exercise.ToString(); exercises.Add(added_exercise); flowLayoutPanel1.Controls.Add(control); Form addexercise = System.Windows.Forms.Application.OpenForms["addexercise"]; if (addexercise != null) { addexercise.Close(); } } else { warning.Text = "EXERCISE ALREADY EXISTS"; warning.Location = new System.Drawing.Point(25, 551); warning.Font = new Font("Bauhaus 93", 15); warning.Visible = true; await Task.Delay(3000); warning.Visible = false; } } private void DurationTimer_Tick(object sender, EventArgs e) { seconds++; if(seconds >= 60) { seconds = 0; minutes++; if(minutes >= 60) { minutes = 0; hours++; } } label2.Text = $"{hours:D2}:{minutes:D2}:{seconds:D2}"; } private void flowLayoutPanel1_Paint(object sender, PaintEventArgs e) { } private void label2_Click(object sender, EventArgs e) { } } }