Update Broadcast button: diff before push, offline=local only, live=API; Go Live empty-field prompt
This commit is contained in:
@@ -21,6 +21,7 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
private readonly Func<LayoutStore> _store;
|
||||
private readonly Func<YouTubeStreamService?> _streamService;
|
||||
private readonly Func<string?> _broadcastId;
|
||||
private readonly Func<bool> _isLive;
|
||||
|
||||
private string _title = string.Empty;
|
||||
private string _description = string.Empty;
|
||||
@@ -31,15 +32,18 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
private bool _isDrawerOpen;
|
||||
private bool _isUpdating;
|
||||
private string? _updateError;
|
||||
private BroadcastMetadata? _lastSyncedSnapshot;
|
||||
|
||||
public LiveBroadcastFormViewModel(
|
||||
Func<LayoutStore> store,
|
||||
Func<YouTubeStreamService?> streamService,
|
||||
Func<string?> broadcastId)
|
||||
Func<string?> broadcastId,
|
||||
Func<bool>? isLive = null)
|
||||
{
|
||||
_store = store;
|
||||
_streamService = streamService;
|
||||
_broadcastId = broadcastId;
|
||||
_isLive = isLive ?? (() => false);
|
||||
UpdateBroadcastCommand = new RelayCommand(_ => _ = UpdateRemoteAsync(), _ => CanUpdateRemote);
|
||||
ToggleDrawerCommand = new RelayCommand(_ => IsDrawerOpen = !IsDrawerOpen);
|
||||
CloseDrawerCommand = new RelayCommand(_ => IsDrawerOpen = false);
|
||||
@@ -92,9 +96,10 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
private set => SetProperty(ref _updateError, value);
|
||||
}
|
||||
|
||||
public bool CanUpdateRemote => !string.IsNullOrEmpty(_broadcastId()) && !IsUpdating;
|
||||
public bool CanUpdateRemote => !IsUpdating;
|
||||
|
||||
/// <summary>Pull everything from the store (startup / Go Live refresh).</summary>
|
||||
/// <summary>Pull everything from the store (startup / Go Live refresh).
|
||||
/// Seeds the synced snapshot so the next Update click knows what is already remote.</summary>
|
||||
public void Load()
|
||||
{
|
||||
var meta = _store().LoadBroadcastMetadata();
|
||||
@@ -110,6 +115,7 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
OnPropertyChanged(nameof(Visibility));
|
||||
OnPropertyChanged(nameof(MadeForKids));
|
||||
OnPropertyChanged(nameof(ScheduledStartText));
|
||||
SnapshotSynced();
|
||||
}
|
||||
|
||||
/// <summary>Called at Go Live with what was actually inserted remotely.</summary>
|
||||
@@ -119,6 +125,7 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
Description = description;
|
||||
ScheduledStartTime = scheduledStart;
|
||||
UpdateError = null;
|
||||
SnapshotSynced();
|
||||
}
|
||||
|
||||
public BroadcastMetadata ToMetadata() => new()
|
||||
@@ -147,13 +154,36 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
{
|
||||
var id = _broadcastId();
|
||||
var service = _streamService();
|
||||
if (id == null || service == null || IsUpdating) return;
|
||||
if (IsUpdating) return;
|
||||
IsUpdating = true;
|
||||
UpdateError = null;
|
||||
try
|
||||
{
|
||||
var error = await service.UpdateBroadcast(id, ToMetadata());
|
||||
var current = ToMetadata();
|
||||
if (!_isLive())
|
||||
{
|
||||
UpdateError = null;
|
||||
IsDrawerOpen = false;
|
||||
return;
|
||||
}
|
||||
if (id == null || service == null)
|
||||
{
|
||||
UpdateError = null;
|
||||
IsDrawerOpen = false;
|
||||
return;
|
||||
}
|
||||
if (_lastSyncedSnapshot != null && current.Equals(_lastSyncedSnapshot))
|
||||
{
|
||||
UpdateError = "No changes to update";
|
||||
return;
|
||||
}
|
||||
var error = await service.UpdateBroadcast(id, current);
|
||||
UpdateError = error;
|
||||
if (error == null)
|
||||
{
|
||||
SnapshotSynced();
|
||||
IsDrawerOpen = false;
|
||||
}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
@@ -165,4 +195,6 @@ public sealed class LiveBroadcastFormViewModel : ViewModelBase
|
||||
IsUpdating = false;
|
||||
}
|
||||
}
|
||||
|
||||
private void SnapshotSynced() => _lastSyncedSnapshot = ToMetadata();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user