Skip to content

Commit

Permalink
Official API support (#148)
Browse files Browse the repository at this point in the history
* [results] first steps to new api support

* [results] add support for race results from official api; add switch in the settings

* [results] support offline save

* [results] support qualifications; better offline save check

* [results] add support for free practice

* [results] support sprint & sprint qualifyings

* [results] basic requests for drivers and teams standings

* [schedule] add basic request template

* [schedule] support official api (needs gmt offset parsing)

* [schedule] support route from schedule to circuit screen with official api

* [circuit] support seeing results when coming from official api; add timezone support

* [race] prefer date saved offline for the race countdown

* [sprint] fix results time/gap

* [sprint] fix results view cutted

* [standings] add support for official api

* [results] overall improvements; fix offline race logic

* [standings] fix car image regression

* [results] supports race fastest lap; localize the settings with link to details page

* [circuit] move dates parsing; code cleanup
  • Loading branch information
BrightDV authored May 22, 2024
1 parent f42ff05 commit 94751b8
Show file tree
Hide file tree
Showing 27 changed files with 1,668 additions and 566 deletions.
134 changes: 96 additions & 38 deletions lib/Screens/circuit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
* Copyright (c) 2022-2024, BrightDV
*/

import 'dart:async';

import 'package:boxbox/Screens/article.dart';
import 'package:boxbox/Screens/race_details.dart';
import 'package:boxbox/api/ergast.dart';
Expand Down Expand Up @@ -52,6 +50,8 @@ class CircuitScreen extends StatelessWidget {

@override
Widget build(BuildContext context) {
String scheduleLastSavedFormat = Hive.box('requests')
.get('scheduleLastSavedFormat', defaultValue: 'ergast');
return Scaffold(
body: isFetched ?? true
? NestedScrollView(
Expand All @@ -76,9 +76,12 @@ class CircuitScreen extends StatelessWidget {
body: SingleChildScrollView(
child: FutureBuilder<Map>(
future: EventTracker().getCircuitDetails(
Convert().circuitIdFromErgastToFormulaOne(
race.circuitId,
),
scheduleLastSavedFormat == 'ergast'
? Convert().circuitIdFromErgastToFormulaOne(
race.circuitId,
)
: race.meetingId,
race: scheduleLastSavedFormat == 'ergast' ? null : race,
),
builder: (context, snapshot) => snapshot.hasData
? Column(
Expand All @@ -92,7 +95,9 @@ class CircuitScreen extends StatelessWidget {
Icons.arrow_forward_rounded,
),
RaceDetailsScreen(
race,
scheduleLastSavedFormat == 'ergast'
? race
: snapshot.data!['raceCustomBBParameter'],
snapshot.data!['meetingContext']['timetables']
[2]['session'] ==
's',
Expand Down Expand Up @@ -131,14 +136,25 @@ class CircuitScreen extends StatelessWidget {
),
snapshot.data!['raceResults'] != null &&
snapshot.data!['raceResults'].isNotEmpty
? RaceResults(snapshot, race)
? RaceResults(
snapshot,
scheduleLastSavedFormat == 'ergast'
? race
: snapshot
.data!['raceCustomBBParameter'],
)
: Container(),
snapshot.data!['curatedSection'] != null
? CuratedSection(
snapshot.data!['curatedSection']['items'])
snapshot.data!['curatedSection']['items'],
)
: Container(),
TrackLayoutImage(race),
CircuitFactsAndHistory(race.circuitId),
CircuitFactsAndHistory(
race.detailsPath != null
? race.detailsPath!
: race.circuitId,
),
],
)
: BoxBoxButton(
Expand Down Expand Up @@ -205,7 +221,10 @@ class CircuitScreen extends StatelessWidget {
Icons.arrow_forward_rounded,
),
RaceDetailsScreen(
race,
scheduleLastSavedFormat == 'ergast'
? race
: snapshot.data![
'raceCustomBBParameter'],
snapshot.data!['meetingContext']
['timetables'][2]
['session'] ==
Expand Down Expand Up @@ -251,15 +270,26 @@ class CircuitScreen extends StatelessWidget {
snapshot.data!['raceResults'] != null &&
snapshot.data!['raceResults']
.isNotEmpty
? RaceResults(snapshot, race)
? RaceResults(
snapshot,
scheduleLastSavedFormat ==
'ergast'
? race
: snapshot.data![
'raceCustomBBParameter'],
)
: Container(),
snapshot.data!['curatedSection'] != null
? CuratedSection(
snapshot.data!['curatedSection']
['items'])
: Container(),
TrackLayoutImage(race),
CircuitFactsAndHistory(race.circuitId),
CircuitFactsAndHistory(
race.detailsPath != null
? race.detailsPath!
: race.circuitId,
),
],
)
: BoxBoxButton(
Expand All @@ -281,42 +311,66 @@ class CircuitScreen extends StatelessWidget {
}
}

// top image behind the title in the sliver appbar
class RaceImageProvider extends StatelessWidget {
Future<String> getCircuitImageUrl(Race race) async {
return await RaceTracksUrls().getRaceTrackImageUrl(race.circuitId);
String getCircuitImageUrl(Race race) {
String scheduleLastSavedFormat = Hive.box('requests')
.get('scheduleLastSavedFormat', defaultValue: 'ergast');
if (scheduleLastSavedFormat == 'ergast') {
return RaceTracksUrls().getRaceTrackImageUrl(race.circuitId);
} else {
String coverUrl = race.raceCoverUrl!;
if (race.country == 'Great Britain') {
coverUrl =
race.raceCoverUrl!.replaceFirst('United_Kingdom', 'Great_Britain');
} else if (race.circuitName == 'Monza') {
coverUrl = race.raceCoverUrl!.replaceFirst('Italy', 'Monza');
} else if (race.circuitName == 'Las Vegas') {
coverUrl =
'https://media.formula1.com/image/upload/f_auto/q_auto/v1677238736/content/dam/fom-website/2018-redesign-assets/Racehub%20header%20images%2016x9/Las Vegas.jpg.transform/fullbleed/image.jpg';
} else if (race.country == 'Abu Dhabi') {
coverUrl = race.raceCoverUrl!
.replaceFirst('United_Arab_Emirates', 'Abu_Dhabi');
} else if (race.country == 'Miami') {
coverUrl = race.raceCoverUrl!.replaceFirst('United_States', 'Miami');
}
return coverUrl;
}
}

final Race race;
const RaceImageProvider(this.race, {Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return FutureBuilder<String>(
future: getCircuitImageUrl(race),
builder: (context, snapshot) {
if (snapshot.hasError) {
return RequestErrorWidget(
snapshot.error.toString(),
);
}
return snapshot.hasData
? CachedNetworkImage(
errorWidget: (context, url, error) =>
const Icon(Icons.error_outlined),
fadeOutDuration: const Duration(seconds: 1),
fadeInDuration: const Duration(seconds: 1),
fit: BoxFit.cover,
imageUrl: snapshot.data!,
placeholder: (context, url) => const LoadingIndicatorUtil(),
)
: const LoadingIndicatorUtil();
},
return CachedNetworkImage(
errorWidget: (context, url, error) => const Icon(Icons.error_outlined),
fadeOutDuration: const Duration(seconds: 1),
fadeInDuration: const Duration(seconds: 1),
fit: BoxFit.cover,
imageUrl: getCircuitImageUrl(race),
placeholder: (context, url) => const LoadingIndicatorUtil(),
);
}
}

// track layout in color above the history and facts section
class TrackLayoutImage extends StatelessWidget {
String getTrackLayoutImageUrl(Race race) {
return RaceTracksUrls().getTrackLayoutImageUrl(race.circuitId);
String scheduleLastSavedFormat = Hive.box('requests')
.get('scheduleLastSavedFormat', defaultValue: 'ergast');
String country = race.country;
if (country == 'Monaco') {
country = 'Monoco';
} else if (country == 'Azerbaijan') {
country = 'Baku';
} else if (race.circuitName == 'Austin') {
country = 'USA';
}
return scheduleLastSavedFormat == 'ergast'
? RaceTracksUrls().getTrackLayoutImageUrl(
race.circuitId,
)
: 'https://media.formula1.com/image/upload/f_auto/q_auto/v1677244987/content/dam/fom-website/2018-redesign-assets/Circuit%20maps%2016x9/${country.replaceAll("-", "_").replaceAll(" ", "_")}_Circuit.png';
}

final Race race;
Expand Down Expand Up @@ -658,13 +712,17 @@ class CircuitFactsAndHistory extends StatelessWidget {
Widget build(BuildContext context) {
bool useDarkMode =
Hive.box('settings').get('darkMode', defaultValue: true) as bool;
String scheduleLastSavedFormat = Hive.box('requests')
.get('scheduleLastSavedFormat', defaultValue: 'ergast');
return Padding(
padding: const EdgeInsets.all(10),
child: FutureBuilder<Map>(
future: FormulaOneScraper().scrapeCircuitFactsAndHistory(
Convert().circuitNameFromErgastToFormulaOneForRaceHub(
circuitId,
),
scheduleLastSavedFormat == 'ergast'
? Convert().circuitNameFromErgastToFormulaOneForRaceHub(
circuitId,
)
: circuitId,
context,
),
builder: (context, snapshot) => snapshot.hasData
Expand Down
16 changes: 13 additions & 3 deletions lib/Screens/driver_details.dart
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,13 @@ class DriverDetailsScreen extends StatelessWidget {
final String driverId;
final String givenName;
final String familyName;
final String? detailsPath;
const DriverDetailsScreen(
this.driverId,
this.givenName,
this.familyName, {
super.key,
this.detailsPath,
});

@override
Expand Down Expand Up @@ -82,7 +84,7 @@ class DriverDetailsScreen extends StatelessWidget {
),
body: TabBarView(
children: [
DriverInfo(driverId),
DriverInfo(driverId, detailsPath: detailsPath),
DriverResults(driverId),
],
),
Expand All @@ -93,7 +95,12 @@ class DriverDetailsScreen extends StatelessWidget {

class DriverInfo extends StatelessWidget {
final String driverId;
const DriverInfo(this.driverId, {super.key});
final String? detailsPath;
const DriverInfo(
this.driverId, {
super.key,
this.detailsPath,
});

@override
Widget build(BuildContext context) {
Expand All @@ -106,7 +113,10 @@ class DriverInfo extends StatelessWidget {
child: DriverImageProvider(driverId, 'driver'),
),
FutureBuilder<List<List>>(
future: FormulaOneScraper().scrapeDriversDetails(driverId),
future: FormulaOneScraper().scrapeDriversDetails(
driverId,
detailsPath,
),
builder: (context, snapshot) => snapshot.hasError
? RequestErrorWidget(
snapshot.error.toString(),
Expand Down
33 changes: 20 additions & 13 deletions lib/Screens/free_practice_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/

import 'package:boxbox/api/driver_components.dart';
import 'package:boxbox/api/formula1.dart';
import 'package:boxbox/helpers/request_error.dart';
import 'package:boxbox/helpers/loading_indicator_util.dart';
import 'package:boxbox/helpers/team_background_color.dart';
Expand All @@ -33,6 +34,7 @@ class FreePracticeScreen extends StatelessWidget {
final String sessionTitle;
final int sessionIndex;
final String circuitId;
final String meetingId;
final int raceYear;
final String raceName;
final String? raceUrl;
Expand All @@ -41,6 +43,7 @@ class FreePracticeScreen extends StatelessWidget {
this.sessionTitle,
this.sessionIndex,
this.circuitId,
this.meetingId,
this.raceYear,
this.raceName, {
Key? key,
Expand All @@ -63,12 +66,14 @@ class FreePracticeScreen extends StatelessWidget {
false,
raceUrl: raceUrl,
)
: FormulaOneScraper().scrapeFreePracticeResult(
: Formula1().getFreePracticeStandings(meetingId, sessionIndex),
// disable scraping for the moment
/* FormulaOneScraper().scrapeFreePracticeResult(
circuitId,
sessionIndex,
'practice-$sessionIndex',
true,
),
), */
builder: (context, snapshot) => snapshot.hasError
? snapshot.error.toString() == 'RangeError: Value not in range: 0'
? Center(
Expand All @@ -85,16 +90,14 @@ class FreePracticeScreen extends StatelessWidget {
'\n' +
snapshot.stackTrace.toString(),
)
: snapshot.hasError
? RequestErrorWidget(snapshot.error.toString())
: snapshot.hasData
? FreePracticeResultsList(
snapshot.data!,
raceYear,
raceName,
sessionIndex,
)
: const LoadingIndicatorUtil(),
: snapshot.hasData
? FreePracticeResultsList(
snapshot.data!,
raceYear,
raceName,
sessionIndex,
)
: const LoadingIndicatorUtil(),
),
);
}
Expand Down Expand Up @@ -334,7 +337,11 @@ class FreePracticeResultItem extends StatelessWidget {
child: Padding(
padding: const EdgeInsets.only(top: 5, bottom: 5),
child: Text(
result.fastestLap == '' ? '--' : result.fastestLap,
index == 0
? ''
: result.fastestLap == ''
? '--'
: result.fastestLap,
textAlign: TextAlign.center,
),
),
Expand Down
Loading

0 comments on commit 94751b8

Please sign in to comment.