Skip to content

Commit

Permalink
fix: fixed dark mode
Browse files Browse the repository at this point in the history
  • Loading branch information
jhomlala committed Nov 12, 2023
1 parent 40193de commit c2b1bc5
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
1 change: 0 additions & 1 deletion example/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ class _MyAppState extends State<MyApp> {
_alice = Alice(
showNotification: true,
showInspectorOnShake: true,
darkTheme: false,
maxCallsCount: 1000,
);
_dio = Dio(BaseOptions(
Expand Down
5 changes: 0 additions & 5 deletions lib/alice.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -50,7 +47,6 @@ class Alice {
GlobalKey<NavigatorState>? navigatorKey,
this.showNotification = true,
this.showInspectorOnShake = false,
this.darkTheme = false,
this.notificationIcon = '@mipmap/ic_launcher',
this.maxCallsCount = 1000,
this.directionality,
Expand All @@ -61,7 +57,6 @@ class Alice {
_navigatorKey,
showNotification: showNotification,
showInspectorOnShake: showInspectorOnShake,
darkTheme: darkTheme,
notificationIcon: notificationIcon,
maxCallsCount: maxCallsCount,
directionality: directionality,
Expand Down
11 changes: 1 addition & 10 deletions lib/core/alice_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<List<AliceHttpCall>> callsSubject =
BehaviorSubject.seeded([]);
Expand All @@ -47,7 +44,6 @@ class AliceCore {

late FlutterLocalNotificationsPlugin _flutterLocalNotificationsPlugin;
GlobalKey<NavigatorState>? navigatorKey;
Brightness _brightness = Brightness.light;
bool _isInspectorOpened = false;
ShakeDetector? _shakeDetector;
StreamSubscription<dynamic>? _callsSubscription;
Expand All @@ -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,
Expand All @@ -78,7 +73,6 @@ class AliceCore {
shakeThresholdGravity: 4,
);
}
_brightness = darkTheme ? Brightness.dark : Brightness.light;
}

/// Dispose subjects and subscriptions
Expand All @@ -88,9 +82,6 @@ class AliceCore {
_callsSubscription?.cancel();
}

/// Get currently used brightness
Brightness get brightness => _brightness;

void _initializeNotificationsPlugin() {
_flutterLocalNotificationsPlugin = FlutterLocalNotificationsPlugin();
final initializationSettingsAndroid =
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions lib/helper/alice_alert_helper.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import 'package:alice/utils/alice_theme.dart';
import 'package:flutter/material.dart';

class AliceAlertHelper {
Expand All @@ -10,7 +11,6 @@ class AliceAlertHelper {
String? secondButtonTitle,
Function? firstButtonAction,
Function? secondButtonAction,
Brightness? brightness,
}) {
final actions = <Widget>[
TextButton(
Expand Down Expand Up @@ -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),
Expand Down
17 changes: 3 additions & 14 deletions lib/helper/alice_save_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,34 +20,27 @@ class AliceSaveHelper {
static void saveCalls(
BuildContext context,
List<AliceHttpCall> calls,
Brightness brightness,
) {
_checkPermissions(context, calls, brightness);
_checkPermissions(context, calls);
}

static Future<void> _checkPermissions(
BuildContext context,
List<AliceHttpCall> 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,
);
}
}
Expand All @@ -56,15 +49,13 @@ class AliceSaveHelper {
static Future<String> _saveToFile(
BuildContext context,
List<AliceHttpCall> calls,
Brightness brightness,
) async {
try {
if (calls.isEmpty) {
AliceAlertHelper.showAlert(
context,
'Error',
'There are no logs to save',
brightness: brightness,
);
return '';
}
Expand Down Expand Up @@ -94,7 +85,6 @@ class AliceSaveHelper {
secondButtonTitle: isAndroid ? 'View file' : null,
secondButtonAction: () =>
isAndroid ? OpenFilex.open(file.path) : null,
brightness: brightness,
);
return file.path;
} else {
Expand All @@ -109,7 +99,6 @@ class AliceSaveHelper {
context,
'Error',
'Failed to save http calls to file',
brightness: brightness,
);
AliceUtils.log(exception.toString());
}
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/page/alice_call_details_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -37,8 +38,7 @@ class _AliceCallDetailsScreenState extends State<AliceCallDetailsScreen>
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<List<AliceHttpCall>>(
stream: widget.core.callsSubject,
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/page/alice_calls_list_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -83,8 +84,7 @@ class _AliceCallsListScreenState extends State<AliceCallsListScreen>
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(
Expand Down
7 changes: 2 additions & 5 deletions lib/ui/page/alice_stats_screen.dart
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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'),
Expand Down
23 changes: 2 additions & 21 deletions lib/ui/widget/alice_log_list_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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) {
Expand Down
44 changes: 44 additions & 0 deletions lib/utils/alice_theme.dart
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit c2b1bc5

Please sign in to comment.