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

feat(webapis): introduce @react-native-webapis/battery-status #2590

Merged
merged 22 commits into from
Oct 10, 2023

Conversation

tido64
Copy link
Member

@tido64 tido64 commented Aug 8, 2023

Description

Initial implementation of Battery Status API for Android, iOS, macOS, and Windows.

Requires #2580.

Test plan

Apply the following changes to the test app:

diff --git a/packages/test-app/babel.config.js b/packages/test-app/babel.config.js
index 69ebd557..a012b7f5 100644
--- a/packages/test-app/babel.config.js
+++ b/packages/test-app/babel.config.js
@@ -13,6 +13,7 @@ module.exports = {
           { runtime: "automatic" },
         ],
         [require("@babel/plugin-transform-react-jsx-source")],
+        [require("@rnx-kit/polyfills")],
       ],
     },
   ],
diff --git a/packages/test-app/src/App.native.tsx b/packages/test-app/src/App.native.tsx
index 599634a9..a9b493ab 100644
--- a/packages/test-app/src/App.native.tsx
+++ b/packages/test-app/src/App.native.tsx
@@ -1,3 +1,5 @@
+// @react-native-webapis
+
 import { acquireTokenWithScopes } from "@rnx-kit/react-native-auth";
 // Both `internal` imports are used to verify that `metro-resolver-symlinks`
 // resolves them correctly when `experimental_retryResolvingFromDisk` is
@@ -7,7 +9,7 @@ import {
   getRemoteDebuggingAvailability,
 } from "internal";
 import { getHermesVersion } from "internal/hermes";
-import React, { useCallback, useMemo, useState } from "react";
+import React, { useCallback, useEffect, useMemo, useState } from "react";
 import type { LayoutChangeEvent } from "react-native";
 import {
   NativeModules,
@@ -186,6 +188,14 @@ function App({ concurrentRoot }: { concurrentRoot?: boolean }) {
     [setFabric]
   );

+  const [batteryLevel, setBatteryLevel] = useState(-1);
+  useEffect(() => {
+    // @ts-expect-error Battery Status API is deprecated
+    navigator.getBattery().then((status) => {
+      setBatteryLevel(status.level);
+    });
+  }, []);
+
   return (
     <SafeAreaView style={styles.body}>
       <StatusBar barStyle={isDarkMode ? "light-content" : "dark-content"} />
@@ -195,6 +205,9 @@ function App({ concurrentRoot }: { concurrentRoot?: boolean }) {
         style={styles.body}
       >
         <Header />
+        <View style={styles.group}>
+          <Feature value={batteryLevel.toFixed(2)}>Battery Level</Feature>
+        </View>
         <View style={styles.group}>
           <Button onPress={startAcquireToken}>Acquire Token</Button>
         </View>

Build and run:

cd packages/test-app
yarn add @rnx-kit/polyfills --dev
yarn add @react-native-webapis/battery-status
yarn build --dependencies

# Android
yarn bundle:android
yarn android

# iOS
yarn bundle:ios
pod install --project-directory=ios
yarn ios

Note: When running in the iOS simulator, the battery level will always be '-1'.

Screenshots

Android iOS macOS Windows
Screenshot_20230808_142640 Image image image

@github-actions github-actions bot added chore Improvements that don't directly affect features feature: sdk This is related to 1RN SDK labels Aug 8, 2023
@tido64 tido64 mentioned this pull request Aug 8, 2023
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch from dc90512 to 9f50b41 Compare August 8, 2023 14:44
@tido64 tido64 marked this pull request as ready for review August 8, 2023 14:44
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch from ca31571 to 6d24a06 Compare August 9, 2023 09:44
@github-actions github-actions bot removed the feature: sdk This is related to 1RN SDK label Aug 9, 2023
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch from 0a3f7e0 to 5bb88c9 Compare August 9, 2023 13:28
@kelset kelset linked an issue Aug 10, 2023 that may be closed by this pull request
@kelset kelset added the feature: webapis This is related to the experimental effort around WebAPIs for RN label Aug 10, 2023
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch from 5bb88c9 to 96efd68 Compare August 16, 2023 12:06
@matthargett
Copy link

is fast refresh something that can be tested by the e2e tester in CI, to capture the problem fixed in the polyfill?

@tido64 tido64 force-pushed the tido/webapis/battery-manager branch 2 times, most recently from 1d25503 to 0e050fb Compare August 18, 2023 08:10
@tido64
Copy link
Member Author

tido64 commented Aug 18, 2023

is fast refresh something that can be tested by the e2e tester in CI, to capture the problem fixed in the polyfill?

I've added tests, but it doesn't look like we're capable of capturing this specific error. We currently don't have e2e tests running on a device (emulator or otherwise). I'm not sure how one would trigger fast refresh outside of running the code. If you have any pointers, I'd really appreciate it.

@tido64 tido64 force-pushed the tido/webapis/battery-manager branch 2 times, most recently from 17079a3 to 46b7e5b Compare August 31, 2023 13:58
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch 2 times, most recently from ddd7404 to f65d01d Compare September 21, 2023 15:43
@tido64 tido64 force-pushed the tido/webapis/battery-manager branch 2 times, most recently from cafc35e to 0f7449f Compare September 26, 2023 10:36
Copy link
Contributor

@kelset kelset left a comment

Choose a reason for hiding this comment

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

overall it all looks great. This PR showcases already some learnings and standards/patterns that we should codify back into the RFC, I'll take care of that.

Thanks for all the work 🙇‍♂️

@tido64 tido64 merged commit c549a8c into main Oct 10, 2023
12 checks passed
@tido64 tido64 deleted the tido/webapis/battery-manager branch October 10, 2023 11:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
chore Improvements that don't directly affect features feature: webapis This is related to the experimental effort around WebAPIs for RN
Projects
None yet
Development

Successfully merging this pull request may close these issues.

make a working prototype of a WebAPI to pair with the RFC
4 participants