Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
tomaszpolanski committed Dec 1, 2023
1 parent c5407c3 commit 536cd1c
Show file tree
Hide file tree
Showing 30 changed files with 432 additions and 397 deletions.
4 changes: 2 additions & 2 deletions lib/alice.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
export 'package:alice/src/model/alice_log.dart';
export 'package:alice/src/alice.dart';
export 'package:alice/src/core/alice_core.dart';
export 'package:alice/src/core/http_client/alice_http_client_extensions.dart';
export 'package:alice/src/core/http/alice_http_extensions.dart';
export 'package:alice/src/core/http_client/alice_http_client_extensions.dart';
export 'package:alice/src/model/alice_log.dart';
3 changes: 2 additions & 1 deletion lib/src/alice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ class Alice {
///method queue will be used to remove elements.
final int maxCallsCount;

///Directionality of app. Directionality of the app will be used if set to null.
///Directionality of app. Directionality of the app will be used if set to
/// null.
final TextDirection? directionality;

///Flag used to show/hide share button
Expand Down
9 changes: 5 additions & 4 deletions lib/src/core/alice_core.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore_for_file: cascade_invocations
import 'dart:async';

import 'package:alice/src/core/alice_logger.dart';
Expand Down Expand Up @@ -32,7 +33,7 @@ class AliceCore {

final Brightness _brightness;
bool _isInspectorOpened = false;
StreamSubscription? _callsSubscription;
StreamSubscription<dynamic>? _callsSubscription;

/// Creates alice core instance
AliceCore({
Expand Down Expand Up @@ -88,7 +89,7 @@ class AliceCore {
final selectedCall = _selectCall(requestId);

if (selectedCall == null) {
debugPrint("Selected call is null");
debugPrint('Selected call is null');
return;
}

Expand All @@ -100,10 +101,10 @@ class AliceCore {

/// Add response to existing alice http call
void addResponse(AliceHttpResponse response, Object requestId) {
final AliceHttpCall? selectedCall = _selectCall(requestId);
final selectedCall = _selectCall(requestId);

if (selectedCall == null) {
debugPrint("Selected call is null");
debugPrint('Selected call is null');
return;
}
selectedCall.loading = false;
Expand Down
5 changes: 3 additions & 2 deletions lib/src/core/alice_logger.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ class AliceLogger {

/// The maximum number of logs to store or `null` for unlimited storage.
///
/// If more logs arrive, the oldest ones (based on their [AliceLog.timestamp]) will
/// If more logs arrive, the oldest ones (based on their [AliceLog.timestamp])
/// will
/// be removed.
int? get maximumSize => _maximumSize;

Expand Down Expand Up @@ -46,7 +47,7 @@ class AliceLogger {
min = mid + 1;
}
}
assert(min == max);
assert(min == max, 'Invalid');
index = min;
}

Expand Down
25 changes: 13 additions & 12 deletions lib/src/core/http/alice_http_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore_for_file: cascade_invocations
import 'dart:convert';

import 'package:alice/src/core/alice_core.dart';
Expand All @@ -15,46 +16,46 @@ class AliceHttpAdapter {
AliceHttpAdapter(this.aliceCore);

void onRequest(http.BaseRequest request, {dynamic body, Object? id}) {
final AliceHttpCall call = AliceHttpCall(id ?? request.hashCode);
final call = AliceHttpCall(id ?? request.hashCode);
call.loading = true;
call.client = "HttpClient (http package)";
call.client = 'HttpClient (http package)';
call.uri = request.url.toString();
call.method = request.method;
var path = request.url.path;
if (path.isEmpty) {
path = "/";
path = '/';
}
call.endpoint = path;

call.server = request.url.host;
if (request.url.scheme == "https") {
if (request.url.scheme == 'https') {
call.secure = true;
}

final AliceHttpRequest httpRequest = AliceHttpRequest();
final httpRequest = AliceHttpRequest();

if (request is http.Request) {
// we are guaranteed` the existence of body and headers
if (body != null) {
httpRequest.body = body;
}
// ignore: cast_nullable_to_non_nullable
httpRequest.body = body ?? request.body ?? "";
httpRequest.body = body ?? request.body ?? '';
httpRequest.size = utf8.encode(httpRequest.body.toString()).length;
httpRequest.headers = Map<String, dynamic>.from(request.headers);
} else if (body == null) {
httpRequest.size = 0;
httpRequest.body = "";
httpRequest.body = '';
} else {
httpRequest.size = utf8.encode(body.toString()).length;
httpRequest.body = body;
}

httpRequest.time = DateTime.now();

String? contentType = "unknown";
if (httpRequest.headers.containsKey("Content-Type")) {
contentType = httpRequest.headers["Content-Type"] as String?;
String? contentType = 'unknown';
if (httpRequest.headers.containsKey('Content-Type')) {
contentType = httpRequest.headers['Content-Type'] as String?;
}

httpRequest.contentType = contentType;
Expand All @@ -77,9 +78,9 @@ class AliceHttpAdapter {
httpResponse.size = utf8.encode((body ?? '').toString()).length;
}
httpResponse.time = DateTime.now();
final Map<String, String> responseHeaders = {};
final responseHeaders = <String, String>{};
response.headers.forEach((header, values) {
responseHeaders[header] = values.toString();
responseHeaders[header] = values;
});
httpResponse.headers = responseHeaders;
aliceCore.addResponse(httpResponse, id ?? response.hashCode);
Expand Down
5 changes: 3 additions & 2 deletions lib/src/core/http/alice_http_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ import 'package:alice/src/alice.dart';
import 'package:http/http.dart';

extension AliceHttpExtensions on Future<Response> {
/// Intercept http request with alice. This extension method provides additional
/// Intercept http request with alice. This extension method provides addition
/// al
/// helpful method to intercept https' response.
Future<Response> interceptWithAlice(Alice alice, {dynamic body}) async {
final Response response = await this;
final response = await this;
alice.onHttpResponse(response, body: body);
return response;
}
Expand Down
29 changes: 15 additions & 14 deletions lib/src/core/http_client/alice_http_client_adapter.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// ignore_for_file: cascade_invocations
import 'dart:convert';
import 'dart:io';

Expand All @@ -15,41 +16,41 @@ class AliceHttpClientAdapter {

/// Handles httpClientRequest and creates http alice call from it
void onRequest(HttpClientRequest request, {dynamic body}) {
final AliceHttpCall call = AliceHttpCall(request.hashCode);
final call = AliceHttpCall(request.hashCode);
call.loading = true;
call.client = "HttpClient (io package)";
call.client = 'HttpClient (io package)';
call.method = request.method;
call.uri = request.uri.toString();

var path = request.uri.path;
if (path.isEmpty) {
path = "/";
path = '/';
}

call.endpoint = path;
call.server = request.uri.host;
if (request.uri.scheme == "https") {
if (request.uri.scheme == 'https') {
call.secure = true;
}
final AliceHttpRequest httpRequest = AliceHttpRequest();
final httpRequest = AliceHttpRequest();
if (body == null) {
httpRequest.size = 0;
httpRequest.body = "";
httpRequest.body = '';
} else {
httpRequest.size = utf8.encode(body.toString()).length;
httpRequest.body = body;
}
httpRequest.time = DateTime.now();
final Map<String, dynamic> headers = <String, dynamic>{};
final headers = <String, dynamic>{};

httpRequest.headers.forEach((header, dynamic value) {
headers[header] = value;
});

httpRequest.headers = headers;
String? contentType = "unknown";
if (headers.containsKey("Content-Type")) {
contentType = headers["Content-Type"] as String?;
String? contentType = 'unknown';
if (headers.containsKey('Content-Type')) {
contentType = headers['Content-Type'] as String?;
}

httpRequest.contentType = contentType;
Expand All @@ -61,23 +62,23 @@ class AliceHttpClientAdapter {
}

/// Handles httpClientRequest and adds response to http alice call
void onResponse(
Future<void> onResponse(
HttpClientResponse response,
HttpClientRequest request, {
dynamic body,
}) async {
final AliceHttpResponse httpResponse = AliceHttpResponse();
final httpResponse = AliceHttpResponse();
httpResponse.status = response.statusCode;

if (body != null) {
httpResponse.body = body;
httpResponse.size = utf8.encode(body.toString()).length;
} else {
httpResponse.body = "";
httpResponse.body = '';
httpResponse.size = 0;
}
httpResponse.time = DateTime.now();
final Map<String, String> headers = {};
final headers = <String, String>{};
response.headers.forEach((header, values) {
headers[header] = values.toString();
});
Expand Down
5 changes: 3 additions & 2 deletions lib/src/core/http_client/alice_http_client_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ import 'dart:io';
import 'package:alice/src/alice.dart';

extension AliceHttpClientExtensions on Future<HttpClientRequest> {
/// Intercept http client with alice. This extension method provides additional
/// Intercept http client with alice. This extension method provides additiona
/// l
/// helpful method to intercept httpClientResponse.
Future<HttpClientResponse> interceptWithAlice(
Alice alice, {
dynamic body,
Map<String, dynamic>? headers,
}) async {
final HttpClientRequest request = await this;
final request = await this;
if (body != null) {
request.write(body);
}
Expand Down
6 changes: 4 additions & 2 deletions lib/src/helper/alice_alert_helper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
// ignore_for_file: cascade_invocations, avoid_dynamic_calls

import 'package:flutter/material.dart';

class AliceAlertHelper {
Expand All @@ -6,13 +8,13 @@ class AliceAlertHelper {
BuildContext context,
String title,
String description, {
String firstButtonTitle = "Accept",
String firstButtonTitle = 'Accept',
String? secondButtonTitle,
Function? firstButtonAction,
Function? secondButtonAction,
Brightness? brightness,
}) {
final List<Widget> actions = [];
final actions = <Widget>[];
actions.add(
TextButton(
onPressed: () {
Expand Down
20 changes: 10 additions & 10 deletions lib/src/helper/alice_conversion_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,35 +7,35 @@ class AliceConversionHelper {
/// Format bytes text
static String formatBytes(int bytes) {
if (bytes < 0) {
return "-1 B";
return '-1 B';
}
if (bytes <= _kilobyteAsByte) {
return "$bytes B";
return '$bytes B';
}
if (bytes <= _megabyteAsByte) {
return "${_formatDouble(bytes / _kilobyteAsByte)} kB";
return '${_formatDouble(bytes / _kilobyteAsByte)} kB';
}

return "${_formatDouble(bytes / _megabyteAsByte)} MB";
return '${_formatDouble(bytes / _megabyteAsByte)} MB';
}

static String _formatDouble(double value) => value.toStringAsFixed(2);

/// Format time in milliseconds
static String formatTime(int timeInMillis) {
if (timeInMillis < 0) {
return "-1 ms";
return '-1 ms';
}
if (timeInMillis <= _secondAsMillisecond) {
return "$timeInMillis ms";
return '$timeInMillis ms';
}
if (timeInMillis <= _minuteAsMillisecond) {
return "${_formatDouble(timeInMillis / _secondAsMillisecond)} s";
return '${_formatDouble(timeInMillis / _secondAsMillisecond)} s';
}

final Duration duration = Duration(milliseconds: timeInMillis);
final duration = Duration(milliseconds: timeInMillis);

return "${duration.inMinutes} min ${duration.inSeconds.remainder(60)} s "
"${duration.inMilliseconds.remainder(1000)} ms";
return '${duration.inMinutes} min ${duration.inSeconds.remainder(60)} s '
'${duration.inMilliseconds.remainder(1000)} ms';
}
}
Loading

0 comments on commit 536cd1c

Please sign in to comment.