configure method

Future<void> configure({
  1. Widget? loader,
  2. List<BaseThemeConfig>? themes,
  3. String? initialThemeId,
  4. Map<String, ToastStyleFactory>? toastNotifications,
  5. Map<Type, dynamic>? modelDecoders,
  6. Map<Type, dynamic>? controllers,
  7. Map<Type, dynamic>? apiDecoders,
  8. Map<Type, NyEvent>? events,
  9. Map<String, dynamic>? formCasts,
  10. String? authKey,
  11. dynamic syncKeys,
  12. bool useErrorStack = false,
  13. ErrorStackLogLevel? errorStackLevel,
  14. Widget errorStackWidget(
    1. FlutterErrorDetails
    )?,
  15. bool monitorAppUsage = false,
  16. bool showDateTimeInLogs = false,
  17. bool broadcastEvents = false,
  18. NyLogCallback? onLog,
  19. NyLocalizationConfig? localization,
})

Configure Nylo with all settings in a single call.

This method provides a cleaner way to initialize Nylo by consolidating all configuration options into a single method call.

Example:

await nylo.configure(
  loader: DesignConfig.loader,
  logo: DesignConfig.logo,
  themes: appThemes,
  initialThemeId: 'light_theme',
  toastNotifications: ToastNotificationConfig.styles,
  modelDecoders: modelDecoders,
  controllers: controllers,
  apiDecoders: apiDecoders,
  authKey: StorageKeysConfig.auth,
  syncKeys: StorageKeysConfig.syncedOnBoot,
  useErrorStack: true,
  monitorAppUsage: true,
  localization: LocalizationConfig(
    localeType: localeType,
    languageCode: 'en',
    assetsDirectory: 'lang/',
  ),
);

Implementation

Future<void> configure({
  // UI
  Widget? loader,
  Widget? logo,

  // Themes
  List<BaseThemeConfig>? themes,
  String? initialThemeId,

  // Notifications
  Map<String, ToastStyleFactory>? toastNotifications,

  // Decoders & Controllers
  Map<Type, dynamic>? modelDecoders,
  Map<Type, dynamic>? controllers,
  Map<Type, dynamic>? apiDecoders,

  // Events
  Map<Type, NyEvent>? events,
  Map<String, dynamic>? formCasts,

  // Auth & Storage
  String? authKey,
  dynamic syncKeys,

  // Features
  bool useErrorStack = false,
  ErrorStackLogLevel? errorStackLevel,
  Widget Function(FlutterErrorDetails)? errorStackWidget,
  bool monitorAppUsage = false,
  bool showDateTimeInLogs = false,
  bool broadcastEvents = false,
  NyLogCallback? onLog,

  // Localization
  NyLocalizationConfig? localization,
}) async {
  // Localization
  if (localization != null) {
    await NyLocalization.instance.init(
      localeType: localization.localeType,
      languageCode: localization.languageCode,
      assetsDirectory: localization.assetsDirectory,
    );
  }

  // UI
  if (loader != null) addLoader(loader);
  if (logo != null) addLogo(logo);

  // Themes
  if (themes != null) {
    NyThemeManager.instance.registerThemes(
      themes,
      initialThemeId: initialThemeId,
    );
  }

  // Notifications
  if (toastNotifications != null) {
    addToastNotifications(toastNotifications);
  }

  // Decoders & Controllers
  if (modelDecoders != null) addModelDecoders(modelDecoders);
  if (controllers != null) addControllers(controllers);
  if (apiDecoders != null) addApiDecoders(apiDecoders);

  // Events
  if (events != null) addEvents(events);
  if (formCasts != null) addFormCasts(formCasts);

  // Auth & Storage
  if (authKey != null) addAuthKey(authKey);
  if (syncKeys != null) await this.syncKeys(syncKeys);

  // Features
  if (useErrorStack) {
    this.useErrorStack(
      level: errorStackLevel ?? ErrorStackLogLevel.verbose,
      errorWidget: errorStackWidget,
    );

    // Auto-wire NyLogger to DevPanelStore
    NyLogger.onLog = (entry) {
      // Forward to DevPanelStore
      switch (entry.type) {
        case 'debug':
          DevPanelStore.instance.debug(entry.message);
          break;
        case 'info':
          DevPanelStore.instance.info(entry.message);
          break;
        case 'error':
          DevPanelStore.instance.error(entry.message);
          break;
        case 'warning':
          DevPanelStore.instance.warning(entry.message);
          break;
        default:
          DevPanelStore.instance.debug(entry.message);
      }
      // Also call user's custom callback if provided
      onLog?.call(entry);
    };
  } else if (onLog != null) {
    // No ErrorStack, just use custom callback
    NyLogger.onLog = onLog;
  }
  if (monitorAppUsage) this.monitorAppUsage();
  if (showDateTimeInLogs) this.showDateTimeInLogs();
  if (broadcastEvents) this.broadcastEvents(true);
}