Skip to content

Privacy-first password manager with built-in email aliasing. Fully encrypted and self-hostable.

5.3 KB
Raw Blame
import UIKit
import React
import React_RCTAppDelegate
import ReactAppDependencyProvider
internal import Expo
internal import ExpoScreenOrientation
import VaultStoreKit

@main
class AppDelegate: UIResponder, UIApplicationDelegate {
  var window: UIWindow?
  private(set) var launchOptions: [UIApplication.LaunchOptionsKey: Any]?
  private var reactNativeDelegate: ReactNativeDelegate?
  private(set) var reactNativeFactory: ExpoReactNativeFactory?

  func application(
    _ application: UIApplication,
    didFinishLaunchingWithOptions launchOptions: [UIApplication.LaunchOptionsKey: Any]? = nil
  ) -> Bool {
    // Handle UI test launch arguments
    #if DEBUG
    if ProcessInfo.processInfo.arguments.contains("--reset-state") {
      resetAppState()
    }
    // Disable UIView animations for UI tests to prevent "wait for app to idle" issues
    // React Native apps have continuous JS bridge activity that prevents XCTest idle detection
    if ProcessInfo.processInfo.arguments.contains("--uitest") {
      UIView.setAnimationsEnabled(false)
    }
    #endif

    let delegate = ReactNativeDelegate()
    let factory = ExpoReactNativeFactory(delegate: delegate)
    delegate.dependencyProvider = RCTAppDependencyProvider()

    reactNativeDelegate = delegate
    reactNativeFactory = factory
    self.launchOptions = launchOptions

    // The window is created and React Native is started in SceneDelegate (UIScene lifecycle).
    return true
  }

  // MARK: - Orientation

  /// Caps every screen to the expo-screen-orientation lock (portrait unless a screen unlocks it); iPad keeps all orientations.
  func application(_ application: UIApplication, supportedInterfaceOrientationsFor window: UIWindow?) -> UIInterfaceOrientationMask {
    if UIDevice.current.userInterfaceIdiom == .pad { return .all }
    let mask = ScreenOrientationRegistry.shared.currentOrientationMask
    return mask.isEmpty ? .portrait : mask
  }

  // MARK: - UIScene

  func application(
    _ application: UIApplication,
    configurationForConnecting connectingSceneSession: UISceneSession,
    options: UIScene.ConnectionOptions
  ) -> UISceneConfiguration {
    let configuration = UISceneConfiguration(name: "Default Configuration", sessionRole: connectingSceneSession.role)
    configuration.delegateClass = SceneDelegate.self
    return configuration
  }

  // MARK: - Test Support

  #if DEBUG
  /// Reset all app state for UI tests
  private func resetAppState() {
    print("[UITest] Resetting app state...")

    // 1. Clear VaultStore data (Keychain, files, UserDefaults)
    let vaultStore = VaultStore.shared
    do {
      try vaultStore.clearVault()
      print("[UITest] Cleared VaultStore")
    } catch {
      print("[UITest] Failed to clear VaultStore: \(error)")
    }

    // 2. Clear AsyncStorage data (React Native's persistent storage)
    // AsyncStorage uses the app's Documents directory with RCTAsyncLocalStorage
    clearAsyncStorage()

    // 3. Clear any additional UserDefaults that might not be in VaultStore
    clearAdditionalUserDefaults()

    print("[UITest] App state reset complete")
  }

  /// Clear React Native AsyncStorage
  private func clearAsyncStorage() {
    // AsyncStorage stores data in Documents/RCTAsyncLocalStorage_V1
    let fileManager = FileManager.default
    guard let documentsPath = fileManager.urls(for: .documentDirectory, in: .userDomainMask).first else {
      return
    }

    let asyncStoragePath = documentsPath.appendingPathComponent("RCTAsyncLocalStorage_V1")

    if fileManager.fileExists(atPath: asyncStoragePath.path) {
      do {
        try fileManager.removeItem(at: asyncStoragePath)
        print("[UITest] Cleared AsyncStorage directory")
      } catch {
        print("[UITest] Failed to clear AsyncStorage: \(error)")
      }
    }
  }

  /// Clear additional UserDefaults entries
  private func clearAdditionalUserDefaults() {
    let defaults = UserDefaults.standard

    // Clear any React Native related keys
    let keysToRemove = defaults.dictionaryRepresentation().keys.filter { key in
      key.hasPrefix("RCT") || key.hasPrefix("react") || key.hasPrefix("expo")
    }

    for key in keysToRemove {
      defaults.removeObject(forKey: key)
    }

    defaults.synchronize()
    print("[UITest] Cleared additional UserDefaults (\(keysToRemove.count) keys)")
  }
  #endif
  
  // MARK: - Remote Notifications
  
  func application(_ application: UIApplication, didRegisterForRemoteNotificationsWithDeviceToken deviceToken: Data) {
    // Handle device token if needed
  }
  
  func application(_ application: UIApplication, didFailToRegisterForRemoteNotificationsWithError error: Error) {
    // Handle registration failure if needed
  }
  
  func application(_ application: UIApplication, didReceiveRemoteNotification userInfo: [AnyHashable : Any], fetchCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
    // Handle remote notification if needed
    completionHandler(.noData)
  }
}

private class ReactNativeDelegate: ExpoReactNativeFactoryDelegate {
  override func sourceURL(for bridge: RCTBridge) -> URL? {
    self.bundleURL()
  }
  
  override func bundleURL() -> URL? {
#if DEBUG
    RCTBundleURLProvider.sharedSettings().jsBundleURL(forBundleRoot: ".expo/.virtual-metro-entry")
#else
    Bundle.main.url(forResource: "main", withExtension: "jsbundle")
#endif
  }
}