desktop_shortcut_utils.dart 3.8 KB

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