Skip to content

Commit

Permalink
Fix #8, Fix #49
Browse files Browse the repository at this point in the history
  • Loading branch information
veselv2010 committed Nov 15, 2019
1 parent aeb7295 commit 345ae60
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 11 deletions.
9 changes: 5 additions & 4 deletions NowPlaying/ConfigWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ namespace NowPlaying
internal class ConfigWriter
{
private string WritePath { get; set; }
private string WriteConfigText { get; set; }

private const string WriteConfigText = "say \"[Spotify] Now Playing: {0}\"";

public ConfigWriter(string writePath)
public ConfigWriter(string writePath, string writeConfigText = "say [Spotify] Now Playing: {0}")
{
Process process = Process.GetProcessesByName("hl2").FirstOrDefault();

Expand All @@ -20,6 +19,8 @@ public ConfigWriter(string writePath)
else
this.WritePath = writePath;

this.WriteConfigText = writeConfigText;

int indexOfAudioCfg = this.WritePath.IndexOf(@"\audio.cfg");
Directory.CreateDirectory(this.WritePath.Remove(indexOfAudioCfg));

Expand All @@ -29,7 +30,7 @@ public ConfigWriter(string writePath)

public void RewriteKeyBinding(CurrentTrackResponse currentTrack)
{
string strForWriting = string.Format(ConfigWriter.WriteConfigText, currentTrack.FullName);
string strForWriting = string.Format(this.WriteConfigText, currentTrack.FullName);

this.RewriteKeyBinding(strForWriting);
}
Expand Down
1 change: 1 addition & 0 deletions NowPlaying/NowPlaying.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@
<Compile Include="GameProcessHook\ProcessExtensions.cs" />
<Compile Include="GameProcessHook\ThreadComponent.cs" />
<Compile Include="GameProcessHook\WinAPI\WinAPIUser32Methods.cs" />
<Compile Include="NowPlayingConfig.cs" />
<Compile Include="UI\AcrylicMaterial.cs" />
<Compile Include="UI\UserControls\CloseButton.xaml.cs">
<DependentUpon>CloseButton.xaml</DependentUpon>
Expand Down
66 changes: 66 additions & 0 deletions NowPlaying/NowPlayingConfig.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.IO;

namespace NowPlaying
{
class NowPlayingConfigWorker
{
private readonly string ConfigName = "NowPlayingConfig.json";

public NowPlayingConfig Config;
public NowPlayingConfigWorker()
{
ReadConfigFile();
if (!Config.CfgText.Contains("{0}"))
{
Config.CfgText = "say [Spotify] Now Playing: {0}";
}
}
private void MakeConfigFile()
{
dynamic Settings = new JObject();
Settings.CfgText = @"say [Spotify] Now Playing: {0}";
Settings.LastUsedKey = "";
Settings.IsAutoSendEnabled = false;
Settings.IsNightModeEnabled = false;
Settings.IsDebugModeEnabled = false;

File.WriteAllText(ConfigName, Settings.ToString());
}

public void SaveConfigFile(NowPlayingConfig SettingsOnExit)
{
dynamic Settings = new JObject();
Settings.CfgText = Config.CfgText;
Settings.IsDebugModeEnabled = Config.IsDebugModeEnabled;
Settings.LastUsedKey = SettingsOnExit.LastUsedKey;
Settings.IsAutoSendEnabled = SettingsOnExit.IsAutoSendEnabled;
Settings.IsNightModeEnabled = SettingsOnExit.IsNightModeEnabled;

File.WriteAllText(ConfigName, Settings.ToString());
}

private void ReadConfigFile()
{
if (File.Exists(ConfigName))
{
Config = JsonConvert.DeserializeObject<NowPlayingConfig>(File.ReadAllText(ConfigName));
}
else
{
MakeConfigFile();
Config = JsonConvert.DeserializeObject<NowPlayingConfig>(File.ReadAllText(ConfigName));
}
}
}

public class NowPlayingConfig
{
public string CfgText { get; set; }
public string LastUsedKey { get; set; }
public bool IsAutoSendEnabled { get; set; }
public bool IsNightModeEnabled { get; set; }
public bool IsDebugModeEnabled { get; set; }
}
}
2 changes: 1 addition & 1 deletion NowPlaying/UI/UserControls/ToggleSwitchNightMode.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public partial class ToggleSwitchNightMode : UserControl

private readonly SolidColorBrush SpotifyGreenBrush = new SolidColorBrush(Color.FromRgb(29, 185, 84));
private readonly SolidColorBrush SpotifyGrayNightBrush = new SolidColorBrush(Color.FromRgb(126, 126, 126));
ThicknessAnimation AnimThickness = new ThicknessAnimation();
private readonly ThicknessAnimation AnimThickness = new ThicknessAnimation();

public bool IsNightModeToggled = false;

Expand Down
39 changes: 33 additions & 6 deletions NowPlaying/UI/Windows/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace NowPlaying.UI.Windows
{
public partial class MainWindow : Window
{
private NowPlayingConfigWorker NowPlayingConfig;
private string PlayingTrackName { get; set; }
private bool IsAutoTrackChangeEnabled { get; set; }
private string CurrentKeyBind { get; set; }
Expand All @@ -29,10 +30,6 @@ public MainWindow()
this.InitializeComponent();

_spotify = new SpotifyRequestsManager(AppInfo.SpotifyClientId, AppInfo.SpotifyClientSecret);

#if DEBUG
DebugCheckBox.Visibility = Visibility.Visible;
#endif
}

private void InitializeTrayMenu()
Expand All @@ -50,6 +47,7 @@ private void InitializeTrayMenu()

private void Window_Loaded(object sender, RoutedEventArgs e)
{
LoadConfigSettings();
this.Hide();

var browserWindow = new BrowserWindow(_spotify);
Expand Down Expand Up @@ -93,7 +91,9 @@ private void ButtonDo_Click(object sender, RoutedEventArgs e)
if (AccountsList.SelectedItem == null)
return;

var cfgWriter = new ConfigWriter($@"{SteamIdLooker.UserdataPath}\{this.GetSelectedAccountId().ToString()}\730\local\cfg\audio.cfg");
var cfgWriter = new ConfigWriter(
$@"{SteamIdLooker.UserdataPath}\{this.GetSelectedAccountId().ToString()}\730\local\cfg\audio.cfg",
this.NowPlayingConfig.Config.CfgText);
cfgWriter.RewriteKeyBinding(trackResp);
}

Expand Down Expand Up @@ -127,7 +127,9 @@ private async void ToggleSwitch_MouseLeftButtonDown(object sender, System.Window
int _SelectedAccount = GetSelectedAccountIndex();
this._cancellationGetSpotifyUpdates = new CancellationTokenSource();

var cfgWriter = new ConfigWriter($@"{SteamIdLooker.UserdataPath}\{this.GetSelectedAccountId().ToString()}\730\local\cfg\audio.cfg");
var cfgWriter = new ConfigWriter(
$@"{SteamIdLooker.UserdataPath}\{this.GetSelectedAccountId().ToString()}\730\local\cfg\audio.cfg",
this.NowPlayingConfig.Config.CfgText);

await Task.Factory.StartNew(() =>
{
Expand Down Expand Up @@ -229,6 +231,12 @@ private void LabelSourceKeysClick(object sender, RoutedEventArgs e)

private void Window_Closing(object sender, System.ComponentModel.CancelEventArgs e)
{
NowPlayingConfig LastSettings = new NowPlayingConfig();
LastSettings.LastUsedKey = TextBoxKeyBind.DefaultTextBox.Text;
LastSettings.IsNightModeEnabled = NightModeSwitch.IsNightModeToggled;
LastSettings.IsAutoSendEnabled = CheckBoxAutoSend.DefaultCheckBox.IsChecked.Value;

NowPlayingConfig.SaveConfigFile(LastSettings);
Program.Dispose();
}

Expand Down Expand Up @@ -397,6 +405,25 @@ private bool SelectionChanged(int _SelectedAccount)
}
return false;
}

private void LoadConfigSettings()
{
NowPlayingConfig = new NowPlayingConfigWorker();

if (NowPlayingConfig.Config.IsNightModeEnabled)
{
NightModeSwitch.Toggle();
ToggleSwitchNightMode_MouseLeftButtonDown(null, null);
}

if (NowPlayingConfig.Config.IsAutoSendEnabled)
CheckBoxAutoSend.DefaultCheckBox.IsChecked = true;

if (NowPlayingConfig.Config.IsDebugModeEnabled)
DebugCheckBox.Visibility = Visibility.Visible;

TextBoxKeyBind.DefaultTextBox.Text = NowPlayingConfig.Config.LastUsedKey;
}
}
}

Expand Down

0 comments on commit 345ae60

Please sign in to comment.