Skip to content

Commit

Permalink
Revert "Focus item when searching"
Browse files Browse the repository at this point in the history
This reverts commit bb92daf.
  • Loading branch information
ferraridamiano committed Feb 1, 2025
1 parent 8e59e05 commit 62c8b1e
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 110 deletions.
14 changes: 4 additions & 10 deletions lib/app_router.dart
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,8 @@ final routerProvider = Provider<GoRouter>(
path: '/conversions/:property',
pageBuilder: (context, state) {
final String property = state.pathParameters['property']!;
final propertyx = property.kebab2PropertyX();
final fragment = state.uri.fragment;
return NoTransitionPage(
child: ConversionPage(
propertyx,
focusedUnit: fragment == '' ? null : fragment,
),
);
final propertyx = kebabStringToPropertyX(property);
return NoTransitionPage(child: ConversionPage(propertyx));
},
),
GoRoute(
Expand All @@ -80,7 +74,7 @@ final routerProvider = Provider<GoRouter>(
path: ':property',
builder: (context, state) {
final String property = state.pathParameters['property']!;
final propertyx = property.kebab2PropertyX();
final propertyx = kebabStringToPropertyX(property);
return ReorderUnitsPage(
selectedProperty: propertyx,
isPropertySelected: true,
Expand All @@ -98,7 +92,7 @@ final routerProvider = Provider<GoRouter>(
path: ':property',
builder: (context, state) {
final String property = state.pathParameters['property']!;
final propertyx = property.kebab2PropertyX();
final propertyx = kebabStringToPropertyX(property);
return HideUnitsPage(
selectedProperty: propertyx,
isPropertySelected: true,
Expand Down
18 changes: 8 additions & 10 deletions lib/pages/app_scaffold.dart
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,10 @@ class AppScaffold extends ConsumerWidget {
}

void clearAll(bool isDrawerFixed) {
final currentProperty =
GoRouterState.of(context).uri.pathSegments.last.kebab2PropertyX();
final currentProperty = kebabStringToPropertyX(GoRouterState.of(context)
.uri
.toString()
.substring('/conversions/'.length));
if (ref
.read(ConversionsNotifier.provider.notifier)
.shouldShowSnackbar(currentProperty)) {
Expand Down Expand Up @@ -61,17 +63,13 @@ class AppScaffold extends ConsumerWidget {

void openSearch() {
ref.read(PropertiesOrderNotifier.provider).whenData((orderList) async {
final result = await showSearch(
final selectedProperty = await showSearch<PROPERTYX?>(
context: context,
delegate: CustomSearchDelegate(orderList),
);
if (result != null) {
final selectedProperty = result.$1;
final selectedUnit = result.$2;
var targetPath = '/conversions/${selectedProperty.toKebabCase()}';
if (selectedUnit != null) {
targetPath = '$targetPath#$selectedUnit';
}
if (selectedProperty != null) {
final String targetPath =
'/conversions/${selectedProperty.toKebabCase()}';

if (context.mounted &&
GoRouterState.of(context).uri.toString() != targetPath) {
Expand Down
99 changes: 44 additions & 55 deletions lib/pages/conversion_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,8 @@ import 'package:intl/intl.dart';

class ConversionPage extends ConsumerWidget {
final PROPERTYX property;
final String? focusedUnit;

const ConversionPage(this.property, {super.key, this.focusedUnit});
const ConversionPage(this.property, {super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
Expand Down Expand Up @@ -60,62 +59,52 @@ class ConversionPage extends ConsumerWidget {
}
}

UnitWidget unitWidgetBuilder(
UnitData unitData,
) {
final focusNode = FocusNode();
if (focusedUnit != null &&
focusedUnit == unitName2KebabCase(unitData.unit.name)) {
focusNode.requestFocus();
}
return UnitWidget(
tffKey: unitData.unit.name.toString(),
unitName: unitMap[unitData.unit.name]!,
unitSymbol: unitData.unit.symbol,
keyboardType: unitData.textInputType,
controller: unitData.tec,
focusNode: focusNode,
validator: (String? input) {
if (input != null) {
if (input != '' && !unitData.getValidator().hasMatch(input)) {
return l10n.invalidCharacters;
UnitWidget unitWidgetBuilder(UnitData unitData) => UnitWidget(
tffKey: unitData.unit.name.toString(),
unitName: unitMap[unitData.unit.name]!,
unitSymbol: unitData.unit.symbol,
keyboardType: unitData.textInputType,
controller: unitData.tec,
validator: (String? input) {
if (input != null) {
if (input != '' && !unitData.getValidator().hasMatch(input)) {
return l10n.invalidCharacters;
}
}
return null;
},
onChanged: (String txt) {
String newTxt = txt;
bool changed = false;
if (newTxt.contains(',')) {
newTxt = newTxt.replaceAll(',', '.');
changed = true;
}
if (newTxt.startsWith('.')) {
newTxt = '0$newTxt';
changed = true;
}
}
return null;
},
onChanged: (String txt) {
String newTxt = txt;
bool changed = false;
if (newTxt.contains(',')) {
newTxt = newTxt.replaceAll(',', '.');
changed = true;
}
if (newTxt.startsWith('.')) {
newTxt = '0$newTxt';
changed = true;
}
if (changed) {
unitData.tec.value = TextEditingValue(
text: newTxt,
selection: TextSelection.collapsed(offset: newTxt.length),
);
}
if (txt == '' || unitData.getValidator().hasMatch(txt)) {
var conversions = ref.read(ConversionsNotifier.provider.notifier);
//just numeral system uses a string for conversion
if (unitData.property == PROPERTYX.numeralSystems) {
conversions.convert(unitData, txt == "" ? null : txt, property);
} else {
conversions.convert(
unitData,
txt == "" ? null : double.parse(txt),
property,
if (changed) {
unitData.tec.value = TextEditingValue(
text: newTxt,
selection: TextSelection.collapsed(offset: newTxt.length),
);
}
}
},
);
}
if (txt == '' || unitData.getValidator().hasMatch(txt)) {
var conversions = ref.read(ConversionsNotifier.provider.notifier);
//just numeral system uses a string for conversion
if (unitData.property == PROPERTYX.numeralSystems) {
conversions.convert(unitData, txt == "" ? null : txt, property);
} else {
conversions.convert(
unitData,
txt == "" ? null : double.parse(txt),
property,
);
}
}
},
);

final unhiddenGridTiles = unhiddenUnitData.map(unitWidgetBuilder).toList();
final hiddenGridTiles = hiddenUnitData.map(unitWidgetBuilder).toList();
Expand Down
21 changes: 10 additions & 11 deletions lib/pages/search_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import 'package:converterpro/utils/utils_widgets.dart';
import 'package:flutter/material.dart';
import 'package:translations/app_localizations.dart';

class CustomSearchDelegate extends SearchDelegate<(PROPERTYX, String?)?> {
class CustomSearchDelegate extends SearchDelegate<PROPERTYX?> {
final List<PROPERTYX> orderList;

CustomSearchDelegate(this.orderList);
Expand All @@ -27,13 +27,12 @@ class CustomSearchDelegate extends SearchDelegate<(PROPERTYX, String?)?> {
Widget buildSuggestions(BuildContext context) {
final Brightness brightness = Theme.of(context).brightness;

final List<SearchUnit> dataSearch =
getSearchUnitsList((PROPERTYX property, String? unitName) {
close(context, (property, unitName));
final List<SearchUnit> dataSearch = getSearchUnitsList((PROPERTYX result) {
close(context, result);
}, context);
final List<SearchGridTile> allConversions = initializeGridSearch(
(PROPERTYX property) {
close(context, (property, null));
(PROPERTYX result) {
close(context, result);
},
context,
brightness == Brightness.dark,
Expand Down Expand Up @@ -77,7 +76,7 @@ class CustomSearchDelegate extends SearchDelegate<(PROPERTYX, String?)?> {

/// This method will return a List of [SearchUnit], needed in order to display the tiles in the search
List<SearchUnit> getSearchUnitsList(
void Function(PROPERTYX, String?) onTap, BuildContext context) {
void Function(PROPERTYX) onTap, BuildContext context) {
List<SearchUnit> searchUnitsList = [];
final propertyUiMap = getPropertyUiMap(context);
final unitUiMap = getUnitUiMap(context);
Expand All @@ -90,15 +89,15 @@ List<SearchUnit> getSearchUnitsList(
searchUnitsList.add(SearchUnit(
iconAsset: propertyImagePath,
unitName: propertyUi.name,
onTap: () => onTap(property.key, null),
onTap: () => onTap(property.key),
));
// Add units in search
searchUnitsList.addAll(
unitUiMap[propertyx]!.entries.map(
unitUiMap[propertyx]!.values.map(
(e) => SearchUnit(
iconAsset: propertyImagePath,
unitName: e.value,
onTap: () => onTap(propertyx, unitName2KebabCase(e.key)),
unitName: e,
onTap: () => onTap(propertyx),
),
),
);
Expand Down
8 changes: 4 additions & 4 deletions lib/utils/navigator_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ AppPage computeSelectedSection(BuildContext context) {

int? computeSelectedConversionPage(
BuildContext context, Map<PROPERTYX, int> inversePropertiesOrdering) {
final segments = GoRouterState.of(context).uri.pathSegments;

if (segments.isNotEmpty && segments[0] == 'conversions') {
return inversePropertiesOrdering[segments.last.kebab2PropertyX()];
final location = GoRouterState.of(context).uri.toString();
if (location.startsWith('/conversions')) {
return inversePropertiesOrdering[
kebabStringToPropertyX(location.split('/').last)];
}
return null;
}
23 changes: 7 additions & 16 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -123,23 +123,14 @@ int color2Int(Color color) =>
_floatToInt8(color.g) << 8 |
_floatToInt8(color.b) << 0;

extension on String {
String _toKebabCase() => replaceAllMapped(
RegExp(r'([A-Z])'), (match) => '-${match[0]!.toLowerCase()}');
}

/// Converts PROPERTYX.digitalData to a kebab String like 'digital-data'
extension KebabCaseProperty on PROPERTYX {
String toKebabCase() => toString().split('.').last._toKebabCase();
extension KebabCaseExtension on PROPERTYX {
String toKebabCase() => toString().split('.').last.replaceAllMapped(
RegExp(r'([A-Z])'), (match) => '-${match[0]!.toLowerCase()}');
}

extension KebabStringToPropertyX on String {
PROPERTYX kebab2PropertyX() {
final lowerCaseString = replaceAll('-', '').toLowerCase();
return PROPERTYX.values.firstWhere(
(e) => e.toString().toLowerCase() == 'propertyx.$lowerCaseString');
}
PROPERTYX kebabStringToPropertyX(String string) {
final lowerCaseString = string.replaceAll('-', '').toLowerCase();
return PROPERTYX.values.firstWhere(
(e) => e.toString().toLowerCase() == 'propertyx.$lowerCaseString');
}

String unitName2KebabCase(dynamic unitName) =>
unitName.toString().split('.').last._toKebabCase();
15 changes: 11 additions & 4 deletions lib/utils/utils_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ class UnitWidget extends StatefulWidget {
final String unitName;
final String? unitSymbol;
final void Function(String)? onChanged;
final FocusNode focusNode;

const UnitWidget({
super.key,
Expand All @@ -22,21 +21,29 @@ class UnitWidget extends StatefulWidget {
required this.unitName,
this.unitSymbol,
this.onChanged,
required this.focusNode,
});

@override
State<UnitWidget> createState() => _UnitWidgetState();
}

class _UnitWidgetState extends State<UnitWidget> {
FocusNode focusNode = FocusNode();

@override
void dispose() {
super.dispose();
focusNode.dispose();
}

@override
Widget build(BuildContext context) {
focusNode.addListener(() => setState(() {}));
return Padding(
padding: const EdgeInsets.symmetric(vertical: 8, horizontal: 15),
child: TextFormField(
key: ValueKey(widget.tffKey),
focusNode: widget.focusNode,
focusNode: focusNode,
style: const TextStyle(fontSize: 16.0),
keyboardType: widget.keyboardType,
controller: widget.controller,
Expand Down Expand Up @@ -70,7 +77,7 @@ class _UnitWidgetState extends State<UnitWidget> {
),
floatingLabelStyle: TextStyle(
fontSize: 20,
color: widget.focusNode.hasFocus
color: focusNode.hasFocus
? Theme.of(context).colorScheme.secondary
: null,
),
Expand Down

0 comments on commit 62c8b1e

Please sign in to comment.