setting_cubit.dart
3.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
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;
final bool wechatInstalled;
const SettingState({
this.h5Version = '',
this.wechatInstalled = false,
});
SettingState copyWith({
String? h5Version,
bool? wechatInstalled,
}) {
return SettingState(
h5Version: h5Version ?? this.h5Version,
wechatInstalled: wechatInstalled ?? this.wechatInstalled,
);
}
@override
List<Object?> get props => [h5Version, wechatInstalled];
}
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();
_checkWechat();
}
void _readH5ShowVersion() {
var h5Version = _prefs.getString(Constant.h5ShowVersionKey) ?? 'unknown';
emit(state.copyWith(h5Version: h5Version));
}
// 微信安装检测
void _checkWechat() {
_fluwx.isWeChatInstalled.then((value) {
emit(state.copyWith(wechatInstalled: value));
});
}
/// 跳转客服(微信小程序)
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();
}
}
}
}
}