configure static method

Future<void> configure({
  1. required String siteId,
  2. required String appKey,
  3. Uri? baseUrl,
  4. HatangLocale? locale,
  5. HatangChatThemeMode themeMode = HatangChatThemeMode.site,
  6. HatangChatTheme? theme,
  7. HatangChatTheme? darkTheme,
  8. Map<HatangLocale, HatangChatStrings>? strings,
  9. GlobalKey<NavigatorState>? navigatorKey,
  10. HatangLogger? logger,
  11. HatangAppInfo? appInfo,
  12. bool autoConnect = true,
  13. bool openOnProactiveMessage = true,
  14. HatangCommerce commerce = HatangCommerce.standard,
  15. HatangFilePick? pickFile,
  16. @visibleForTesting Client? httpClient,
  17. @visibleForTesting SocketConnector? connector,
  18. @visibleForTesting KeyValueStore? secureStore,
  19. @visibleForTesting KeyValueStore? prefsStore,
})

Configures the SDK for siteId with the site app key appKey (hak_…, from the website's Mobile apps tab in the dashboard). Calling it again replaces the previous instance.

baseUrl defaults to the production gateway. locale forces a language (otherwise: the stored choice, the site default, the device). themeMode, theme and darkTheme control colours; strings overrides wording per locale. Pass a navigatorKey so handleNotification and proactive conversations can open the chat without a context. With autoConnect (default) the SDK bootstraps and connects at once.

Implementation

static Future<void> configure({
  required String siteId,
  required String appKey,
  Uri? baseUrl,
  HatangLocale? locale,
  HatangChatThemeMode themeMode = HatangChatThemeMode.site,
  HatangChatTheme? theme,
  HatangChatTheme? darkTheme,
  Map<HatangLocale, HatangChatStrings>? strings,
  GlobalKey<NavigatorState>? navigatorKey,
  HatangLogger? logger,
  HatangAppInfo? appInfo,
  bool autoConnect = true,
  bool openOnProactiveMessage = true,
  HatangCommerce commerce = HatangCommerce.standard,
  HatangFilePick? pickFile,
  @visibleForTesting http.Client? httpClient,
  @visibleForTesting SocketConnector? connector,
  @visibleForTesting KeyValueStore? secureStore,
  @visibleForTesting KeyValueStore? prefsStore,
}) async {
  if (siteId.isEmpty) throw ArgumentError.value(siteId, 'siteId', 'must not be empty');
  if (appKey.isEmpty) throw ArgumentError.value(appKey, 'appKey', 'must not be empty');
  _instance?.dispose();
  final info = appInfo ?? await loadAppInfo();
  final prefs = prefsStore ?? PrefsStore();
  final secure = secureStore ?? SecureStore(fallback: prefs);
  final engine = ChatEngine(
    api: WidgetApi(
      baseUrl: baseUrl ?? hatangDefaultBaseUrl,
      siteId: siteId,
      appKey: appKey,
      userAgent: info.userAgent,
      client: httpClient,
    ),
    connector: connector ?? connectWebSocket,
    store: SessionStore(siteId: siteId, secure: secure, prefs: prefs),
    appInfo: info,
    logger: logger,
    preferredLocale: locale?.code,
    openOnProactiveMessage: openOnProactiveMessage,
  );
  final sdk = HatangChat._(
    engine: engine,
    siteId: siteId,
    themeMode: themeMode,
    theme: theme,
    darkTheme: darkTheme,
    strings: strings,
    navigatorKey: navigatorKey,
    commerce: commerce,
    pickFile: pickFile,
    lifecycle: HatangLifecycleObserver(engine),
  );
  _instance = sdk;
  sdk._lifecycle?.attach();
  if (autoConnect) await engine.start();
}