164 lines
4.5 KiB
C#
164 lines
4.5 KiB
C#
using System.Collections.ObjectModel;
|
|
using System.Windows.Input;
|
|
using ytLive.Helpers;
|
|
using ytLive.Models;
|
|
using ytLive.Services;
|
|
|
|
namespace ytLive.ViewModels;
|
|
|
|
public class MainViewModel : ViewModelBase
|
|
{
|
|
private readonly YouTubeAuthService _youtubeAuth;
|
|
private readonly YouTubeStreamService _youtubeStream;
|
|
private readonly YouTubeChatService _youtubeChat;
|
|
|
|
private Scene? _activeScene;
|
|
private StreamStatus _streamStatus = StreamStatus.Offline;
|
|
private StreamHealth _currentHealth = new();
|
|
private string _streamTitle = string.Empty;
|
|
private string _streamKey = string.Empty;
|
|
private bool _isYouTubeConnected;
|
|
|
|
public ObservableCollection<Scene> Scenes { get; } = new();
|
|
public ObservableCollection<ChatMessage> ChatMessages { get; } = new();
|
|
|
|
public Scene? ActiveScene
|
|
{
|
|
get => _activeScene;
|
|
set => SetProperty(ref _activeScene, value);
|
|
}
|
|
|
|
public StreamStatus StreamStatus
|
|
{
|
|
get => _streamStatus;
|
|
set
|
|
{
|
|
if (SetProperty(ref _streamStatus, value))
|
|
{
|
|
OnPropertyChanged(nameof(IsOffline));
|
|
OnPropertyChanged(nameof(IsLive));
|
|
OnPropertyChanged(nameof(PrimaryButtonText));
|
|
}
|
|
}
|
|
}
|
|
|
|
public bool IsOffline => StreamStatus == StreamStatus.Offline;
|
|
public bool IsLive => StreamStatus == StreamStatus.Streaming;
|
|
|
|
public StreamHealth CurrentHealth
|
|
{
|
|
get => _currentHealth;
|
|
set => SetProperty(ref _currentHealth, value);
|
|
}
|
|
|
|
public string StreamTitle
|
|
{
|
|
get => _streamTitle;
|
|
set => SetProperty(ref _streamTitle, value);
|
|
}
|
|
|
|
public string StreamKey
|
|
{
|
|
get => _streamKey;
|
|
set => SetProperty(ref _streamKey, value);
|
|
}
|
|
|
|
public bool IsYouTubeConnected
|
|
{
|
|
get => _isYouTubeConnected;
|
|
set
|
|
{
|
|
if (SetProperty(ref _isYouTubeConnected, value))
|
|
OnPropertyChanged(nameof(PrimaryButtonText));
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// Single, context-aware primary button. Follows the user's journey:
|
|
/// not connected → connect, connected+idle → go live, live → stop.
|
|
/// </summary>
|
|
public string PrimaryButtonText => !IsYouTubeConnected
|
|
? "Connect to YouTube"
|
|
: IsLive
|
|
? "Stop Stream"
|
|
: "Go Live";
|
|
|
|
// Commands
|
|
public ICommand AddSceneCommand { get; }
|
|
public ICommand RemoveSceneCommand { get; }
|
|
public ICommand PrimaryCommand { get; }
|
|
|
|
public MainViewModel()
|
|
{
|
|
_youtubeAuth = new YouTubeAuthService("", ""); // TODO: load from config
|
|
_youtubeStream = new YouTubeStreamService(_youtubeAuth);
|
|
_youtubeChat = new YouTubeChatService(_youtubeAuth);
|
|
|
|
_youtubeChat.MessageReceived += OnChatMessageReceived;
|
|
|
|
AddSceneCommand = new RelayCommand(_ => AddScene());
|
|
RemoveSceneCommand = new RelayCommand(scene => RemoveScene(scene as Scene));
|
|
PrimaryCommand = new RelayCommand(_ => PrimaryAction());
|
|
|
|
// Start with a default scene
|
|
AddScene("Scene 1");
|
|
}
|
|
|
|
private void AddScene(string? name = null)
|
|
{
|
|
var scene = new Scene { Name = name ?? $"Scene {Scenes.Count + 1}" };
|
|
Scenes.Add(scene);
|
|
ActiveScene = scene;
|
|
}
|
|
|
|
private void RemoveScene(Scene? scene)
|
|
{
|
|
if (scene == null) return;
|
|
Scenes.Remove(scene);
|
|
if (ActiveScene == scene)
|
|
ActiveScene = Scenes.FirstOrDefault();
|
|
}
|
|
|
|
private void OnChatMessageReceived(ChatMessage message)
|
|
{
|
|
System.Windows.Application.Current.Dispatcher.Invoke(() =>
|
|
{
|
|
ChatMessages.Add(message);
|
|
if (ChatMessages.Count > 500)
|
|
ChatMessages.RemoveAt(0);
|
|
});
|
|
}
|
|
|
|
private async void StartStream()
|
|
{
|
|
StreamStatus = StreamStatus.Connecting;
|
|
|
|
// TODO: Initialize capture pipeline, encode, and push to RTMP
|
|
// For now, simulate connection
|
|
await Task.Delay(500);
|
|
StreamStatus = StreamStatus.Streaming;
|
|
}
|
|
|
|
private void StopStream()
|
|
{
|
|
StreamStatus = StreamStatus.Offline;
|
|
}
|
|
|
|
private void ConnectYouTube()
|
|
{
|
|
// TODO: Launch OAuth2 flow in browser
|
|
// For now, this is a stub
|
|
IsYouTubeConnected = false;
|
|
}
|
|
|
|
private void PrimaryAction()
|
|
{
|
|
if (!IsYouTubeConnected)
|
|
ConnectYouTube();
|
|
else if (IsLive)
|
|
StopStream();
|
|
else
|
|
StartStream();
|
|
}
|
|
}
|