setting_cubit.dart 3.28 KB
import 'dart:io';

import 'package:appframe/config/constant.dart';
import 'package:appframe/config/locator.dart';
import 'package:appframe/config/routes.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:fluwx/fluwx.dart';
import 'package:path_provider/path_provider.dart';
import 'package:shared_preferences/shared_preferences.dart';

class SettingState extends Equatable {
  final String h5Version;

  const SettingState({
    this.h5Version = '',
  });

  SettingState copyWith({
    String? h5Version,
  }) {
    return SettingState(
      h5Version: h5Version ?? this.h5Version,
    );
  }

  @override
  List<Object?> get props => [h5Version];
}

class SettingCubit extends Cubit<SettingState> {
  final SharedPreferences _prefs = getIt.get<SharedPreferences>();
  final Fluwx _fluwx = getIt.get<Fluwx>();

  SettingCubit() : super(const SettingState()) {
    _init();
  }

  void _init() {
    _readH5ShowVersion();
  }

  void _readH5ShowVersion() {
    var h5Version = _prefs.getString(Constant.h5ShowVersionKey) ?? 'unknown';
    emit(state.copyWith(h5Version: h5Version));
  }

  /// 跳转客服(微信小程序)
  void goCs() {
    _fluwx.open(
      target: MiniProgram(
        username: 'gh_0ed02e873abc',
        path: '/pages/agentChat/index?showAuthDirectly=1&agentId=eiXH0MAJmjgl',
        miniProgramType: WXMiniProgramType.release,
      ),
    );
  }

  /// 退出登录
  Future<void> logout() async {
    // 删除所有auth_开头的key
    _prefs.getKeys().forEach((key) {
      if (key.startsWith('auth_')) {
        _prefs.remove(key);
      }
    });

    // IM 登出
    // await getIt.get<ImService>().logout();

    router.go('/loginMain');
  }

  /// 切换日志模式
  Future<void> handleToggleDebug() async {
    var debug = _prefs.getInt('debug') ?? 0;
    debug = (debug == 0 ? 1 : 0);
    _prefs.setInt('debug', debug);

    // 通知 WebCubit 更新 H5 状态
    WebCubitHolder.instance?.notifyDebugStatus(debug);
  }

  /// 清理缓存
  Future<void> clearStorage() async {
    // 1 清理 WebView 相关缓存
    WebCubitHolder.instance?.clearWebCache();

    // 2 清理非 h5_version 的缓存
    _prefs.getKeys().forEach((key) async {
      if (!key.startsWith('h5')) {
        await _prefs.remove(key);
      }
    });

    // 3 清理 http_dist_assets 下的非当前版本号的文件和目录
    var dir = await getApplicationSupportDirectory();
    var httpDir = Directory('${dir.path}/${Constant.h5DistDir}');
    if (httpDir.existsSync()) {
      var version = _prefs.getString(Constant.h5VersionKey) ?? Constant.h5Version;

      await for (final entity in httpDir.list()) {
        if (entity is Directory) {
          if (!entity.path.endsWith(version)) {
            await entity.delete(recursive: true);
          }
        } else if (entity is File) {
          if (!entity.path.endsWith('$version.zip')) {
            await entity.delete();
          }
        }
      }
    }

    // 4 清理临时目录下的所有文件和目录
    var tempDir = await getTemporaryDirectory();
    if (tempDir.existsSync()) {
      await for (final entity in tempDir.list()) {
        if (entity is Directory) {
          await entity.delete(recursive: true);
        } else {
          await entity.delete();
        }
      }
    }
  }
}