Skip to content

Commit

Permalink
chore: optimization for memory saving
Browse files Browse the repository at this point in the history
  • Loading branch information
eymenefealtun committed Sep 6, 2023
1 parent 584d456 commit 9d534be
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 93 deletions.
4 changes: 2 additions & 2 deletions TarotType.Main/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@
WindowStartupLocation="CenterScreen"
Background="#eeeee4">

<Grid x:Name="mainGrid"
Loaded="mainGrid_Loaded">
<Grid x:Name="mainGrid">

<Grid>
<StackPanel Orientation="Horizontal"
Expand Down Expand Up @@ -107,6 +106,7 @@
Width="300"
VerticalContentAlignment="Center"
FontSize="25"
Padding="5,0,0,0"
TextChanged="tboxWrite_TextChanged"
BorderBrush="Transparent"
BorderThickness="0"
Expand Down
114 changes: 59 additions & 55 deletions TarotType.Main/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Threading;
using TarotType.Main.Properties;
using TarotType.Main.Settings;
using TarotType.Main.Utilities;
using TarotType.Main.Utilities.Words.EnglishFolder;
using TarotType.Main.View;
using System.Resources;

namespace TarotType.Main
{
Expand All @@ -26,7 +28,23 @@ public partial class MainWindow : Window
int _numberOfFalseKeyStroke;

int _currentWord1Index = 0;

int _second = 60;
public int Second
{
get
{
return this._second;
}
set
{
_second = value;
lblTimer.Content = _second.ToString();
}
}


int _numberOfWordsInEachCall = 20;

string _targetText;
string _currentTextOfTextBox;
Expand All @@ -35,8 +53,13 @@ public partial class MainWindow : Window
bool _isStartedBefore;
bool _canComboBoxChangedFired = false;
bool _canSettignsChange = false;
public bool IsRefreshing = false;

string[] _sourceWords;

public static string[] _sourceWords;
public static string[] _resultWordArray;
public static Random _random;
public static ResourceManager _resources;

string _lightThemeCode = "#eeeee4";
string _darkThemeCode = "#1e1e1e";
Expand All @@ -45,6 +68,9 @@ public MainWindow()
{
InitializeComponent();

_random = new Random();
_resources = new ResourceManager(typeof(Resources));

_words1 = new List<Label>();
_words2 = new List<Label>();

Expand All @@ -54,14 +80,18 @@ public MainWindow()
_dispatcherTimer.Interval = new TimeSpan(0, 0, 1);


//_second = 60;

SourceManager.CurrentLanguage = new English();
Preferences.CurrentTheme = _lightThemeCode;
Preferences.LanguageName = "English";
cBoxLanguages.SelectedValue = "English";

//Preferences.GetPreferences(btnTheme, this, cBoxLanguages);
string initialLanguageName = nameof(English);

Preferences.LanguageName = initialLanguageName;
cBoxLanguages.SelectedValue = initialLanguageName;

_sourceWords = SourceManager.GetLanguageArray(SourceManager.CurrentLanguage);
//_sourceWords = SourceManager.GetLanguageArray();
_resultWordArray = new string[_numberOfWordsInEachCall];

if (SourceManager.CurrentLanguage.FlowDirection() == SourceManager.flowDirections.right)
{
Expand All @@ -73,7 +103,6 @@ public MainWindow()
RefreshGame();
}

public bool IsRefreshing = false;
private void btnRefresh_Click(object sender, RoutedEventArgs e)
{
IsRefreshing = true;
Expand All @@ -82,11 +111,9 @@ private void btnRefresh_Click(object sender, RoutedEventArgs e)

private void dispatcherTimer_Tick(object sender, EventArgs e)
{
lblTimer.Content = _second.ToString();
_second--;
lblTimer.Content = _second.ToString();
Second--;

if (_second == 0)
if (Second == 0)
{
tboxWrite.IsEnabled = false;
ResultWindow resultWindow = new ResultWindow(_numberOfTrueWords, _numberOfWrongWords, _numberOfTrueWords, _numberOfKeyStroke, _numberOfTrueKeyStroke, _numberOfFalseKeyStroke);
Expand All @@ -95,15 +122,13 @@ private void dispatcherTimer_Tick(object sender, EventArgs e)

RefreshGame();
}

}

private void tboxWrite_TextChanged(object sender, TextChangedEventArgs e)
{
_currentTextOfTextBox = tboxWrite.Text;


if (!_isStartedBefore && tboxWrite.Text != " " && tboxWrite.Text != "")
if (!_isStartedBefore && !string.IsNullOrWhiteSpace(tboxWrite.Text))
{
_isStartedBefore = true;
_dispatcherTimer.Start();
Expand All @@ -123,12 +148,12 @@ private void tboxWrite_TextChanged(object sender, TextChangedEventArgs e)
string currentTargetText = _targetText.Substring(0, currentLegth);

if (currentTargetText != _currentTextOfTextBox)
CurrentTextWrong(_words1[_currentWord1Index]);
CurrentTextFalse(_words1[_currentWord1Index]);
else
CurrentTextTrue(_words1[_currentWord1Index]);
CurrentTextRight(_words1[_currentWord1Index]);
}
else
CurrentTextWrong(_words1[_currentWord1Index]);
CurrentTextFalse(_words1[_currentWord1Index]);

}

Expand All @@ -155,15 +180,15 @@ private void tboxWrite_PreviewKeyDown(object sender, KeyEventArgs e) //when text
_isTextBoxChangedCanFire = false;

if (_currentTextOfTextBox == _targetText)
TextDoneTrue(_words1[_currentWord1Index], _words1[_currentWord1Index + 1]);
MarkedAsCorrect(_words1[_currentWord1Index], _words1[_currentWord1Index + 1]);
else
TextDoneWrong(_words1[_currentWord1Index], _words1[_currentWord1Index + 1]);
MarkedAsIncorrect(_words1[_currentWord1Index], _words1[_currentWord1Index + 1]);
}
else
_isTextBoxChangedCanFire = true;
}

private void TextDoneTrue(Label lbl, Label nextLabel)
private void MarkedAsCorrect(Label lbl, Label nextLabel)
{
_numberOfTrueWords++;

Expand All @@ -174,7 +199,7 @@ private void TextDoneTrue(Label lbl, Label nextLabel)
_currentWord1Index++;
}

private void TextDoneWrong(Label lbl, Label nextLabel)
private void MarkedAsIncorrect(Label lbl, Label nextLabel)
{
_numberOfWrongWords++;

Expand All @@ -185,17 +210,17 @@ private void TextDoneWrong(Label lbl, Label nextLabel)
_currentWord1Index++;
}

private void CurrentTextWrong(Label lbl)
private void CurrentTextFalse(Label lbl)
{
lbl.Background = Brushes.Red;
_numberOfFalseKeyStroke++;
}

private void CurrentTextTrue(Label lbl)
private void CurrentTextRight(Label lbl)
{
lbl.Background = Brushes.LightGray;
_numberOfTrueKeyStroke++;
}
}

private void GetAnotherStack(List<Label> words2)
{
Expand Down Expand Up @@ -243,22 +268,21 @@ private void RefreshGame()

}


private void RefreshStack(StackPanel panel, List<Label> labels)
{
panel.Children.Clear();
labels.Clear();

string[] wordArray = WordManager.GetRandomWord(20, _sourceWords);
_resultWordArray = WordManager.GetRandomWord(_numberOfWordsInEachCall);

int currentLength = 0;

for (int i = 0; i < wordArray.Length; i++)
for (int i = 0; i < _resultWordArray.Length; i++)
{

Label lbl = new Label();
lbl.Background = i == 0 && labels == _words1 ? Brushes.LightGray : Brushes.Transparent; //First word set to Light Gray
lbl.Content = wordArray[i];
lbl.Content = _resultWordArray[i];
lbl.Style = (Style)FindResource("MainTextBlockTheme");
//Calculations in order not to break the size limit of stack panel.

Expand Down Expand Up @@ -287,31 +311,19 @@ private void btnTheme_Click(object sender, RoutedEventArgs e)

private void cBoxLanguages_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
//if (_canComboBoxChangedFired == false)
//{
// _canComboBoxChangedFired = true;
// return;
//}


SettingsChanged(Preferences.CurrentTheme, cBoxLanguages.SelectedValue.ToString());

Mouse.OverrideCursor = Cursors.Wait;

_sourceWords = SourceManager.GetLanguageArray(SourceManager.CurrentLanguage);

if (SourceManager.CurrentLanguage.FlowDirection() == SourceManager.flowDirections.right)
{
tboxWrite.FlowDirection = FlowDirection.RightToLeft;
stckPanel1.FlowDirection = FlowDirection.RightToLeft;
stckPanel1.FlowDirection = FlowDirection.RightToLeft;
}
else
{
tboxWrite.FlowDirection = FlowDirection.LeftToRight;
stckPanel1.FlowDirection = FlowDirection.LeftToRight;
stckPanel1.FlowDirection = FlowDirection.LeftToRight;
}

FlowDirection flowDirection = SourceManager.CurrentLanguage.FlowDirection() == SourceManager.flowDirections.right ? FlowDirection.RightToLeft : FlowDirection.LeftToRight;

tboxWrite.FlowDirection = flowDirection;
stckPanel1.FlowDirection = flowDirection;
stckPanel1.FlowDirection = flowDirection;

RefreshGame();

Expand All @@ -322,7 +334,7 @@ private void SettingsChanged(string themeCode, string languageName)
{
if (_canSettignsChange == false)
{
_canSettignsChange = true;
_canSettignsChange = true;
return;
}

Expand All @@ -331,16 +343,8 @@ private void SettingsChanged(string themeCode, string languageName)
SourceManager.CurrentLanguage = SourceManager._languageDictionary.FirstOrDefault(x => x.Value.ToString() == Preferences.LanguageName).Key;
this.Background = (SolidColorBrush)new BrushConverter().ConvertFrom(Preferences.CurrentTheme);


//this.Background = (SolidColorBrush)new BrushConverter().ConvertFrom(Preferences.CurrentTheme);
//Preferences.SetPreferences();
//Preferences.GetPreferences(btnTheme, this, cBoxLanguages);
}

private void mainGrid_Loaded(object sender, RoutedEventArgs e)
{

}

}

}
4 changes: 2 additions & 2 deletions TarotType.Main/TarotType.Main.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
<UseWPF>true</UseWPF>


<PlatformTarget>x64</PlatformTarget>
<!--<PlatformTarget>x64</PlatformTarget>
<RuntimeIdentifier>win-x64</RuntimeIdentifier>
<PublishSingleFile>true</PublishSingleFile>
<SelfContained>true</SelfContained>
<IncludeNativeLibrariesForSelfExtract>true</IncludeNativeLibrariesForSelfExtract>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>
<EnableCompressionInSingleFile>true</EnableCompressionInSingleFile>-->
</PropertyGroup>

<ItemGroup>
Expand Down
24 changes: 4 additions & 20 deletions TarotType.Main/Utilities/SourceManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Reflection;
using System.Collections.Generic;
using System.IO;
using System.Collections.Generic;
using TarotType.Main.Utilities.Words;
using TarotType.Main.Utilities.Words.Azerbaijani;
using TarotType.Main.Utilities.Words.Turkish;
Expand All @@ -12,8 +10,6 @@
using TarotType.Main.Utilities.Words.Armenian;
using TarotType.Main.Utilities.Words.Greek;
using TarotType.Main.Utilities.Words.EnglishFolder;
using TarotType.Main.Properties;
using System.Resources;
using TarotType.Main.Settings;

namespace TarotType.Main.Utilities
Expand All @@ -24,7 +20,6 @@ public static class SourceManager
public static Language? CurrentLanguage { get; set; }

public static Dictionary<Language, languages> _languageDictionary = new Dictionary<Language, languages>()
//public static Dictionary<Language, string> _languageDictionary = new Dictionary<Language, string>()
{
{ new Arabic(), languages.Arabic},
{ new Armenian(), languages.Armenian},
Expand All @@ -36,18 +31,8 @@ public static class SourceManager
{ new Persian(), languages.Persian},
{ new Spanish(), languages.Spanish},
{ new Turkish(), languages.Turkish},
// { new Arabic(), Resources.Arabic},
//{ new Armenian(), Resources.Armenian},
//{ new Azerbaijani(), Resources.Azerbaijani},
//{ new English(), Resources.English},
//{ new French(), Resources.French},
//{ new Greek(), Resources.Greek},
//{ new Kurdish(), Resources.Kurdish},
//{ new Persian(), Resources.Persian},
//{ new Spanish(), Resources.Spanish},
//{ new Turkish(), Resources.Turkish},

};

public enum languages
{
Arabic,
Expand All @@ -61,6 +46,7 @@ public enum languages
Spanish,
Turkish
}

public enum flowDirections
{
left,
Expand All @@ -69,9 +55,7 @@ public enum flowDirections

public static string[] GetLanguageArray(Language language)
{
ResourceManager resources = new ResourceManager(typeof(Resources));

return resources.GetString(Preferences.LanguageName).Split(',');
return MainWindow._resources.GetString(Preferences.LanguageName).Split(',');
}

}
Expand Down
Loading

0 comments on commit 9d534be

Please sign in to comment.