style: Lint using very_good_analysis

This commit is contained in:
PapaTutuWawa 2022-09-06 14:08:36 +02:00
parent acd186e0b6
commit 3aad6b617d
30 changed files with 231 additions and 254 deletions

View File

@ -4,6 +4,14 @@ Platform-specific code for Moxxy. Not intended for use outside of Moxxy.
This repo is based on [very_good_flutter_plugin](https://github.com/VeryGoodOpenSource/very_good_flutter_plugin).
## Development
The development of this package is based on [melos](https://pub.dev/packages/melos).
To make all packages link to each other locally, begin by running `melos bootstrap`. After editing
the code and making your changes, please run `melos run analyze` to make sure that no linter warnings
are left inside the code.
## Acknowledgements
- [ekasetiawans](https://github.com/ekasetiawans) for [flutter_background_service](https://github.com/ekasetiawans/flutter_background_service). moxplatform_android is basically just a copy and paste of [flutter_background_service](https://github.com/ekasetiawans/flutter_background_service).

View File

@ -1,3 +1,8 @@
analyzer:
exclude:
- brick/**
include: package:very_good_analysis/analysis_options.yaml
linter:
rules:
public_member_api_docs: false
lines_longer_than_80_chars: false
use_setters_to_change_properties: false
avoid_positional_boolean_parameters: false
avoid_bool_literals_in_conditional_expressions: false

View File

@ -0,0 +1 @@
include: ../../analysis_options.yaml

View File

@ -1,12 +1,4 @@
import "package:moxplatform_platform_interface/moxplatform_platform_interface.dart";
import "package:moxplatform_platform_interface/src/isolate.dart";
import "package:moxplatform_platform_interface/src/media.dart";
library moxplatform;
export "package:moxplatform_platform_interface/src/isolate.dart" show IsolateHandler;
export "package:moxplatform_platform_interface/src/media.dart" show MediaScannerImplementation;
export "package:moxplatform_platform_interface/src/service.dart" show BackgroundService;
class MoxplatformPlugin {
static IsolateHandler get handler => MoxplatformInterface.handler;
static MediaScannerImplementation get media => MoxplatformInterface.media;
}
export 'src/plugin.dart';
export 'src/types.dart';

View File

@ -0,0 +1,6 @@
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
class MoxplatformPlugin {
static IsolateHandler get handler => MoxplatformInterface.handler;
static MediaScannerImplementation get media => MoxplatformInterface.media;
}

View File

@ -1,4 +1,4 @@
import "package:moxlib/awaitabledatasender.dart";
import 'package:moxlib/awaitabledatasender.dart';
abstract class BackgroundCommand implements JsonImplementation {}
abstract class BackgroundEvent implements JsonImplementation {}

View File

@ -19,6 +19,10 @@ dependencies:
flutter:
sdk: flutter
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.4
moxplatform_android:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
@ -26,11 +30,8 @@ dependencies:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.4
dev_dependencies:
flutter_test:
sdk: flutter
plugin_platform_interface: 2.1.2
very_good_analysis: 2.4.0

View File

@ -0,0 +1 @@
include: ../../analysis_options.yaml

View File

@ -1,15 +1,6 @@
import "package:moxplatform_android/isolate_android.dart";
import "package:moxplatform_android/media_android.dart";
library moxplatform_android;
import "package:moxplatform_platform_interface/moxplatform_platform_interface.dart";
class MoxplatformAndroidPlugin extends MoxplatformInterface {
static void registerWith() {
print("MoxplatformAndroidPlugin: Registering implementation");
MoxplatformInterface.handler = AndroidIsolateHandler();
MoxplatformInterface.media = AndroidMediaScannerImplementation();
}
@override
Future<String> getPlatformName() async => "Android";
}
export 'src/isolate_android.dart';
export 'src/media_android.dart';
export 'src/plugin_android.dart';
export 'src/service_android.dart';

View File

@ -1,17 +0,0 @@
import 'package:flutter/foundation.dart';
import 'package:flutter/services.dart';
import 'moxplatform_android_platform_interface.dart';
/// An implementation of [MoxplatformAndroidPlatform] that uses method channels.
class MethodChannelMoxplatformAndroid extends MoxplatformAndroidPlatform {
/// The method channel used to interact with the native platform.
@visibleForTesting
final methodChannel = const MethodChannel('moxplatform_android');
@override
Future<String?> getPlatformVersion() async {
final version = await methodChannel.invokeMethod<String>('getPlatformVersion');
return version;
}
}

View File

@ -1,29 +0,0 @@
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
import 'moxplatform_android_method_channel.dart';
abstract class MoxplatformAndroidPlatform extends PlatformInterface {
/// Constructs a MoxplatformAndroidPlatform.
MoxplatformAndroidPlatform() : super(token: _token);
static final Object _token = Object();
static MoxplatformAndroidPlatform _instance = MethodChannelMoxplatformAndroid();
/// The default instance of [MoxplatformAndroidPlatform] to use.
///
/// Defaults to [MethodChannelMoxplatformAndroid].
static MoxplatformAndroidPlatform get instance => _instance;
/// Platform-specific implementations should set this with their own
/// platform-specific class that extends [MoxplatformAndroidPlatform] when
/// they register themselves.
static set instance(MoxplatformAndroidPlatform instance) {
PlatformInterface.verifyToken(instance, _token);
_instance = instance;
}
Future<String?> getPlatformVersion() {
throw UnimplementedError('platformVersion() has not been implemented.');
}
}

View File

@ -1,63 +0,0 @@
import "dart:convert";
import "dart:ui";
import "package:moxplatform_platform_interface/src/service.dart";
import "package:moxplatform/types.dart";
import "package:moxlib/awaitabledatasender.dart";
import "package:flutter/material.dart";
import "package:flutter/services.dart";
import "package:logging/logging.dart";
import "package:uuid/uuid.dart";
import "package:meta/meta.dart";
class AndroidBackgroundService extends BackgroundService {
@internal
static const MethodChannel channel = MethodChannel("me.polynom.moxplatform_android_bg");
final Logger _log;
AndroidBackgroundService()
: _log = Logger("AndroidBackgroundService"),
super();
@override
void setNotification(String title, String body) {
channel.invokeMethod(
"setNotificationBody",
[ body ]
);
}
@override
void sendEvent(BackgroundEvent event, { String? id }) {
final data = DataWrapper(
id ?? const Uuid().v4(),
event
);
// NOTE: *S*erver to *F*oreground
_log.fine("S2F: ${data.toJson().toString()}");
channel.invokeMethod("sendData", jsonEncode(data.toJson()));
}
@override
void init(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent
) {
WidgetsFlutterBinding.ensureInitialized();
// Ensure that all native plugins are registered against this FlutterEngine, so that
// we can use path_provider, notifications, ...
DartPluginRegistrant.ensureInitialized();
// Register the event handler
channel.setMethodCallHandler((MethodCall call) async {
await handleEvent(jsonDecode(call.arguments));
});
setNotification("Moxxy", "Preparing...");
_log.finest("Running...");
entrypoint();
}
}

View File

@ -1,43 +1,47 @@
import "dart:convert";
import "dart:ui";
import "package:moxplatform_android/service_android.dart";
import "package:flutter/widgets.dart";
import "package:flutter/services.dart";
import "package:logging/logging.dart";
import "package:moxplatform_platform_interface/src/isolate.dart";
import "package:moxplatform_platform_interface/src/service.dart";
import "package:moxplatform/types.dart";
import "package:moxlib/awaitabledatasender.dart";
import "package:get_it/get_it.dart";
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/services.dart';
import 'package:flutter/widgets.dart';
import 'package:get_it/get_it.dart';
import 'package:logging/logging.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_android/src/service_android.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
/// An [AwaitableDataSender] that uses flutter_background_service.
class BackgroundServiceDataSender extends AwaitableDataSender<BackgroundCommand, BackgroundEvent> {
BackgroundServiceDataSender()
: _channel = const MethodChannel('me.polynom.moxplatform_android'), super();
final MethodChannel _channel;
BackgroundServiceDataSender() : _channel = MethodChannel("me.polynom.moxplatform_android"), super();
@override
Future<void> sendDataImpl(DataWrapper data) async {
await _channel.invokeMethod("sendData", jsonEncode(data.toJson()));
await _channel.invokeMethod<void>('sendData', jsonEncode(data.toJson()));
}
}
@pragma('vm:entry-point')
Future<void> androidEntrypoint() async {
print("androidEntrypoint: Called on new FlutterEngine");
// ignore: avoid_print
print('androidEntrypoint: Called on new FlutterEngine');
WidgetsFlutterBinding.ensureInitialized();
/*
AndroidBackgroundService.channel.setMethodCallHandler((MethodCall call) async {
print(call.method);
});*/
final data = jsonDecode(await AndroidBackgroundService.channel.invokeMethod("getExtraData", []));
final int entrypointHandle = data["genericEntrypoint"]!;
final result = await AndroidBackgroundService.channel.invokeMethod<String>(
'getExtraData',
<void>[],
);
final data = jsonDecode(result!) as Map<String, dynamic>;
final entrypointHandle = data['genericEntrypoint']! as int;
final entrypointCallbackHandle = CallbackHandle.fromRawHandle(entrypointHandle);
final entrypoint = PluginUtilities.getCallbackFromHandle(entrypointCallbackHandle);
final int handleUIEventHandle = data["eventHandle"]!;
final handleUIEventHandle = data['eventHandle']! as int;
final handleUIEventCallbackHandle = CallbackHandle.fromRawHandle(handleUIEventHandle);
final handleUIEvent = PluginUtilities.getCallbackFromHandle(handleUIEventCallbackHandle);
@ -45,26 +49,27 @@ Future<void> androidEntrypoint() async {
GetIt.I.registerSingleton<BackgroundService>(srv);
srv.init(
entrypoint! as Future<void> Function(),
handleUIEvent! as Future<void> Function(Map<String, dynamic>? data)
handleUIEvent! as Future<void> Function(Map<String, dynamic>? data),
);
}
/// The Android specific implementation of the [IsolateHandler].
class AndroidIsolateHandler extends IsolateHandler {
AndroidIsolateHandler()
: _channel = const MethodChannel('me.polynom.moxplatform_android'),
_dataSender = BackgroundServiceDataSender(),
_log = Logger('AndroidIsolateHandler'),
super();
final BackgroundServiceDataSender _dataSender;
final MethodChannel _channel;
final Logger _log;
AndroidIsolateHandler()
: _channel = MethodChannel("me.polynom.moxplatform_android"),
_dataSender = BackgroundServiceDataSender(),
_log = Logger("AndroidIsolateHandler"),
super();
@override
Future<void> attach(Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent) async {
_channel.setMethodCallHandler((MethodCall call) async {
await handleIsolateEvent(jsonDecode(call.arguments));
final args = call.arguments as String;
await handleIsolateEvent(jsonDecode(args) as Map<String, dynamic>);
});
}
@ -72,35 +77,35 @@ class AndroidIsolateHandler extends IsolateHandler {
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
) async {
_log.finest("Called start");
_log.finest('Called start');
WidgetsFlutterBinding.ensureInitialized();
final androidEntrypointHandle = PluginUtilities.getCallbackHandle(androidEntrypoint)!.toRawHandle();
_log.finest('androidEntrypointHandle: $androidEntrypointHandle');
await _channel.invokeMethod("configure", [
androidEntrypointHandle,
jsonEncode({
"genericEntrypoint": PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
"eventHandle": PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
}),
await _channel.invokeMethod<void>('configure', <dynamic>[
androidEntrypointHandle,
jsonEncode({
'genericEntrypoint': PluginUtilities.getCallbackHandle(entrypoint)!.toRawHandle(),
'eventHandle': PluginUtilities.getCallbackHandle(handleUIEvent)!.toRawHandle()
}),
]);
await attach(handleIsolateEvent);
final result = await _channel.invokeMethod("start", []);
if (result) {
_log.finest("Service successfully started");
final result = await _channel.invokeMethod<bool>('start', <void>[]);
if (result == true) {
_log.finest('Service successfully started');
} else {
_log.severe("Service failed to start");
_log.severe('Service failed to start');
}
}
@override
Future<bool> isRunning() async {
WidgetsFlutterBinding.ensureInitialized();
return await _channel.invokeMethod("isRunning", []);
return (await _channel.invokeMethod<bool>('isRunning', <void>[])) == true;
}
@override

View File

@ -1,5 +1,5 @@
import "package:moxplatform_platform_interface/src/media.dart";
import "package:media_scanner/media_scanner.dart";
import 'package:media_scanner/media_scanner.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
class AndroidMediaScannerImplementation extends MediaScannerImplementation {
@override

View File

@ -0,0 +1,15 @@
import 'package:moxplatform_android/src/isolate_android.dart';
import 'package:moxplatform_android/src/media_android.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
class MoxplatformAndroidPlugin extends MoxplatformInterface {
static void registerWith() {
// ignore: avoid_print
print('MoxplatformAndroidPlugin: Registering implementation');
MoxplatformInterface.handler = AndroidIsolateHandler();
MoxplatformInterface.media = AndroidMediaScannerImplementation();
}
@override
Future<String> getPlatformName() async => 'Android';
}

View File

@ -0,0 +1,64 @@
import 'dart:convert';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:logging/logging.dart';
import 'package:meta/meta.dart';
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxplatform/moxplatform.dart';
import 'package:moxplatform_platform_interface/moxplatform_platform_interface.dart';
import 'package:uuid/uuid.dart';
class AndroidBackgroundService extends BackgroundService {
AndroidBackgroundService()
: _log = Logger('AndroidBackgroundService'),
super();
@internal
static const MethodChannel channel = MethodChannel('me.polynom.moxplatform_android_bg');
final Logger _log;
@override
void setNotification(String title, String body) {
channel.invokeMethod<void>(
'setNotificationBody',
[body],
);
}
@override
void sendEvent(BackgroundEvent event, { String? id }) {
final data = DataWrapper(
id ?? const Uuid().v4(),
event,
);
// NOTE: *S*erver to *F*oreground
_log.fine('S2F: ${data.toJson().toString()}');
channel.invokeMethod<void>('sendData', jsonEncode(data.toJson()));
}
@override
void init(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent,
) {
WidgetsFlutterBinding.ensureInitialized();
// Ensure that all native plugins are registered against this FlutterEngine, so that
// we can use path_provider, notifications, ...
DartPluginRegistrant.ensureInitialized();
// Register the event handler
channel.setMethodCallHandler((MethodCall call) async {
final args = call.arguments! as String;
await handleEvent(jsonDecode(args) as Map<String, dynamic>);
});
setNotification('Moxxy', 'Preparing...');
_log.finest('Running...');
entrypoint();
}
}

View File

@ -20,23 +20,25 @@ flutter:
dependencies:
flutter:
sdk: flutter
moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
get_it: 7.2.0
logging: 1.0.2
media_scanner: 2.0.0
meta: ^1.7.0
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.4
logging: 1.0.2
get_it: 7.2.0
uuid: 3.0.5
media_scanner: 2.0.0
moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
moxplatform_platform_interface:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
plugin_platform_interface: 2.1.2
meta: ^1.7.0
uuid: 3.0.5
dev_dependencies:
flutter_test:
sdk: flutter
very_good_analysis: 2.4.0

View File

@ -1,4 +1 @@
include: package:flutter_lints/flutter.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
include: ../../analysis_options.yaml

View File

@ -30,3 +30,4 @@ dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
very_good_analysis: 2.4.0

View File

@ -1,4 +1 @@
include: package:flutter_lints/flutter.yaml
# Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options
include: ../../analysis_options.yaml

View File

@ -12,9 +12,8 @@ dependencies:
sdk: flutter
moxplatform:
path: ../moxplatform
#hosted: https://git.polynom.me/api/packages/Moxxy/pub
#version: 0.1.11
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
moxplatform_generic:
path: ../moxplatform_generic
@ -22,6 +21,7 @@ dev_dependencies:
flutter_test:
sdk: flutter
flutter_lints: ^2.0.0
very_good_analysis: 2.4.0
flutter:
plugin:

View File

@ -0,0 +1 @@
include: ../../analysis_options.yaml

View File

@ -1,29 +1,8 @@
import "package:moxplatform_platform_interface/src/isolate.dart";
import "package:moxplatform_platform_interface/src/isolate_stub.dart";
import "package:moxplatform_platform_interface/src/media.dart";
import "package:moxplatform_platform_interface/src/media_stub.dart";
import "package:plugin_platform_interface/plugin_platform_interface.dart";
library moxplatform_platform_interface;
abstract class MoxplatformInterface extends PlatformInterface {
/// Constructs a MyPluginPlatform.
MoxplatformInterface() : super(token: _token);
static final Object _token = Object();
static IsolateHandler _handler = StubIsolateHandler();
static MediaScannerImplementation _media = StubMediaScannerImplementation();
static IsolateHandler get handler => _handler;
static set handler(IsolateHandler instance) {
_handler = instance;
}
static MediaScannerImplementation get media => _media;
static set media(MediaScannerImplementation instance) {
_media = instance;
}
/// Return the current platform name.
Future<String?> getPlatformName();
}
export 'src/interface.dart';
export 'src/isolate.dart';
export 'src/isolate_stub.dart';
export 'src/media.dart';
export 'src/media_stub.dart';
export 'src/service.dart';

View File

@ -0,0 +1,17 @@
import 'package:moxplatform_platform_interface/src/isolate.dart';
import 'package:moxplatform_platform_interface/src/isolate_stub.dart';
import 'package:moxplatform_platform_interface/src/media.dart';
import 'package:moxplatform_platform_interface/src/media_stub.dart';
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
abstract class MoxplatformInterface extends PlatformInterface {
MoxplatformInterface() : super(token: _token);
static final Object _token = Object();
static IsolateHandler handler = StubIsolateHandler();
static MediaScannerImplementation media = StubMediaScannerImplementation();
/// Return the current platform name.
Future<String?> getPlatformName();
}

View File

@ -1,4 +1,4 @@
import "package:moxlib/awaitabledatasender.dart";
import 'package:moxlib/awaitabledatasender.dart';
/// A class abstracting the interaction between the UI isolate and the background
/// service, which is either a regular isolate or an Android foreground service.
@ -6,17 +6,17 @@ import "package:moxlib/awaitabledatasender.dart";
abstract class IsolateHandler {
/// Start the background service.
/// [entrypoint] is the entrypoint that is run inside the new isolate.
/// [handeUiEvent] is a handler function that is called when the isolate receives data from the UI.
/// [handleUIEvent] is a handler function that is called when the isolate receives data from the UI.
/// [handleIsolateEvent] is a handler function that is called when the UI receives data from the service.
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
);
/// Make sure that the UI event handler is registered without starting the isolate.
Future<void> attach(
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
);
/// Return true if the background service is running. False if it's not.

View File

@ -1,5 +1,5 @@
import "package:moxlib/awaitabledatasender.dart";
import "package:moxplatform_platform_interface/src/isolate.dart";
import 'package:moxlib/awaitabledatasender.dart';
import 'package:moxplatform_platform_interface/src/isolate.dart';
class StubDataSender extends AwaitableDataSender {
StubDataSender() : super();
@ -9,24 +9,25 @@ class StubDataSender extends AwaitableDataSender {
}
class StubIsolateHandler extends IsolateHandler {
final StubDataSender _sender;
StubIsolateHandler() : _sender = StubDataSender();
final StubDataSender _sender;
@override
Future<void> attach(
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
) async {
print("STUB ATTACHED!!!!!!");
// ignore: avoid_print
print('STUB ATTACHED!!!!!!');
}
@override
Future<void> start(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleUIEvent,
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent
Future<void> Function(Map<String, dynamic>? data) handleIsolateEvent,
) async {
print("STUB STARTED!!!!!!");
// ignore: avoid_print
print('STUB STARTED!!!!!!');
}
@override

View File

@ -1,4 +1,5 @@
/// Wrapper around platform-specific media scanning
// ignore: one_member_abstracts
abstract class MediaScannerImplementation {
/// Let the platform-specific media scanner scan the file at [path].
void scanFile(String path);

View File

@ -1,4 +1,4 @@
import "package:moxplatform_platform_interface/src/media.dart";
import 'package:moxplatform_platform_interface/src/media.dart';
class StubMediaScannerImplementation extends MediaScannerImplementation {
@override

View File

@ -1,4 +1,4 @@
import "package:moxplatform/types.dart";
import 'package:moxplatform/moxplatform.dart';
abstract class BackgroundService {
/// Set the notification of the background service, if available
@ -12,6 +12,6 @@ abstract class BackgroundService {
/// data.
void init(
Future<void> Function() entrypoint,
Future<void> Function(Map<String, dynamic>? data) handleEvent
Future<void> Function(Map<String, dynamic>? data) handleEvent,
);
}

View File

@ -12,15 +12,16 @@ dependencies:
flutter:
sdk: flutter
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.4
moxplatform:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.11
moxlib:
hosted: https://git.polynom.me/api/packages/Moxxy/pub
version: 0.1.4
plugin_platform_interface: 2.1.2
dev_dependencies:
flutter_test:
sdk: flutter
very_good_analysis: 2.4.0