desktop_shortcut_utils.dart 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import 'dart:async';
  2. import 'dart:io';
  3. import 'package:app_links/app_links.dart';
  4. import 'package:electronic_assistant/base/base_controller.dart';
  5. import 'package:electronic_assistant/main.dart';
  6. import 'package:electronic_assistant/router/app_pages.dart';
  7. import 'package:electronic_assistant/utils/launcher_url_util.dart';
  8. import 'package:electronic_assistant/utils/mmkv_util.dart';
  9. import 'package:electronic_assistant/utils/toast_util.dart';
  10. import 'package:flutter/material.dart';
  11. import 'package:get/get.dart';
  12. import 'package:get/get_core/src/get_main.dart';
  13. import 'package:url_launcher/url_launcher.dart';
  14. import '../dialog/desktop_shortcut_dialog.dart';
  15. import '../dialog/ios_shortcut_guide_dialog.dart';
  16. import '../module/shortcut/view.dart';
  17. import 'android_shortcut.dart';
  18. class DesktopShortcutUtils {
  19. DesktopShortcutUtils._();
  20. static const String routePath = 'route_path';
  21. static const String _showMaxTimeTag = 'showMaxTime';
  22. static const int _showMaxFrequency = 1;
  23. static Map<String, dynamic>? intentMap;
  24. static StreamSubscription<Uri>? linkSubscription;
  25. static AppLinks appLinks = AppLinks();
  26. static Future<void> registerDesktopShortcut() async {
  27. if (Platform.isAndroid) {
  28. androidShortCut.register();
  29. } else if (Platform.isIOS) {
  30. //TODO IOS
  31. registerIosDeeplink();
  32. }
  33. }
  34. static Future<void> registerIosDeeplink() async {
  35. // Handle links
  36. linkSubscription = appLinks.uriLinkStream.listen((uri) {
  37. debugPrint('onAppLink: $uri');
  38. if (uri.scheme == "xiaoting") {
  39. DesktopShortcutUtils.setLaunchAction(LaunchAction.recordAudioAction);
  40. DesktopShortcutUtils.setRouteAction(LaunchAction.recordAudioAction);
  41. }
  42. });
  43. }
  44. static void isShowTipsDialog(void Function() nextCallback) {
  45. if (!_isShow()) {
  46. nextCallback();
  47. return;
  48. }
  49. if (Platform.isAndroid) {
  50. showAddDesktopShortcutDialog(onConfirm: () {
  51. showAddDesktopShortcutTipsDialog(onConfirm: () {
  52. androidShortCut.addRecordShortcut();
  53. }, onDismiss: () {
  54. nextCallback();
  55. });
  56. }, onCancel: () {
  57. nextCallback();
  58. });
  59. } else if (Platform.isAndroid) {
  60. showIosShortcutGuideDialog(
  61. onConfirm: () async {
  62. await ShortCutPage.start();
  63. nextCallback();
  64. },
  65. onCancel: () {
  66. nextCallback();
  67. },
  68. );
  69. }
  70. _setShowOnce();
  71. }
  72. static Future<void> _launchUrl() async {
  73. LauncherUrlUtil.launchHttpUrl('https://xiaoting.atmob.com/record',
  74. mode: LaunchMode.externalApplication);
  75. }
  76. static bool _isShow() {
  77. return KVUtil.getInt(_showMaxTimeTag, 0) < _showMaxFrequency;
  78. }
  79. static void _setShowOnce() {
  80. int maxFrequency = KVUtil.getInt(_showMaxTimeTag, 0);
  81. maxFrequency++;
  82. KVUtil.putInt(_showMaxTimeTag, maxFrequency);
  83. }
  84. static void requestAddDesktopShortcut() {
  85. if (Platform.isAndroid) {
  86. showAddDesktopShortcutTipsDialog(
  87. onConfirm: () {
  88. androidShortCut.addRecordShortcut();
  89. },
  90. onDismiss: () {});
  91. } else if (Platform.isIOS) {
  92. showIosShortcutGuideDialog(
  93. onConfirm: () {
  94. ShortCutPage.start();
  95. },
  96. );
  97. }
  98. _setShowOnce();
  99. }
  100. static void setLaunchAction(String action) {
  101. intentMap ??= {};
  102. intentMap?[LaunchAction.key] = action;
  103. }
  104. static Map<String, dynamic>? getRouteMap() {
  105. return intentMap;
  106. }
  107. static void clearRouteMap() {
  108. intentMap = null;
  109. }
  110. static void setRouteAction(String recordAudioAction) {
  111. if (recordAudioAction == LaunchAction.recordAudioAction) {
  112. Get.toNamed(RoutePath.record);
  113. }
  114. }
  115. }
  116. class LaunchAction {
  117. static const String key = 'launchAction';
  118. static const String recordAudioAction = 'record_audio';
  119. }