From dabd27c6ba0dc615837feec02638be4b560cad50 Mon Sep 17 00:00:00 2001
From: Martin Hustoles <martin.hustoles@student.reutlingen-university.de>
Date: Sun, 29 Dec 2024 02:45:05 +0100
Subject: [PATCH] added Timer functionality

---
 .../Screens/Regulaer/02Session.xaml           | 16 ++--
 .../Screens/Regulaer/02Session.xaml.cs        | 93 +++++++++++++++++--
 .../Styles/Styles.xaml                        | 46 +++++++++
 3 files changed, 140 insertions(+), 15 deletions(-)

diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml
index 027756c..59c33b9 100644
--- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml
+++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml
@@ -9,12 +9,12 @@
         Height="550"  Width="900">
     <Grid>
         <header:HeaderTemplate VerticalAlignment="Top"/>
-
-        <Button Content="Subscription" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,180,0" VerticalAlignment="Top" Height="26" Width="80" Click="Option1_Click"/>
-        <Button Content="Contract" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,100,0" VerticalAlignment="Top" Height="26" Width="80" Click="Option2_Click"/>
-        <Button Content="Distractions" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,20,0" VerticalAlignment="Top" Height="26" Width="80" Click="Option3_Click"/>
-
-        <!-- DELETE -->
-        <Label Content="Session Page" HorizontalAlignment="Center" Margin="0,234,0,0" VerticalAlignment="Top" Height="108" Width="439" FontSize="48" HorizontalContentAlignment="Center" VerticalContentAlignment="Center"/>
+        <Button Content="Subscription" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,180,0" VerticalAlignment="Top" Height="26" Width="80"/>
+        <Button Content="Contact" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,100,0" VerticalAlignment="Top" Height="26" Width="80"/>
+        <Button Content="Distractions" Style="{StaticResource TopMenuButon}" HorizontalAlignment="Right" Margin="0,4,20,0" VerticalAlignment="Top" Height="26" Width="80"/>
+        <TextBlock Name ="Title" Text="Focus Session 0 of 0" Style="{StaticResource Heading1}" HorizontalAlignment="Center" Height="42" TextWrapping="Wrap" VerticalAlignment="Top" Width="442" Margin="0,133,0,0"/>
+        <TextBlock Name="Clock" Text="00:00" Style="{StaticResource Heading1}" HorizontalAlignment="Center" Height="104" TextWrapping="Wrap" VerticalAlignment="Top" Width="326" Margin="0,180,0,0" FontSize="72" FontWeight="Bold"/>
+        <Rectangle Name="ProgressBar" Style="{StaticResource CircleSmall}" HorizontalAlignment="Center" Margin="0,289,0,0" VerticalAlignment="Top" Width="407"/>
+        <Button Content="Start" Name="PlayPauseButton" Style="{StaticResource PlayPauseButton}" HorizontalAlignment="Center" Margin="0,346,0,0" VerticalAlignment="Top" RenderTransformOrigin="0.5,0.5" Click="Button_Click"/>
     </Grid>
-</Window>
+</Window>
\ No newline at end of file
diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs
index a3902cd..b92c042 100644
--- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs
+++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Screens/Regulaer/02Session.xaml.cs
@@ -1,4 +1,5 @@
-using System.Text;
+using System.Diagnostics;
+using System.Text;
 using System.Windows;
 using System.Windows.Controls;
 using System.Windows.Data;
@@ -8,6 +9,7 @@ using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using System.Windows.Navigation;
 using System.Windows.Shapes;
+using System.Windows.Threading;
 
 namespace InnoLabProjektDektopApp
 {
@@ -16,22 +18,99 @@ namespace InnoLabProjektDektopApp
     /// </summary>
     public partial class Session : Window
     {
+
+        private Timer timer;
+        private const int maxTime = (0 * 60) + 5;//(40 * 60) + 0;
+        private int seconds = maxTime; // (Minutes * [constant]) + Seconds
+        private int step = 0;
+        private const int sessions = 4;
+        private int currentSession = 1;
+
         public Session()
         {
             InitializeComponent();
             GlobalSettings.setDefaults(this);
+
+            timer = new Timer(timertick, null,0,1000);
+
+            updateTexts();
+        }
+
+        private void timertick(object state)
+        {
+            Dispatcher.Invoke(() =>
+            {
+                seconds += step;
+                updateTexts();
+                ProgressBar.Fill = GradientGenerator.generateTwoColorBrush(Color.FromRgb(72, 98, 132), Color.FromRgb(222, 222, 222), 1 - (seconds / (double)maxTime));
+                if (seconds <= 0)
+                {
+                    stopTimer();
+                    currentSession++;
+                    seconds = maxTime;
+
+                    if (currentSession > sessions)
+                    {
+                        timer.Dispose();
+                    }
+                }
+            });
+        }
+
+        public void startTimer()
+        {
+            if (seconds <= 0) return;
+            step = -1;
+            PlayPauseButton.Content = "Pause";
+        }
+
+        public void stopTimer()
+        {
+            step = 0;
+            PlayPauseButton.Content = "Start";
         }
-        private void Option1_Click(object sender, RoutedEventArgs e)
+
+        public void setTime(int minutes, int seconds)
         {
-            //this.Content = new AnotherWindow().Content;
+            if (minutes < 0 || minutes > maxTime / 60 || seconds < 0 || seconds > maxTime % 60) return;
+            seconds = minutes * 60 + seconds;
+            updateTexts();
         }
-        private void Option2_Click(object sender, RoutedEventArgs e)
+
+
+
+        private void updateTexts()
         {
-            //this.Content = new AnotherWindow().Content;
+            Clock.Text = seconds / 60 + ":" + (seconds % 60 < 10 ? "0" : "") + seconds % 60;
+            Title.Text = "Focus Session " + currentSession + " of " + sessions;
         }
-        private void Option3_Click(object sender, RoutedEventArgs e)
+
+        private void Button_Click(object sender, RoutedEventArgs e)
         {
-            //this.Content = new AnotherWindow().Content;
+            if (step < 0) {
+                stopTimer();
+            } else {
+                startTimer();        
+            }
         }
     }
+}
+
+public static class GradientGenerator
+{
+    public static Brush generateTwoColorBrush(Color color1, Color color2, double ratio)
+    {
+        GradientStopCollection collection = new GradientStopCollection();
+
+        collection.Add(new GradientStop(color1, 0));
+        collection.Add(new GradientStop(color1, ratio));
+        collection.Add(new GradientStop(color2, ratio));
+        collection.Add(new GradientStop(color2, 1.0));
+
+        LinearGradientBrush brush = new LinearGradientBrush(collection);
+        brush.StartPoint = new Point(0, 0);
+        brush.EndPoint = new Point(1, 0);
+
+        return brush;
+    }
 }
\ No newline at end of file
diff --git a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Styles/Styles.xaml b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Styles/Styles.xaml
index 457b851..f89aa15 100644
--- a/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Styles/Styles.xaml
+++ b/InnoLabProjektDektopApp/InnoLabProjektDektopApp/Styles/Styles.xaml
@@ -59,4 +59,50 @@
         <Setter Property="BorderThickness" Value="0"/>
     </Style>
 
+    <Style TargetType="Rectangle" x:Key="MainColor">
+        <Setter Property="Stroke" Value="DarkGray"/>
+        <Setter Property="StrokeThickness" Value="2"/>
+    </Style>
+
+    <Style TargetType="Rectangle" x:Key="CircleSmall">
+        <Setter Property="Fill" Value="#DEDEDE"/>
+        <Setter Property="StrokeThickness" Value="0"/>
+        <Setter Property="Width" Value="30"/>
+        <Setter Property="Height" Value="30"/>
+        <Setter Property="RadiusX" Value="15"/>
+        <Setter Property="RadiusY" Value="15"/>
+    </Style>
+
+    <Style TargetType="Button" x:Key="PlayPauseButton">
+        <Setter Property="Background" Value="#486284"/>
+        <Setter Property="Foreground" Value="#ffffff"/>
+        <Setter Property="FontSize" Value="20"/>
+        <Setter Property="VerticalAlignment"  Value="Center"/>
+        <Setter Property="BorderThickness" Value="0"/>
+        <Setter Property="Width" Value="80"/>
+        <Setter Property="Height" Value="60"/>
+        <Setter Property="Template">
+            <Setter.Value>
+                <ControlTemplate TargetType="Button">
+                    <Border Background="{TemplateBinding Background}" 
+                        CornerRadius="30">
+                        <ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
+                    </Border>
+                </ControlTemplate>
+            </Setter.Value>
+        </Setter>
+        <Style.Triggers>
+            <Trigger Property="IsMouseOver" Value="True">
+                <Setter Property="Background" Value="#394f6b"/>
+            </Trigger>
+        </Style.Triggers>
+    </Style>
+
+    <Style TargetType="TextBlock" x:Key="Heading1">
+        <Setter Property="Foreground" Value="#486284"/>
+        <Setter Property="TextAlignment" Value="Center"/>
+        <Setter Property="FontSize" Value="35"/>
+    </Style>
+
+
 </ResourceDictionary>
\ No newline at end of file
-- 
GitLab