Skip to content

Commit

Permalink
cleanup main
Browse files Browse the repository at this point in the history
  • Loading branch information
mocodesmo committed May 2, 2024
1 parent 1133a2c commit 5a4cb9a
Show file tree
Hide file tree
Showing 4 changed files with 110 additions and 141 deletions.
79 changes: 79 additions & 0 deletions lib/_ui/security_overlay.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import 'package:bb_mobile/locator.dart';
import 'package:bb_mobile/styles.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:no_screenshot/no_screenshot.dart';

class AppLifecycleOverlay extends StatefulWidget {
const AppLifecycleOverlay({super.key, required this.child});

final Widget child;

@override
State<AppLifecycleOverlay> createState() => _AppLifecycleOverlayState();
}

class _AppLifecycleOverlayState extends State<AppLifecycleOverlay>
with WidgetsBindingObserver {
bool shouldBlur = false;
final _noScreenshot = NoScreenshot.instance;

final sensitivePaths = [
'/home/import',
'/home/wallet/wallet-settings/open-backup',
'/home/wallet/wallet-settings/wallet-settings/backup',
'/home/wallet/wallet-settings/wallet-settings/test-backup',
'/home/wallet-settings/open-backup',
'/home/wallet-settings/wallet-settings/backup',
'/home/wallet-settings/wallet-settings/test-backup',
];

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);

locator<GoRouter>().routerDelegate.addListener(() {
final routePath = locator<GoRouter>()
.routerDelegate
.currentConfiguration
.routes
.map((RouteBase e) => (e as GoRoute).path)
.join();
// print(routePath);
if (sensitivePaths.any((path) => routePath.startsWith(path))) {
_noScreenshot.screenshotOff();
} else {
_noScreenshot.screenshotOn();
}
});
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// _noScreenshot.screenshotOff();
setState(() {
shouldBlur = state == AppLifecycleState.inactive ||
state == AppLifecycleState.paused ||
state == AppLifecycleState.hidden ||
state == AppLifecycleState.detached;
});
}

@override
Widget build(BuildContext context) {
return ColoredBox(
color: context.colour.primary,
child: Opacity(
opacity: shouldBlur ? 0 : 1,
child: widget.child,
),
);
}
}
12 changes: 12 additions & 0 deletions lib/home/listeners.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:bb_mobile/_model/wallet.dart';
import 'package:bb_mobile/_pkg/logger.dart';
import 'package:bb_mobile/_pkg/wallet/address.dart';
import 'package:bb_mobile/_pkg/wallet/balance.dart';
import 'package:bb_mobile/_pkg/wallet/create.dart';
Expand Down Expand Up @@ -91,3 +92,14 @@ class WalletBlocListeners extends StatelessWidget {
);
}
}

class BBlocObserver extends BlocObserver {
@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
locator<Logger>().log(
error.toString() + '\n' + stackTrace.toString(),
printToConsole: true,
);
super.onError(bloc, error, stackTrace);
}
}
90 changes: 6 additions & 84 deletions lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:developer';

import 'package:bb_mobile/_pkg/i18n.dart';
import 'package:bb_mobile/_pkg/logger.dart';
import 'package:bb_mobile/_ui/security_overlay.dart';
import 'package:bb_mobile/currency/bloc/currency_cubit.dart';
import 'package:bb_mobile/home/bloc/home_cubit.dart';
import 'package:bb_mobile/home/deep_linking.dart';
Expand All @@ -26,17 +27,16 @@ import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:flutter_translate/flutter_translate.dart';
import 'package:go_router/go_router.dart';
import 'package:lwk_dart/lwk_dart.dart';
import 'package:no_screenshot/no_screenshot.dart';
import 'package:oktoast/oktoast.dart';

Future main({bool fromTest = false}) async {
FlutterError.onError = _handleFlutterError;
FlutterError.onError = (err) =>
log('Flutter Error:' + err.toString(minLevel: DiagnosticLevel.warning));

runZonedGuarded(() async {
if (!fromTest) WidgetsFlutterBinding.ensureInitialized();
await LibLwk.init();
await LibBoltz.init();
print('-------runapp---------');
await dotenv.load(isOptional: true);
Bloc.observer = BBlocObserver();
// await FlutterWindowManager.addFlags(FlutterWindowManager.FLAG_SECURE);
Expand Down Expand Up @@ -125,7 +125,9 @@ class BullBitcoinWalletApp extends StatelessWidget {
data: MediaQuery.of(context).copyWith(
textScaler: TextScaler.noScaling,
),
child: AppLifecycleOverlay(child: child),
child: AppLifecycleOverlay(
child: child,
),
),
),
),
Expand All @@ -142,83 +144,3 @@ class BullBitcoinWalletApp extends StatelessWidget {
);
}
}

class AppLifecycleOverlay extends StatefulWidget {
const AppLifecycleOverlay({super.key, required this.child});

final Widget child;

@override
State<AppLifecycleOverlay> createState() => _AppLifecycleOverlayState();
}

class _AppLifecycleOverlayState extends State<AppLifecycleOverlay>
with WidgetsBindingObserver {
bool shouldBlur = false;
final _noScreenshot = NoScreenshot.instance;

final sensitivePaths = [
'/home/import',
'/home/wallet/wallet-settings/open-backup',
'/home/wallet/wallet-settings/wallet-settings/backup',
'/home/wallet/wallet-settings/wallet-settings/test-backup',
'/home/wallet-settings/open-backup',
'/home/wallet-settings/wallet-settings/backup',
'/home/wallet-settings/wallet-settings/test-backup',
];

@override
void initState() {
super.initState();
WidgetsBinding.instance.addObserver(this);

locator<GoRouter>().routerDelegate.addListener(() {
final routePath = locator<GoRouter>()
.routerDelegate
.currentConfiguration
.routes
.map((RouteBase e) => (e as GoRoute).path)
.join();
// print(routePath);
if (sensitivePaths.any((path) => routePath.startsWith(path))) {
_noScreenshot.screenshotOff();
} else {
_noScreenshot.screenshotOn();
}
});
}

@override
void dispose() {
WidgetsBinding.instance.removeObserver(this);
super.dispose();
}

@override
void didChangeAppLifecycleState(AppLifecycleState state) {
// _noScreenshot.screenshotOff();
setState(() {
shouldBlur = state == AppLifecycleState.inactive ||
state == AppLifecycleState.paused ||
state == AppLifecycleState.hidden ||
state == AppLifecycleState.detached;
});
}

@override
Widget build(BuildContext context) {
return ColoredBox(
color: context.colour.primary,
child: Opacity(
opacity: shouldBlur ? 0 : 1,
child: widget.child,
),
);
}
}

void _handleFlutterError(FlutterErrorDetails details) {
log(
'Flutter Error:' + details.toString(minLevel: DiagnosticLevel.warning),
);
}
70 changes: 13 additions & 57 deletions lib/routes.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:bb_mobile/_model/transaction.dart';
import 'package:bb_mobile/_pkg/logger.dart';
import 'package:bb_mobile/_ui/logger_page.dart';
import 'package:bb_mobile/auth/page.dart';
import 'package:bb_mobile/create/page.dart';
Expand Down Expand Up @@ -233,39 +232,9 @@ GoRouter setupRouter() => GoRouter(
],
);

class BBlocObserver extends BlocObserver {
// @override
// void onEvent(Bloc bloc, Object? event) {
// super.onEvent(bloc, event);
// debugPrint('\n\n' + event.runtimeType.toString());
// }

// @override
// void onChange(BlocBase bloc, Change change) {
// super.onChange(bloc, change);
// // debugPrint('\n\n' + change.toString());
// }

// @override
// void onCreate(BlocBase bloc) {
// super.onCreate(bloc);
// // debugPrint('\n\n' + bloc.toString());
// }

// @override
// void onTransition(Bloc bloc, Transition transition) {
// super.onTransition(bloc, transition);
// // debugPrint('\n\n' + transition.toString());
// }

@override
void onError(BlocBase bloc, Object error, StackTrace stackTrace) {
locator<Logger>().log(
error.toString() + '\n' + stackTrace.toString(),
printToConsole: true,
);
super.onError(bloc, error, stackTrace);
}
class NavName extends Cubit<String> {
NavName() : super('');
void update(String name) => emit(name);
}

class GoRouterObserver extends NavigatorObserver {
Expand All @@ -279,33 +248,20 @@ class GoRouterObserver extends NavigatorObserver {
locator<NavName>().update(previousRoute?.settings.name ?? '');
}

@override
void didRemove(Route<dynamic> route, Route<dynamic>? previousRoute) {
print('didRemove: $route');
}

@override
void didReplace({Route<dynamic>? newRoute, Route<dynamic>? oldRoute}) {
locator<NavName>().update(newRoute?.settings.name ?? '');
}
}

class NavName extends Cubit<String> {
NavName() : super('');

void update(String name) {
print('nav: $name');
emit(name);
}
}

extension GoRouterExtension on GoRouter {
String location() {
final lastMatch = routerDelegate.currentConfiguration.last;
final matchList = lastMatch is ImperativeRouteMatch
? lastMatch.matches
: routerDelegate.currentConfiguration;
final String location = matchList.uri.toString();
return location;
}
}
// extension GoRouterExtension on GoRouter {
// String location() {
// final lastMatch = routerDelegate.currentConfiguration.last;
// final matchList = lastMatch is ImperativeRouteMatch
// ? lastMatch.matches
// : routerDelegate.currentConfiguration;
// final String location = matchList.uri.toString();
// return location;
// }
// }

0 comments on commit 5a4cb9a

Please sign in to comment.