update example app.set logger data listenable.

master
小赖 4 years ago
parent cc42a73919
commit 97476abb35

@ -1,5 +1,10 @@
# CHANGELOG # CHANGELOG
## [0.1.3]
* listenable value.
* update example app.
## [0.1.2] ## [0.1.2]
* performance enhanced. * performance enhanced.

@ -9,10 +9,7 @@ A well log tool on flutter, support Dio and custom type data.
* init logger * init logger
```dart ```dart
Future.delayed( PowerLogger.start(context);
Duration(milliseconds: 300),
() => PowerLogger.init(context),
);
``` ```
* add data to logger * add data to logger

@ -28,29 +28,17 @@ class _MyHomePageState extends State<MyHomePage> {
@override @override
void initState() { void initState() {
super.initState(); super.initState();
Dio().get("https://192.168.31.255:8888").catchError((e) { NetTool.init();
LoggerData.addData(e); NetTool.dio.get("https://192.168.31.255:8888");
}); NetTool.dio.get("https://www.baidu.com", queryParameters: {'test': 'test'});
Dio().get("https://www.baidu.com", queryParameters: {'test': 'test'}).then( NetTool.dio.get("https://www.baidu.com/ahefbawfbe.html");
(e) { NetTool.dio.post(
LoggerData.addData(e); "https://www.baidu.com/ahefbawfbe.html",
}); data: FormData.fromMap({'test': 'test'}),
Dio().get("https://www.baidu.com/ahefbawfbe.html").catchError((e) { );
LoggerData.addData(e);
});
Dio()
.post("https://www.baidu.com/ahefbawfbe.html",
data: FormData.fromMap({'test': 'test'}))
.catchError((e) {
LoggerData.addData(e);
});
Dio() NetTool.dio.get(
.get( "https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png");
"https://www.baidu.com/img/PCtm_d9c8750bed0b3c7d089fa7d55720d6cf.png")
.then((e) {
LoggerData.addData(e);
});
PowerLogger.start(context); PowerLogger.start(context);
LoggerData.addData('TEST'); LoggerData.addData('TEST');
} }
@ -64,3 +52,22 @@ class _MyHomePageState extends State<MyHomePage> {
); );
} }
} }
class NetTool {
static Dio dio = Dio();
static init() {
dio.interceptors.add(InterceptorsWrapper(
onRequest: (options) async {
return options;
},
onResponse: (response) async {
LoggerData.addData(response);
return response;
},
onError: (DioError e) async {
LoggerData.addData(e);
return e;
},
));
}
}

@ -1,13 +1,15 @@
import 'package:flutter/foundation.dart';
import 'package:power_logger/power_logger.dart'; import 'package:power_logger/power_logger.dart';
/// logger data storage /// logger data storage
class LoggerData { class LoggerData {
static int _maxLength = 100; static int _maxLength = 100;
static List<dynamic> _data = [];
/// the real logger data. /// the real logger data.
static List<dynamic> get data => _data; static List<dynamic> get data => _listenableData.value;
static _LoggerDataNotifer<List<dynamic>> _listenableData =
_LoggerDataNotifer([]);
/// set the logger max number. /// set the logger max number.
static setMax(int max) { static setMax(int max) {
@ -17,15 +19,32 @@ class LoggerData {
/// add data to logger /// add data to logger
static addData(dynamic data) { static addData(dynamic data) {
if (PowerLogger.debug) { if (PowerLogger.debug) {
if (_data.length < _maxLength) if (_listenableData.value.length < _maxLength) {
_data.insert(0, data); _listenableData.value.insert(0, data);
else { _listenableData.notify();
_data.removeLast(); } else {
_data.insert(0, data); _listenableData.value.removeLast();
_listenableData.value.insert(0, data);
_listenableData.notify();
} }
} }
} }
/// clear all logger /// clear all logger
static clear() => _data.clear(); static clear() => _listenableData.value.clear();
/// add data listener
static addListener(Function listener) {
_listenableData.addListener(listener);
}
/// remove data listener
static removeListener(Function listener) {
_listenableData.removeListener(listener);
}
}
class _LoggerDataNotifer<T> extends ValueNotifier {
_LoggerDataNotifer(value) : super(value);
notify() => this.notifyListeners();
} }

@ -1,5 +1,3 @@
import 'dart:ui';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:power_logger/src/widgets/logger_fab.dart'; import 'package:power_logger/src/widgets/logger_fab.dart';

@ -132,14 +132,14 @@ class _DioResponseViewState extends State<DioResponseView> {
); );
} }
_buildSingleText(String title, String data) { // _buildSingleText(String title, String data) {
return data == null || data.length == 0 // return data == null || data.length == 0
? SizedBox() // ? SizedBox()
: BoxView( // : BoxView(
title: Text('Params'), // title: Text('Params'),
child: SelectableText(data.toString()), // child: SelectableText(data.toString()),
); // );
} // }
@override @override
void initState() { void initState() {

@ -10,6 +10,20 @@ class LoggerView extends StatefulWidget {
} }
class _LoggerViewState extends State<LoggerView> { class _LoggerViewState extends State<LoggerView> {
updateList() => setState(() {});
@override
void initState() {
super.initState();
LoggerData.addListener(updateList);
}
@override
void dispose() {
LoggerData.removeListener(updateList);
super.dispose();
}
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
return ListView.builder( return ListView.builder(

Loading…
Cancel
Save