PHP 7.4.33
Preview: RCTAppState.mm Size: 3.97 KB
/var/www/uibuilder.cmshelp.dk/httpdocs/node_modules/react-native/React/CoreModules/RCTAppState.mm
/*
 * 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.
 */

#import "RCTAppState.h"

#import <FBReactNativeSpec/FBReactNativeSpec.h>
#import <React/RCTAssert.h>
#import <React/RCTBridge.h>
#import <React/RCTEventDispatcherProtocol.h>
#import <React/RCTUtils.h>

#import "CoreModulesPlugins.h"

static NSString *RCTCurrentAppState()
{
  static NSDictionary *states;
  static dispatch_once_t onceToken;
  dispatch_once(&onceToken, ^{
    states = @{
      @(UIApplicationStateActive) : @"active",
      @(UIApplicationStateBackground) : @"background",
      @(UIApplicationStateInactive) : @"inactive"
    };
  });

  if (RCTRunningInAppExtension()) {
    return @"extension";
  }

  return states[@(RCTSharedApplication().applicationState)] ?: @"unknown";
}

@interface RCTAppState () <NativeAppStateSpec>
@end

@implementation RCTAppState {
  NSString *_lastKnownState;
}

RCT_EXPORT_MODULE()

+ (BOOL)requiresMainQueueSetup
{
  return YES;
}

- (dispatch_queue_t)methodQueue
{
  return dispatch_get_main_queue();
}

- (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)constantsToExport
{
  return (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)[self getConstants];
}

- (facebook::react::ModuleConstants<JS::NativeAppState::Constants>)getConstants
{
  __block facebook::react::ModuleConstants<JS::NativeAppState::Constants> constants;
  RCTUnsafeExecuteOnMainQueueSync(^{
    constants = facebook::react::typedConstants<JS::NativeAppState::Constants>({
        .initialAppState = RCTCurrentAppState(),
    });
  });

  return constants;
}

#pragma mark - Lifecycle

- (NSArray<NSString *> *)supportedEvents
{
  return @[ @"appStateDidChange", @"memoryWarning" ];
}

- (void)startObserving
{
  for (NSString *name in @[
         UIApplicationDidBecomeActiveNotification,
         UIApplicationDidEnterBackgroundNotification,
         UIApplicationDidFinishLaunchingNotification,
         UIApplicationWillResignActiveNotification,
         UIApplicationWillEnterForegroundNotification
       ]) {
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(handleAppStateDidChange:)
                                                 name:name
                                               object:nil];
  }

  [[NSNotificationCenter defaultCenter] addObserver:self
                                           selector:@selector(handleMemoryWarning)
                                               name:UIApplicationDidReceiveMemoryWarningNotification
                                             object:nil];
}

- (void)stopObserving
{
  [[NSNotificationCenter defaultCenter] removeObserver:self];
}

#pragma mark - App Notification Methods

- (void)handleMemoryWarning
{
  [self sendEventWithName:@"memoryWarning" body:nil];
}

- (void)handleAppStateDidChange:(NSNotification *)notification
{
  NSString *newState;

  if ([notification.name isEqualToString:UIApplicationWillResignActiveNotification]) {
    newState = @"inactive";
  } else if ([notification.name isEqualToString:UIApplicationWillEnterForegroundNotification]) {
    newState = @"background";
  } else {
    newState = RCTCurrentAppState();
  }

  if (![newState isEqualToString:_lastKnownState]) {
    _lastKnownState = newState;
    [self sendEventWithName:@"appStateDidChange" body:@{@"app_state" : _lastKnownState}];
  }
}

#pragma mark - Public API

/**
 * Get the current background/foreground state of the app
 */
RCT_EXPORT_METHOD(getCurrentAppState : (RCTResponseSenderBlock)callback error : (__unused RCTResponseSenderBlock)error)
{
  callback(@[ @{@"app_state" : RCTCurrentAppState()} ]);
}

- (std::shared_ptr<facebook::react::TurboModule>)getTurboModule:
    (const facebook::react::ObjCTurboModule::InitParams &)params
{
  return std::make_shared<facebook::react::NativeAppStateSpecJSI>(params);
}

@end

Class RCTAppStateCls(void)
{
  return RCTAppState.class;
}

Directory Contents

Dirs: 1 × Files: 57
Name Size Perms Modified Actions
- drwxr-xr-x 2025-03-28 11:04:43
Edit Download
2.08 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.79 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
563 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.40 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
17.27 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
313 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
11.61 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
354 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.34 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
560 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
7.79 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
747 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.75 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
273 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.97 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
285 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.10 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
305 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
10.95 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
360 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
9.44 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.81 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
19.67 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.39 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
18.50 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
287 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.66 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
557 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
7.44 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.87 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
6.37 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
618 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.05 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
418 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
1.31 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
281 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
4.42 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
355 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
3.02 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
665 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.39 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
15.07 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
319 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
2.71 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
2.16 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
27.56 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
321 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
988 B lrw-r--r-- 2025-03-28 11:04:43
Edit Download
481 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
6.00 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.03 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
12.10 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
448 B lrw-r--r-- 2025-03-28 11:04:41
Edit Download
9.24 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
1.07 KB lrw-r--r-- 2025-03-28 11:04:41
Edit Download
6.25 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
3.01 KB lrw-r--r-- 2025-03-28 11:04:43
Edit Download
If ZipArchive is unavailable, a .tar will be created (no compression).