Skip to content

Commit

Permalink
chore: remove redundant code
Browse files Browse the repository at this point in the history
  • Loading branch information
ramin-deriv committed Jul 18, 2024
1 parent a51b632 commit 6719136
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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<T> = 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<T> = 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<T> = Widget Function(T? value, String? title);

/// A Custom chip with a disabled and enabled design based on [isSelected].
class CustomChip extends StatelessWidget {
class CustomChip<T> extends StatelessWidget {
/// Initializes a [CustomChip] widget.
const CustomChip({
this.value,
Expand All @@ -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<T>? labelBuilder;

/// To get the widget to be shown inside chips.
final LabelWidgetBuilder? labelWidgetBuilder;
final LabelWidgetBuilder<T>? labelWidgetBuilder;

/// Called when a custom chip is tapped.
/// Pass [onTap] as null to disable the functionality of the chips.
final OnTapCustomChip? onTap;
final OnTapCustomChip<T>? onTap;

/// The border radius of chips container.
final double borderRadius;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,34 +57,19 @@ class MobileToolsBottomSheetContent extends StatefulWidget {

class _MobileToolsBottomSheetContentState
extends State<MobileToolsBottomSheetContent> {
String? _selectedChip = IndicatorTabLabel.all;
IndicatorTabLabel? _selectedChip = IndicatorTabLabel.all;

List<IndicatorItemModel> 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.
Expand Down Expand Up @@ -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<CustomChip>((tabLabel) => tabLabel ==
IndicatorTabLabel.active
? CustomChip<IndicatorTabLabel>(
labelBuilder: (_, __) => IndicatorTabLabel.activeCount(
indicatorsRepo.items.length,
),
value: IndicatorTabLabel.active,
onTap: _onChipTapped,
isSelected: _selectedChip == IndicatorTabLabel.active,
borderRadius: ThemeProvider.margin40,
)
: CustomChip<IndicatorTabLabel>(
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),
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
}

0 comments on commit 6719136

Please sign in to comment.