// Author: fengshengxiong
// Date: 2024/6/26
// Description: firebase_crashlytics管理

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:flutter/foundation.dart';
import 'package:tone_snap/utils/log_util.dart';

class FirebaseCrashlyticsManager {
  static Future<void> setEnabled() async {
    await FirebaseCrashlytics.instance.setCrashlyticsCollectionEnabled(kReleaseMode);
  }

  static void recordFlutterError() {
    // 非异步错误
    FlutterError.onError = (errorDetails) {
      // 插件错误 flutter_cache_manager
      // https://github.com/Baseflow/flutter_cache_manager/issues/460
      if (errorDetails.exceptionAsString().contains('No host specified in URI')) {
        return;
      }
      LogUtil.e(errorDetails.exception);
      FirebaseCrashlytics.instance.recordFlutterError(errorDetails);
    };
  }

  static void recordError() {
    // 异步错误
    PlatformDispatcher.instance.onError = (error, stack) {
      LogUtil.e(error);
      FirebaseCrashlytics.instance.recordError(error, stack);
      return true;
    };
  }
}
