Added a display preferences manager

This commit is contained in:
Luke Pulverenti 2013-04-05 15:34:33 -04:00
parent 701c0887ad
commit 31c2d98532
11 changed files with 175 additions and 114 deletions

View File

@ -32,7 +32,7 @@ namespace MediaBrowser.Api
/// Gets or sets the id.
/// </summary>
/// <value>The id.</value>
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "POST")]
[ApiMember(Name = "Id", Description = "Item Id", IsRequired = true, DataType = "string", ParameterType = "path", Verb = "GET")]
public Guid Id { get; set; }
}
@ -42,9 +42,9 @@ namespace MediaBrowser.Api
public class DisplayPreferencesService : BaseApiService
{
/// <summary>
/// The _user manager
/// The _display preferences manager
/// </summary>
private readonly IUserManager _userManager;
private readonly IDisplayPreferencesManager _displayPreferencesManager;
/// <summary>
/// The _json serializer
/// </summary>
@ -53,12 +53,12 @@ namespace MediaBrowser.Api
/// <summary>
/// Initializes a new instance of the <see cref="DisplayPreferencesService" /> class.
/// </summary>
/// <param name="userManager">The user manager.</param>
/// <param name="jsonSerializer">The json serializer.</param>
public DisplayPreferencesService(IUserManager userManager, IJsonSerializer jsonSerializer)
/// <param name="displayPreferencesManager">The display preferences manager.</param>
public DisplayPreferencesService(IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager)
{
_userManager = userManager;
_jsonSerializer = jsonSerializer;
_displayPreferencesManager = displayPreferencesManager;
}
/// <summary>
@ -67,7 +67,7 @@ namespace MediaBrowser.Api
/// <param name="request">The request.</param>
public object Get(GetDisplayPreferences request)
{
var task = _userManager.GetDisplayPreferences(request.Id);
var task = _displayPreferencesManager.GetDisplayPreferences(request.Id);
return ToOptimizedResult(task.Result);
}
@ -86,7 +86,7 @@ namespace MediaBrowser.Api
// Serialize to json and then back so that the core doesn't see the request dto type
var displayPreferences = _jsonSerializer.DeserializeFromString<DisplayPreferences>(_jsonSerializer.SerializeToString(request));
var task = _userManager.SaveDisplayPreferences(displayPreferences, CancellationToken.None);
var task = _displayPreferencesManager.SaveDisplayPreferences(displayPreferences, CancellationToken.None);
Task.WaitAll(task);
}

View File

@ -0,0 +1,28 @@
using MediaBrowser.Model.Entities;
using System;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Controller.Library
{
/// <summary>
/// Interface IDisplayPreferencesManager
/// </summary>
public interface IDisplayPreferencesManager
{
/// <summary>
/// Gets the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId);
/// <summary>
/// Saves display preferences for an item
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken);
}
}

View File

@ -1,7 +1,6 @@
using MediaBrowser.Common.Events;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Entities;
using System;
using System.Collections.Generic;
using System.Threading;
@ -9,6 +8,9 @@ using System.Threading.Tasks;
namespace MediaBrowser.Controller.Library
{
/// <summary>
/// Interface IUserManager
/// </summary>
public interface IUserManager
{
/// <summary>
@ -173,7 +175,7 @@ namespace MediaBrowser.Controller.Library
Task ChangePassword(User user, string newPassword);
/// <summary>
/// Saves display preferences for an item
/// Saves the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="userDataId">The user data id.</param>
@ -184,26 +186,11 @@ namespace MediaBrowser.Controller.Library
CancellationToken cancellationToken);
/// <summary>
/// Gets the display preferences.
/// Gets the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="userDataId">The user data id.</param>
/// <returns>Task{DisplayPreferences}.</returns>
/// <returns>Task{UserItemData}.</returns>
Task<UserItemData> GetUserData(Guid userId, Guid userDataId);
/// <summary>
/// Gets the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId);
/// <summary>
/// Saves display preferences for an item
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken);
}
}

View File

@ -88,6 +88,7 @@
<Compile Include="Entities\Movies\BoxSet.cs" />
<Compile Include="Entities\Movies\Movie.cs" />
<Compile Include="Entities\Person.cs" />
<Compile Include="Library\IDisplayPreferencesManager.cs" />
<Compile Include="Library\ILibrarySearchEngine.cs" />
<Compile Include="Library\PlaybackProgressEventArgs.cs" />
<Compile Include="Entities\Studio.cs" />

View File

@ -0,0 +1,93 @@
using MediaBrowser.Controller;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
using System.Threading;
using System.Threading.Tasks;
namespace MediaBrowser.Server.Implementations.Library
{
/// <summary>
/// Class DisplayPreferencesManager
/// </summary>
public class DisplayPreferencesManager : IDisplayPreferencesManager
{
/// <summary>
/// The _logger
/// </summary>
private readonly ILogger _logger;
/// <summary>
/// The _display preferences
/// </summary>
private readonly ConcurrentDictionary<Guid, Task<DisplayPreferences>> _displayPreferences = new ConcurrentDictionary<Guid, Task<DisplayPreferences>>();
/// <summary>
/// Initializes a new instance of the <see cref="DisplayPreferencesManager"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public DisplayPreferencesManager(ILogger logger)
{
_logger = logger;
}
/// <summary>
/// Gets the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
public Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId)
{
return _displayPreferences.GetOrAdd(displayPreferencesId, keyName => RetrieveDisplayPreferences(displayPreferencesId));
}
/// <summary>
/// Retrieves the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
private async Task<DisplayPreferences> RetrieveDisplayPreferences(Guid displayPreferencesId)
{
var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(displayPreferencesId).ConfigureAwait(false);
return displayPreferences ?? new DisplayPreferences { Id = displayPreferencesId };
}
/// <summary>
/// Saves display preferences for an item
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken)
{
if (displayPreferences == null)
{
throw new ArgumentNullException("displayPreferences");
}
if (displayPreferences.Id == Guid.Empty)
{
throw new ArgumentNullException("displayPreferences.Id");
}
try
{
await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(displayPreferences,
cancellationToken).ConfigureAwait(false);
var newValue = Task.FromResult(displayPreferences);
// Once it succeeds, put it into the dictionary to make it available to everyone else
_displayPreferences.AddOrUpdate(displayPreferences.Id, newValue, delegate { return newValue; });
}
catch (Exception ex)
{
_logger.ErrorException("Error saving display preferences", ex);
throw;
}
}
}
}

View File

@ -5,7 +5,6 @@ using MediaBrowser.Controller.Configuration;
using MediaBrowser.Controller.Entities;
using MediaBrowser.Controller.Library;
using MediaBrowser.Model.Connectivity;
using MediaBrowser.Model.Entities;
using MediaBrowser.Model.Logging;
using System;
using System.Collections.Concurrent;
@ -99,11 +98,6 @@ namespace MediaBrowser.Server.Implementations.Library
/// <value>The configuration manager.</value>
private IServerConfigurationManager ConfigurationManager { get; set; }
/// <summary>
/// The _user data
/// </summary>
private readonly ConcurrentDictionary<Guid, Task<DisplayPreferences>> _displayPreferences = new ConcurrentDictionary<Guid, Task<DisplayPreferences>>();
private readonly ConcurrentDictionary<string, Task<UserItemData>> _userData = new ConcurrentDictionary<string, Task<UserItemData>>();
/// <summary>
@ -165,63 +159,6 @@ namespace MediaBrowser.Server.Implementations.Library
}
#endregion
/// <summary>
/// Gets the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
public Task<DisplayPreferences> GetDisplayPreferences(Guid displayPreferencesId)
{
return _displayPreferences.GetOrAdd(displayPreferencesId, keyName => RetrieveDisplayPreferences(displayPreferencesId));
}
/// <summary>
/// Retrieves the display preferences.
/// </summary>
/// <param name="displayPreferencesId">The display preferences id.</param>
/// <returns>DisplayPreferences.</returns>
private async Task<DisplayPreferences> RetrieveDisplayPreferences(Guid displayPreferencesId)
{
var displayPreferences = await Kernel.Instance.DisplayPreferencesRepository.GetDisplayPreferences(displayPreferencesId).ConfigureAwait(false);
return displayPreferences ?? new DisplayPreferences { Id = displayPreferencesId };
}
/// <summary>
/// Saves display preferences for an item
/// </summary>
/// <param name="displayPreferences">The display preferences.</param>
/// <param name="cancellationToken">The cancellation token.</param>
/// <returns>Task.</returns>
public async Task SaveDisplayPreferences(DisplayPreferences displayPreferences, CancellationToken cancellationToken)
{
if (displayPreferences == null)
{
throw new ArgumentNullException("displayPreferences");
}
if (displayPreferences.Id == Guid.Empty)
{
throw new ArgumentNullException("displayPreferences.Id");
}
try
{
await Kernel.Instance.DisplayPreferencesRepository.SaveDisplayPreferences(displayPreferences,
cancellationToken).ConfigureAwait(false);
var newValue = Task.FromResult(displayPreferences);
// Once it succeeds, put it into the dictionary to make it available to everyone else
_displayPreferences.AddOrUpdate(displayPreferences.Id, newValue, delegate { return newValue; });
}
catch (Exception ex)
{
_logger.ErrorException("Error saving display preferences", ex);
throw;
}
}
/// <summary>
/// Gets a User by Id
/// </summary>
@ -802,11 +739,11 @@ namespace MediaBrowser.Server.Implementations.Library
}
/// <summary>
/// Gets the display preferences.
/// Gets the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="userDataId">The user data id.</param>
/// <returns>Task{DisplayPreferences}.</returns>
/// <returns>Task{UserItemData}.</returns>
public Task<UserItemData> GetUserData(Guid userId, Guid userDataId)
{
var key = userId + userDataId.ToString();
@ -815,11 +752,11 @@ namespace MediaBrowser.Server.Implementations.Library
}
/// <summary>
/// Retrieves the display preferences.
/// Retrieves the user data.
/// </summary>
/// <param name="userId">The user id.</param>
/// <param name="userDataId">The user data id.</param>
/// <returns>DisplayPreferences.</returns>
/// <returns>Task{UserItemData}.</returns>
private async Task<UserItemData> RetrieveUserData(Guid userId, Guid userDataId)
{
var userdata = await Kernel.Instance.UserDataRepository.GetUserData(userId, userDataId).ConfigureAwait(false);

View File

@ -130,6 +130,7 @@
<Compile Include="HttpServer\SwaggerService.cs" />
<Compile Include="IO\DirectoryWatchers.cs" />
<Compile Include="Library\CoreResolutionIgnoreRule.cs" />
<Compile Include="Library\DisplayPreferencesManager.cs" />
<Compile Include="Library\LibraryManager.cs" />
<Compile Include="Library\LuceneSearchEngine.cs" />
<Compile Include="Library\ResolverHelper.cs" />

View File

@ -165,7 +165,7 @@ namespace MediaBrowser.ServerApplication
await CompositionRoot.Init();
var win = new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer);
var win = new MainWindow(CompositionRoot.LogManager, CompositionRoot, CompositionRoot.ServerConfigurationManager, CompositionRoot.UserManager, CompositionRoot.LibraryManager, CompositionRoot.JsonSerializer, CompositionRoot.DisplayPreferencesManager);
win.Show();
}

View File

@ -1,8 +1,4 @@
using System.Diagnostics;
using System.Net.Cache;
using System.Net.Http;
using System.Net.Sockets;
using MediaBrowser.Api;
using MediaBrowser.Api;
using MediaBrowser.Common;
using MediaBrowser.Common.Configuration;
using MediaBrowser.Common.Constants;
@ -46,8 +42,10 @@ using MediaBrowser.ServerApplication.Implementations;
using MediaBrowser.WebDashboard.Api;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net.Sockets;
using System.Reflection;
using System.Threading;
using System.Threading.Tasks;
@ -140,6 +138,11 @@ namespace MediaBrowser.ServerApplication
/// </summary>
/// <value>The UDP server.</value>
private UdpServer UdpServer { get; set; }
/// <summary>
/// Gets or sets the display preferences manager.
/// </summary>
/// <value>The display preferences manager.</value>
internal IDisplayPreferencesManager DisplayPreferencesManager { get; set; }
/// <summary>
/// The full path to our startmenu shortcut
@ -212,6 +215,9 @@ namespace MediaBrowser.ServerApplication
ProviderManager = new ProviderManager(HttpClient, ServerConfigurationManager, DirectoryWatchers, LogManager);
RegisterSingleInstance(ProviderManager);
DisplayPreferencesManager = new DisplayPreferencesManager(LogManager.GetLogger("DisplayPreferencesManager"));
RegisterSingleInstance(DisplayPreferencesManager);
RegisterSingleInstance<ILibrarySearchEngine>(() => new LuceneSearchEngine());
SetKernelProperties();

View File

@ -33,7 +33,7 @@ namespace MediaBrowser.ServerApplication
private readonly IJsonSerializer _jsonSerializer;
private readonly ILibraryManager _libraryManager;
private readonly IUserManager _userManager;
private readonly IDisplayPreferencesManager _displayPreferencesManager;
/// <summary>
/// The current user
@ -42,12 +42,18 @@ namespace MediaBrowser.ServerApplication
/// <summary>
/// Initializes a new instance of the <see cref="LibraryExplorer" /> class.
/// </summary>
public LibraryExplorer(IJsonSerializer jsonSerializer, ILogger logger, IApplicationHost appHost, IUserManager userManager, ILibraryManager libraryManager)
/// <param name="jsonSerializer">The json serializer.</param>
/// <param name="logger">The logger.</param>
/// <param name="appHost">The app host.</param>
/// <param name="userManager">The user manager.</param>
/// <param name="libraryManager">The library manager.</param>
/// <param name="displayPreferencesManager">The display preferences manager.</param>
public LibraryExplorer(IJsonSerializer jsonSerializer, ILogger logger, IApplicationHost appHost, IUserManager userManager, ILibraryManager libraryManager, IDisplayPreferencesManager displayPreferencesManager)
{
_logger = logger;
_jsonSerializer = jsonSerializer;
_libraryManager = libraryManager;
_userManager = userManager;
_displayPreferencesManager = displayPreferencesManager;
InitializeComponent();
lblVersion.Content = "Version: " + appHost.ApplicationVersion;
@ -91,7 +97,7 @@ namespace MediaBrowser.ServerApplication
var currentFolder = folder;
Task.Factory.StartNew(() =>
{
var prefs = ddlProfile.SelectedItem != null ? _userManager.GetDisplayPreferences(currentFolder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id)).Result ?? new DisplayPreferences { SortBy = ItemSortBy.SortName } : new DisplayPreferences { SortBy = ItemSortBy.SortName };
var prefs = ddlProfile.SelectedItem != null ? _displayPreferencesManager.GetDisplayPreferences(currentFolder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id)).Result ?? new DisplayPreferences { SortBy = ItemSortBy.SortName } : new DisplayPreferences { SortBy = ItemSortBy.SortName };
var node = new TreeViewItem { Tag = currentFolder };
var subChildren = currentFolder.GetChildren(CurrentUser, prefs.IndexBy);
@ -144,7 +150,7 @@ namespace MediaBrowser.ServerApplication
var subFolder = item as Folder;
if (subFolder != null)
{
var prefs = _userManager.GetDisplayPreferences(subFolder.GetDisplayPreferencesId(user.Id)).Result;
var prefs = _displayPreferencesManager.GetDisplayPreferences(subFolder.GetDisplayPreferencesId(user.Id)).Result;
AddChildren(node, OrderBy(subFolder.GetChildren(user), user, prefs.SortBy), user);
node.Header = item.Name + " (" + node.Items.Count + ")";
@ -201,7 +207,7 @@ namespace MediaBrowser.ServerApplication
var prefs =
await
_userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id));
_displayPreferencesManager.GetDisplayPreferences(folder.GetDisplayPreferencesId((ddlProfile.SelectedItem as User).Id));
ddlIndexBy.SelectedItem = prefs != null
? prefs.IndexBy ?? LocalizedStrings.Instance.GetString("NoneDispPref")
@ -360,7 +366,7 @@ namespace MediaBrowser.ServerApplication
var folder = treeItem != null
? treeItem.Tag as Folder
: null;
var prefs = folder != null ? _userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences {SortBy = ItemSortBy.SortName};
var prefs = folder != null ? _displayPreferencesManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences { SortBy = ItemSortBy.SortName };
if (folder != null && prefs.IndexBy != ddlIndexBy.SelectedItem as string)
{
//grab UI context so we can update within the below task
@ -401,7 +407,7 @@ namespace MediaBrowser.ServerApplication
var folder = treeItem != null
? treeItem.Tag as Folder
: null;
var prefs = folder != null ? _userManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences();
var prefs = folder != null ? _displayPreferencesManager.GetDisplayPreferences(folder.GetDisplayPreferencesId(CurrentUser.Id)).Result : new DisplayPreferences();
if (folder != null && prefs.SortBy != ddlSortBy.SelectedItem as string)
{
//grab UI context so we can update within the below task

View File

@ -43,6 +43,7 @@ namespace MediaBrowser.ServerApplication
private readonly IUserManager _userManager;
private readonly ILibraryManager _libraryManager;
private readonly IJsonSerializer _jsonSerializer;
private readonly IDisplayPreferencesManager _displayPreferencesManager;
/// <summary>
/// Initializes a new instance of the <see cref="MainWindow" /> class.
@ -51,7 +52,7 @@ namespace MediaBrowser.ServerApplication
/// <param name="logger">The logger.</param>
/// <param name="appHost">The app host.</param>
/// <exception cref="System.ArgumentNullException">logger</exception>
public MainWindow(ILogManager logManager, IApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer)
public MainWindow(ILogManager logManager, IApplicationHost appHost, IServerConfigurationManager configurationManager, IUserManager userManager, ILibraryManager libraryManager, IJsonSerializer jsonSerializer, IDisplayPreferencesManager displayPreferencesManager)
{
if (logManager == null)
{
@ -73,6 +74,7 @@ namespace MediaBrowser.ServerApplication
_userManager = userManager;
_libraryManager = libraryManager;
_jsonSerializer = jsonSerializer;
_displayPreferencesManager = displayPreferencesManager;
InitializeComponent();
@ -223,7 +225,7 @@ namespace MediaBrowser.ServerApplication
/// <param name="e">The <see cref="RoutedEventArgs" /> instance containing the event data.</param>
private void cmOpenExplorer_click(object sender, RoutedEventArgs e)
{
new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager).Show();
new LibraryExplorer(_jsonSerializer, _logger, _appHost, _userManager, _libraryManager, _displayPreferencesManager).Show();
}
/// <summary>