Skip to content

Commit

Permalink
Kotlinify DeviceInfoModule
Browse files Browse the repository at this point in the history
Summary:
# Changelog:
[Internal] -

As in the title.

Differential Revision: D57248069
  • Loading branch information
rshest authored and facebook-github-bot committed May 11, 2024
1 parent 3f17c8b commit 537d2c8
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 111 deletions.
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)
}
}

0 comments on commit 537d2c8

Please sign in to comment.