-
Notifications
You must be signed in to change notification settings - Fork 7
feat: add device detection for (ios/android) and enable appClip default #31
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
Changes from 2 commits
17241be
328088f
9800402
8061cf6
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 |
---|---|---|
|
@@ -35,6 +35,7 @@ import { | |
import { validateContext, validateFunctionParams, validateParameters, validateSignature, validateURL } from './utils/validationUtils' | ||
import { fetchStatusUrl, initSession, updateSession } from './utils/sessionUtils' | ||
import { assertValidSignedClaim, createLinkWithTemplateData, getWitnessesForClaim } from './utils/proofUtils' | ||
import { userAgentIsAndroid, userAgentIsIOS } from './utils/device' | ||
import loggerModule from './utils/logger'; | ||
const logger = loggerModule.logger | ||
|
||
|
@@ -174,6 +175,18 @@ export class ReclaimProofRequest { | |
{ paramName: 'appSecret', input: appSecret, isString: true } | ||
], 'the constructor') | ||
|
||
if (!options) { | ||
options = {}; | ||
} | ||
|
||
if (!options.device) { | ||
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 this in constructor rather than init method. Set the default device and useAppClip fields in constructor 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. done ! |
||
if (userAgentIsIOS) { | ||
options.device = "ios"; | ||
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 enum for 'ios' |
||
} else if (userAgentIsAndroid) { | ||
options.device = "android"; | ||
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 enum for 'android' |
||
} | ||
} | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
// check if options is provided and validate each property of options | ||
if (options) { | ||
if (options.acceptAiProviders) { | ||
|
@@ -186,6 +199,9 @@ export class ReclaimProofRequest { | |
{ paramName: 'log', input: options.log } | ||
], 'the constructor') | ||
} | ||
if (options.useAppClip === undefined) { | ||
options.useAppClip = true; | ||
} | ||
if (options.useAppClip) { | ||
validateFunctionParams([ | ||
{ paramName: 'useAppClip', input: options.useAppClip } | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
const navigatorDefined = typeof navigator !== 'undefined'; | ||
const windowDefined = typeof window !== 'undefined'; | ||
|
||
const userAgent = navigatorDefined ? navigator.userAgent.toLowerCase() : ''; | ||
const userAgentData = navigatorDefined ? (navigator as Navigator & { userAgentData?: { platform: string } }).userAgentData : undefined; | ||
|
||
export const userAgentIsAndroid = userAgent.includes("android"); | ||
coderabbitai[bot] marked this conversation as resolved.
Show resolved
Hide resolved
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 enum for 'android' |
||
|
||
const isIpad = | ||
windowDefined && | ||
navigatorDefined && | ||
(userAgentData?.platform === 'iPad' || userAgent.includes('ipad')); | ||
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 enum for 'ipad' |
||
|
||
export const userAgentIsIOS = | ||
/iphone|ipod/i.test(userAgent) || isIpad; | ||
|
||
export const userAgentIsMobile = | ||
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent) || | ||
(windowDefined && 'orientation' in window); | ||
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. 💡 Verification agent 🧩 Analysis chainVerify orientation-based mobile detection reliability. The mobile detection logic is comprehensive, but the 🌐 Web query:
💡 Result: The Deprecation StatusThe Better AlternativesSeveral more modern and standardized alternatives exist for detecting device orientation: 1. Screen.orientation APIThe official replacement for 2. matchMedia() for Orientation DetectionOne of the most recommended and easiest approaches is using the const portrait = window.matchMedia("(orientation: portrait)").matches;
// portrait will be true or false This method not only detects the current orientation but can also listen for orientation changes[5]: window.matchMedia("(orientation: portrait)").addEventListener("change", e => {
const portrait = e.matches;
if (portrait) {
// Handle portrait orientation
} else {
// Handle landscape orientation
}
}); This approach is compatible with all major browsers, including Safari, and works reliably on mobile and tablet devices[5]. 3. DeviceOrientationEventFor more detailed orientation data, you can use the window.addEventListener("deviceorientation", handleOrientation, true);
function handleOrientation(event) {
const absolute = event.absolute;
const alpha = event.alpha; // rotation around z-axis
const beta = event.beta; // front to back motion (x-axis)
const gamma = event.gamma; // left to right motion (y-axis)
// Use orientation data
} This event provides detailed values for device rotation along different axes, including alpha (z-axis rotation), beta (x-axis rotation), and gamma (y-axis rotation)[1][3]. 4. Feature Detection for Touch CapabilityIf your goal is specifically to detect mobile devices to optimize for touch interfaces, it's better to directly test for touch capability rather than making assumptions based on orientation: if (navigator.maxTouchPoints > 1) {
// Device supports multi-touch
} This approach aligns with modern best practices of testing for specific features rather than attempting to identify device types[8]. Industry TrendsThe Android platform is also moving away from allowing apps to restrict orientation. Android 16 is removing the ability for apps to restrict orientation and resizability at the platform level, shifting to a model where apps should adapt seamlessly to different screen sizes and orientations[2]. This industry-wide trend emphasizes the importance of responsive, adaptive design rather than orientation detection and restriction. Should You Still Use window.orientation?While In conclusion, for modern web development, it's best to use Citations:
Replace deprecated The • Use the Screen Orientation API export const userAgentIsMobile =
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent) ||
- (windowDefined && 'orientation' in window);
+ (windowDefined && window.screen?.orientation?.type.startsWith('portrait')); • Or use a media query via - (windowDefined && 'orientation' in window);
+ (windowDefined && window.matchMedia('(orientation: portrait)').matches); • For touch-capable devices, feature-detect touch input: - (windowDefined && 'orientation' in window);
+ (windowDefined && navigator.maxTouchPoints > 1); You can even combine these checks for broader coverage: export const userAgentIsMobile =
/android|webos|iphone|ipad|ipod|blackberry|iemobile|opera mini/i.test(userAgent) ||
- (windowDefined && 'orientation' in window);
+ (windowDefined && (
+ window.screen?.orientation?.type.startsWith('portrait') ||
+ window.matchMedia('(orientation: portrait)').matches ||
+ navigator.maxTouchPoints > 1
+ )); This ensures robust orientation detection across modern browsers. 🤖 Prompt for AI Agents
|
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.
make it 3.0.3
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.
updated to 3.0.3