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

Better detection of device orientation based on browser feature availability #5172

Merged
merged 6 commits into from
May 20, 2022

Conversation

ozyx
Copy link
Member

@ozyx ozyx commented May 9, 2022

Closes #4875

Describe your changes:

Adds multiple methods of detecting device orientation (Portrait vs. Landscape) based on browser feature availability. Prefer to use screen.orientation if available, falling back to using the deprecated window.orientation (in case of using Safari mobile browser), and finally falling back to comparing window.innerHeight and window.innerWidth.

Injects an instance of the Agent utility class into the Notebook Vue component in order to directly call these device and orientation classification methods. Previously, the Notebook would check for specific classes within the document.body.classList on re-render in order to determine device type and orientation. However, this leads to a race-condition when changing orientation on a phone, since the Vue component re-renders before the DeviceClassifier can add/remove the proper classes to the document.body.

It seems to me that the Agent utility class might be better as a collection of static utility methods, since it does not necessarily need to maintain state. However, this has broader implications on testing methods and overall architecture, so for this PR I went with the strategy of directly injecting an instance of Agent as we do in the initialization of the DeviceClassifier plugin. I suppose this helps to decouple DeviceClassifier and Notebook, so maybe a good strategy? Open to any opinions on this.

All Submissions:

  • Have you followed the guidelines in our Contributing document?
  • Have you checked to ensure there aren't other open Pull Requests for the same update/change?
  • Is this change backwards compatible? For example, developers won't need to change how they are calling the API or how they've extended core plugins such as Tables or Plots.

Author Checklist

  • Changes address original issue?
  • Unit tests included and/or updated with changes?
  • Command line build passes?
  • Has this been smoke tested?
  • Testing instructions included in associated issue?

Reviewer Checklist

  • Changes appear to address issue?
  • Changes appear not to be breaking changes?
  • Appropriate unit tests included?
  • Code style and in-line documentation are appropriate?
  • Commit messages meet standards?
  • Has associated issue been labelled unverified? (only applicable if this PR closes the issue)
  • Has associated issue been labelled bug? (only applicable if this PR is for a bug fix)

ozyx added 4 commits May 6, 2022 14:51
- Prefer to use screen.orientation API first

- Then check for window.orientation (e.g. if using Safari browser on a mobile device)

- Finally, fall back to using window.innerWidth/window.innerHeight
- Prefer to use hasOwnProperty() over truthy check
- Provides Agent instance to Notebook component

- Determine device type and orientation using Agent methods instead of checking against body.classList
@codecov
Copy link

codecov bot commented May 9, 2022

Codecov Report

Merging #5172 (cc4cd6c) into master (4891656) will decrease coverage by 0.25%.
The diff coverage is 100.00%.

❗ Current head cc4cd6c differs from pull request most recent head 947fc66. Consider uploading reports for the commit 947fc66 to get more accurate results

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #5172      +/-   ##
==========================================
- Coverage   50.15%   49.90%   -0.26%     
==========================================
  Files         548      534      -14     
  Lines       20101    19555     -546     
  Branches     1859     1767      -92     
==========================================
- Hits        10082     9759     -323     
+ Misses       9538     9350     -188     
+ Partials      481      446      -35     
Impacted Files Coverage Δ
...c/plugins/DeviceClassifier/src/DeviceClassifier.js 57.14% <ø> (+3.80%) ⬆️
src/plugins/notebook/components/Notebook.vue 20.80% <100.00%> (-0.29%) ⬇️
src/plugins/notebook/plugin.js 92.59% <100.00%> (+0.28%) ⬆️
src/utils/agent/Agent.js 85.29% <100.00%> (+4.52%) ⬆️
src/plugins/flexibleLayout/utils/container.js 0.00% <0.00%> (-100.00%) ⬇️
src/plugins/formActions/EditPropertiesAction.js 38.23% <0.00%> (-52.95%) ⬇️
src/plugins/conditionWidget/plugin.js 50.00% <0.00%> (-50.00%) ⬇️
src/api/forms/components/controls/TextField.vue 50.00% <0.00%> (-50.00%) ⬇️
src/plugins/telemetryTable/TelemetryTableType.js 50.00% <0.00%> (-50.00%) ⬇️
...rc/api/forms/components/controls/TextAreaField.vue 0.00% <0.00%> (-50.00%) ⬇️
... and 65 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 4891656...947fc66. Read the comment docs.

@akhenry akhenry removed the request for review from nikhilmandlik May 9, 2022 21:32
@@ -52,7 +52,6 @@ export default (agent, document) => {
if (agent.isMobile()) {
const mediaQuery = window.matchMedia("(orientation: landscape)");
function eventHandler(event) {
console.log("changed");
Copy link
Contributor

Choose a reason for hiding this comment

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

Yikes, good catch.

// address in https://github.com/nasa/openmct/issues/4875
// eslint-disable-next-line compat/compat
const isPortrait = window.screen.orientation.type.includes('portrait');
const isPhone = this.agent.isPhone();
Copy link
Contributor

Choose a reason for hiding this comment

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

👍

@akhenry akhenry enabled auto-merge (squash) May 20, 2022 19:26
@akhenry akhenry removed the request for review from unlikelyzero May 20, 2022 19:27
@akhenry akhenry merged commit 037886a into master May 20, 2022
@akhenry akhenry deleted the mct4875 branch May 20, 2022 19:37
@akhenry
Copy link
Contributor

akhenry commented May 20, 2022

Awesome work with this, thanks @ozyx !

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.

Unsupported API in notebook - screen.orientation
3 participants