From 96a7f1cfdc80c83330e1ce46301232a5724a0ab8 Mon Sep 17 00:00:00 2001 From: laiiihz Date: Wed, 14 Apr 2021 10:41:33 +0800 Subject: [PATCH] update logger fab & logger data --- CHANGELOG.md | 5 ++ example/lib/main.dart | 4 +- example/pubspec.lock | 2 +- lib/external_lib/pretty_json.dart | 2 +- lib/src/builders/dio_error_builder.dart | 6 ++- lib/src/builders/dio_responses_builder.dart | 5 +- lib/src/data/logger_data.dart | 32 +++++++++--- lib/src/parser/data_parser.dart | 17 +++++-- lib/src/parser/unfocus_parser.dart | 6 ++- lib/src/power_logger.dart | 34 ++++++++++--- lib/src/views/info_view.dart | 3 +- lib/src/widgets/logger_fab.dart | 55 ++++++++++----------- pubspec.yaml | 4 +- 13 files changed, 117 insertions(+), 58 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 1ec9a0a..367bbc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,10 @@ # CHANGELOG +## [1.1.0] + +* performance enhanced. +* add initAlignment. + ## [1.0.1-nullsafety.1] * add logger. diff --git a/example/lib/main.dart b/example/lib/main.dart index 266fb7a..bd5ac00 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -39,7 +39,7 @@ class _MyHomePageState extends State { NetTool.dio.get( "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png"); PowerLogger.start(context); - LoggerData.addData('TEST'); + LoggerData.addData('TEST', tag: 'TAG'); } bool loading = false; @@ -77,7 +77,7 @@ class _MyHomePageState extends State { : () async { loading = true; setState(() {}); - await NetTool.dio.get(_textController.text); + await NetTool.dio.get(_textController.text).catchError((e) {}); loading = false; setState(() {}); }, diff --git a/example/pubspec.lock b/example/pubspec.lock index caf273d..bbf15de 100644 --- a/example/pubspec.lock +++ b/example/pubspec.lock @@ -171,7 +171,7 @@ packages: path: ".." relative: true source: path - version: "1.0.1-nullsafety.1" + version: "1.1.0" sky_engine: dependency: transitive description: flutter diff --git a/lib/external_lib/pretty_json.dart b/lib/external_lib/pretty_json.dart index 9831967..67b87ee 100644 --- a/lib/external_lib/pretty_json.dart +++ b/lib/external_lib/pretty_json.dart @@ -4,4 +4,4 @@ String prettyJson(dynamic json, {int indent = 2}) { var spaces = ' ' * indent; var encoder = JsonEncoder.withIndent(spaces); return encoder.convert(json); -} \ No newline at end of file +} diff --git a/lib/src/builders/dio_error_builder.dart b/lib/src/builders/dio_error_builder.dart index e3993c7..cd2e8af 100644 --- a/lib/src/builders/dio_error_builder.dart +++ b/lib/src/builders/dio_error_builder.dart @@ -5,7 +5,9 @@ import 'package:power_logger/src/view/dio_error_view.dart'; ///Dio Error builder class DioErrorBuilder extends StatefulWidget { final DioError data; - DioErrorBuilder({Key? key, required this.data}) : super(key: key); + final DateTime date; + DioErrorBuilder({Key? key, required this.data, required this.date}) + : super(key: key); @override _DioErrorBuilderState createState() => _DioErrorBuilderState(); @@ -54,7 +56,7 @@ class _DioErrorBuilderState extends State { ), ), subtitle: Text( - widget.data.message, + '${widget.data.message}\n${widget.date}', style: TextStyle( color: Colors.black45, fontWeight: FontWeight.w300, diff --git a/lib/src/builders/dio_responses_builder.dart b/lib/src/builders/dio_responses_builder.dart index 61b2280..154d0d3 100644 --- a/lib/src/builders/dio_responses_builder.dart +++ b/lib/src/builders/dio_responses_builder.dart @@ -5,7 +5,9 @@ import 'package:power_logger/src/view/dio_response_view.dart'; /// Dio Response Builder class DioResponseBuilder extends StatefulWidget { final Response data; - DioResponseBuilder({Key? key,required this.data}) : super(key: key); + final DateTime date; + DioResponseBuilder({Key? key, required this.data, required this.date}) + : super(key: key); @override _DioResponseBuilderState createState() => _DioResponseBuilderState(); @@ -32,6 +34,7 @@ class _DioResponseBuilderState extends State { color: Colors.black87, ), ), + subtitle: Text(widget.date.toString()), trailing: Row( mainAxisSize: MainAxisSize.min, children: [ diff --git a/lib/src/data/logger_data.dart b/lib/src/data/logger_data.dart index 0dfa8df..c5ac1c3 100644 --- a/lib/src/data/logger_data.dart +++ b/lib/src/data/logger_data.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:logger/logger.dart'; + import 'package:power_logger/power_logger.dart'; /// logger data storage @@ -15,10 +16,10 @@ class LoggerData { static Logger _logger = Logger(); /// the real logger data. - static List get data => _listenableData.value; + static List get data => _listenableData.value; - static _LoggerDataNotifer> _listenableData = - _LoggerDataNotifer([]); + static _LoggerDataNotifer> _listenableData = + _LoggerDataNotifer>([]); /// set the logger max number. static setMax(int max) { @@ -26,14 +27,20 @@ class LoggerData { } /// add data to logger - static addData(dynamic data) { + static addData(dynamic data, {String? tag}) { if (PowerLogger.debug) { if (_listenableData.value.length < _maxLength) { - _listenableData.value.insert(0, data); + _listenableData.value.insert( + 0, + LoggerInnerData(rawData: data, date: DateTime.now(), tag: tag), + ); _listenableData.notify(); } else { _listenableData.value.removeLast(); - _listenableData.value.insert(0, data); + _listenableData.value.insert( + 0, + LoggerInnerData(rawData: data, date: DateTime.now(), tag: tag), + ); _listenableData.notify(); } } @@ -56,6 +63,17 @@ class LoggerData { } class _LoggerDataNotifer extends ValueNotifier { - _LoggerDataNotifer(value) : super(value); + _LoggerDataNotifer(T value) : super(value); notify() => this.notifyListeners(); } + +class LoggerInnerData { + final dynamic? rawData; + final DateTime date; + final String? tag; + LoggerInnerData({ + required this.rawData, + required this.date, + this.tag, + }); +} diff --git a/lib/src/parser/data_parser.dart b/lib/src/parser/data_parser.dart index a5a0c7a..ed5edcd 100644 --- a/lib/src/parser/data_parser.dart +++ b/lib/src/parser/data_parser.dart @@ -1,14 +1,23 @@ import 'package:dio/dio.dart'; import 'package:flutter/material.dart'; +import 'package:power_logger/power_logger.dart'; import 'package:power_logger/src/builders/dio_responses_builder.dart'; import 'package:power_logger/src/builders/dio_error_builder.dart'; import 'package:power_logger/src/parser/unfocus_parser.dart'; ///LoggerDataParser class LoggerDataParser { - static Widget builder(dynamic data) { - if (data is DioError) return DioErrorBuilder(data: data); - if (data is Response) return DioResponseBuilder(data: data); - return UnfocusParser(data: data); + static Widget builder(LoggerInnerData data) { + if (data.rawData is DioError) + return DioErrorBuilder( + data: data.rawData, + date: data.date, + ); + if (data.rawData is Response) + return DioResponseBuilder( + data: data.rawData, + date: data.date, + ); + return UnfocusParser(data: data.rawData, tag: data.tag); } } diff --git a/lib/src/parser/unfocus_parser.dart b/lib/src/parser/unfocus_parser.dart index 70ddbc2..e57892d 100644 --- a/lib/src/parser/unfocus_parser.dart +++ b/lib/src/parser/unfocus_parser.dart @@ -3,7 +3,9 @@ import 'package:flutter/material.dart'; /// UnfocusParser class UnfocusParser extends StatelessWidget { final dynamic data; - const UnfocusParser({Key? key, required this.data}) : super(key: key); + final String? tag; + const UnfocusParser({Key? key, required this.data, this.tag}) + : super(key: key); @override Widget build(BuildContext context) { @@ -24,7 +26,7 @@ class UnfocusParser extends StatelessWidget { maxLines: 1, ), trailing: Chip( - label: Text(data.runtimeType.toString()), + label: Text(tag ?? data.runtimeType.toString()), ), ), ); diff --git a/lib/src/power_logger.dart b/lib/src/power_logger.dart index 0f18b15..e6cfb8f 100644 --- a/lib/src/power_logger.dart +++ b/lib/src/power_logger.dart @@ -5,6 +5,10 @@ class PowerLogger { /// debug tag static bool debug = true; + static OverlayEntry? entry; + + static late BuildContext globalContext; + ///初始化 /// ///默认开启debug @@ -15,7 +19,8 @@ class PowerLogger { @Deprecated("use PowerLogger.start") static init(BuildContext context, {bool debug = true}) { PowerLogger.debug = debug; - if (debug) if (debug) _insertToOverlay(context); + globalContext = context; + if (debug) if (debug) _insertToOverlay(); } ///初始化 @@ -25,18 +30,33 @@ class PowerLogger { ///```dart ///PowerLogger.start(context,debug:true); ///``` - static start(BuildContext context, {bool debug = true}) { + static start( + BuildContext context, { + bool debug = true, + Alignment initAlignment = Alignment.center, + }) { WidgetsBinding.instance!.addPostFrameCallback((_) { PowerLogger.debug = debug; - if (debug) _insertToOverlay(context); + globalContext = context; + if (debug) _insertToOverlay(initAlignment: initAlignment); }); } - static _insertToOverlay(BuildContext context) { - Overlay.of(context)!.insert(OverlayEntry( + static _insertToOverlay({Alignment initAlignment = Alignment.center}) { + entry = OverlayEntry( builder: (context) { - return LoggerFab(); + return LoggerFab(initAlignment: initAlignment); }, - )); + ); + Overlay.of(globalContext)!.insert(entry!); + } + + static insertToOverlay({Alignment initAlignment = Alignment.center}) => + _insertToOverlay( + initAlignment: initAlignment, + ); + + static removeFromOverlay() { + if (entry != null) entry!.remove(); } } diff --git a/lib/src/views/info_view.dart b/lib/src/views/info_view.dart index f8ca8c7..2667414 100644 --- a/lib/src/views/info_view.dart +++ b/lib/src/views/info_view.dart @@ -119,7 +119,8 @@ class _InfoViewState extends State _buildTile('incremental', androidInfo!.version.incremental), _buildTile('release', androidInfo!.version.release), _buildTile('securityPatch', androidInfo!.version.securityPatch), - _buildTile('previewSdkInt', androidInfo!.version.previewSdkInt.toString()), + _buildTile( + 'previewSdkInt', androidInfo!.version.previewSdkInt.toString()), _buildTile('sdkInt', androidInfo!.version.sdkInt.toString()), _buildTile('systemFeatures', androidInfo!.systemFeatures.join('\n')), ])); diff --git a/lib/src/widgets/logger_fab.dart b/lib/src/widgets/logger_fab.dart index 5fd3ca9..a4fc2b2 100644 --- a/lib/src/widgets/logger_fab.dart +++ b/lib/src/widgets/logger_fab.dart @@ -1,22 +1,25 @@ import 'package:flutter/material.dart'; import 'package:flutter/physics.dart'; +import 'package:power_logger/power_logger.dart'; import 'package:power_logger/src/power_logger_view.dart'; class LoggerFab extends StatefulWidget { - LoggerFab({Key? key}) : super(key: key); + final Alignment initAlignment; + LoggerFab({Key? key, this.initAlignment = Alignment.center}) + : super(key: key); @override - _LoggerFabState createState() => _LoggerFabState(); + LoggerFabState createState() => LoggerFabState(); } -class _LoggerFabState extends State +class LoggerFabState extends State with SingleTickerProviderStateMixin { - bool showSubPage = false; AnimationController? _animationController; - var _dragAlignment = Alignment.center; + late Alignment _dragAlignment; late Animation _animation; final _spring = const SpringDescription(mass: 15, stiffness: 1000, damping: 0.7); + Alignment get alignment => _dragAlignment; double _normalizeVelocity(Offset velocity, Size size) { final normalizedVelocity = Offset( @@ -50,6 +53,7 @@ class _LoggerFabState extends State @override void initState() { super.initState(); + _dragAlignment = widget.initAlignment; _animationController = AnimationController.unbounded(vsync: this) ..addListener(() => setState(() => _dragAlignment = _animation.value)); } @@ -65,29 +69,24 @@ class _LoggerFabState extends State final size = MediaQuery.of(context).size; return Align( alignment: _dragAlignment, - child: AnimatedOpacity( - duration: Duration(milliseconds: 300), - opacity: showSubPage ? 0 : 1, - child: GestureDetector( - onPanStart: (details) => _animationController!.stop(canceled: true), - onPanUpdate: (details) => setState(() => _dragAlignment += Alignment( - details.delta.dx / (size.width / 2), - details.delta.dy / (size.height / 2), - )), - onPanEnd: (details) => - _runAnimation(details.velocity.pixelsPerSecond, size), - onTap: showSubPage - ? null - : () async { - showSubPage = true; - await Navigator.push( - context, - MaterialPageRoute(builder: (context) => PowerLoggerView()), - ); - showSubPage = false; - }, - child: const _FabButton(), - ), + child: GestureDetector( + onPanStart: (details) => _animationController!.stop(canceled: true), + onPanUpdate: (details) => setState(() => _dragAlignment += Alignment( + details.delta.dx / (size.width / 2), + details.delta.dy / (size.height / 2), + )), + onPanEnd: (details) => + _runAnimation(details.velocity.pixelsPerSecond, size), + onTap: () async { + Alignment initAlignment = _dragAlignment; + PowerLogger.removeFromOverlay(); + await Navigator.push( + context, + MaterialPageRoute(builder: (context) => PowerLoggerView()), + ); + PowerLogger.insertToOverlay(initAlignment: initAlignment); + }, + child: const _FabButton(), ), ); } diff --git a/pubspec.yaml b/pubspec.yaml index 0cea5b6..b14a681 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: power_logger description: A well log tool on flutter, support Dio and custom type data. -version: 1.0.1-nullsafety.1 +version: 1.1.0 homepage: https://github.com/laiiihz/power_logger environment: @@ -10,7 +10,7 @@ environment: dependencies: flutter: sdk: flutter - dio: ^4.0.0-prev3 + dio: ^4.0.0 flutter_highlight: ^0.7.0 device_info: ^2.0.0 package_info: ^2.0.0