Skip to content

Commit

Permalink
Improve Plex Responsiveness in Desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
da3dsoul committed Sep 26, 2023
1 parent 996c4f0 commit 1f32b51
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 23 deletions.
2 changes: 1 addition & 1 deletion Shoko.Commons
Submodule Shoko.Commons updated 1 files
+1 −1 Shoko.Models
45 changes: 25 additions & 20 deletions Shoko.Desktop/UserControls/Settings/PlexSettings.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,41 +48,46 @@ public PlexSettings()

this.DataContext = _settings = new VM_PlexSettings();
worker = new BackgroundWorker();
worker.DoWork += async (o, e) =>
worker.DoWork += (o, e) =>
{
do Thread.Sleep(TimeSpan.FromSeconds(10));
while (VM_ShokoServer.Instance.CurrentUser == null);
await RefreshAsync();
RefreshAsync();
};
worker.RunWorkerAsync();

btnRefresh.Click += RefreshClick;
}

private async void RefreshClick(object sender, RoutedEventArgs e) => await RefreshAsync();
private async void RefreshClick(object sender, RoutedEventArgs e) => await Task.Run(RefreshAsync);

private async Task RefreshAsync()
private void RefreshAsync()
{
await Task.Run(() =>
_settings.UpdateServers();
var currentServer = VM_ShokoServer.Instance.ShokoPlex.CurrentDevice(VM_ShokoServer.Instance.CurrentUser.JMMUserID);
if (currentServer == null) return;

var index = -1;
for (var i = 0; i < _settings.PlexDevices.Count; i++)
{
Application.Current.Dispatcher.Invoke(() => _settings.UpdateServers());
var currentServer = VM_ShokoServer.Instance.ShokoPlex.CurrentDevice(VM_ShokoServer.Instance.CurrentUser.JMMUserID);
if (currentServer == null) return;
if (_settings.PlexDevices[i].ClientIdentifier == currentServer.ClientIdentifier)
index = i;
}

Application.Current.Dispatcher.Invoke(() =>
{
for (var i = 0; i < _settings.PlexDevices.Count; i++)
{
if (_settings.PlexDevices[i].ClientIdentifier == currentServer.ClientIdentifier)
cbServer.SelectedIndex = i;
}
if (index != -1)
Application.Current.Dispatcher.Invoke(() => cbServer.SelectedIndex = index);

var toSelect = lstPlexIDs.Items.Cast<Directory>()
.Where(itm => VM_ShokoServer.Instance.Plex_Sections.Contains(itm.Key)).ToList();

lstPlexIDs.SelectedItems.Clear();
foreach (var itm in lstPlexIDs.Items)
if (VM_ShokoServer.Instance.Plex_Sections.Contains(((Directory)itm).Key))
lstPlexIDs.SelectedItems.Add(itm);
});
Application.Current.Dispatcher.Invoke(() =>
{
lstPlexIDs.SelectedItems.Clear();
foreach (var directory in toSelect)
{
lstPlexIDs.SelectedItems.Add(directory);
}
});
}
}
Expand Down
11 changes: 9 additions & 2 deletions Shoko.Desktop/ViewModel/VM_PlexSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Shoko.Commons.Notification;
using Shoko.Desktop.Utilities;
using Shoko.Desktop.ViewModel.Server;
Expand Down Expand Up @@ -55,7 +56,13 @@ public bool IsAuthenticated
public ObservableCollectionEx<MediaDevice> PlexDevices
{
get => _plexDevices;
set { _plexDevices = value; NotifyPropertyChanged(); NotifyPropertyChanged(nameof(PlexDirectories)); UpdateDirectories(); }
set
{
_plexDevices = value;
NotifyPropertyChanged();
NotifyPropertyChanged(nameof(PlexDirectories));
UpdateDirectories();
}
}

public void UpdateDirectories()
Expand All @@ -69,7 +76,7 @@ public void UpdateServers()
{
var devices = VM_ShokoServer.Instance.ShokoPlex.AvailableDevices(VM_ShokoServer.Instance.CurrentUser.JMMUserID);

_plexDevices.ReplaceRange(devices);
Application.Current.Dispatcher.Invoke(() => _plexDevices.ReplaceRange(devices));
NotifyPropertyChanged(nameof(PlexDirectories));
}

Expand Down

0 comments on commit 1f32b51

Please sign in to comment.