diff --git a/packages/deriv_mobile_chart_wrapper/lib/src/core_widgets/custom_chip.dart b/packages/deriv_mobile_chart_wrapper/lib/src/core_widgets/custom_chip.dart index 4a86bec5a..a9a9c72db 100644 --- a/packages/deriv_mobile_chart_wrapper/lib/src/core_widgets/custom_chip.dart +++ b/packages/deriv_mobile_chart_wrapper/lib/src/core_widgets/custom_chip.dart @@ -3,16 +3,16 @@ import 'package:flutter/material.dart'; import 'package:deriv_theme/deriv_theme.dart'; /// The type of function to be passed to [CustomChip]'s `onTap` property. -typedef OnTapCustomChip = void Function(String? value, String? title); +typedef OnTapCustomChip = void Function(T? value, String? title); /// Will be called to get the content that needs to be shown inside chips. -typedef LabelBuilder = String Function(String? value, String? title); +typedef LabelBuilder = String Function(T? value, String? title); /// Will be called to get the content that needs to be shown inside chips. -typedef LabelWidgetBuilder = Widget Function(String? value, String? title); +typedef LabelWidgetBuilder = Widget Function(T? value, String? title); /// A Custom chip with a disabled and enabled design based on [isSelected]. -class CustomChip extends StatelessWidget { +class CustomChip extends StatelessWidget { /// Initializes a [CustomChip] widget. const CustomChip({ this.value, @@ -38,17 +38,17 @@ class CustomChip extends StatelessWidget { final String? title; /// The value text to be shown inside of the chip. - final String? value; + final T? value; /// To get the content to be shown inside chips. - final LabelBuilder? labelBuilder; + final LabelBuilder? labelBuilder; /// To get the widget to be shown inside chips. - final LabelWidgetBuilder? labelWidgetBuilder; + final LabelWidgetBuilder? labelWidgetBuilder; /// Called when a custom chip is tapped. /// Pass [onTap] as null to disable the functionality of the chips. - final OnTapCustomChip? onTap; + final OnTapCustomChip? onTap; /// The border radius of chips container. final double borderRadius; diff --git a/packages/deriv_mobile_chart_wrapper/lib/src/mobile_tools_ui/mobile_tools_bottom_sheet_content.dart b/packages/deriv_mobile_chart_wrapper/lib/src/mobile_tools_ui/mobile_tools_bottom_sheet_content.dart index 155975802..898e98145 100644 --- a/packages/deriv_mobile_chart_wrapper/lib/src/mobile_tools_ui/mobile_tools_bottom_sheet_content.dart +++ b/packages/deriv_mobile_chart_wrapper/lib/src/mobile_tools_ui/mobile_tools_bottom_sheet_content.dart @@ -57,34 +57,19 @@ class MobileToolsBottomSheetContent extends StatefulWidget { class _MobileToolsBottomSheetContentState extends State { - String? _selectedChip = IndicatorTabLabel.all; + IndicatorTabLabel? _selectedChip = IndicatorTabLabel.all; List get filteredIndicators { - if (_selectedChip == IndicatorTabLabel.momentum) { - // Filter momentum indicators - return MobileToolsBottomSheetContent.indicators - .where( - (indicator) => indicator.category == IndicatorCategory.momentum) - .toList(); - } - - if (_selectedChip == IndicatorTabLabel.volatility) { - // Filter volatility indicators - return MobileToolsBottomSheetContent.indicators - .where( - (indicator) => indicator.category == IndicatorCategory.volatility) - .toList(); - } - // Filter moving averages indicators - if (_selectedChip == IndicatorTabLabel.movingAverages) { - return MobileToolsBottomSheetContent.indicators - .where((indicator) => - indicator.category == IndicatorCategory.movingAverages) - .toList(); - } - - // Otherwise return All indicators - return MobileToolsBottomSheetContent.indicators; + return _selectedChip == null || _selectedChip == IndicatorTabLabel.all + ? MobileToolsBottomSheetContent.indicators + : MobileToolsBottomSheetContent.indicators + .where( + // TODO(Ramin): Check if we can only have one enum to use for + // labels and indicators' model category. + (indicator) => + indicator.category == _selectedChip?.toIndicatorCategory, + ) + .toList(); } /// Returns `true` if the limit of active indicators is reached. @@ -307,50 +292,34 @@ class _MobileToolsBottomSheetContentState isHorizontalPaddingEnabled: true, horizontalPadding: Dimens.margin16, items: [ - CustomChip( - labelBuilder: (_, __) => - IndicatorTabLabel.activeCount(indicatorsRepo.items.length), - value: IndicatorTabLabel.active, - onTap: _onChipTapped, - isSelected: _selectedChip == IndicatorTabLabel.active, - borderRadius: ThemeProvider.margin40, - ), - CustomChip( - value: IndicatorTabLabel.all, - onTap: _onChipTapped, - isSelected: _selectedChip == IndicatorTabLabel.all, - borderRadius: ThemeProvider.margin40, - ), - CustomChip( - // title: 'Momentum', - value: IndicatorTabLabel.momentum, - onTap: _onChipTapped, - isSelected: _selectedChip == IndicatorTabLabel.momentum, - borderRadius: ThemeProvider.margin40, - ), - CustomChip( - value: IndicatorTabLabel.volatility, - onTap: _onChipTapped, - isSelected: _selectedChip == IndicatorTabLabel.volatility, - borderRadius: ThemeProvider.margin40, - ), - CustomChip( - value: IndicatorTabLabel.movingAverages, - onTap: _onChipTapped, - isSelected: _selectedChip == IndicatorTabLabel.movingAverages, - borderRadius: ThemeProvider.margin40, - ), + ...IndicatorTabLabel.values + .map((tabLabel) => tabLabel == + IndicatorTabLabel.active + ? CustomChip( + labelBuilder: (_, __) => IndicatorTabLabel.activeCount( + indicatorsRepo.items.length, + ), + value: IndicatorTabLabel.active, + onTap: _onChipTapped, + isSelected: _selectedChip == IndicatorTabLabel.active, + borderRadius: ThemeProvider.margin40, + ) + : CustomChip( + value: tabLabel, + labelBuilder: (_, __) => tabLabel.title, + onTap: _onChipTapped, + isSelected: _selectedChip == tabLabel, + borderRadius: ThemeProvider.margin40, + )) + .toList(), ], ), ), ); } - void _onChipTapped(String? value, String? title) { - setState(() { - _selectedChip = value; - }); - } + void _onChipTapped(IndicatorTabLabel? value, String? title) => + setState(() => _selectedChip = value); Widget _buildHeader(BuildContext context) => Container( padding: const EdgeInsets.symmetric(vertical: Dimens.margin16), diff --git a/packages/deriv_mobile_chart_wrapper/lib/src/models/indicator_tab_label.dart b/packages/deriv_mobile_chart_wrapper/lib/src/models/indicator_tab_label.dart index b61d21093..2878f49e1 100644 --- a/packages/deriv_mobile_chart_wrapper/lib/src/models/indicator_tab_label.dart +++ b/packages/deriv_mobile_chart_wrapper/lib/src/models/indicator_tab_label.dart @@ -1,9 +1,40 @@ -/// A class to define label of indicators chip. -class IndicatorTabLabel { +import 'package:deriv_mobile_chart_wrapper/src/enums.dart'; + +/// An enum to define label of indicators chip. +enum IndicatorTabLabel { + active, + all, + momentum, + volatility, + movingAverages; + static String activeCount(int count) => 'Active ($count)'; - static const String active = 'Active'; - static const String all = 'All'; - static const String momentum = 'Momentum'; - static const String volatility = 'Volatility'; - static const String movingAverages = 'Moving averages'; + + String get title { + switch (this) { + case IndicatorTabLabel.active: + return 'Active'; + case IndicatorTabLabel.all: + return 'All'; + case IndicatorTabLabel.momentum: + return 'Momentum'; + case IndicatorTabLabel.volatility: + return 'Volatility'; + case IndicatorTabLabel.movingAverages: + return 'Moving averages'; + } + } + + IndicatorCategory? get toIndicatorCategory { + switch (this) { + case IndicatorTabLabel.momentum: + return IndicatorCategory.momentum; + case IndicatorTabLabel.volatility: + return IndicatorCategory.volatility; + case IndicatorTabLabel.movingAverages: + return IndicatorCategory.movingAverages; + default: + return null; + } + } }