diff --git a/lib/pages/home_view/home_view.dart b/lib/pages/home_view/home_view.dart index 2bdcbace0..2e7c38202 100644 --- a/lib/pages/home_view/home_view.dart +++ b/lib/pages/home_view/home_view.dart @@ -16,15 +16,13 @@ import 'package:stackwallet/providers/ui/unread_notifications_provider.dart'; import 'package:stackwallet/services/exchange/exchange_data_loading_service.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; +import 'package:stackwallet/utilities/logger.dart'; +import 'package:stackwallet/utilities/prefs.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/stack_dialog.dart'; -import 'package:stackwallet/utilities/logger.dart'; - -import 'package:stackwallet/utilities/prefs.dart'; - class HomeView extends ConsumerStatefulWidget { const HomeView({Key? key}) : super(key: key); @@ -45,7 +43,7 @@ class _HomeViewState extends ConsumerState { bool _exitEnabled = false; - final _cnLoadingService = ExchangeDataLoadingService(); + final _exchangeDataLoadingService = ExchangeDataLoadingService(); Future _onWillPop() async { // go to home view when tapping back on the main exchange view @@ -88,7 +86,7 @@ class _HomeViewState extends ConsumerState { // final externalCalls = Prefs.instance.externalCalls; if (externalCalls) { - _cnLoadingService.loadAll(ref); + _exchangeDataLoadingService.loadAll(ref); } else { Logging.instance.log("User does not want to use external calls", level: LogLevel.Info); @@ -290,6 +288,9 @@ class _HomeViewState extends ConsumerState { _ref.listen(homeViewPageIndexStateProvider, (previous, next) { if (next is int) { + if (next == 1) { + _loadCNData(); + } if (next >= 0 && next <= 1) { _pageController.animateToPage( next, diff --git a/lib/pages/home_view/sub_widgets/home_view_button_bar.dart b/lib/pages/home_view/sub_widgets/home_view_button_bar.dart index e502d4537..61a6c4185 100644 --- a/lib/pages/home_view/sub_widgets/home_view_button_bar.dart +++ b/lib/pages/home_view/sub_widgets/home_view_button_bar.dart @@ -1,5 +1,3 @@ -import 'dart:async'; - import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/providers/providers.dart'; @@ -8,8 +6,6 @@ import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/stack_dialog.dart'; -import 'package:stackwallet/utilities/prefs.dart'; - class HomeViewButtonBar extends ConsumerStatefulWidget { const HomeViewButtonBar({Key? key}) : super(key: key); @@ -18,8 +14,8 @@ class HomeViewButtonBar extends ConsumerStatefulWidget { } class _HomeViewButtonBarState extends ConsumerState { - final DateTime _lastRefreshed = DateTime.now(); - final Duration _refreshInterval = const Duration(hours: 1); + // final DateTime _lastRefreshed = DateTime.now(); + // final Duration _refreshInterval = const Duration(hours: 1); @override void initState() { @@ -104,34 +100,14 @@ class _HomeViewButtonBarState extends ConsumerState { if (selectedIndex != 1) { ref.read(homeViewPageIndexStateProvider.state).state = 1; } - DateTime now = DateTime.now(); - final _cnLoadingService = ExchangeDataLoadingService(); - final externalCalls = Prefs.instance.externalCalls; - if (!externalCalls) { + // DateTime now = DateTime.now(); + if (ref.read(prefsChangeNotifierProvider).externalCalls) { print("loading?"); - unawaited(_cnLoadingService.loadAll(ref)); - } - if (now.difference(_lastRefreshed) > _refreshInterval) { - // bool okPressed = false; - // showDialog( - // context: context, - // barrierDismissible: false, - // builder: (_) => const StackDialog( - // // builder: (_) => StackOkDialog( - // title: "Refreshing ChangeNOW data", - // message: "This may take a while", - // // onOkPressed: (value) { - // // if (value == "OK") { - // // okPressed = true; - // // } - // // }, - // ), - // ); await ExchangeDataLoadingService().loadAll(ref); - // if (!okPressed && mounted) { - // Navigator.of(context).pop(); - // } } + // if (now.difference(_lastRefreshed) > _refreshInterval) { + // await ExchangeDataLoadingService().loadAll(ref); + // } }, child: Text( "Exchange", diff --git a/lib/pages/stack_privacy_calls.dart b/lib/pages/stack_privacy_calls.dart index 6065c90dc..2aa2a5c8a 100644 --- a/lib/pages/stack_privacy_calls.dart +++ b/lib/pages/stack_privacy_calls.dart @@ -1,8 +1,8 @@ import 'package:flutter/material.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_svg/svg.dart'; -import 'package:stackwallet/hive/db.dart'; import 'package:stackwallet/pages/pinpad_views/create_pin_view.dart'; +import 'package:stackwallet/pages_desktop_specific/create_password/create_password_view.dart'; import 'package:stackwallet/providers/global/prefs_provider.dart'; import 'package:stackwallet/utilities/assets.dart'; import 'package:stackwallet/utilities/constants.dart'; @@ -12,9 +12,6 @@ import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; -import 'package:stackwallet/providers/global/prefs_provider.dart'; -import 'package:stackwallet/utilities/prefs.dart'; - class StackPrivacyCalls extends ConsumerStatefulWidget { const StackPrivacyCalls({ Key? key, @@ -31,19 +28,19 @@ class StackPrivacyCalls extends ConsumerStatefulWidget { class _StackPrivacyCalls extends ConsumerState { late final bool isDesktop; - bool isEasy = Prefs.instance.externalCalls; - final PageController _pageController = - PageController(initialPage: 0, keepPage: true); + late bool isEasy; + late bool infoToggle; @override void initState() { isDesktop = Util.isDesktop; + isEasy = ref.read(prefsChangeNotifierProvider).externalCalls; + infoToggle = isEasy; super.initState(); } @override void dispose() { - _pageController.dispose(); super.dispose(); } @@ -59,135 +56,161 @@ class _StackPrivacyCalls extends ConsumerState { ), ), body: SafeArea( - child: PageView( - controller: _pageController, - physics: const NeverScrollableScrollPhysics(), - children: [ - Padding( - padding: const EdgeInsets.fromLTRB(0, 40, 0, 0), - child: Column( - mainAxisAlignment: MainAxisAlignment.center, - children: [ - Text( - "Choose your Stack experience", - style: STextStyles.pageTitleH1(context), - ), - const SizedBox( - height: 8, - ), - Text( - "You can change it later in Settings", - style: STextStyles.subtitle(context), - ), - const SizedBox( - height: 36, - ), - const Padding( - padding: EdgeInsets.symmetric( - horizontal: 16, - ), - child: PrivacyToggle(), - ), - const SizedBox( - height: 36, - ), - Padding( - padding: const EdgeInsets.all(16.0), - child: RoundedWhiteContainer( - child: Center( - child: RichText( - textAlign: TextAlign.left, - text: TextSpan( - style: STextStyles.label(context) - .copyWith(fontSize: 12.0), - children: ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - ) - ? [ - const TextSpan( - text: - "Exchange data preloaded for a seamless experience."), - const TextSpan( - text: - "\n\nCoinGecko enabled: (24 hour price change shown in-app, total wallet value shown in USD or other currency)."), - TextSpan( - text: - "\n\nRecommended for most crypto users.", - style: TextStyle( - color: Theme.of(context) - .extension()! - .textDark, - fontWeight: FontWeight.w600, - ), - ), - ] - : [ - const TextSpan( - text: - "Exchange data not preloaded (slower experience)."), - const TextSpan( - text: - "\n\nCoinGecko disabled (price changes not shown, no wallet value shown in other currencies)."), - TextSpan( - text: - "\n\nRecommended for the privacy conscious.", - style: TextStyle( - color: Theme.of(context) - .extension()! - .textDark, - fontWeight: FontWeight.w600, - ), - ), - ], - ), - ), + child: Padding( + padding: const EdgeInsets.fromLTRB(0, 40, 0, 0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + Text( + "Choose your Stack experience", + style: STextStyles.pageTitleH1(context), + ), + const SizedBox( + height: 8, + ), + Text( + "You can change it later in Settings", + style: STextStyles.subtitle(context), + ), + const SizedBox( + height: 36, + ), + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 16, + ), + child: PrivacyToggle( + externalCallsEnabled: isEasy, + onChanged: (externalCalls) { + isEasy = externalCalls; + setState(() { + infoToggle = isEasy; + }); + }, + ), + ), + const SizedBox( + height: 36, + ), + Padding( + padding: const EdgeInsets.all(16.0), + child: RoundedWhiteContainer( + child: Center( + child: RichText( + textAlign: TextAlign.left, + text: TextSpan( + style: + STextStyles.label(context).copyWith(fontSize: 12.0), + children: infoToggle + ? [ + const TextSpan( + text: + "Exchange data preloaded for a seamless experience."), + const TextSpan( + text: + "\n\nCoinGecko enabled: (24 hour price change shown in-app, total wallet value shown in USD or other currency)."), + TextSpan( + text: + "\n\nRecommended for most crypto users.", + style: TextStyle( + color: Theme.of(context) + .extension()! + .textDark, + fontWeight: FontWeight.w600, + ), + ), + ] + : [ + const TextSpan( + text: + "Exchange data not preloaded (slower experience)."), + const TextSpan( + text: + "\n\nCoinGecko disabled (price changes not shown, no wallet value shown in other currencies)."), + TextSpan( + text: + "\n\nRecommended for the privacy conscious.", + style: TextStyle( + color: Theme.of(context) + .extension()! + .textDark, + fontWeight: FontWeight.w600, + ), + ), + ], ), ), ), - const Spacer( - flex: 4, - ), - Padding( - padding: const EdgeInsets.symmetric( - horizontal: 16, - vertical: 16, - ), - child: Row( - children: [ - Expanded( - child: ContinueButton( - isDesktop: isDesktop, - isSettings: widget.isSettings, - isEasy: ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - ), - ), - ), - ], - ), - ), - ], + ), ), - ), - ], + const Spacer( + flex: 4, + ), + Padding( + padding: const EdgeInsets.symmetric( + horizontal: 16, + vertical: 16, + ), + child: Row( + children: [ + Expanded( + child: ContinueButton( + isDesktop: isDesktop, + label: !widget.isSettings ? "Continue" : "Save changes", + onPressed: () { + ref.read(prefsChangeNotifierProvider).externalCalls = + isEasy; + if (!widget.isSettings) { + if (isDesktop) { + Navigator.of(context).pushNamed( + CreatePasswordView.routeName, + ); + } else { + Navigator.of(context).pushNamed( + CreatePinView.routeName, + ); + } + } else { + Navigator.pop(context); + } + }, + ), + ), + ], + ), + ), + ], + ), ), ), ); } } -class PrivacyToggle extends ConsumerStatefulWidget { - const PrivacyToggle({Key? key}) : super(key: key); +class PrivacyToggle extends StatefulWidget { + const PrivacyToggle({ + Key? key, + required this.externalCallsEnabled, + this.onChanged, + }) : super(key: key); + + final bool externalCallsEnabled; + final void Function(bool)? onChanged; @override - ConsumerState createState() => _PrivacyToggleState(); + State createState() => _PrivacyToggleState(); } -class _PrivacyToggleState extends ConsumerState { +class _PrivacyToggleState extends State { + late bool externalCallsEnabled; + + @override + void initState() { + // initial toggle state + externalCallsEnabled = widget.externalCallsEnabled; + super.initState(); + } + @override Widget build(BuildContext context) { return Row( @@ -196,11 +219,7 @@ class _PrivacyToggleState extends ConsumerState { child: RawMaterialButton( fillColor: Theme.of(context).extension()!.popupBG, shape: RoundedRectangleBorder( - side: !ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - ) + side: !externalCallsEnabled ? BorderSide.none : BorderSide( color: Theme.of(context) @@ -213,7 +232,12 @@ class _PrivacyToggleState extends ConsumerState { ), ), onPressed: () { - ref.read(prefsChangeNotifierProvider).externalCalls = true; + setState(() { + // update toggle state + externalCallsEnabled = true; + }); + // call callback with newly set value + widget.onChanged?.call(externalCallsEnabled); }, child: Padding( padding: const EdgeInsets.all( @@ -244,11 +268,7 @@ class _PrivacyToggleState extends ConsumerState { ), ], ), - if (ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - )) + if (externalCallsEnabled) Positioned( top: 4, right: 4, @@ -261,11 +281,7 @@ class _PrivacyToggleState extends ConsumerState { .infoItemIcons, ), ), - if (!ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - )) + if (!externalCallsEnabled) Positioned( top: 4, right: 4, @@ -293,11 +309,7 @@ class _PrivacyToggleState extends ConsumerState { elevation: 0, fillColor: Theme.of(context).extension()!.popupBG, shape: RoundedRectangleBorder( - side: ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - ) + side: externalCallsEnabled ? BorderSide.none : BorderSide( color: Theme.of(context) @@ -310,7 +322,12 @@ class _PrivacyToggleState extends ConsumerState { ), ), onPressed: () { - ref.read(prefsChangeNotifierProvider).externalCalls = false; + setState(() { + // update toggle state + externalCallsEnabled = false; + }); + // call callback with newly set value + widget.onChanged?.call(externalCallsEnabled); }, child: Padding( padding: const EdgeInsets.all( @@ -342,11 +359,7 @@ class _PrivacyToggleState extends ConsumerState { ), ], ), - if (!ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - )) + if (!externalCallsEnabled) Positioned( top: 4, right: 4, @@ -359,11 +372,7 @@ class _PrivacyToggleState extends ConsumerState { .infoItemIcons, ), ), - if (ref.watch( - prefsChangeNotifierProvider.select( - (value) => value.externalCalls, - ), - )) + if (externalCallsEnabled) Positioned( top: 4, right: 4, @@ -388,58 +397,47 @@ class _PrivacyToggleState extends ConsumerState { } } -class ContinueButton extends StatelessWidget { - const ContinueButton( - {Key? key, - required this.isDesktop, - required this.isSettings, - required this.isEasy}) - : super(key: key); +class ContinueButton extends ConsumerWidget { + const ContinueButton({ + Key? key, + required this.isDesktop, + required this.onPressed, + required this.label, + }) : super(key: key); + final String label; final bool isDesktop; - final bool isSettings; - final bool isEasy; + final VoidCallback onPressed; @override - Widget build(BuildContext context) { - return !isDesktop - ? TextButton( - style: Theme.of(context) - .extension()! - .getPrimaryEnabledButtonColor(context), - onPressed: () { - Prefs.instance.externalCalls = isEasy; - if (!isSettings) { - Navigator.of(context).pushNamed(CreatePinView.routeName); - } else { - Navigator.pop(context); - } - }, - child: Text( - !isSettings ? "Continue" : "Save changes", - style: STextStyles.button(context), - ), - ) - : SizedBox( - width: 328, - height: 70, - child: TextButton( - style: Theme.of(context) - .extension()! - .getPrimaryEnabledButtonColor(context), - onPressed: () { - Prefs.instance.externalCalls = isEasy; - - if (!isSettings) { - Navigator.of(context).pushNamed(CreatePinView.routeName); - } - }, - child: Text( - !isSettings ? "Continue" : "Save changes", - style: STextStyles.button(context).copyWith(fontSize: 20), - ), - ), - ); + Widget build(BuildContext context, WidgetRef ref) { + if (isDesktop) { + return SizedBox( + width: 328, + height: 70, + child: TextButton( + style: Theme.of(context) + .extension()! + .getPrimaryEnabledButtonColor(context), + onPressed: onPressed, + child: Text( + label, + style: STextStyles.button(context).copyWith(fontSize: 20), + ), + ), + ); + } else { + return TextButton( + style: Theme.of(context) + .extension()! + .getPrimaryEnabledButtonColor(context), + onPressed: onPressed, + child: Text( + label, + style: STextStyles.button(context), + ), + ); + } } } diff --git a/lib/utilities/prefs.dart b/lib/utilities/prefs.dart index cc818f982..0109755f7 100644 --- a/lib/utilities/prefs.dart +++ b/lib/utilities/prefs.dart @@ -546,6 +546,8 @@ class Prefs extends ChangeNotifier { boxName: DB.boxNamePrefs, key: "startupWalletId") as String?; } + // incognito mode off by default + // allow external network calls such as exchange data and price info bool _externalCalls = true; bool get externalCalls => _externalCalls;