medialocation_controller.dart 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. import 'package:get/get.dart';
  2. import '../../../../utils/log/logger.dart';
  3. import '../../../../utils/system_helper.dart';
  4. import '../../../constants/enums.dart';
  5. import '../../../controllers/api_controller.dart';
  6. import '../../../controllers/base_core_api.dart';
  7. import '../../../controllers/core_controller.dart';
  8. import '../../../data/models/banner/banner_list.dart';
  9. import '../../../data/models/launch/groups.dart';
  10. import '../../../data/sp/ix_sp.dart';
  11. import '../../../dialog/error_dialog.dart';
  12. import '../../../modules/home/controllers/home_controller.dart';
  13. import '../../../routes/app_pages.dart';
  14. class MedialocationController extends GetxController {
  15. final TAG = 'MedialocationController';
  16. final ApiController apiController = Get.find<ApiController>();
  17. final CoreController coreController = Get.find<CoreController>();
  18. final HomeController homeController = Get.find<HomeController>();
  19. // VPN连接状态(计算属性,基于 coreController 状态和 bannerLocation)
  20. bool get isConnected =>
  21. _isSameLocation && coreController.state == ConnectionState.connected;
  22. bool get isConnecting =>
  23. _isSameLocation && coreController.state == ConnectionState.connecting;
  24. // 判断当前选中的节点是否是 bannerLocation
  25. bool get _isSameLocation {
  26. final location = bannerLocation.value;
  27. if (location == null) return false;
  28. final selectedLocation = homeController.selectedLocation;
  29. return selectedLocation != null &&
  30. (selectedLocation.id == location.id ||
  31. selectedLocation.code == location.code);
  32. }
  33. // Banner 数据
  34. final Rxn<Banner> banner = Rxn<Banner>();
  35. String get bannerTitle => banner.value?.title ?? 'Movies&TV';
  36. String get bannerImg => banner.value?.img ?? '';
  37. // Banner 列表
  38. final _bannerList = <Banner>[].obs;
  39. List<Banner> get bannerList => _bannerList;
  40. set bannerList(List<Banner> value) => _bannerList.assignAll(value);
  41. // Banner 背景图信息
  42. final Rxn<Banner> bannerInfo = Rxn<Banner>();
  43. String get bannerInfoImg => bannerInfo.value?.img ?? '';
  44. // Banner 关联的节点
  45. final Rxn<Location> bannerLocation = Rxn<Location>();
  46. @override
  47. void onInit() {
  48. super.onInit();
  49. if (Get.arguments != null) {
  50. if (Get.arguments['banner'] != null) {
  51. banner.value = Get.arguments['banner'] as Banner;
  52. }
  53. if (Get.arguments['type'] != null) {
  54. getBanner(position: Get.arguments['type'] as String);
  55. }
  56. }
  57. }
  58. /// 连接VPN - 连接到 bannerLocation
  59. void connect() {
  60. final location = bannerLocation.value;
  61. if (location == null) {
  62. log(TAG, 'Cannot connect: bannerLocation is null');
  63. return;
  64. }
  65. if (isConnecting) return;
  66. // 将 Location 转换为 Locations 并选中
  67. final locations = Locations(
  68. id: location.id,
  69. name: location.name,
  70. code: location.code,
  71. icon: location.icon,
  72. country: location.country,
  73. sort: location.sort,
  74. );
  75. // 选中节点并触发连接
  76. homeController.selectLocation(locations, locationSelectionType: 'media');
  77. coreController.selectLocationConnect();
  78. }
  79. /// 断开VPN
  80. void disconnect() {
  81. BaseCoreApi().disconnect();
  82. }
  83. /// 获取 banner 列表
  84. /// [position] banner 位置类型,如 "banner"、"media"、"nine" 等
  85. Future<void> getBanner({
  86. String position = 'media',
  87. bool isCache = true,
  88. }) async {
  89. try {
  90. // 先读取缓存数据
  91. final cacheBanners = IXSP.getBanner(position);
  92. if (cacheBanners != null && isCache) {
  93. if (cacheBanners.list != null && cacheBanners.list!.isNotEmpty) {
  94. bannerList = cacheBanners.list!;
  95. }
  96. if (cacheBanners.bannerInfo != null) {
  97. bannerInfo.value = cacheBanners.bannerInfo;
  98. }
  99. if (cacheBanners.location != null) {
  100. bannerLocation.value = cacheBanners.location;
  101. }
  102. log(TAG, 'Loaded banner from cache for position: $position');
  103. }
  104. // 请求最新数据
  105. final banners = await apiController.getBanner(position: position);
  106. if (banners.list != null && banners.list!.isNotEmpty) {
  107. bannerList = banners.list!;
  108. }
  109. if (banners.bannerInfo != null) {
  110. bannerInfo.value = banners.bannerInfo;
  111. }
  112. if (banners.location != null) {
  113. bannerLocation.value = banners.location;
  114. }
  115. // 保存到缓存
  116. await IXSP.saveBanner(position, banners);
  117. log(TAG, 'Banner updated and cached for position: $position');
  118. } catch (e) {
  119. log(TAG, 'Error loading banners for position $position: $e');
  120. }
  121. }
  122. //点击banner
  123. void onBannerTap(Banner? banner) {
  124. if (banner?.action == null) return;
  125. final action = BannerAction.values.firstWhere(
  126. (e) => e.toString().split('.').last == banner?.action,
  127. orElse: () => BannerAction.notice, // 默认值
  128. );
  129. switch (action) {
  130. case BannerAction.urlOut:
  131. if (banner?.data != null) {
  132. SystemHelper.openWebPage(banner!.data!);
  133. }
  134. break;
  135. case BannerAction.urlIn:
  136. if (banner?.data != null) {
  137. Get.toNamed(
  138. Routes.WEB,
  139. arguments: {
  140. 'title': banner?.title ?? '',
  141. 'url': banner?.data ?? '',
  142. },
  143. );
  144. }
  145. break;
  146. case BannerAction.deepLink:
  147. if (banner?.data != null) {
  148. // Handle deep link
  149. SystemHelper.handleDeepLink(banner!.data!);
  150. }
  151. break;
  152. case BannerAction.openPkg:
  153. if (banner?.data != null) {
  154. // Open specific package
  155. SystemHelper.openPackage(banner!.data!);
  156. }
  157. break;
  158. case BannerAction.notice:
  159. if (banner?.data != null) {
  160. // Show notice dialog
  161. ErrorDialog.show(
  162. title: banner?.title ?? '',
  163. message: banner?.content,
  164. );
  165. }
  166. break;
  167. case BannerAction.page:
  168. if (banner?.data != null) {
  169. final uri = Uri.parse(banner!.data!);
  170. Get.toNamed(
  171. uri.path,
  172. arguments: {...uri.queryParameters, 'banner': banner},
  173. );
  174. }
  175. break;
  176. }
  177. }
  178. }