diff --git a/lib/core/channel/bg_run.dart b/lib/core/channel/bg_run.dart new file mode 100644 index 00000000..1c1646fb --- /dev/null +++ b/lib/core/channel/bg_run.dart @@ -0,0 +1,16 @@ +import 'package:flutter/services.dart'; +import 'package:toolbox/data/res/misc.dart'; + +class BgRunMC { + const BgRunMC._(); + + static const _channel = MethodChannel('${Miscs.pkgName}/app_retain'); + + static void moveToBg() { + _channel.invokeMethod('sendToBackground'); + } + + static void startService() { + _channel.invokeMethod('startService'); + } +} diff --git a/lib/core/channel/home_widget.dart b/lib/core/channel/home_widget.dart new file mode 100644 index 00000000..77c85a50 --- /dev/null +++ b/lib/core/channel/home_widget.dart @@ -0,0 +1,12 @@ +import 'package:flutter/services.dart'; +import 'package:toolbox/data/res/misc.dart'; + +class HomeWidgetMC { + const HomeWidgetMC._(); + + static const _channel = MethodChannel('${Miscs.pkgName}/home_widget'); + + static void update() { + _channel.invokeMethod('update'); + } +} diff --git a/lib/data/res/misc.dart b/lib/data/res/misc.dart index f1c7e523..85d03d99 100644 --- a/lib/data/res/misc.dart +++ b/lib/data/res/misc.dart @@ -1,7 +1,5 @@ import 'dart:convert'; -import 'package:flutter/services.dart'; - class Miscs { const Miscs._(); @@ -20,10 +18,7 @@ class Miscs { /// Max debug log lines static const maxDebugLogLines = 100; - /// Method Channels static const pkgName = 'tech.lolli.toolbox'; - static const bgRunChannel = MethodChannel('$pkgName/app_retain'); - static const homeWidgetChannel = MethodChannel('$pkgName/home_widget'); static const jsonEncoder = JsonEncoder.withIndent(' '); } diff --git a/lib/main.dart b/lib/main.dart index b07b6ecb..b2a34bab 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -6,6 +6,7 @@ import 'package:logging/logging.dart'; import 'package:macos_window_utils/window_manipulator.dart'; import 'package:provider/provider.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:toolbox/core/channel/bg_run.dart'; import 'package:toolbox/data/res/provider.dart'; import 'package:toolbox/data/res/store.dart'; @@ -28,7 +29,6 @@ import 'data/provider/sftp.dart'; import 'data/provider/snippet.dart'; import 'data/provider/virtual_keyboard.dart'; import 'data/res/color.dart'; -import 'data/res/misc.dart'; import 'locator.dart'; import 'view/widget/custom_appbar.dart'; import 'view/widget/rebuild.dart'; @@ -89,7 +89,7 @@ Future initApp() async { if (isAndroid) { // Only start service when [bgRun] is true. if (Stores.setting.bgRun.fetch()) { - Miscs.bgRunChannel.invokeMethod('startService'); + BgRunMC.startService(); } // SharedPreferences is only used on Android for saving home widgets settings. SharedPreferences.setPrefix(''); diff --git a/lib/view/page/home.dart b/lib/view/page/home.dart index df0fe83d..028194b5 100644 --- a/lib/view/page/home.dart +++ b/lib/view/page/home.dart @@ -4,6 +4,8 @@ import 'package:after_layout/after_layout.dart'; import 'package:flutter/material.dart'; import 'package:flutter_gen/gen_l10n/l10n.dart'; import 'package:get_it/get_it.dart'; +import 'package:toolbox/core/channel/bg_run.dart'; +import 'package:toolbox/core/channel/home_widget.dart'; import 'package:toolbox/core/extension/context/dialog.dart'; import 'package:toolbox/data/res/github_id.dart'; import 'package:toolbox/data/res/logger.dart'; @@ -88,7 +90,7 @@ class _HomePageState extends State // Keep running in background on Android device if (isAndroid && Stores.setting.bgRun.fetch()) { if (Providers.app.moveBg) { - Miscs.bgRunChannel.invokeMethod('sendToBackground'); + BgRunMC.moveToBg(); } } else { Providers.server.setDisconnected(); @@ -350,7 +352,7 @@ class _HomePageState extends State void updateHomeWidget() { if (Stores.setting.autoUpdateHomeWidget.fetch()) { - Miscs.homeWidgetChannel.invokeMethod('update'); + HomeWidgetMC.update(); } }