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

Kotlinify DeviceInfoModule #44536

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/react-native/ReactAndroid/api/ReactAndroid.api
Original file line number Diff line number Diff line change
Expand Up @@ -3241,10 +3241,10 @@ public abstract interface class com/facebook/react/modules/debug/interfaces/Deve
public abstract fun setRemoteJSDebugEnabled (Z)V
}

public class com/facebook/react/modules/deviceinfo/DeviceInfoModule : com/facebook/fbreact/specs/NativeDeviceInfoSpec, com/facebook/react/bridge/LifecycleEventListener {
public final class com/facebook/react/modules/deviceinfo/DeviceInfoModule : com/facebook/fbreact/specs/NativeDeviceInfoSpec, com/facebook/react/bridge/LifecycleEventListener {
public fun <init> (Landroid/content/Context;)V
public fun <init> (Lcom/facebook/react/bridge/ReactApplicationContext;)V
public fun emitUpdateDimensionsEvent ()V
public final fun emitUpdateDimensionsEvent ()V
public fun getTypedExportedConstants ()Ljava/util/Map;
public fun invalidate ()V
public fun onHostDestroy ()V
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

package com.facebook.react.modules.deviceinfo

import android.content.Context
import com.facebook.fbreact.specs.NativeDeviceInfoSpec
import com.facebook.react.bridge.LifecycleEventListener
import com.facebook.react.bridge.ReactApplicationContext
import com.facebook.react.bridge.ReactNoCrashSoftException
import com.facebook.react.bridge.ReactSoftExceptionLogger
import com.facebook.react.bridge.ReadableMap
import com.facebook.react.module.annotations.ReactModule
import com.facebook.react.uimanager.DisplayMetricsHolder.getDisplayMetricsWritableMap
import com.facebook.react.uimanager.DisplayMetricsHolder.initDisplayMetricsIfNotInitialized

/** Module that exposes Android Constants to JS. */
@ReactModule(name = NativeDeviceInfoSpec.NAME)
public class DeviceInfoModule : NativeDeviceInfoSpec, LifecycleEventListener {
private var reactApplicationContext: ReactApplicationContext? = null
private var fontScale: Float
private var previousDisplayMetrics: ReadableMap? = null

public constructor(reactContext: ReactApplicationContext) : super(reactContext) {
initDisplayMetricsIfNotInitialized(reactContext)
fontScale = reactContext.resources.configuration.fontScale
reactContext.addLifecycleEventListener(this)
reactApplicationContext = reactContext
}

public constructor(context: Context) : super(null) {
reactApplicationContext = null
initDisplayMetricsIfNotInitialized(context)
fontScale = context.resources.configuration.fontScale
}

override public fun getTypedExportedConstants(): Map<String, Any> {
val displayMetrics = getDisplayMetricsWritableMap(fontScale.toDouble())

// Cache the initial dimensions for later comparison in emitUpdateDimensionsEvent
previousDisplayMetrics = displayMetrics.copy()
return mapOf("Dimensions" to displayMetrics.toHashMap())
}

override fun onHostResume() {
val newFontScale = reactApplicationContext?.resources?.configuration?.fontScale
if (newFontScale != null && newFontScale != fontScale) {
fontScale = newFontScale
emitUpdateDimensionsEvent()
}
}

override fun onHostPause(): Unit = Unit

override fun onHostDestroy(): Unit = Unit

public fun emitUpdateDimensionsEvent() {
reactApplicationContext?.let { context ->
if (context.hasActiveReactInstance()) {
// Don't emit an event to JS if the dimensions haven't changed
val displayMetrics = getDisplayMetricsWritableMap(fontScale.toDouble())
if (previousDisplayMetrics == null) {
previousDisplayMetrics = displayMetrics.copy()
} else if (displayMetrics != previousDisplayMetrics) {
previousDisplayMetrics = displayMetrics.copy()
context.emitDeviceEvent("didUpdateDimensions", displayMetrics)
}
} else {
ReactSoftExceptionLogger.logSoftException(
NativeDeviceInfoSpec.NAME,
ReactNoCrashSoftException(
"No active CatalystInstance, cannot emitUpdateDimensionsEvent"))
}
}
}

override fun invalidate() {
super.invalidate()
reactApplicationContext?.removeLifecycleEventListener(this)
}
}
Loading