diff --git a/CHANGELOG.md b/CHANGELOG.md index 9e9756fa..b84911aa 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ # 0.4.0 * [BREAKING_CHANGE] Updated dart min version to 3.0.0. +* [BREAKING_CHANGE] Removed `darkTheme` parameter. Alice will now automatically detect the color scheme. * Updated dependencies. * Fixed lints. * Updated example. diff --git a/example/lib/main.dart b/example/lib/main.dart index ad07bc00..03cf7136 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -33,7 +33,6 @@ class _MyAppState extends State { _alice = Alice( showNotification: true, showInspectorOnShake: true, - darkTheme: false, maxCallsCount: 1000, ); _dio = Dio(BaseOptions( diff --git a/lib/alice.dart b/lib/alice.dart index 26b75dbf..53548616 100644 --- a/lib/alice.dart +++ b/lib/alice.dart @@ -23,9 +23,6 @@ class Alice { /// with sensors) final bool showInspectorOnShake; - /// Should inspector use dark theme - final bool darkTheme; - /// Icon url for notification final String notificationIcon; @@ -50,7 +47,6 @@ class Alice { GlobalKey? navigatorKey, this.showNotification = true, this.showInspectorOnShake = false, - this.darkTheme = false, this.notificationIcon = '@mipmap/ic_launcher', this.maxCallsCount = 1000, this.directionality, @@ -61,7 +57,6 @@ class Alice { _navigatorKey, showNotification: showNotification, showInspectorOnShake: showInspectorOnShake, - darkTheme: darkTheme, notificationIcon: notificationIcon, maxCallsCount: maxCallsCount, directionality: directionality, diff --git a/lib/core/alice_core.dart b/lib/core/alice_core.dart index 9af7187a..fd860a6d 100644 --- a/lib/core/alice_core.dart +++ b/lib/core/alice_core.dart @@ -23,9 +23,6 @@ class AliceCore { /// with sensors) final bool showInspectorOnShake; - /// Should inspector use dark theme - final bool darkTheme; - /// Rx subject which contains all intercepted http calls final BehaviorSubject> callsSubject = BehaviorSubject.seeded([]); @@ -47,7 +44,6 @@ class AliceCore { late FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin; GlobalKey? navigatorKey; - Brightness _brightness = Brightness.light; bool _isInspectorOpened = false; ShakeDetector? _shakeDetector; StreamSubscription? _callsSubscription; @@ -60,7 +56,6 @@ class AliceCore { this.navigatorKey, { required this.showNotification, required this.showInspectorOnShake, - required this.darkTheme, required this.notificationIcon, required this.maxCallsCount, this.directionality, @@ -78,7 +73,6 @@ class AliceCore { shakeThresholdGravity: 4, ); } - _brightness = darkTheme ? Brightness.dark : Brightness.light; } /// Dispose subjects and subscriptions @@ -88,9 +82,6 @@ class AliceCore { _callsSubscription?.cancel(); } - /// Get currently used brightness - Brightness get brightness => _brightness; - void _initializeNotificationsPlugin() { _flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin(); final initializationSettingsAndroid = @@ -311,7 +302,7 @@ class AliceCore { /// Save all calls to file void saveHttpRequests(BuildContext context) { - AliceSaveHelper.saveCalls(context, callsSubject.value, _brightness); + AliceSaveHelper.saveCalls(context, callsSubject.value); } /// Adds new log to Alice logger. diff --git a/lib/helper/alice_alert_helper.dart b/lib/helper/alice_alert_helper.dart index e0b69c60..e0ff7ca0 100644 --- a/lib/helper/alice_alert_helper.dart +++ b/lib/helper/alice_alert_helper.dart @@ -1,3 +1,4 @@ +import 'package:alice/utils/alice_theme.dart'; import 'package:flutter/material.dart'; class AliceAlertHelper { @@ -10,7 +11,6 @@ class AliceAlertHelper { String? secondButtonTitle, Function? firstButtonAction, Function? secondButtonAction, - Brightness? brightness, }) { final actions = [ TextButton( @@ -39,7 +39,7 @@ class AliceAlertHelper { builder: (BuildContext buildContext) { return Theme( data: ThemeData( - brightness: brightness ?? Brightness.light, + colorScheme: AliceTheme.getColorScheme(), ), child: AlertDialog( title: Text(title), diff --git a/lib/helper/alice_save_helper.dart b/lib/helper/alice_save_helper.dart index 5bed4df8..1112e368 100644 --- a/lib/helper/alice_save_helper.dart +++ b/lib/helper/alice_save_helper.dart @@ -20,34 +20,27 @@ class AliceSaveHelper { static void saveCalls( BuildContext context, List calls, - Brightness brightness, ) { - _checkPermissions(context, calls, brightness); + _checkPermissions(context, calls); } static Future _checkPermissions( BuildContext context, List calls, - Brightness brightness, ) async { final status = await Permission.storage.status; if (status.isGranted) { - await _saveToFile( - context, - calls, - brightness, - ); + await _saveToFile(context, calls); } else { final status = await Permission.storage.request(); if (status.isGranted) { - await _saveToFile(context, calls, brightness); + await _saveToFile(context, calls); } else { AliceAlertHelper.showAlert( context, 'Permission error', "Permission not granted. Couldn't save logs.", - brightness: brightness, ); } } @@ -56,7 +49,6 @@ class AliceSaveHelper { static Future _saveToFile( BuildContext context, List calls, - Brightness brightness, ) async { try { if (calls.isEmpty) { @@ -64,7 +56,6 @@ class AliceSaveHelper { context, 'Error', 'There are no logs to save', - brightness: brightness, ); return ''; } @@ -94,7 +85,6 @@ class AliceSaveHelper { secondButtonTitle: isAndroid ? 'View file' : null, secondButtonAction: () => isAndroid ? OpenFilex.open(file.path) : null, - brightness: brightness, ); return file.path; } else { @@ -109,7 +99,6 @@ class AliceSaveHelper { context, 'Error', 'Failed to save http calls to file', - brightness: brightness, ); AliceUtils.log(exception.toString()); } diff --git a/lib/ui/page/alice_call_details_screen.dart b/lib/ui/page/alice_call_details_screen.dart index 60c81bb0..3c7886cd 100644 --- a/lib/ui/page/alice_call_details_screen.dart +++ b/lib/ui/page/alice_call_details_screen.dart @@ -6,6 +6,7 @@ import 'package:alice/ui/widget/alice_call_overview_widget.dart'; import 'package:alice/ui/widget/alice_call_request_widget.dart'; import 'package:alice/ui/widget/alice_call_response_widget.dart'; import 'package:alice/utils/alice_constants.dart'; +import 'package:alice/utils/alice_theme.dart'; import 'package:collection/collection.dart' show IterableExtension; import 'package:flutter/material.dart'; import 'package:share_plus/share_plus.dart'; @@ -37,8 +38,7 @@ class _AliceCallDetailsScreenState extends State textDirection: widget.core.directionality ?? Directionality.of(context), child: Theme( data: ThemeData( - brightness: widget.core.brightness, - colorScheme: ColorScheme.light(secondary: AliceConstants.lightRed), + colorScheme: AliceTheme.getColorScheme(), ), child: StreamBuilder>( stream: widget.core.callsSubject, diff --git a/lib/ui/page/alice_calls_list_screen.dart b/lib/ui/page/alice_calls_list_screen.dart index 21d96ca6..23074a19 100644 --- a/lib/ui/page/alice_calls_list_screen.dart +++ b/lib/ui/page/alice_calls_list_screen.dart @@ -11,6 +11,7 @@ import 'package:alice/ui/widget/alice_call_list_item_widget.dart'; import 'package:alice/ui/widget/alice_log_list_widget.dart'; import 'package:alice/ui/widget/alice_raw_log_list_widger.dart'; import 'package:alice/utils/alice_constants.dart'; +import 'package:alice/utils/alice_theme.dart'; import 'package:flutter/material.dart'; class AliceCallsListScreen extends StatefulWidget { @@ -83,8 +84,7 @@ class _AliceCallsListScreenState extends State widget._aliceCore.directionality ?? Directionality.of(context), child: Theme( data: ThemeData( - brightness: widget._aliceCore.brightness, - colorScheme: ColorScheme.light(secondary: AliceConstants.lightRed), + colorScheme: AliceTheme.getColorScheme(), ), child: Scaffold( appBar: AppBar( diff --git a/lib/ui/page/alice_stats_screen.dart b/lib/ui/page/alice_stats_screen.dart index d264ff91..982e4753 100644 --- a/lib/ui/page/alice_stats_screen.dart +++ b/lib/ui/page/alice_stats_screen.dart @@ -1,7 +1,7 @@ import 'package:alice/core/alice_core.dart'; import 'package:alice/helper/alice_conversion_helper.dart'; import 'package:alice/model/alice_http_call.dart'; -import 'package:alice/utils/alice_constants.dart'; +import 'package:alice/utils/alice_theme.dart'; import 'package:flutter/material.dart'; class AliceStatsScreen extends StatelessWidget { @@ -14,10 +14,7 @@ class AliceStatsScreen extends StatelessWidget { return Directionality( textDirection: aliceCore.directionality ?? Directionality.of(context), child: Theme( - data: ThemeData( - brightness: aliceCore.brightness, - colorScheme: ColorScheme.light(secondary: AliceConstants.lightRed), - ), + data: ThemeData(colorScheme: AliceTheme.getColorScheme()), child: Scaffold( appBar: AppBar( title: const Text('Alice - HTTP Inspector - Stats'), diff --git a/lib/ui/widget/alice_log_list_widget.dart b/lib/ui/widget/alice_log_list_widget.dart index 04ada72a..a5423aed 100644 --- a/lib/ui/widget/alice_log_list_widget.dart +++ b/lib/ui/widget/alice_log_list_widget.dart @@ -2,6 +2,7 @@ import 'dart:convert'; import 'dart:ui'; import 'package:alice/model/alice_log.dart'; +import 'package:alice/utils/alice_theme.dart'; import 'package:flutter/foundation.dart'; import 'package:flutter/material.dart'; import 'package:flutter/services.dart'; @@ -123,27 +124,7 @@ class AliceLogEntryWidget extends StatelessWidget { } Color _getTextColor(BuildContext context) { - final theme = Theme.of(context); - switch (log.level) { - case DiagnosticLevel.hidden: - return Colors.grey; - case DiagnosticLevel.fine: - return Colors.grey; - case DiagnosticLevel.debug: - return Colors.black; - case DiagnosticLevel.info: - return Colors.black; - case DiagnosticLevel.warning: - return Colors.orange; - case DiagnosticLevel.hint: - return Colors.grey; - case DiagnosticLevel.summary: - return Colors.black; - case DiagnosticLevel.error: - return theme.colorScheme.error; - case DiagnosticLevel.off: - return Colors.purple; - } + return AliceTheme.getTextColor(context, log.level); } IconData _getLogIcon(DiagnosticLevel level) { diff --git a/lib/utils/alice_theme.dart b/lib/utils/alice_theme.dart new file mode 100644 index 00000000..de84c36b --- /dev/null +++ b/lib/utils/alice_theme.dart @@ -0,0 +1,44 @@ +import 'package:alice/utils/alice_constants.dart'; +import 'package:flutter/material.dart'; +import 'package:flutter/scheduler.dart'; + +class AliceTheme { + static bool _isDarkMode() { + final brightness = + SchedulerBinding.instance.platformDispatcher.platformBrightness; + final isDarkMode = brightness == Brightness.dark; + return isDarkMode; + } + + static ColorScheme getColorScheme() { + if (_isDarkMode()) { + return ColorScheme.dark(primary: AliceConstants.lightRed); + } else { + return ColorScheme.light(primary: AliceConstants.lightRed); + } + } + + static Color getTextColor(BuildContext context, DiagnosticLevel level) { + final theme = Theme.of(context); + switch (level) { + case DiagnosticLevel.hidden: + return Colors.grey; + case DiagnosticLevel.fine: + return Colors.grey; + case DiagnosticLevel.debug: + return Theme.of(context).colorScheme.onSurface; + case DiagnosticLevel.info: + return Theme.of(context).colorScheme.onSurface; + case DiagnosticLevel.warning: + return Colors.orange; + case DiagnosticLevel.hint: + return Colors.grey; + case DiagnosticLevel.summary: + return Theme.of(context).colorScheme.onSurface; + case DiagnosticLevel.error: + return theme.colorScheme.error; + case DiagnosticLevel.off: + return Colors.purple; + } + } +}