Skip to content

Commit a8a649f

Browse files
committed
fix(formula e): start readding features
1 parent 8307ac9 commit a8a649f

File tree

5 files changed

+155
-57
lines changed

5 files changed

+155
-57
lines changed

lib/Screens/Racing/circuit.dart

Lines changed: 11 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ import 'package:boxbox/l10n/app_localizations.dart';
2828
import 'package:boxbox/providers/circuit/requests.dart';
2929
import 'package:boxbox/providers/circuit/ui.dart';
3030
import 'package:boxbox/scraping/formula_one.dart';
31-
import 'package:cached_network_image/cached_network_image.dart';
3231
import 'package:flutter/material.dart';
3332
import 'package:font_awesome_flutter/font_awesome_flutter.dart';
3433
import 'package:go_router/go_router.dart';
@@ -97,18 +96,10 @@ class CircuitScreenContent extends StatelessWidget {
9796
);
9897
},
9998
blendMode: BlendMode.dstIn,
100-
child: CachedNetworkImage(
101-
errorWidget: (context, url, error) =>
102-
const Icon(Icons.error_outlined),
103-
fadeOutDuration: const Duration(milliseconds: 300),
104-
fadeInDuration: const Duration(milliseconds: 300),
105-
fit: BoxFit.cover,
106-
imageUrl: details['raceImage']['url'],
107-
placeholder: (context, url) => const LoadingIndicatorUtil(),
108-
colorBlendMode: BlendMode.darken,
109-
height: MediaQuery.of(context).size.width > 780
110-
? MediaQuery.of(context).size.height
111-
: MediaQuery.of(context).size.height * (4 / 9),
99+
child: CircuitUIProvider().getRaceImage(
100+
details['meetingId'] ?? '',
101+
details,
102+
context,
112103
),
113104
),
114105
Padding(
@@ -124,20 +115,9 @@ class CircuitScreenContent extends StatelessWidget {
124115
children: [
125116
ClipRRect(
126117
borderRadius: BorderRadius.circular(6.0),
127-
child: CachedNetworkImage(
128-
errorWidget: (context, url, error) =>
129-
SizedBox(width: 53),
130-
fadeOutDuration: const Duration(milliseconds: 300),
131-
fadeInDuration: const Duration(milliseconds: 300),
132-
placeholder: (context, url) => SizedBox(width: 53),
133-
fit: BoxFit.cover,
134-
imageUrl: details['raceCountryFlag']['url'],
135-
height: MediaQuery.of(context).size.width > 780
136-
? 60
137-
: 30,
138-
width: MediaQuery.of(context).size.width > 780
139-
? 106
140-
: 53,
118+
child: CircuitUIProvider().getRaceFlag(
119+
details,
120+
context,
141121
),
142122
),
143123
Padding(
@@ -150,7 +130,8 @@ class CircuitScreenContent extends StatelessWidget {
150130
: 3,
151131
),
152132
child: Text(
153-
details['race']['meetingCountryName'],
133+
CircuitRequestsProvider()
134+
.getCircuitCountryName(details),
154135
style: TextStyle(
155136
fontFamily: 'Northwell',
156137
fontSize: MediaQuery.of(context).size.width > 780
@@ -164,7 +145,8 @@ class CircuitScreenContent extends StatelessWidget {
164145
Padding(
165146
padding: EdgeInsets.symmetric(horizontal: 20),
166147
child: Text(
167-
details['race']['meetingOfficialName'],
148+
CircuitRequestsProvider()
149+
.getCircuitOfficialName(details),
168150
style: TextStyle(
169151
fontWeight: FontWeight.w400,
170152
fontSize: 16,

lib/api/formulae.dart

Lines changed: 32 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -542,30 +542,39 @@ class FormulaE {
542542
}
543543
}
544544

545-
Future<Map> getSessionsAndRaceDetails(Race race) async {
546-
Map sessions = await getSessions(race.meetingId);
547-
548-
Race raceWithSessions = Race(
549-
race.round,
550-
race.meetingId,
551-
race.raceName,
552-
race.date,
553-
sessions['original']['sessions'].last['startTime'],
554-
race.circuitId,
555-
race.circuitName,
556-
race.circuitUrl,
557-
race.country,
558-
sessions['sessionDates'],
559-
raceCoverUrl: race.raceCoverUrl,
560-
sessionStates: sessions['sessionStates'],
545+
Future<Map> getRaceDetails(String raceId) async {
546+
//String endpoint = Hive.box('settings')
547+
// .get('server', defaultValue: defaultF1Endpoint) as String;
548+
Uri url = Uri.parse(
549+
//endpoint != defaultF1Endpoint
550+
//? '$endpoint/fe/formula-e/v1/races/championshipId=$championshipId'
551+
'$defaultEndpoint/formula-e/v1/races/$raceId',
552+
);
553+
var response = await http.get(
554+
url,
555+
headers: {
556+
'Accept': 'application/json',
557+
'User-Agent':
558+
'Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:124.0) Gecko/20100101 Firefox/124.0',
559+
},
560+
);
561+
Map<String, dynamic> responseAsJson = json.decode(
562+
utf8.decode(response.bodyBytes),
561563
);
562564

565+
return responseAsJson;
566+
}
567+
568+
Future<Map> getSessionsAndRaceDetails(String meetingId) async {
569+
Map sessions = await getSessions(meetingId);
570+
Map race = await getRaceDetails(meetingId);
571+
563572
String endpoint = Hive.box('settings')
564573
.get('server', defaultValue: defaultF1Endpoint) as String;
565574
Uri url = Uri.parse(
566575
endpoint != defaultF1Endpoint
567-
? '$endpoint/fe/content/formula-e/EN/contentTypes=video&contentTypes=news&page=0&pageSize=10&references=FORMULA_E_RACE:${race.meetingId}&onlyRestrictedContent=false&detail=DETAILED'
568-
: '$defaultEndpoint/content/formula-e/EN?contentTypes=video&contentTypes=news&page=0&pageSize=10&references=FORMULA_E_RACE:${race.meetingId}&onlyRestrictedContent=false&detail=DETAILED',
576+
? '$endpoint/fe/content/formula-e/EN/contentTypes=video&contentTypes=news&page=0&pageSize=10&references=FORMULA_E_RACE:${meetingId}&onlyRestrictedContent=false&detail=DETAILED'
577+
: '$defaultEndpoint/content/formula-e/EN?contentTypes=video&contentTypes=news&page=0&pageSize=10&references=FORMULA_E_RACE:${meetingId}&onlyRestrictedContent=false&detail=DETAILED',
569578
);
570579
http.Response response = await http.get(
571580
url,
@@ -580,10 +589,12 @@ class FormulaE {
580589
);
581590

582591
Map formatedMap = {
583-
'raceCustomBBParameter': raceWithSessions,
584-
'sessionsIdsCustomBBParameter': sessions['sessionIds'],
585-
'contentsCustomBBParameter': responseAsJson['content'],
592+
'sessionsIds': sessions['sessionIds'],
593+
'content': responseAsJson['content'],
594+
'race': race,
595+
'meetingId': meetingId,
586596
};
597+
Hive.box('requests').put('feCircuitDetails-$meetingId', formatedMap);
587598
return formatedMap;
588599
}
589600

lib/config/router.dart

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ import 'package:boxbox/helpers/route_handler.dart';
5050
import 'package:flutter/material.dart';
5151
import 'package:boxbox/l10n/app_localizations.dart';
5252
import 'package:go_router/go_router.dart';
53+
import 'package:hive_flutter/hive_flutter.dart';
5354

5455
class RouterLocalConfig {
5556
static final router = GoRouter(
@@ -290,16 +291,26 @@ class RouterLocalConfig {
290291
name: 'racing',
291292
path: 'racing/:meetingId',
292293
builder: (context, state) {
293-
try {
294-
int.parse(state.pathParameters['meetingId']!);
295-
} catch (_) {
296-
return CircuitScreenFromMeetingName(
294+
// will be updated with Rewrite Part 2 (championship-specific routes)
295+
// it ensures compatibility in the meantime
296+
String championship = Hive.box('settings')
297+
.get('championship', defaultValue: 'Formula 1') as String;
298+
if (championship == 'Formula 1') {
299+
try {
300+
int.parse(state.pathParameters['meetingId']!);
301+
} catch (_) {
302+
return CircuitScreenFromMeetingName(
303+
state.pathParameters['meetingId']!,
304+
);
305+
}
306+
return CircuitScreen(
307+
state.pathParameters['meetingId']!,
308+
);
309+
} else {
310+
return CircuitScreen(
297311
state.pathParameters['meetingId']!,
298312
);
299313
}
300-
return CircuitScreen(
301-
state.pathParameters['meetingId']!,
302-
);
303314
},
304315
routes: [
305316
GoRoute(

lib/providers/circuit/requests.dart

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
import 'package:boxbox/api/formula1.dart';
21+
import 'package:boxbox/api/formulae.dart';
2122
import 'package:hive_flutter/hive_flutter.dart';
2223

2324
class CircuitRequestsProvider {
@@ -27,6 +28,9 @@ class CircuitRequestsProvider {
2728
if (championship == 'Formula 1') {
2829
return Hive.box('requests')
2930
.get('f1CircuitDetails-$meetingId', defaultValue: {});
31+
} else if (championship == 'Formula E') {
32+
return Hive.box('requests')
33+
.get('feCircuitDetails-$meetingId', defaultValue: {});
3034
} else {
3135
return {};
3236
}
@@ -37,8 +41,34 @@ class CircuitRequestsProvider {
3741
.get('championship', defaultValue: 'Formula 1') as String;
3842
if (championship == 'Formula 1') {
3943
return Formula1().getCircuitDetails(meetingId);
44+
} else if (championship == 'Formula E') {
45+
return FormulaE().getSessionsAndRaceDetails(meetingId);
4046
} else {
4147
return {};
4248
}
4349
}
50+
51+
String getCircuitCountryName(Map details) {
52+
String championship = Hive.box('settings')
53+
.get('championship', defaultValue: 'Formula 1') as String;
54+
if (championship == 'Formula 1') {
55+
return details['race']['meetingCountryName'];
56+
} else if (championship == 'Formula E') {
57+
return details['race']['city'];
58+
} else {
59+
return '';
60+
}
61+
}
62+
63+
String getCircuitOfficialName(Map details) {
64+
String championship = Hive.box('settings')
65+
.get('championship', defaultValue: 'Formula 1') as String;
66+
if (championship == 'Formula 1') {
67+
return details['race']['meetingOfficialName'];
68+
} else if (championship == 'Formula E') {
69+
return details['race']['name'];
70+
} else {
71+
return '';
72+
}
73+
}
4474
}

lib/providers/circuit/ui.dart

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919

2020
import 'package:boxbox/Screens/Racing/circuit.dart';
2121
import 'package:boxbox/Screens/Racing/circuit_details.dart';
22+
import 'package:boxbox/api/formulae.dart';
2223
import 'package:boxbox/helpers/buttons.dart';
24+
import 'package:boxbox/helpers/loading_indicator_util.dart';
2325
import 'package:boxbox/l10n/app_localizations.dart';
26+
import 'package:cached_network_image/cached_network_image.dart';
2427
import 'package:flutter/material.dart';
2528
import 'package:hive_flutter/hive_flutter.dart';
2629

@@ -145,4 +148,65 @@ class CircuitUIProvider {
145148
return Container();
146149
}
147150
}
151+
152+
Widget getRaceImage(String meetingId, Map details, BuildContext context) {
153+
String championship = Hive.box('settings')
154+
.get('championship', defaultValue: 'Formula 1') as String;
155+
if (championship == 'Formula 1') {
156+
return CachedNetworkImage(
157+
errorWidget: (context, url, error) => const Icon(Icons.error_outlined),
158+
fadeOutDuration: const Duration(milliseconds: 300),
159+
fadeInDuration: const Duration(milliseconds: 300),
160+
fit: BoxFit.cover,
161+
imageUrl: details['raceImage']['url'],
162+
placeholder: (context, url) => const LoadingIndicatorUtil(),
163+
colorBlendMode: BlendMode.darken,
164+
height: MediaQuery.of(context).size.width > 780
165+
? MediaQuery.of(context).size.height
166+
: MediaQuery.of(context).size.height * (4 / 9),
167+
);
168+
} else if (championship == 'Formula E') {
169+
return FutureBuilder(
170+
future: FormulaE().getCircuitImageUrl(meetingId),
171+
builder: (context, snapshot) => snapshot.hasError
172+
? Container()
173+
: snapshot.hasData
174+
? CachedNetworkImage(
175+
errorWidget: (context, url, error) =>
176+
const Icon(Icons.error_outlined),
177+
fadeOutDuration: const Duration(milliseconds: 300),
178+
fadeInDuration: const Duration(milliseconds: 300),
179+
fit: BoxFit.cover,
180+
imageUrl: snapshot.data!,
181+
placeholder: (context, url) => Container(),
182+
colorBlendMode: BlendMode.darken,
183+
height: MediaQuery.of(context).size.width > 780
184+
? MediaQuery.of(context).size.height
185+
: MediaQuery.of(context).size.height * (4 / 9),
186+
)
187+
: Container(),
188+
);
189+
} else {
190+
return Container();
191+
}
192+
}
193+
194+
Widget getRaceFlag(Map details, BuildContext context) {
195+
String championship = Hive.box('settings')
196+
.get('championship', defaultValue: 'Formula 1') as String;
197+
if (championship == 'Formula 1') {
198+
return CachedNetworkImage(
199+
errorWidget: (context, url, error) => SizedBox(width: 53),
200+
fadeOutDuration: const Duration(milliseconds: 300),
201+
fadeInDuration: const Duration(milliseconds: 300),
202+
placeholder: (context, url) => SizedBox(width: 53),
203+
fit: BoxFit.cover,
204+
imageUrl: details['raceCountryFlag']['url'],
205+
height: MediaQuery.of(context).size.width > 780 ? 60 : 30,
206+
width: MediaQuery.of(context).size.width > 780 ? 106 : 53,
207+
);
208+
} else {
209+
return Container();
210+
}
211+
}
148212
}

0 commit comments

Comments
 (0)