Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Avatar, ChannelManager references #30

Merged
merged 9 commits into from
Nov 18, 2024
3 changes: 1 addition & 2 deletions MTGOSDK.Tests/src/Tests/MTGOSDK.API/Users.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ public void Test_CurrentUser()
}

[RateLimit(ms: 100)]
[TestCase(2650356, "TheQonfused")]
[TestCase(3136075, "VidereBot1")]
[TestCase(3136078, "VidereBot2")]
public void Test_GetUser(int id, string name)
Expand Down Expand Up @@ -111,6 +110,6 @@ public void ValidateAvatar(Avatar avatar)
Assert.That(avatar.Name, Is.Not.EqualTo(string.Empty));
Assert.That(avatar.Card.Id, Is.GreaterThan(0));
Assert.That(avatar.Id, Is.GreaterThan(0));
Assert.That(avatar.View, Is.Not.EqualTo(default(Uri)));
// Assert.That(avatar.IsLoaded, Is.True);
}
}
4 changes: 2 additions & 2 deletions MTGOSDK/src/API/Chat/ChannelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
**/

using MTGOSDK.API.Interface.ViewModels;
using MTGOSDK.Core.Reflection;
using static MTGOSDK.Core.Reflection.DLRWrapper;

using Shiny.Core.Interfaces;
Expand Down Expand Up @@ -62,7 +61,8 @@ public static Channel GetChannel(string name) =>
/// The internal reference to the base chat manager.
/// </summary>
private static readonly IChatManager s_chatManager =
ObjectProvider.Get<IShellViewModel>().ChatManager;
Bind<IChatManager>(
Unbind(ObjectProvider.Get<IShellViewModel>()).ChatManager);

internal static ChatSessionViewModel? GetChatForChannel(dynamic channel) =>
Optional<ChatSessionViewModel>(
Expand Down
15 changes: 14 additions & 1 deletion MTGOSDK/src/API/ObjectProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ public static class ObjectProvider
/// the created callback as the <see cref="Get"/> method will invoke this
/// method when the <paramref name="useLazy"/> parameter is set to <c>true</c>.
/// </remarks>
/// <exception cref="TypeInitializationException">
/// Thrown when the given type cannot be retrieved on invocation.
/// </exception>
private static dynamic Defer(
string queryPath,
bool useCache = true,
Expand All @@ -44,7 +47,17 @@ private static dynamic Defer(
Log.Trace("Creating lazy instance of type {Type}", queryPath);
dynamic instance = new LazyRemoteObject();
var resetter = instance.Set(new Func<dynamic>(() =>
Get(queryPath, useCache, useHeap, useLazy: false)));
{
try
{
return Get(queryPath, useCache, useHeap, useLazy: false);
}
catch (InvalidOperationException e)
{
throw new TypeInitializationException(
$"Failed to retrieve instance of type {queryPath}.", e);
}
}));

// Store the resetter callback to reset the lazy instance when
// the RemoteClient is disposed or the ObjectProvider cache is reset.
Expand Down
39 changes: 18 additions & 21 deletions MTGOSDK/src/API/Users/Avatar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,41 +25,38 @@ public sealed class Avatar(dynamic avatar) : DLRWrapper<IAvatar>
/// <summary>
/// The associated visual resource for the Avatar.
/// </summary>
private readonly IVisualResource Image = Bind<IVisualResource>(avatar.Image);
private readonly ICardDefinition CardDefinition =
Bind<ICardDefinition>(avatar.CardDefinition);

//
// IAvatar wrapper properties
//

/// <summary>
/// The name of the Avatar.
/// The unique identifier of the Avatar resource.
/// </summary>
public string Name => @base.Name;
/// <remarks>
/// This corresponds to the ID of the associated card definition,
/// which can be fetched with the <see cref="Collection.CardManager"/> class.
/// </remarks>
public int Id => CardDefinition.Id;

/// <summary>
/// The associated card definition.
/// The name of the Avatar.
/// </summary>
public Card Card => new(@base.CardDefinition);

//
// IVisualResource wrapper properties
//
public string Name => @base.Name;

/// <summary>
/// The unique identifier of the Avatar resource.
/// The associated card definition.
/// </summary>
[Default(-1)]
public int Id => Image.Id;
public Card Card => new(CardDefinition);

/// <summary>
/// The Uri of the Avatar resource.
/// Whether the Avatar resource has been loaded.
/// </summary>
public Uri View => Cast<Uri>(Unbind(Image).View);

//
// IVisualResource wrapper events
//

public EventProxy ViewChanged =
new(/* IVisualResource */ avatar.Image, nameof(ViewChanged));
/// <remarks>
/// This corresponds to the loading state of the card definition's visual
/// resources, which may be fetched separately after an Avatar is obtained.
public bool IsLoaded =>
Try<bool>(() => Unbind(CardDefinition).m_resourceloaded);
}