Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable Swap and Buy crypto features based on user preference #909

Merged
merged 14 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,8 @@ class _MaterialAppWithThemeState extends ConsumerState<MaterialAppWithTheme>
// TODO: this should probably run unawaited. Keep commented out for now as proper community nodes ui hasn't been implemented yet
// unawaited(_nodeService.updateCommunityNodes());

if (AppConfig.hasFeature(AppFeature.swap)) {
if (AppConfig.hasFeature(AppFeature.swap) &&
ref.read(prefsChangeNotifierProvider).enableExchange) {
await ExchangeDataLoadingService.instance.initDB();
// run without awaiting
if (ref.read(prefsChangeNotifierProvider).externalCalls &&
Expand Down
18 changes: 17 additions & 1 deletion lib/pages/home_view/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import 'package:flutter_svg/svg.dart';

import '../../app_config.dart';
import '../../providers/global/notifications_provider.dart';
import '../../providers/global/prefs_provider.dart';
import '../../providers/ui/home_view_index_provider.dart';
import '../../providers/ui/unread_notifications_provider.dart';
import '../../services/event_bus/events/global/tor_connection_status_changed_event.dart';
Expand Down Expand Up @@ -172,6 +173,20 @@ class _HomeViewState extends ConsumerState<HomeView> {
@override
Widget build(BuildContext context) {
debugPrint("BUILD: $runtimeType");

// dirty hack
ref.listen(
prefsChangeNotifierProvider.select((value) => value.enableExchange),
(prev, next) {
if (next == false &&
mounted &&
ref.read(homeViewPageIndexStateProvider) != 0) {
WidgetsBinding.instance.addPostFrameCallback(
(_) => ref.read(homeViewPageIndexStateProvider.state).state = 0,
);
}
});

return WillPopScope(
onWillPop: _onWillPop,
child: Background(
Expand Down Expand Up @@ -345,7 +360,8 @@ class _HomeViewState extends ConsumerState<HomeView> {
),
body: Column(
children: [
if (_children.length > 1)
if (_children.length > 1 &&
ref.watch(prefsChangeNotifierProvider).enableExchange)
Container(
decoration: BoxDecoration(
color: Theme.of(context)
Expand Down
2 changes: 0 additions & 2 deletions lib/pages/home_view/sub_widgets/home_view_button_bar.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ class _HomeViewButtonBarState extends ConsumerState<HomeViewButtonBar> {

@override
Widget build(BuildContext context) {
//todo: check if print needed
// debugPrint("BUILD: HomeViewButtonBar");
final selectedIndex = ref.watch(homeViewPageIndexStateProvider.state).state;
return Row(
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,55 @@ class AdvancedSettingsView extends StatelessWidget {
},
),
),
// showExchange pref.
const SizedBox(
height: 8,
),
RoundedWhiteContainer(
child: Consumer(
builder: (_, ref, __) {
return RawMaterialButton(
// splashColor: Theme.of(context).extension<StackColors>()!.highlight,
materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(
Constants.size.circularBorderRadius,
),
),
onPressed: null,
child: Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Row(
mainAxisAlignment: MainAxisAlignment.spaceBetween,
children: [
Text(
"Enable exchange features",
style: STextStyles.titleBold12(context),
textAlign: TextAlign.left,
),
SizedBox(
height: 20,
width: 40,
child: DraggableSwitchButton(
isOn: ref.watch(
prefsChangeNotifierProvider.select(
(value) => value.enableExchange,
),
),
onValueChanged: (newValue) {
ref
.read(prefsChangeNotifierProvider)
.enableExchange = newValue;
},
),
),
],
),
),
);
},
),
),
const SizedBox(
height: 8,
),
Expand Down
11 changes: 7 additions & 4 deletions lib/pages/token_view/sub_widgets/token_summary.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,9 @@ class TokenWalletOptions extends ConsumerWidget {

@override
Widget build(BuildContext context, WidgetRef ref) {
final prefs = ref.watch(prefsChangeNotifierProvider);
final showExchange = prefs.enableExchange;

return Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Expand Down Expand Up @@ -251,11 +254,11 @@ class TokenWalletOptions extends ConsumerWidget {
subLabel: "Send",
iconAssetPathSVG: Assets.svg.arrowUpRight,
),
if (AppConfig.hasFeature(AppFeature.swap))
if (AppConfig.hasFeature(AppFeature.swap) && showExchange)
const SizedBox(
width: 16,
),
if (AppConfig.hasFeature(AppFeature.swap))
if (AppConfig.hasFeature(AppFeature.swap) && showExchange)
TokenOptionsButton(
onPressed: () => _onExchangePressed(context),
subLabel: "Swap",
Expand All @@ -265,11 +268,11 @@ class TokenWalletOptions extends ConsumerWidget {
),
),
),
if (AppConfig.hasFeature(AppFeature.buy))
if (AppConfig.hasFeature(AppFeature.buy) && showExchange)
const SizedBox(
width: 16,
),
if (AppConfig.hasFeature(AppFeature.buy))
if (AppConfig.hasFeature(AppFeature.buy) && showExchange)
TokenOptionsButton(
onPressed: () => _onBuyPressed(context),
subLabel: "Buy",
Expand Down
9 changes: 7 additions & 2 deletions lib/pages/wallet_view/wallet_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,9 @@ class _WalletViewState extends ConsumerState<WalletView> {

final coin = ref.watch(pWalletCoin(walletId));

final prefs = ref.watch(prefsChangeNotifierProvider);
final showExchange = prefs.enableExchange;

return ConditionalParent(
condition: _rescanningOnOpen,
builder: (child) {
Expand Down Expand Up @@ -1053,15 +1056,17 @@ class _WalletViewState extends ConsumerState<WalletView> {
),
if (Constants.enableExchange &&
ref.watch(pWalletCoin(walletId)) is! FrostCurrency &&
AppConfig.hasFeature(AppFeature.swap))
AppConfig.hasFeature(AppFeature.swap) &&
showExchange)
WalletNavigationBarItemData(
label: "Swap",
icon: const ExchangeNavIcon(),
onTap: () => _onExchangePressed(context),
),
if (Constants.enableExchange &&
ref.watch(pWalletCoin(walletId)) is! FrostCurrency &&
AppConfig.hasFeature(AppFeature.buy))
AppConfig.hasFeature(AppFeature.buy) &&
showExchange)
WalletNavigationBarItemData(
label: "Buy",
icon: const BuyNavIcon(),
Expand Down
36 changes: 31 additions & 5 deletions lib/pages_desktop_specific/desktop_menu.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import 'package:flutter_svg/flutter_svg.dart';

import '../app_config.dart';
import '../providers/desktop/current_desktop_menu_item.dart';
import '../providers/providers.dart';
import '../themes/stack_colors.dart';
import '../utilities/assets.dart';
import '../utilities/text_styles.dart';
Expand Down Expand Up @@ -58,6 +59,7 @@ class _DesktopMenuState extends ConsumerState<DesktopMenu> {
late final DMIController torButtonController;

double _width = expandedWidth;
bool get _isMinimized => _width < expandedWidth;

void updateSelectedMenuItem(DesktopMenuItemId idKey) {
widget.onSelectionWillChange?.call(idKey);
Expand Down Expand Up @@ -112,6 +114,10 @@ class _DesktopMenuState extends ConsumerState<DesktopMenu> {

@override
Widget build(BuildContext context) {
final prefs = ref.watch(prefsChangeNotifierProvider);

final showExchange = prefs.enableExchange;

return Material(
color: Theme.of(context).extension<StackColors>()!.popupBG,
child: AnimatedContainer(
Expand Down Expand Up @@ -161,7 +167,7 @@ class _DesktopMenuState extends ConsumerState<DesktopMenu> {
onPressed: () {
ref.read(currentDesktopMenuItemProvider.state).state =
DesktopMenuItemId.settings;
ref.watch(selectedSettingsMenuItemStateProvider.state).state =
ref.read(selectedSettingsMenuItemStateProvider.state).state =
4;
},
),
Expand All @@ -179,97 +185,116 @@ class _DesktopMenuState extends ConsumerState<DesktopMenu> {
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
DesktopMenuItem(
key: const ValueKey('myStack'),
duration: duration,
icon: const DesktopMyStackIcon(),
label: "My ${AppConfig.prefix}",
value: DesktopMenuItemId.myStack,
onChanged: updateSelectedMenuItem,
controller: controllers[0],
isExpandedInitially: !_isMinimized,
),
if (AppConfig.hasFeature(AppFeature.swap))
if (AppConfig.hasFeature(AppFeature.swap) &&
showExchange) ...[
const SizedBox(
height: 2,
),
if (AppConfig.hasFeature(AppFeature.swap))
DesktopMenuItem(
key: const ValueKey('swap'),
duration: duration,
icon: const DesktopExchangeIcon(),
label: "Swap",
value: DesktopMenuItemId.exchange,
onChanged: updateSelectedMenuItem,
controller: controllers[1],
isExpandedInitially: !_isMinimized,
),
if (AppConfig.hasFeature(AppFeature.buy))
],
if (AppConfig.hasFeature(AppFeature.buy) &&
showExchange) ...[
const SizedBox(
height: 2,
),
if (AppConfig.hasFeature(AppFeature.buy))
DesktopMenuItem(
key: const ValueKey('buy'),
duration: duration,
icon: const DesktopBuyIcon(),
label: "Buy crypto",
value: DesktopMenuItemId.buy,
onChanged: updateSelectedMenuItem,
controller: controllers[2],
isExpandedInitially: !_isMinimized,
),
],
const SizedBox(
height: 2,
),
DesktopMenuItem(
key: const ValueKey('notifications'),
duration: duration,
icon: const DesktopNotificationsIcon(),
label: "Notifications",
value: DesktopMenuItemId.notifications,
onChanged: updateSelectedMenuItem,
controller: controllers[3],
isExpandedInitially: !_isMinimized,
),
const SizedBox(
height: 2,
),
DesktopMenuItem(
key: const ValueKey('addressBook'),
duration: duration,
icon: const DesktopAddressBookIcon(),
label: "Address Book",
value: DesktopMenuItemId.addressBook,
onChanged: updateSelectedMenuItem,
controller: controllers[4],
isExpandedInitially: !_isMinimized,
),
const SizedBox(
height: 2,
),
DesktopMenuItem(
key: const ValueKey('settings'),
duration: duration,
icon: const DesktopSettingsIcon(),
label: "Settings",
value: DesktopMenuItemId.settings,
onChanged: updateSelectedMenuItem,
controller: controllers[5],
isExpandedInitially: !_isMinimized,
),
const SizedBox(
height: 2,
),
DesktopMenuItem(
key: const ValueKey('support'),
duration: duration,
icon: const DesktopSupportIcon(),
label: "Support",
value: DesktopMenuItemId.support,
onChanged: updateSelectedMenuItem,
controller: controllers[6],
isExpandedInitially: !_isMinimized,
),
const SizedBox(
height: 2,
),
DesktopMenuItem(
key: const ValueKey('about'),
duration: duration,
icon: const DesktopAboutIcon(),
label: "About",
value: DesktopMenuItemId.about,
onChanged: updateSelectedMenuItem,
controller: controllers[7],
isExpandedInitially: !_isMinimized,
),
const Spacer(),
if (!Platform.isIOS)
DesktopMenuItem(
key: const ValueKey('exit'),
duration: duration,
labelLength: 123,
icon: const DesktopExitIcon(),
Expand All @@ -287,6 +312,7 @@ class _DesktopMenuState extends ConsumerState<DesktopMenu> {
// }
},
controller: controllers[8],
isExpandedInitially: !_isMinimized,
),
],
),
Expand Down
13 changes: 12 additions & 1 deletion lib/pages_desktop_specific/desktop_menu_item.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ import 'desktop_menu.dart';

class DMIController {
VoidCallback? toggle;

DMIController();

void dispose() {
toggle = null;
}
Expand Down Expand Up @@ -237,6 +240,7 @@ class DesktopMenuItem<T> extends ConsumerStatefulWidget {
required this.duration,
this.labelLength = 125,
this.controller,
required this.isExpandedInitially,
});

final Widget icon;
Expand All @@ -246,6 +250,7 @@ class DesktopMenuItem<T> extends ConsumerStatefulWidget {
final Duration duration;
final double labelLength;
final DMIController? controller;
final bool isExpandedInitially;

@override
ConsumerState<DesktopMenuItem<T>> createState() => _DesktopMenuItemState<T>();
Expand Down Expand Up @@ -287,11 +292,17 @@ class _DesktopMenuItemState<T> extends ConsumerState<DesktopMenuItem<T>>
labelLength = widget.labelLength;
controller = widget.controller;

_iconOnly = !widget.isExpandedInitially;
controller?.toggle = toggle;
animationController = AnimationController(
vsync: this,
duration: duration,
)..forward();
);
if (_iconOnly) {
animationController.value = 0;
} else {
animationController.value = 1;
}

super.initState();
}
Expand Down
Loading
Loading