desktop_shortcut_utils.dart 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import 'dart:io';
  2. import 'package:electronic_assistant/base/base_controller.dart';
  3. import 'package:electronic_assistant/router/app_pages.dart';
  4. import 'package:electronic_assistant/utils/mmkv_util.dart';
  5. import 'package:get/get.dart';
  6. import 'package:get/get_core/src/get_main.dart';
  7. import '../dialog/desktop_shortcut_dialog.dart';
  8. import 'android_shortcut.dart';
  9. class DesktopShortcutUtils {
  10. DesktopShortcutUtils._();
  11. static const String routePath = 'route_path';
  12. static const String _showMaxTimeTag = 'showMaxTime';
  13. static const int _showMaxFrequency = 1;
  14. static Map<String, dynamic>? intentMap;
  15. static Future<void> registerDesktopShortcut() async {
  16. if (Platform.isAndroid) {
  17. androidShortCut.register();
  18. } else if (Platform.isIOS) {
  19. //TODO IOS
  20. }
  21. }
  22. static void isShowTipsDialog(void Function() nextCallback) {
  23. if (!_isShow()) {
  24. nextCallback();
  25. return;
  26. }
  27. showAddDesktopShortcutDialog(onConfirm: () {
  28. if (Platform.isAndroid) {
  29. showAddDesktopShortcutTipsDialog(onConfirm: () {
  30. androidShortCut.addRecordShortcut();
  31. }, onDismiss: () {
  32. nextCallback();
  33. });
  34. } else if (Platform.isIOS) {
  35. //TODO IOS
  36. }
  37. }, onCancel: () {
  38. nextCallback();
  39. });
  40. _setShowOnce();
  41. }
  42. static bool _isShow() {
  43. return KVUtil.getInt(_showMaxTimeTag, 0) < _showMaxFrequency;
  44. }
  45. static void _setShowOnce() {
  46. int maxFrequency = KVUtil.getInt(_showMaxTimeTag, 0);
  47. maxFrequency++;
  48. KVUtil.putInt(_showMaxTimeTag, maxFrequency);
  49. }
  50. static void requestAddDesktopShortcut() {
  51. if (Platform.isAndroid) {
  52. showAddDesktopShortcutTipsDialog(
  53. onConfirm: () {
  54. androidShortCut.addRecordShortcut();
  55. },
  56. onDismiss: () {});
  57. } else if (Platform.isIOS) {
  58. //TODO IOS
  59. }
  60. }
  61. static void setLaunchAction(String action) {
  62. intentMap ??= {};
  63. intentMap?[LaunchAction.key] = action;
  64. }
  65. static Map<String, dynamic>? getRouteMap() {
  66. return intentMap;
  67. }
  68. static void clearRouteMap() {
  69. intentMap = null;
  70. }
  71. static void setRouteAction(String recordAudioAction) {
  72. if (recordAudioAction == LaunchAction.recordAudioAction) {
  73. Get.toNamed(RoutePath.record);
  74. }
  75. }
  76. }
  77. class LaunchAction {
  78. static const String key = 'launchAction';
  79. static const String recordAudioAction = 'record_audio';
  80. }