mirror of
https://github.com/zjs81/meshcore-open.git
synced 2026-07-06 08:46:40 +10:00
add platforminfo helper
This commit is contained in:
@@ -30,6 +30,7 @@ import '../storage/message_store.dart';
|
|||||||
import '../storage/unread_store.dart';
|
import '../storage/unread_store.dart';
|
||||||
import '../utils/app_logger.dart';
|
import '../utils/app_logger.dart';
|
||||||
import '../utils/battery_utils.dart';
|
import '../utils/battery_utils.dart';
|
||||||
|
import '../utils/platform_info.dart';
|
||||||
import 'meshcore_protocol.dart';
|
import 'meshcore_protocol.dart';
|
||||||
|
|
||||||
class MeshCoreUuids {
|
class MeshCoreUuids {
|
||||||
@@ -693,8 +694,7 @@ class MeshCoreConnector extends ChangeNotifier {
|
|||||||
await _scanSubscription?.cancel();
|
await _scanSubscription?.cancel();
|
||||||
|
|
||||||
// On iOS/macOS, wait for Bluetooth to be powered on before scanning
|
// On iOS/macOS, wait for Bluetooth to be powered on before scanning
|
||||||
if (defaultTargetPlatform == TargetPlatform.iOS ||
|
if (PlatformInfo.isIOS || PlatformInfo.isMacOS) {
|
||||||
defaultTargetPlatform == TargetPlatform.macOS) {
|
|
||||||
// Wait for adapter state to be powered on
|
// Wait for adapter state to be powered on
|
||||||
final adapterState = await FlutterBluePlus.adapterState.first;
|
final adapterState = await FlutterBluePlus.adapterState.first;
|
||||||
if (adapterState != BluetoothAdapterState.on) {
|
if (adapterState != BluetoothAdapterState.on) {
|
||||||
|
|||||||
@@ -1,7 +1,6 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
import 'dart:io' show Platform;
|
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
|
import '../utils/platform_info.dart';
|
||||||
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
import 'package:flutter_blue_plus/flutter_blue_plus.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
|
|
||||||
@@ -265,7 +264,7 @@ class _ScannerScreenState extends State<ScannerScreen> {
|
|||||||
],
|
],
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
if (Platform.isAndroid)
|
if (PlatformInfo.isAndroid)
|
||||||
TextButton(
|
TextButton(
|
||||||
onPressed: () => FlutterBluePlus.turnOn(),
|
onPressed: () => FlutterBluePlus.turnOn(),
|
||||||
child: Text(context.l10n.scanner_enableBluetooth),
|
child: Text(context.l10n.scanner_enableBluetooth),
|
||||||
|
|||||||
@@ -1,12 +1,11 @@
|
|||||||
import 'dart:io';
|
import '../utils/platform_info.dart';
|
||||||
|
|
||||||
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
import 'package:flutter_foreground_task/flutter_foreground_task.dart';
|
||||||
|
|
||||||
class BackgroundService {
|
class BackgroundService {
|
||||||
bool _initialized = false;
|
bool _initialized = false;
|
||||||
|
|
||||||
Future<void> initialize() async {
|
Future<void> initialize() async {
|
||||||
if (!Platform.isAndroid || _initialized) return;
|
if (!PlatformInfo.isAndroid || _initialized) return;
|
||||||
FlutterForegroundTask.init(
|
FlutterForegroundTask.init(
|
||||||
androidNotificationOptions: AndroidNotificationOptions(
|
androidNotificationOptions: AndroidNotificationOptions(
|
||||||
channelId: 'meshcore_background',
|
channelId: 'meshcore_background',
|
||||||
@@ -29,7 +28,7 @@ class BackgroundService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> start() async {
|
Future<void> start() async {
|
||||||
if (!Platform.isAndroid) return;
|
if (!PlatformInfo.isAndroid) return;
|
||||||
if (!_initialized) {
|
if (!_initialized) {
|
||||||
await initialize();
|
await initialize();
|
||||||
}
|
}
|
||||||
@@ -43,7 +42,7 @@ class BackgroundService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Future<void> stop() async {
|
Future<void> stop() async {
|
||||||
if (!Platform.isAndroid) return;
|
if (!PlatformInfo.isAndroid) return;
|
||||||
final running = await FlutterForegroundTask.isRunningService;
|
final running = await FlutterForegroundTask.isRunningService;
|
||||||
if (!running) return;
|
if (!running) return;
|
||||||
await FlutterForegroundTask.stopService();
|
await FlutterForegroundTask.stopService();
|
||||||
|
|||||||
@@ -4,6 +4,7 @@ import 'package:meshcore_open/connector/meshcore_connector.dart';
|
|||||||
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
import 'package:meshcore_open/connector/meshcore_protocol.dart';
|
||||||
import 'package:path_provider/path_provider.dart';
|
import 'package:path_provider/path_provider.dart';
|
||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
|
import '../utils/platform_info.dart';
|
||||||
|
|
||||||
import 'package:share_plus/share_plus.dart';
|
import 'package:share_plus/share_plus.dart';
|
||||||
|
|
||||||
@@ -109,6 +110,10 @@ class GpxExport {
|
|||||||
String shareText,
|
String shareText,
|
||||||
String subject,
|
String subject,
|
||||||
) async {
|
) async {
|
||||||
|
if (PlatformInfo.isWeb) {
|
||||||
|
debugPrint("GPX export is not supported on Web.");
|
||||||
|
return gpxExportNotAvailable;
|
||||||
|
}
|
||||||
if (_contacts.isEmpty) {
|
if (_contacts.isEmpty) {
|
||||||
debugPrint("No repeaters to export – nothing to share.");
|
debugPrint("No repeaters to export – nothing to share.");
|
||||||
return gpxExportNoContacts;
|
return gpxExportNoContacts;
|
||||||
|
|||||||
@@ -0,0 +1,32 @@
|
|||||||
|
import 'package:flutter/foundation.dart';
|
||||||
|
import 'dart:io' show Platform;
|
||||||
|
|
||||||
|
/// Utility class to safely check the current platform across web and native.
|
||||||
|
///
|
||||||
|
/// Using `Platform` from `dart:io` directly on Web causes a crash.
|
||||||
|
/// This class handles the `kIsWeb` check first to avoid those crashes.
|
||||||
|
class PlatformInfo {
|
||||||
|
/// Whether the app is running in a web browser.
|
||||||
|
static bool get isWeb => kIsWeb;
|
||||||
|
|
||||||
|
/// Whether the app is running on Android.
|
||||||
|
static bool get isAndroid => !kIsWeb && Platform.isAndroid;
|
||||||
|
|
||||||
|
/// Whether the app is running on iOS.
|
||||||
|
static bool get isIOS => !kIsWeb && Platform.isIOS;
|
||||||
|
|
||||||
|
/// Whether the app is running on macOS.
|
||||||
|
static bool get isMacOS => !kIsWeb && Platform.isMacOS;
|
||||||
|
|
||||||
|
/// Whether the app is running on Windows.
|
||||||
|
static bool get isWindows => !kIsWeb && Platform.isWindows;
|
||||||
|
|
||||||
|
/// Whether the app is running on Linux.
|
||||||
|
static bool get isLinux => !kIsWeb && Platform.isLinux;
|
||||||
|
|
||||||
|
/// Whether the app is running on a mobile platform (Android or iOS).
|
||||||
|
static bool get isMobile => isAndroid || isIOS;
|
||||||
|
|
||||||
|
/// Whether the app is running on a desktop platform (macOS, Windows, or Linux).
|
||||||
|
static bool get isDesktop => isMacOS || isWindows || isLinux;
|
||||||
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import '../utils/platform_info.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
@@ -326,8 +327,7 @@ class _RepeaterLoginDialogState extends State<RepeaterLoginDialog> {
|
|||||||
},
|
},
|
||||||
onSubmitted: (_) => _handleLogin(),
|
onSubmitted: (_) => _handleLogin(),
|
||||||
autofocus:
|
autofocus:
|
||||||
!(defaultTargetPlatform == TargetPlatform.android ||
|
!PlatformInfo.isMobile &&
|
||||||
defaultTargetPlatform == TargetPlatform.iOS) &&
|
|
||||||
_passwordController.text.isEmpty,
|
_passwordController.text.isEmpty,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
import 'dart:async';
|
import 'dart:async';
|
||||||
|
import '../utils/platform_info.dart';
|
||||||
|
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter/foundation.dart';
|
import 'package:flutter/foundation.dart';
|
||||||
@@ -274,8 +275,7 @@ class _RoomLoginDialogState extends State<RoomLoginDialog> {
|
|||||||
),
|
),
|
||||||
onSubmitted: (_) => _handleLogin(),
|
onSubmitted: (_) => _handleLogin(),
|
||||||
autofocus:
|
autofocus:
|
||||||
!(defaultTargetPlatform == TargetPlatform.android ||
|
!PlatformInfo.isMobile &&
|
||||||
defaultTargetPlatform == TargetPlatform.iOS) &&
|
|
||||||
_passwordController.text.isEmpty,
|
_passwordController.text.isEmpty,
|
||||||
),
|
),
|
||||||
const SizedBox(height: 12),
|
const SizedBox(height: 12),
|
||||||
|
|||||||
Reference in New Issue
Block a user