I think I broke it
This commit is contained in:
+84
-48
@@ -3,18 +3,25 @@ namespace PainoBar_Helper
|
||||
public partial class StationForm : Form
|
||||
{
|
||||
private readonly PianobarManager _pianobarManager;
|
||||
private List<string> _stations;
|
||||
private List<string> _filteredStations;
|
||||
private List<StationItem> _stations;
|
||||
private List<StationItem> _filteredStations;
|
||||
private bool _stationsLoaded = false;
|
||||
private bool _stationEntriesStarted = false;
|
||||
|
||||
private sealed class StationItem
|
||||
{
|
||||
public int Index { get; set; }
|
||||
public string Name { get; set; } = string.Empty;
|
||||
public override string ToString() => Name;
|
||||
}
|
||||
|
||||
public StationForm(PianobarManager pianobarManager)
|
||||
{
|
||||
InitializeComponent();
|
||||
|
||||
_pianobarManager = pianobarManager;
|
||||
_stations = new List<string>();
|
||||
_filteredStations = new List<string>();
|
||||
_stations = new List<StationItem>();
|
||||
_filteredStations = new List<StationItem>();
|
||||
|
||||
InitializeControls();
|
||||
}
|
||||
@@ -30,6 +37,11 @@ namespace PainoBar_Helper
|
||||
{
|
||||
lstStations.DoubleClick += LstStations_DoubleClick;
|
||||
}
|
||||
|
||||
if (refresh_button != null)
|
||||
{
|
||||
refresh_button.Click += refresh_button_Click;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnShown(EventArgs e)
|
||||
@@ -40,32 +52,58 @@ namespace PainoBar_Helper
|
||||
LoadStations();
|
||||
}
|
||||
|
||||
private void LoadStations()
|
||||
private void LoadStations(bool forceRefresh = false)
|
||||
{
|
||||
_stations.Clear();
|
||||
_filteredStations.Clear();
|
||||
_stationsLoaded = false;
|
||||
_stationEntriesStarted = false;
|
||||
|
||||
// Subscribe to output to capture station list
|
||||
if (forceRefresh)
|
||||
_stations.Clear();
|
||||
|
||||
var knownStations = _pianobarManager.GetKnownStationEntries();
|
||||
if (knownStations.Count > 0)
|
||||
{
|
||||
_stations = knownStations
|
||||
.Select(s => new StationItem { Index = s.Index, Name = s.Name })
|
||||
.OrderBy(s => s.Index)
|
||||
.ToList();
|
||||
|
||||
_stationEntriesStarted = true;
|
||||
if (lstStations != null)
|
||||
UpdateStationList();
|
||||
}
|
||||
|
||||
_pianobarManager.OutputReceived -= PianobarManager_OutputReceived;
|
||||
_pianobarManager.OutputReceived += PianobarManager_OutputReceived;
|
||||
|
||||
// Send command to show stations
|
||||
try
|
||||
// Only request station list on explicit refresh.
|
||||
if (forceRefresh)
|
||||
{
|
||||
_pianobarManager.ShowStations();
|
||||
|
||||
// Show loading message
|
||||
if (lstStations != null)
|
||||
try
|
||||
{
|
||||
lstStations.Items.Clear();
|
||||
lstStations.Items.Add("Loading stations...");
|
||||
_pianobarManager.ShowStations();
|
||||
|
||||
if (lstStations != null && _stations.Count == 0)
|
||||
{
|
||||
lstStations.Items.Clear();
|
||||
lstStations.Items.Add("Loading stations...");
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
MessageBox.Show($"Failed to load stations:\n{ex.Message}", "Error",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
else
|
||||
{
|
||||
MessageBox.Show($"Failed to load stations:\n{ex.Message}", "Error",
|
||||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||||
_stationsLoaded = true;
|
||||
if (_stations.Count == 0 && lstStations != null)
|
||||
{
|
||||
lstStations.Items.Clear();
|
||||
lstStations.Items.Add("No cached stations. Click Refresh stations.");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -79,16 +117,24 @@ namespace PainoBar_Helper
|
||||
if (string.IsNullOrEmpty(line))
|
||||
return;
|
||||
|
||||
// Parse station list from Pianobar output
|
||||
// Handles formats like: "0) Station", "*0) Station", " 12) Station"
|
||||
var match = System.Text.RegularExpressions.Regex.Match(line, @"^\s*\*?\s*(\d+)\)\s*(.+)$");
|
||||
var match = System.Text.RegularExpressions.Regex.Match(line, @"^\s*(\d+)\)\s*(?:[qQ]\s+)?(.+?)\s*$");
|
||||
if (match.Success)
|
||||
{
|
||||
_stationEntriesStarted = true;
|
||||
int stationIndex = int.Parse(match.Groups[1].Value);
|
||||
string stationName = match.Groups[2].Value.Trim();
|
||||
if (!_stations.Contains(stationName))
|
||||
|
||||
var existing = _stations.FirstOrDefault(s => s.Index == stationIndex);
|
||||
if (existing == null)
|
||||
{
|
||||
_stations.Add(stationName);
|
||||
_stations.Add(new StationItem { Index = stationIndex, Name = stationName });
|
||||
_stations = _stations.OrderBy(s => s.Index).ToList();
|
||||
if (!IsDisposed && IsHandleCreated)
|
||||
BeginInvoke(() => UpdateStationList());
|
||||
}
|
||||
else if (!string.Equals(existing.Name, stationName, StringComparison.Ordinal))
|
||||
{
|
||||
existing.Name = stationName;
|
||||
if (!IsDisposed && IsHandleCreated)
|
||||
BeginInvoke(() => UpdateStationList());
|
||||
}
|
||||
@@ -96,7 +142,8 @@ namespace PainoBar_Helper
|
||||
}
|
||||
|
||||
// Detect end of station list only after we started receiving entries
|
||||
if (_stationEntriesStarted && line.Contains("Select station:", StringComparison.OrdinalIgnoreCase))
|
||||
if (_stationEntriesStarted &&
|
||||
(line.Contains("Select station:", StringComparison.OrdinalIgnoreCase) || line == "[?]"))
|
||||
{
|
||||
_stationsLoaded = true;
|
||||
_pianobarManager.OutputReceived -= PianobarManager_OutputReceived;
|
||||
@@ -154,7 +201,7 @@ namespace PainoBar_Helper
|
||||
else
|
||||
{
|
||||
_filteredStations = _stations
|
||||
.Where(s => s.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
.Where(s => s.Name.IndexOf(searchText, StringComparison.OrdinalIgnoreCase) >= 0)
|
||||
.ToList();
|
||||
}
|
||||
|
||||
@@ -166,17 +213,17 @@ namespace PainoBar_Helper
|
||||
#region Station Selection
|
||||
|
||||
// Wire this to btnSelectStation.Click in the Designer
|
||||
public void btnSelectStation_Click(object? sender, EventArgs e)
|
||||
public async void btnSelectStation_Click(object? sender, EventArgs e)
|
||||
{
|
||||
SelectCurrentStation();
|
||||
await SelectCurrentStationAsync();
|
||||
}
|
||||
|
||||
private void LstStations_DoubleClick(object? sender, EventArgs e)
|
||||
private async void LstStations_DoubleClick(object? sender, EventArgs e)
|
||||
{
|
||||
SelectCurrentStation();
|
||||
await SelectCurrentStationAsync();
|
||||
}
|
||||
|
||||
private void SelectCurrentStation()
|
||||
private async Task SelectCurrentStationAsync()
|
||||
{
|
||||
if (lstStations == null)
|
||||
return;
|
||||
@@ -188,21 +235,12 @@ namespace PainoBar_Helper
|
||||
return;
|
||||
}
|
||||
|
||||
string selectedStation = lstStations.SelectedItem?.ToString() ?? string.Empty;
|
||||
if (string.IsNullOrEmpty(selectedStation))
|
||||
return;
|
||||
|
||||
// Find the index in the original station list
|
||||
int stationIndex = _stations.IndexOf(selectedStation);
|
||||
if (stationIndex < 0)
|
||||
if (lstStations.SelectedItem is not StationItem selectedStation)
|
||||
return;
|
||||
|
||||
try
|
||||
{
|
||||
// Send the station index to Pianobar
|
||||
// Pianobar expects the station number (0-based index)
|
||||
_pianobarManager.SendCommand(stationIndex.ToString());
|
||||
|
||||
await _pianobarManager.SelectStationAsync(selectedStation.Index);
|
||||
DialogResult = DialogResult.OK;
|
||||
Close();
|
||||
}
|
||||
@@ -213,6 +251,11 @@ namespace PainoBar_Helper
|
||||
}
|
||||
}
|
||||
|
||||
public void refresh_button_Click(object? sender, EventArgs e)
|
||||
{
|
||||
LoadStations(forceRefresh: true);
|
||||
}
|
||||
|
||||
#endregion
|
||||
|
||||
#region Cancel
|
||||
@@ -220,13 +263,6 @@ namespace PainoBar_Helper
|
||||
// Wire this to btnCancelStation.Click in the Designer (or Cancel button)
|
||||
public void btnCancelStation_Click(object? sender, EventArgs e)
|
||||
{
|
||||
// Send 'q' to cancel station selection in Pianobar
|
||||
try
|
||||
{
|
||||
_pianobarManager.SendCommand('q');
|
||||
}
|
||||
catch { }
|
||||
|
||||
DialogResult = DialogResult.Cancel;
|
||||
Close();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user