mirror of
https://github.com/lollipopkit/flutter_server_box.git
synced 2025-12-17 07:14:28 +01:00
opt.: method channels
This commit is contained in:
16
lib/core/channel/bg_run.dart
Normal file
16
lib/core/channel/bg_run.dart
Normal file
@@ -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');
|
||||
}
|
||||
}
|
||||
12
lib/core/channel/home_widget.dart
Normal file
12
lib/core/channel/home_widget.dart
Normal file
@@ -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');
|
||||
}
|
||||
}
|
||||
@@ -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(' ');
|
||||
}
|
||||
|
||||
@@ -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<void> 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('');
|
||||
|
||||
@@ -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<HomePage>
|
||||
// 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<HomePage>
|
||||
|
||||
void updateHomeWidget() {
|
||||
if (Stores.setting.autoUpdateHomeWidget.fetch()) {
|
||||
Miscs.homeWidgetChannel.invokeMethod('update');
|
||||
HomeWidgetMC.update();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user