/*
 * Copyright (c) Facebook, Inc. and its 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 androidx.annotation.Nullable;
import com.facebook.react.bridge.LifecycleEventListener;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactNoCrashSoftException;
import com.facebook.react.bridge.ReactSoftException;
import com.facebook.react.bridge.ReadableMap;
import com.facebook.react.bridge.WritableNativeMap;
import com.facebook.react.module.annotations.ReactModule;
import com.facebook.react.modules.core.DeviceEventManagerModule;
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
import com.facebook.react.uimanager.DisplayMetricsHolder;
import java.util.HashMap;
import java.util.Map;

/** Module that exposes Android Constants to JS. */
@ReactModule(name = DeviceInfoModule.NAME)
public class DeviceInfoModule extends ReactContextBaseJavaModule
    implements LifecycleEventListener, TurboModule {

  public static final String NAME = "DeviceInfo";

  private @Nullable ReactApplicationContext mReactApplicationContext;
  private float mFontScale;
  private @Nullable ReadableMap mPreviousDisplayMetrics;

  public DeviceInfoModule(ReactApplicationContext reactContext) {
    super(reactContext);
    DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext);
    mFontScale = reactContext.getResources().getConfiguration().fontScale;
    mReactApplicationContext = reactContext;
    mReactApplicationContext.addLifecycleEventListener(this);
  }

  public DeviceInfoModule(Context context) {
    super(null);
    mReactApplicationContext = null;
    DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(context);
    mFontScale = context.getResources().getConfiguration().fontScale;
  }

  @Override
  public String getName() {
    return NAME;
  }

  @Override
  public @Nullable Map<String, Object> getConstants() {
    HashMap<String, Object> constants = new HashMap<>();
    constants.put("Dimensions", DisplayMetricsHolder.getDisplayMetricsMap(mFontScale));
    return constants;
  }

  @Override
  public void onHostResume() {
    if (mReactApplicationContext == null) {
      return;
    }

    float fontScale = mReactApplicationContext.getResources().getConfiguration().fontScale;
    if (mFontScale != fontScale) {
      mFontScale = fontScale;
      emitUpdateDimensionsEvent();
    }
  }

  @Override
  public void onHostPause() {}

  @Override
  public void onHostDestroy() {}

  public void emitUpdateDimensionsEvent() {
    if (mReactApplicationContext == null) {
      return;
    }

    if (mReactApplicationContext.hasActiveCatalystInstance()) {
      // Don't emit an event to JS if the dimensions haven't changed
      WritableNativeMap displayMetrics =
          DisplayMetricsHolder.getDisplayMetricsNativeMap(mFontScale);
      if (!displayMetrics.equals(mPreviousDisplayMetrics)) {
        mReactApplicationContext
            .getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
            .emit("didUpdateDimensions", displayMetrics);
        mPreviousDisplayMetrics = displayMetrics;
      }
    } else {
      ReactSoftException.logSoftException(
          NAME,
          new ReactNoCrashSoftException(
              "No active CatalystInstance, cannot emitUpdateDimensionsEvent"));
    }
  }

  @Override
  public void invalidate() {}
}

@@ -3115,24 +3115,81 @@
 Metrics)) %7B%0A
+        mPreviousDisplayMetrics = displayMetrics.copy();%0A
         mRea
@@ -3350,58 +3350,8 @@
 s);%0A
-        mPreviousDisplayMetrics = displayMetrics;%0A
     

Save a copy of DisplayMetrics native map in DeviceInfoModule