home_controller.dart 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. import 'package:get/get.dart';
  2. import 'package:nomo/app/controllers/api_controller.dart';
  3. import 'package:nomo/utils/misc.dart';
  4. import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';
  5. import '../../../../utils/system_helper.dart';
  6. import '../../../controllers/base_core_api.dart';
  7. import '../../../../utils/awesome_notifications_helper.dart';
  8. import '../../../../utils/log/logger.dart';
  9. import '../../../base/base_controller.dart';
  10. import '../../../constants/enums.dart';
  11. import '../../../controllers/core_controller.dart';
  12. import '../../../data/models/banner/banner_list.dart';
  13. import '../../../data/models/launch/groups.dart';
  14. import '../../../data/sp/ix_sp.dart';
  15. import '../../../dialog/error_dialog.dart';
  16. import '../../../routes/app_pages.dart';
  17. /// 主页控制器
  18. class HomeController extends BaseController {
  19. final coreController = Get.find<CoreController>();
  20. final apiController = Get.find<ApiController>();
  21. final TAG = 'HomeController';
  22. final _refreshController = RefreshController(initialRefresh: false);
  23. RefreshController get refreshController => _refreshController;
  24. final _currentBannerIndex = 0.obs;
  25. int get currentBannerIndex => _currentBannerIndex.value;
  26. set currentBannerIndex(int value) => _currentBannerIndex.value = value;
  27. // 最近位置是否展开
  28. final _isRecentLocationsExpanded = false.obs;
  29. bool get isRecentLocationsExpanded => _isRecentLocationsExpanded.value;
  30. set isRecentLocationsExpanded(bool value) =>
  31. _isRecentLocationsExpanded.value = value;
  32. /// 收起最近位置列表
  33. void collapseRecentLocations() {
  34. if (_isRecentLocationsExpanded.value) {
  35. _isRecentLocationsExpanded.value = false;
  36. }
  37. }
  38. // 统计信息
  39. final _uplinkBytes = 0.obs;
  40. final _downlinkBytes = 0.obs;
  41. int get uplinkBytes => _uplinkBytes.value;
  42. int get downlinkBytes => _downlinkBytes.value;
  43. // 延迟信息
  44. final _currentDelay = 0.obs;
  45. int get currentDelay => _currentDelay.value;
  46. // 当前选择的位置
  47. final _selectedLocation = Rxn<Locations>();
  48. Locations? get selectedLocation => _selectedLocation.value;
  49. set selectedLocation(Locations? value) => _selectedLocation.value = value;
  50. // 最近使用的位置列表
  51. final _recentLocations = <Locations>[].obs;
  52. List<Locations> get recentLocations =>
  53. _recentLocations.where((loc) => loc.id != selectedLocation?.id).toList();
  54. // Banner 列表
  55. final _bannerList = <Banner>[].obs;
  56. List<Banner> get bannerList => _bannerList;
  57. set bannerList(List<Banner> value) => _bannerList.assignAll(value);
  58. // Banner 列表
  59. final _nineBannerList = <Banner>[].obs;
  60. List<Banner> get nineBannerList => _nineBannerList;
  61. set nineBannerList(List<Banner> value) => _nineBannerList.assignAll(value);
  62. @override
  63. void onInit() {
  64. super.onInit();
  65. _initializeLocations();
  66. getBanner(position: 'nine');
  67. getBanner(position: 'banner');
  68. // 桌面模式不需要应用内通知
  69. if (!isDesktop) {
  70. // 延迟100ms后初始化通知
  71. // Future.delayed(const Duration(milliseconds: 100), () {
  72. // AwesomeNotificationsHelper.init();
  73. // AwesomeNotificationsHelper.showPushNoticeDialog();
  74. // });
  75. }
  76. checkUpdate();
  77. }
  78. Future<void> checkUpdate() async {
  79. final isVpnRunning = await BaseCoreApi().isConnected() ?? false;
  80. if (!isVpnRunning) {
  81. await apiController.checkUpdate();
  82. }
  83. }
  84. /// 初始化位置数据
  85. void _initializeLocations() {
  86. // 从 SharedPreferences 加载保存的节点数据
  87. _loadSavedLocations();
  88. }
  89. /// 从 SharedPreferences 加载保存的节点数据
  90. void _loadSavedLocations() {
  91. try {
  92. // 加载当前选中的节点
  93. final selectedLocationData = IXSP.getSelectedLocation();
  94. if (selectedLocationData != null) {
  95. final savedLocation = Locations.fromJson(selectedLocationData);
  96. // 检查保存的节点是否存在于当前 groups 中
  97. if (_isLocationExistsInGroups(savedLocation)) {
  98. selectedLocation = savedLocation;
  99. } else {
  100. // 如果节点不存在于 groups 中,选中第一个可用节点
  101. // 如果当前节点是连接中的状态,则断开连接
  102. if (coreController.state != ConnectionState.disconnected) {
  103. BaseCoreApi().disconnect();
  104. }
  105. log(
  106. TAG,
  107. 'Saved location not found in groups, selecting first available',
  108. );
  109. _selectFirstAvailableLocation();
  110. }
  111. } else {
  112. // 如果没有保存的节点,选中第一个可用节点
  113. _selectFirstAvailableLocation();
  114. }
  115. // 加载最近选择的节点列表
  116. final recentLocationsData = IXSP.getRecentLocations();
  117. if (recentLocationsData.isNotEmpty) {
  118. _recentLocations.assignAll(
  119. recentLocationsData.map((e) => Locations.fromJson(e)).toList(),
  120. );
  121. }
  122. } catch (e) {
  123. log(TAG, 'Error loading saved locations: $e');
  124. }
  125. }
  126. /// 检查节点是否存在于当前 groups 中
  127. bool _isLocationExistsInGroups(Locations location) {
  128. final launch = IXSP.getLaunch();
  129. final groups = launch?.groups;
  130. if (groups == null) return false;
  131. // 检查 normal 列表
  132. if (groups.normal?.list != null) {
  133. for (var locationList in groups.normal!.list!) {
  134. if (locationList.locations != null) {
  135. for (var loc in locationList.locations!) {
  136. if (loc.id == location.id) {
  137. return true;
  138. }
  139. }
  140. }
  141. }
  142. }
  143. // 检查 streaming 列表
  144. if (groups.streaming?.list != null) {
  145. for (var locationList in groups.streaming!.list!) {
  146. if (locationList.locations != null) {
  147. for (var loc in locationList.locations!) {
  148. if (loc.id == location.id) {
  149. return true;
  150. }
  151. }
  152. }
  153. }
  154. }
  155. return false;
  156. }
  157. /// 选中第一个可用节点
  158. void _selectFirstAvailableLocation() {
  159. try {
  160. final launch = IXSP.getLaunch();
  161. final normalList = launch?.groups?.normal?.list;
  162. if (normalList != null && normalList.isNotEmpty) {
  163. // 遍历找到第一个有可用节点的国家
  164. for (var locationList in normalList) {
  165. if (locationList.locations != null &&
  166. locationList.locations!.isNotEmpty) {
  167. // 选中第一个节点
  168. final firstLocation = locationList.locations!.first;
  169. selectLocation(firstLocation);
  170. break;
  171. }
  172. }
  173. }
  174. } catch (e) {
  175. log(TAG, 'Error selecting first available location: $e');
  176. }
  177. }
  178. /// 选择位置
  179. void selectLocation(
  180. Locations location, {
  181. String locationSelectionType = 'auto',
  182. }) {
  183. selectedLocation = location;
  184. coreController.locationSelectionType = locationSelectionType;
  185. // 更新最近使用列表
  186. _updateRecentLocations(location);
  187. // 保存到 SharedPreferences
  188. _saveLocationsToStorage();
  189. }
  190. // 从节点列表中选择节点后需要延迟300ms
  191. void handleConnect({bool delay = false}) {
  192. if (delay) {
  193. // 延迟300ms
  194. Future.delayed(const Duration(milliseconds: 300), () {
  195. coreController.selectLocationConnect();
  196. });
  197. } else {
  198. coreController.selectLocationConnect();
  199. }
  200. }
  201. /// 更新最近使用的位置列表
  202. void _updateRecentLocations(Locations location) {
  203. // 移除已存在的位置
  204. _recentLocations.removeWhere((loc) => loc.id == location.id);
  205. // 添加到列表开头
  206. _recentLocations.insert(0, location);
  207. // 保持最多4个最近使用的位置(过滤掉当前选中后能显示3个)
  208. if (_recentLocations.length > 4) {
  209. _recentLocations.removeRange(4, _recentLocations.length);
  210. }
  211. }
  212. /// 保存位置数据到 SharedPreferences
  213. void _saveLocationsToStorage() {
  214. try {
  215. // 保存当前选中的节点
  216. if (selectedLocation != null) {
  217. IXSP.saveSelectedLocation(selectedLocation!.toJson());
  218. }
  219. // 保存最近选择的节点列表
  220. final recentLocationsJson = _recentLocations
  221. .map((e) => e.toJson())
  222. .toList();
  223. IXSP.saveRecentLocations(recentLocationsJson);
  224. } catch (e) {
  225. log(TAG, 'Error saving locations to storage: $e');
  226. }
  227. }
  228. void onRefresh() async {
  229. try {
  230. await apiController.refreshLaunch();
  231. getBanner(position: 'nine', isCache: false);
  232. getBanner(position: 'banner', isCache: false);
  233. refreshController.refreshCompleted();
  234. } catch (e) {
  235. refreshController.refreshFailed();
  236. }
  237. }
  238. /// 当 Launch 数据更新时刷新节点
  239. void refreshOnLaunchChanged() {
  240. log(TAG, 'Launch data changed, refreshing locations');
  241. _loadSavedLocations();
  242. }
  243. // 设置默认auto连接
  244. void setDefaultAutoConnect() {
  245. coreController.locationSelectionType = 'auto';
  246. coreController.handleConnection();
  247. }
  248. /// 获取 banner 列表
  249. /// [position] banner 位置类型,如 "banner"、"media"、"nine" 等
  250. Future<void> getBanner({
  251. String position = 'banner',
  252. bool isCache = true,
  253. }) async {
  254. try {
  255. // 先读取缓存数据
  256. final cacheBanners = IXSP.getBanner(position);
  257. if (cacheBanners != null &&
  258. cacheBanners.list != null &&
  259. cacheBanners.list!.isNotEmpty &&
  260. isCache) {
  261. if (position == 'banner') {
  262. bannerList = cacheBanners.list!;
  263. } else if (position == 'nine') {
  264. nineBannerList = cacheBanners.list!;
  265. }
  266. log(TAG, 'Loaded banner from cache for position: $position');
  267. }
  268. // 请求最新数据
  269. final banners = await apiController.getBanner(position: position);
  270. if (banners.list != null) {
  271. if (position == 'banner') {
  272. bannerList = banners.list!;
  273. } else if (position == 'nine') {
  274. nineBannerList = banners.list!;
  275. }
  276. // 保存到缓存
  277. await IXSP.saveBanner(position, banners);
  278. log(TAG, 'Banner updated and cached for position: $position');
  279. }
  280. } catch (e) {
  281. log(TAG, 'Error loading banners for position $position: $e');
  282. }
  283. }
  284. //点击banner
  285. void onBannerTap(Banner? banner) {
  286. if (banner?.action == null) return;
  287. final action = BannerAction.values.firstWhere(
  288. (e) => e.toString().split('.').last == banner?.action,
  289. orElse: () => BannerAction.notice, // 默认值
  290. );
  291. switch (action) {
  292. case BannerAction.urlOut:
  293. if (banner?.data != null) {
  294. SystemHelper.openWebPage(banner!.data!);
  295. }
  296. break;
  297. case BannerAction.urlIn:
  298. if (banner?.data != null) {
  299. Get.toNamed(
  300. Routes.WEB,
  301. arguments: {
  302. 'title': banner?.title ?? '',
  303. 'url': banner?.data ?? '',
  304. },
  305. );
  306. }
  307. break;
  308. case BannerAction.deepLink:
  309. if (banner?.data != null) {
  310. // Handle deep link
  311. SystemHelper.handleDeepLink(banner!.data!);
  312. }
  313. break;
  314. case BannerAction.openPkg:
  315. if (banner?.data != null) {
  316. // Open specific package
  317. SystemHelper.openPackage(banner!.data!);
  318. }
  319. break;
  320. case BannerAction.notice:
  321. if (banner?.data != null) {
  322. // Show notice dialog
  323. ErrorDialog.show(
  324. title: banner?.title ?? '',
  325. message: banner?.content,
  326. );
  327. }
  328. break;
  329. case BannerAction.page:
  330. if (banner?.data != null) {
  331. final uri = Uri.parse(banner!.data!);
  332. Get.toNamed(
  333. uri.path,
  334. arguments: {...uri.queryParameters, 'banner': banner},
  335. );
  336. }
  337. break;
  338. }
  339. }
  340. }