init static method

void init({
  1. bool isDebug = false,
  2. String? baseUrl,
  3. Dio? dio,
  4. UpdateApiParser? parser,
  5. ErrorHandler? errorHandler,
  6. UpdateDialogBuilder? dialogBuilder,
  7. ForcedUpdatePageBuilder? forcedUpdatePageBuilder,
  8. OssConfig? ossConfig,
  9. UpgraderCallback? onEvent,
  10. bool enableLog = false,
  11. @Deprecated('Use maxValidationRetryCount instead') int maxRetryCount = 3,
  12. int? maxValidationRetryCount,
  13. int maxNetworkRetryCount = 2,
  14. int minFreeSpaceMarginBytes = 64 * 1024 * 1024,
  15. bool autoStartForcedDownload = false,
  16. bool barrierDismissible = true,
})

初始化配置,应在 App 启动时调用一次。

onEvent:统一的升级事件回调,所有流程事件(检测、下载、校验、 安装、清理)均通过此回调输出。调用方可根据 UpgraderEvent.type 自行分发业务逻辑。

enableLog:是否发送通用日志事件,默认为 false。 检查、下载和安装等业务事件不受此开关影响。

Implementation

static void init({
  bool isDebug = false,
  String? baseUrl,
  Dio? dio,
  UpdateApiParser? parser,
  ErrorHandler? errorHandler,
  UpdateDialogBuilder? dialogBuilder,
  ForcedUpdatePageBuilder? forcedUpdatePageBuilder,
  OssConfig? ossConfig,
  UpgraderCallback? onEvent,
  bool enableLog = false,
  @Deprecated('Use maxValidationRetryCount instead') int maxRetryCount = 3,
  int? maxValidationRetryCount,
  int maxNetworkRetryCount = 2,
  int minFreeSpaceMarginBytes = 64 * 1024 * 1024,
  bool autoStartForcedDownload = false,
  bool barrierDismissible = true,
}) {
  final validationRetryCount = maxValidationRetryCount ?? maxRetryCount;
  if (validationRetryCount < 0) {
    throw ArgumentError.value(
      validationRetryCount,
      'maxValidationRetryCount',
      'must not be negative',
    );
  }
  if (maxNetworkRetryCount < 0) {
    throw ArgumentError.value(
      maxNetworkRetryCount,
      'maxNetworkRetryCount',
      'must not be negative',
    );
  }
  if (minFreeSpaceMarginBytes < 0) {
    throw ArgumentError.value(
      minFreeSpaceMarginBytes,
      'minFreeSpaceMarginBytes',
      'must not be negative',
    );
  }
  final inst = instance;
  inst._isDebugging = isDebug;
  if (parser != null) inst._parser = parser;
  inst._errorHandler = errorHandler;
  inst._forcedUpdatePageBuilder = forcedUpdatePageBuilder;
  inst._ossConfig = ossConfig;
  inst._onEvent = onEvent;
  inst._enableLog = enableLog;
  inst._maxValidationRetryCount = validationRetryCount;
  inst._maxNetworkRetryCount = maxNetworkRetryCount;
  inst._minFreeSpaceMarginBytes = minFreeSpaceMarginBytes;
  inst._autoStartForcedDownload = autoStartForcedDownload;
  inst._barrierDismissible = barrierDismissible;

  inst._dialogBuilder =
      dialogBuilder ??
      (context, updateInfo, statusNotifier, progressNotifier) =>
          MyUpdateDialog(
            updateInfo: updateInfo,
            statusNotifier: statusNotifier,
            progressNotifier: progressNotifier,
          );

  if (dio != null) {
    inst._dio = dio;
  } else {
    inst._dio = Dio(
      BaseOptions(
        baseUrl: baseUrl ?? '',
        connectTimeout: const Duration(seconds: 15),
        receiveTimeout: const Duration(minutes: 30),
      ),
    );

    if (isDebug) {
      inst._dio.interceptors.add(
        InterceptorsWrapper(
          onResponse: (response, handler) {
            debugPrint('[TinyUpgrader] 响应: ${response.statusCode}');
            return handler.next(response);
          },
          onError: (error, handler) {
            debugPrint('[TinyUpgrader] 请求错误: ${error.message}');
            return handler.next(error);
          },
        ),
      );
    }
  }

  inst._initialized = true;
  inst._bindForegroundDownloadEvents();
  inst._emit(UpgraderEventType.init, 'TinyUpgrader 初始化完成', {
    'isDebug': isDebug,
    'enableLog': enableLog,
    'maxValidationRetryCount': inst._maxValidationRetryCount,
    'maxNetworkRetryCount': maxNetworkRetryCount,
    'minFreeSpaceMarginBytes': minFreeSpaceMarginBytes,
    'autoStartForcedDownload': autoStartForcedDownload,
    'barrierDismissible': barrierDismissible,
    'hasOssConfig': ossConfig != null,
    'hasCustomParser': parser != null,
    'hasCustomDialog': dialogBuilder != null,
    'hasForcedPage': forcedUpdatePageBuilder != null,
  });
}