Skip to content

Conversation

@CoderGamester
Copy link
Owner

@CoderGamester CoderGamester commented Jan 5, 2025

New:

  • Added two new static event calls to UiService that are triggered when the app changes resolution and another trigger when changes screen orientation
  • The new UiServiceMonoComponent was added to the project for internal purposes, in order to support Unity's loop or generic GameObjects dependency code (e.g. the screen resolution trigger change)
  • Added new AdjustScreenSizeFitter to extend the ui behaviour of Unity's ContentSizeFitter in order to allow the LayoutElement to fit between the LayoutElement.minSize & LayoutElement.flexibleSize, values defined in the Unity's inspector

Changed:

  • Renamed UiPresenterData to UiPresenter to make it less verbose

Summary by CodeRabbit

Release Notes for Version 0.11.0

  • New Features

    • Added screen resolution and orientation change tracking
    • Introduced AdjustScreenSizeFitter to enhance Unity's layout sizing capabilities
  • Improvements

    • Renamed UiPresenterData<T> to UiPresenter<T> for improved code clarity
    • Enhanced UI service with new event monitoring for screen changes
  • Technical Updates

    • Added UiServiceMonoComponent to support advanced UI lifecycle management

- Added two new static event calls to UiService that are triggered when the app changes resolution and another trigger when changes screen orientation
- The new *UiServiceMonoComponent* was added to the project for internal purposes, in order to support Unity's loop or generic *GameObjects* dependency code (e.g. the screen resolution trigger change)
- Added new *AdjustScreenSizeFitter* to extend the ui behaviour of Unity's *ContentSizeFitter* in order to allow the *LayoutElement* to fit between the *LayoutElement.minSize* & *LayoutElement.flexibleSize*, values defined in the Unity's inspector

**Changed**:
- Renamed *UiPresenterData<T>* to *UiPresenter<T>* to make it less verbose
@coderabbitai
Copy link

coderabbitai bot commented Jan 5, 2025

📝 Walkthrough

Walkthrough

This pull request introduces version 0.11.0 of the UI service library, focusing on enhancing screen resolution and orientation handling. The changes include renaming the UiPresenterData<T> class to UiPresenter<T>, adding a new UiServiceMonoComponent to track screen changes, and creating an AdjustScreenSizeFitter to improve layout management. Two new static events in UiService now support monitoring device orientation and resolution changes, providing more robust UI adaptation mechanisms.

Changes

File Change Summary
CHANGELOG.md Updated to version 0.11.0, documenting new features
Runtime/UiPresenter.cs Renamed UiPresenterData<T> to UiPresenter<T>
Runtime/UiService.cs Added static events OnOrientationChanged and OnResolutionChanged
Modified initialization and UI loading methods
Runtime/UiServiceMonoComponent.cs New component to monitor screen resolution and orientation changes
Runtime/AdjustScreenSizeFitter.cs New component for enhanced content size fitting
package.json Version bumped from 0.10.0 to 0.11.0

Sequence Diagram

sequenceDiagram
    participant UI as UiService
    participant Mono as UiServiceMonoComponent
    participant Screen as Screen/Device

    Screen->>Mono: Resolution/Orientation Change
    Mono->>UI: Trigger OnResolutionChanged
    Mono->>UI: Trigger OnOrientationChanged
    UI->>UI: Update UI Components
Loading

Possibly related PRs

Poem

🐰 Hop, hop, UI's getting smart!
Screens twist and turn, we're playing our part
Resolution dances, orientation sings
CodeRabbit's magic makes UI's wings
Presenting data with elegant grace! 🖥️


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR. (Beta)
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🧹 Nitpick comments (4)
Runtime/AdjustScreenSizeFitter.cs (1)

59-70: Avoid repeated size checks every frame if unnecessary.
Using LateUpdate to check for resolution changes is acceptable; however, for performance-sensitive applications, you could provide a way to disable or reduce checks if the resolution rarely changes (e.g., on non-mobile platforms). This optimization might eliminate superfluous per-frame calls.

Runtime/UiServiceMonoComponent.cs (1)

21-28: Consider throttling or debouncing resolution change events.
When the resolution fluctuates rapidly (e.g., device transitions or user resizing windows), it may invoke UiService.OnResolutionChanged repeatedly. If the app performs complex operations in this callback, consider implementing throttling or debouncing logic to minimize excessive calls.

Runtime/UiService.cs (1)

255-261: Write more descriptive error logs.
When the cast to UiPresenter<TData> fails, the log incorrectly states "Implement it to allow it to open with initial defined data". Consider specifying the actual type mismatch to help debugging.

- Debug.LogError($"The UiPresenter {type} is not a UiPresenter<TData> type you. " +
-               $"Implement it to allow it to open with initial defined data");
+ Debug.LogError($"Failed to cast {type} to UiPresenter<{typeof(TData)}>." +
+               "Ensure that the UI prefab implements the correct generic presenter type.");
CHANGELOG.md (1)

9-13: Improve clarity and reduce wordiness in feature descriptions.

The features are well-documented, but the text can be more concise and grammatically correct.

Apply these improvements:

 **New**:
-Added two new static event calls to UiService that are triggered when the app changes resolution and another trigger when changes screen orientation
-The new *UiServiceMonoComponent* was added to the project for internal purposes, in order to support Unity's loop or generic *GameObjects* dependency code (e.g. the screen resolution trigger change)
-Added new *AdjustScreenSizeFitter* to extend the ui behaviour of Unity's *ContentSizeFitter* in order to allow the *LayoutElement* to fit between the *LayoutElement.minSize* & *LayoutElement.flexibleSize*, values defined in the Unity's inspector
+- Added two new static events to UiService: one for app resolution changes and another for screen orientation changes
+- Added *UiServiceMonoComponent* to support Unity's loop and *GameObject* dependencies for screen resolution changes
+- Added *AdjustScreenSizeFitter* to extend Unity's *ContentSizeFitter*, allowing *LayoutElement* to fit between *LayoutElement.minSize* and *LayoutElement.flexibleSize* defined in Unity's inspector
🧰 Tools
🪛 LanguageTool

[grammar] ~10-~10: Possible agreement error. The noun ‘event’ seems to be countable.
Context: ...-05

New:

  • Added two new static event calls to UiService that are triggered w...

(CD_NN)


[style] ~11-~11: Consider a shorter alternative to avoid wordiness.
Context: ...d to the project for internal purposes, in order to support Unity's loop or generic *GameOb...

(IN_ORDER_TO_PREMIUM)


[style] ~12-~12: Consider a shorter alternative to avoid wordiness.
Context: ...ehaviour of Unity's ContentSizeFitter in order to allow the LayoutElement to fit betwee...

(IN_ORDER_TO_PREMIUM)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between cc8cccd and 4dcf006.

📒 Files selected for processing (8)
  • CHANGELOG.md (1 hunks)
  • Runtime/AdjustScreenSizeFitter.cs (1 hunks)
  • Runtime/AdjustScreenSizeFitter.cs.meta (1 hunks)
  • Runtime/UiPresenter.cs (1 hunks)
  • Runtime/UiService.cs (4 hunks)
  • Runtime/UiServiceMonoComponent.cs (1 hunks)
  • Runtime/UiServiceMonoComponent.cs.meta (1 hunks)
  • package.json (1 hunks)
✅ Files skipped from review due to trivial changes (2)
  • package.json
  • Runtime/AdjustScreenSizeFitter.cs.meta
🧰 Additional context used
🪛 LanguageTool
CHANGELOG.md

[grammar] ~10-~10: Possible agreement error. The noun ‘event’ seems to be countable.
Context: ...-05

New:

  • Added two new static event calls to UiService that are triggered w...

(CD_NN)


[style] ~11-~11: Consider a shorter alternative to avoid wordiness.
Context: ...d to the project for internal purposes, in order to support Unity's loop or generic *GameOb...

(IN_ORDER_TO_PREMIUM)


[style] ~12-~12: Consider a shorter alternative to avoid wordiness.
Context: ...ehaviour of Unity's ContentSizeFitter in order to allow the LayoutElement to fit betwee...

(IN_ORDER_TO_PREMIUM)

🔇 Additional comments (9)
Runtime/AdjustScreenSizeFitter.cs (1)

39-45: Consider handling missing Canvas scenarios.
If GetComponentInParent<Canvas>() returns null, calling GetComponent<RectTransform>() may lead to a NullReferenceException. You may want to add a null check or gracefully handle the case where a Canvas is not found, to prevent runtime errors in unconventional setups or test scripts.

Would you like me to generate a shell script to identify all references to GetComponentInParent<Canvas>() and check if they handle null scenarios?

Runtime/UiServiceMonoComponent.cs (1)

30-45: Handle orientation transitions beyond standard states.
While ignoring Unknown, FaceUp, and FaceDown is fine, certain devices might briefly report these orientations while rotating, causing potential missed triggers. Evaluating how quickly you check transitions may help avoid skipping relevant orientation events on rapidly rotating devices.

Runtime/UiPresenter.cs (1)

89-89: Renaming to UiPresenter<T> is more concise.
This rename effectively reduces verbosity. Ensure any existing references to the old name, especially in documentation or third-party integrations, are updated to prevent confusion.

Runtime/UiService.cs (3)

15-16: Events for resolution/orientation changes look good.
These static readonly UnityEvent fields add flexibility for listening to UI changes across the system. Just be mindful of event subscribers to avoid memory leaks if you add and forget to remove listeners.


66-66: Consider DontDestroyOnLoad usage.
Attaching UiServiceMonoComponent and calling DontDestroyOnLoad ensures the UI persists between scene loads. Confirm this is desired in all contexts; if the UI is scene-specific, you may not want it to persist.


71-71: Appropriate async call usage.
Using LoadUiAsync(_loadingSpinnerType).Forget() clarifies that you're intentionally discarding the task. This practice is fine, provided you handle potential exceptions within that method to avoid unhandled promise rejections.

Runtime/UiServiceMonoComponent.cs.meta (1)

1-11: No issues with meta file.
This .meta file is standard for Unity’s asset/database tracking.

CHANGELOG.md (2)

7-8: LGTM!

The version header follows the Keep a Changelog format and semantic versioning correctly.


14-16: LGTM!

The entry clearly documents the breaking change of renaming UiPresenterData<T> to UiPresenter<T>.

Comment on lines +110 to +129
private float GetSize(int axis, Vector2 resolution)
{
var minSize = LayoutUtility.GetMinSize(RectTransform, axis);
var maxSize = LayoutUtility.GetFlexibleSize(RectTransform, axis);
var resolutionX = resolution.x - _padding.left - _padding.right;
var resolutionY = resolution.y - _padding.top - _padding.bottom;

if (axis == 0)
{
if(resolutionX < minSize && minSize > 0) return minSize;
if(resolutionX >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;

return RectTransform.sizeDelta.x > resolutionX && resolutionX > 0 ? resolutionX : RectTransform.sizeDelta.x;
}

if(resolutionY < minSize && minSize > 0) return minSize;
if(resolutionY >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;

return RectTransform.sizeDelta.y > resolutionY && resolutionY > 0 ? resolutionY : RectTransform.sizeDelta.y;
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Validate padding vs. resolution constraints.
In GetSize, the padding applied could exceed the resolution if misconfigured, potentially yielding negative or unusable values. Consider clamping the final size to a minimum of zero or providing user feedback if the sum of _padding.left + _padding.right exceeds resolution.x (and similarly for y).

- var resolutionX = resolution.x - _padding.left - _padding.right;
- var resolutionY = resolution.y - _padding.top - _padding.bottom;
+ var resolutionX = Mathf.Max(0, resolution.x - _padding.left - _padding.right);
+ var resolutionY = Mathf.Max(0, resolution.y - _padding.top - _padding.bottom);
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private float GetSize(int axis, Vector2 resolution)
{
var minSize = LayoutUtility.GetMinSize(RectTransform, axis);
var maxSize = LayoutUtility.GetFlexibleSize(RectTransform, axis);
var resolutionX = resolution.x - _padding.left - _padding.right;
var resolutionY = resolution.y - _padding.top - _padding.bottom;
if (axis == 0)
{
if(resolutionX < minSize && minSize > 0) return minSize;
if(resolutionX >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;
return RectTransform.sizeDelta.x > resolutionX && resolutionX > 0 ? resolutionX : RectTransform.sizeDelta.x;
}
if(resolutionY < minSize && minSize > 0) return minSize;
if(resolutionY >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;
return RectTransform.sizeDelta.y > resolutionY && resolutionY > 0 ? resolutionY : RectTransform.sizeDelta.y;
}
private float GetSize(int axis, Vector2 resolution)
{
var minSize = LayoutUtility.GetMinSize(RectTransform, axis);
var maxSize = LayoutUtility.GetFlexibleSize(RectTransform, axis);
var resolutionX = Mathf.Max(0, resolution.x - _padding.left - _padding.right);
var resolutionY = Mathf.Max(0, resolution.y - _padding.top - _padding.bottom);
if (axis == 0)
{
if(resolutionX < minSize && minSize > 0) return minSize;
if(resolutionX >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;
return RectTransform.sizeDelta.x > resolutionX && resolutionX > 0 ? resolutionX : RectTransform.sizeDelta.x;
}
if(resolutionY < minSize && minSize > 0) return minSize;
if(resolutionY >= maxSize && maxSize > minSize && maxSize > 1) return maxSize;
return RectTransform.sizeDelta.y > resolutionY && resolutionY > 0 ? resolutionY : RectTransform.sizeDelta.y;
}

@CoderGamester CoderGamester merged commit ca6f9a3 into master Jan 5, 2025
1 check passed
@coderabbitai coderabbitai bot mentioned this pull request Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants