Skip to content
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
19 changes: 13 additions & 6 deletions cloudflare-worker/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default {
if (url.pathname === '/health') {
return new Response(JSON.stringify({ status: 'ok' }), {
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
...getCorsHeaders(request),
},
});
Expand All @@ -53,7 +53,7 @@ export default {
return new Response(JSON.stringify(festivalsData), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Cache-Control': FESTIVALS_CACHE_CONTROL,
...getCorsHeaders(request),
},
Expand Down Expand Up @@ -81,6 +81,13 @@ export default {
// Clone the response and add CORS headers
const newHeaders = new Headers(response.headers);
setCorsHeaders(newHeaders, request);

// Ensure JSON responses explicitly declare UTF-8 encoding
// This prevents mojibake when non-ASCII characters (é, ö, ä, ñ) are present
const contentType = newHeaders.get('Content-Type');
if (contentType && contentType.includes('application/json') && !contentType.includes('charset')) {
newHeaders.set('Content-Type', 'application/json; charset=utf-8');
}

return new Response(response.body, {
status: response.status,
Expand All @@ -91,7 +98,7 @@ export default {
return new Response(JSON.stringify({ error: 'Proxy error', message: error.message }), {
status: 502,
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
...getCorsHeaders(request),
},
});
Expand Down Expand Up @@ -124,7 +131,7 @@ async function handleAvailableBeverageTypes(festivalId, request) {
}), {
status: 404,
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
...getCorsHeaders(request),
},
});
Expand All @@ -142,7 +149,7 @@ async function handleAvailableBeverageTypes(festivalId, request) {
}), {
status: 200,
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
'Cache-Control': 'public, max-age=3600', // Cache for 1 hour
...getCorsHeaders(request),
},
Expand All @@ -154,7 +161,7 @@ async function handleAvailableBeverageTypes(festivalId, request) {
}), {
status: 500,
headers: {
'Content-Type': 'application/json',
'Content-Type': 'application/json; charset=utf-8',
...getCorsHeaders(request),
},
});
Expand Down
5 changes: 3 additions & 2 deletions lib/screens/drinks_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -845,9 +845,10 @@ class _StyleFilterSheet extends StatelessWidget {
final styleCounts = beerProvider.styleCountsMap;
final selectedStyles = beerProvider.selectedStyles;

// Sort styles alphabetically (don't move selected to top to avoid jumping)
// Sort styles alphabetically using locale-aware comparison
// This ensures non-ASCII characters (é, ñ, etc.) sort correctly
final sortedStyles = List<String>.from(styles);
sortedStyles.sort((a, b) => a.compareTo(b));
sortedStyles.sort(StringComparisonHelper.compareLocaleAware);

return Container(
padding: const EdgeInsets.all(16),
Expand Down
6 changes: 5 additions & 1 deletion lib/services/beer_api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,11 @@ class BeerApiService {
.timeout(timeout);

if (response.statusCode == 200) {
final data = json.decode(response.body) as Map<String, dynamic>;
// Decode as UTF-8 to handle non-ASCII characters properly (é, ñ, etc.)
// Using response.body defaults to Latin-1 if no charset in Content-Type,
// which causes "Rosé" to display as "Rosé" (mojibake)
final jsonString = utf8.decode(response.bodyBytes);
final data = json.decode(jsonString) as Map<String, dynamic>;
return _parseDrinks(data, festival.id);
} else if (response.statusCode == 404) {
// Beverage type not available for this festival
Expand Down
34 changes: 34 additions & 0 deletions lib/utils/string_comparison_helper.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/// Helper class for locale-aware string comparisons
///
/// Provides methods to properly sort and compare strings containing
/// non-ASCII characters (e.g., "rosé", "café") in a human-friendly way.
class StringComparisonHelper {
// Private constructor to prevent instantiation
StringComparisonHelper._();

/// Locale-aware case-insensitive string comparison
///
/// This ensures that strings with accented characters (é, ñ, ü, etc.)
/// are sorted in a reasonable alphabetical order. While not perfect for
/// all locales, this approach handles common European accented characters
/// properly for beer/wine/cider style names.
///
/// The comparison is case-insensitive, so "IPA", "Ipa", and "ipa" are
/// treated as equal.
///
/// Examples:
/// - "Café" comes right after "Cafe"
/// - "Rosé" comes right after "Rose"
/// - "IPA" and "ipa" are treated as equal
///
/// For sorting lists:
/// ```dart
/// styles.sort(StringComparisonHelper.compareLocaleAware);
/// ```
static int compareLocaleAware(String a, String b) {
// Use case-insensitive comparison
// This handles accented characters reasonably well for European languages
// by comparing the lowercase versions
return a.toLowerCase().compareTo(b.toLowerCase());
}
}
1 change: 1 addition & 0 deletions lib/utils/utils.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export 'abv_strength_helper.dart';
export 'beverage_type_helper.dart';
export 'category_color_helper.dart';
export 'string_comparison_helper.dart';
export 'string_formatting_helper.dart';
export 'style_description_helper.dart';
export 'url_launcher_helper.dart';
126 changes: 126 additions & 0 deletions test/drinks_screen_style_filter_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -342,5 +342,131 @@ void main() {
// Verify Stout is selected but stays in alphabetical position
expect(thirdCheckbox.value, true);
});

testWidgets('styles with non-ASCII characters sort correctly',
(WidgetTester tester) async {
// Override the test drinks to include non-ASCII characters
final drinksWithAccents = [
Drink(
product: const Product(
id: 'drink1',
name: 'Rose Cider',
abv: 5.0,
category: 'cider',
dispense: 'keg',
style: 'Rose',
),
producer: const Producer(
id: 'cidery1',
name: 'Test Cidery',
location: 'France',
products: [],
),
festivalId: 'cbf2025',
),
Drink(
product: const Product(
id: 'drink2',
name: 'Rosé Cider',
abv: 5.2,
category: 'cider',
dispense: 'keg',
style: 'Rosé',
),
producer: const Producer(
id: 'cidery1',
name: 'Test Cidery',
location: 'France',
products: [],
),
festivalId: 'cbf2025',
),
Drink(
product: const Product(
id: 'drink3',
name: 'Cafe Stout',
abv: 6.0,
category: 'beer',
dispense: 'cask',
style: 'Cafe',
),
producer: const Producer(
id: 'brewery1',
name: 'Test Brewery',
location: 'UK',
products: [],
),
festivalId: 'cbf2025',
),
Drink(
product: const Product(
id: 'drink4',
name: 'Café Stout',
abv: 6.2,
category: 'beer',
dispense: 'cask',
style: 'Café',
),
producer: const Producer(
id: 'brewery1',
name: 'Test Brewery',
location: 'UK',
products: [],
),
festivalId: 'cbf2025',
),
];

// Create new provider with accented test data
final accentProvider = BeerProvider(
apiService: mockApiService,
festivalService: mockFestivalService,
analyticsService: mockAnalyticsService,
);

when(mockApiService.fetchAllDrinks(any))
.thenAnswer((_) async => drinksWithAccents);

await accentProvider.initialize();
await accentProvider.loadDrinks();

await tester.pumpWidget(
ChangeNotifierProvider<BeerProvider>.value(
value: accentProvider,
child: const MaterialApp(
home: DrinksScreen(),
),
),
);
await tester.pumpAndSettle();

// Open style filter
await tester.tap(find.text('Style'));
await tester.pumpAndSettle();

// Find all CheckboxListTiles
final checkboxes = find.byType(CheckboxListTile);
expect(checkboxes, findsNWidgets(4));

// Verify locale-aware alphabetical order:
// Cafe, Café, Rose, Rosé
final firstCheckbox = tester.widget<CheckboxListTile>(checkboxes.at(0));
final secondCheckbox = tester.widget<CheckboxListTile>(checkboxes.at(1));
final thirdCheckbox = tester.widget<CheckboxListTile>(checkboxes.at(2));
final fourthCheckbox = tester.widget<CheckboxListTile>(checkboxes.at(3));

expect((firstCheckbox.title as Text).data, 'Cafe (1)');
expect((secondCheckbox.title as Text).data, 'Café (1)');
expect((thirdCheckbox.title as Text).data, 'Rose (1)');
expect((fourthCheckbox.title as Text).data, 'Rosé (1)');

// Verify the accented characters display correctly (not garbled)
expect((secondCheckbox.title as Text).data?.contains('é'), true,
reason: 'Café should display the é character correctly');
expect((fourthCheckbox.title as Text).data?.contains('é'), true,
reason: 'Rosé should display the é character correctly');

accentProvider.dispose();
});
});
}
Loading
Loading