desktop_shortcut_utils.dart 3.4 KB

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