-
Notifications
You must be signed in to change notification settings - Fork 1
Release 0.9.0 #17
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
Release 0.9.0 #17
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -19,6 +19,9 @@ public class UiService : IUiServiceInit | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly IDictionary<int, GameObject> _layers = new Dictionary<int, GameObject>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private readonly IDictionary<Type, UiPresenter> _uiPresenters = new Dictionary<Type, UiPresenter>(); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Type _loadingSpinnerType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Transform _uiParent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public IReadOnlyDictionary<Type, UiPresenter> LoadedPresenters => new Dictionary<Type, UiPresenter>(_uiPresenters); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -28,8 +31,8 @@ public class UiService : IUiServiceInit | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public IReadOnlyDictionary<int, UiSetConfig> UiSets => new Dictionary<int, UiSetConfig>(_uiSets); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Type _loadingSpinnerType; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| private Transform _uiParent; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public IReadOnlyList<Type> VisiblePresenters => new List<Type>(_visibleUiList); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public UiService() : this(new UiAssetLoader()) { } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -65,6 +68,18 @@ public void Init(UiConfigs configs) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public T GetUi<T>() where T : UiPresenter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _uiPresenters[typeof(T)] as T; | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+71
to
+75
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add error handling and XML documentation for GetUi. The current implementation has potential failure points:
Consider this safer implementation with proper documentation: +/// <summary>
+/// Gets a UI presenter of type T if it exists in the loaded presenters.
+/// </summary>
+/// <typeparam name="T">The type of UI presenter to retrieve.</typeparam>
+/// <returns>The UI presenter of type T.</returns>
+/// <exception cref="KeyNotFoundException">Thrown when the UI presenter is not loaded.</exception>
+/// <exception cref="InvalidCastException">Thrown when the loaded presenter doesn't match type T.</exception>
public T GetUi<T>() where T : UiPresenter
{
- return _uiPresenters[typeof(T)] as T;
+ if (!_uiPresenters.TryGetValue(typeof(T), out var presenter))
+ {
+ throw new KeyNotFoundException($"UI presenter of type {typeof(T)} is not loaded.");
+ }
+
+ if (presenter is not T typedPresenter)
+ {
+ throw new InvalidCastException($"Loaded presenter is of type {presenter.GetType()} which cannot be cast to {typeof(T)}");
+ }
+
+ return typedPresenter;
}📝 Committable suggestion
Suggested change
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public bool IsVisible<T>() where T : UiPresenter | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return _visibleUiList.Contains(typeof(T)); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public void AddUiConfig(UiConfig config) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
@@ -219,12 +234,6 @@ public void UnloadUiSet(int setId) | |||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public List<Type> GetAllVisibleUi() | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| return new List<Type>(_visibleUiList); | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
| /// <inheritdoc /> | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| public async Task<UiPresenter> OpenUiAsync(Type type) | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🛠️ Refactor suggestion
Consider caching the VisiblePresenters list for better performance.
The current implementation creates a new List on every property access. For frequent access patterns, this could impact performance.
Consider caching the list and invalidating it only when the visible presenters change:
Then set
_visiblePresentersDirty = truein methods that modify_visibleUiList: