Skip to content

Commit

Permalink
[code] use consts to facilitate maintenance
Browse files Browse the repository at this point in the history
  • Loading branch information
BrightDV committed Jul 17, 2024
1 parent 324ff8e commit 5532a29
Show file tree
Hide file tree
Showing 15 changed files with 98 additions and 47 deletions.
3 changes: 2 additions & 1 deletion lib/Screens/custom_home_feed_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Copyright (c) 2022-2024, BrightDV
*/

import 'package:boxbox/helpers/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';
Expand All @@ -34,7 +35,7 @@ class _CustomeHomeFeedSettingsScreenState
extends State<CustomeHomeFeedSettingsScreen> {
@override
Widget build(BuildContext context) {
const String officialFeed = "https://api.formula1.com";
final String officialFeed = Constants().F1_API_URL;
List savedFeedUrl = Hive.box('settings')
.get('homeFeed', defaultValue: [officialFeed, "api"]) as List;
List customFeeds =
Expand Down
3 changes: 2 additions & 1 deletion lib/Screens/racehub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import 'package:boxbox/Screens/circuit.dart';
import 'package:boxbox/api/event_tracker.dart';
import 'package:boxbox/api/race_components.dart';
import 'package:boxbox/helpers/buttons.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:boxbox/helpers/hover.dart';
import 'package:boxbox/helpers/loading_indicator_util.dart';
import 'package:boxbox/helpers/request_error.dart';
Expand Down Expand Up @@ -518,7 +519,7 @@ class PdfViewer extends StatelessWidget {

@override
Widget build(BuildContext context) {
const String defaultServer = "https://api.formula1.com";
final String defaultServer = Constants().F1_API_URL;
String server = Hive.box('settings')
.get('server', defaultValue: defaultServer) as String;
return Scaffold(
Expand Down
3 changes: 2 additions & 1 deletion lib/Screens/server_settings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
* Copyright (c) 2022-2024, BrightDV
*/

import 'package:boxbox/helpers/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';
Expand All @@ -32,7 +33,7 @@ class ServerSettingsScreen extends StatefulWidget {
class _ServerSettingsScreenState extends State<ServerSettingsScreen> {
@override
Widget build(BuildContext context) {
const String officialServer = "https://api.formula1.com";
final String officialServer = Constants().F1_API_URL;
List customServers =
Hive.box('settings').get('customServers', defaultValue: []) as List;
String savedServer = Hive.box('settings')
Expand Down
35 changes: 19 additions & 16 deletions lib/api/brightcove.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

import 'dart:convert';

import 'package:boxbox/helpers/constants.dart';
import 'package:boxbox/helpers/download.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_cache_manager/file.dart';
Expand All @@ -43,13 +44,15 @@ class BrightCove {
String championship = Hive.box('settings')
.get('championship', defaultValue: 'Formula 1') as String;
late Map responseAsJson;
final String f1BrightCovePlayerId = Constants().F1_BRIGHTCOVE_PLAYER_ID;
final String fEBrightCovePlayerId = Constants().FE_BRIGHTCOVE_PLAYER_ID;
if (kIsWeb) {
late Uri uri;
if (articleChampionship != null) {
uri = Uri.parse(
articleChampionship == 'Formula 1'
? 'https://edge.api.brightcove.com/playback/v1/accounts/6057949432001/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/6275361344001/videos/$videoId',
? 'https://edge.api.brightcove.com/playback/v1/accounts/$f1BrightCovePlayerId/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/$fEBrightCovePlayerId/videos/$videoId',
);
} else if (player != null) {
uri = Uri.parse(
Expand All @@ -58,20 +61,20 @@ class BrightCove {
} else {
uri = Uri.parse(
championship == 'Formula 1'
? 'https://edge.api.brightcove.com/playback/v1/accounts/6057949432001/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/6275361344001/videos/$videoId',
? 'https://edge.api.brightcove.com/playback/v1/accounts/$f1BrightCovePlayerId/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/$fEBrightCovePlayerId/videos/$videoId',
);
}
Response res = await get(
uri,
headers: {
'Accept': articleChampionship != null
? articleChampionship == 'Formula 1'
? ' application/json;pk=BCpkADawqM1hQVBuXkSlsl6hUsBZQMmrLbIfOjJQ3_n8zmPOhlNSwZhQBF6d5xggxm0t052lQjYyhqZR3FW2eP03YGOER9ihJkUnIhRZGBxuLhnL-QiFpvcDWIh_LvwN5j8zkjTtGKarhsdV'
: ' application/json;pk=BCpkADawqM0CZElkVwfs62q-JTOc4CeZSNJRfxT923qzbMSqp6qn5VEgWV1iao1cEf2sXX9ce8achTuOfYKUvfchSis_rB5Sxz_ih70GYLYdf8bZgHi3Yq1VK_6v3mj29rxxcJjF3OX6Ri32'
? Constants().F1_BRIGHTCOVE_PLAYER_KEY
: Constants().FE_BRIGHTCOVE_PLAYER_KEY
: championship == 'Formula 1'
? ' application/json;pk=BCpkADawqM1hQVBuXkSlsl6hUsBZQMmrLbIfOjJQ3_n8zmPOhlNSwZhQBF6d5xggxm0t052lQjYyhqZR3FW2eP03YGOER9ihJkUnIhRZGBxuLhnL-QiFpvcDWIh_LvwN5j8zkjTtGKarhsdV'
: ' application/json;pk=BCpkADawqM0CZElkVwfs62q-JTOc4CeZSNJRfxT923qzbMSqp6qn5VEgWV1iao1cEf2sXX9ce8achTuOfYKUvfchSis_rB5Sxz_ih70GYLYdf8bZgHi3Yq1VK_6v3mj29rxxcJjF3OX6Ri32',
? Constants().F1_BRIGHTCOVE_PLAYER_KEY
: Constants().FE_BRIGHTCOVE_PLAYER_KEY
},
);
responseAsJson = jsonDecode(res.body);
Expand All @@ -80,26 +83,26 @@ class BrightCove {

if (articleChampionship != null) {
url = articleChampionship == 'Formula 1'
? 'https://edge.api.brightcove.com/playback/v1/accounts/6057949432001/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/6275361344001/videos/$videoId';
? 'https://edge.api.brightcove.com/playback/v1/accounts/$f1BrightCovePlayerId/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/$fEBrightCovePlayerId/videos/$videoId';
} else if (player != null) {
url =
'https://edge.api.brightcove.com/playback/v1/accounts/$player/videos/$videoId';
} else {
url = championship == 'Formula 1'
? 'https://edge.api.brightcove.com/playback/v1/accounts/6057949432001/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/6275361344001/videos/$videoId';
? 'https://edge.api.brightcove.com/playback/v1/accounts/$f1BrightCovePlayerId/videos/$videoId'
: 'https://edge.api.brightcove.com/playback/v1/accounts/$fEBrightCovePlayerId/videos/$videoId';
}
final Future<File> fileStream = videoCache.getSingleFile(
url,
headers: {
'Accept': articleChampionship != null
? articleChampionship == 'Formula 1'
? ' application/json;pk=BCpkADawqM1hQVBuXkSlsl6hUsBZQMmrLbIfOjJQ3_n8zmPOhlNSwZhQBF6d5xggxm0t052lQjYyhqZR3FW2eP03YGOER9ihJkUnIhRZGBxuLhnL-QiFpvcDWIh_LvwN5j8zkjTtGKarhsdV'
: ' application/json;pk=BCpkADawqM0CZElkVwfs62q-JTOc4CeZSNJRfxT923qzbMSqp6qn5VEgWV1iao1cEf2sXX9ce8achTuOfYKUvfchSis_rB5Sxz_ih70GYLYdf8bZgHi3Yq1VK_6v3mj29rxxcJjF3OX6Ri32'
? Constants().F1_BRIGHTCOVE_PLAYER_KEY
: Constants().FE_BRIGHTCOVE_PLAYER_KEY
: championship == 'Formula 1'
? ' application/json;pk=BCpkADawqM1hQVBuXkSlsl6hUsBZQMmrLbIfOjJQ3_n8zmPOhlNSwZhQBF6d5xggxm0t052lQjYyhqZR3FW2eP03YGOER9ihJkUnIhRZGBxuLhnL-QiFpvcDWIh_LvwN5j8zkjTtGKarhsdV'
: ' application/json;pk=BCpkADawqM0CZElkVwfs62q-JTOc4CeZSNJRfxT923qzbMSqp6qn5VEgWV1iao1cEf2sXX9ce8achTuOfYKUvfchSis_rB5Sxz_ih70GYLYdf8bZgHi3Yq1VK_6v3mj29rxxcJjF3OX6Ri32',
? Constants().F1_BRIGHTCOVE_PLAYER_KEY
: Constants().FE_BRIGHTCOVE_PLAYER_KEY
},
);
final response = await fileStream;
Expand Down
25 changes: 14 additions & 11 deletions lib/api/ergast.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,13 @@ import 'dart:convert';
import 'package:boxbox/api/driver_components.dart';
import 'package:boxbox/api/race_components.dart';
import 'package:boxbox/api/team_components.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:boxbox/helpers/convert_ergast_and_formula_one.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;

class _ErgastApiCalls {
final String defaultEndpoint = Constants().ERGAST_API_URL;
List<DriverResult> formatRaceStandings(Map raceStandings) {
List<DriverResult> formatedRaceStandings = [];
List jsonResponse =
Expand Down Expand Up @@ -94,7 +96,7 @@ class _ErgastApiCalls {
return formatRaceStandings(results);
} else {
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/$round/results.json',
'$defaultEndpoint/f1/${DateTime.now().year}/$round/results.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand All @@ -112,7 +114,7 @@ class _ErgastApiCalls {

FutureOr<List<DriverResult>> getSprintStandings(String round) async {
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/$round/sprint.json');
'$defaultEndpoint/f1/${DateTime.now().year}/$round/sprint.json');
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
List<DriverResult> formatedRaceStandings = [];
Expand Down Expand Up @@ -173,7 +175,7 @@ class _ErgastApiCalls {
String round) async {
List<DriverQualificationResult> driversResults = [];
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/$round/qualifying.json',
'$defaultEndpoint/f1/${DateTime.now().year}/$round/qualifying.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand Down Expand Up @@ -247,7 +249,7 @@ class _ErgastApiCalls {
return formatLastStandings(driversStandings);
} else {
var url = Uri.parse(
'https://ergast.com/api/f1/current/driverStandings.json',
'$defaultEndpoint/f1/current/driverStandings.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand All @@ -261,7 +263,7 @@ class _ErgastApiCalls {

Future<List<String>> getDriverList(String year) async {
var url = Uri.parse(
'https://ergast.com/api/f1/$year/drivers.json',
'$defaultEndpoint/f1/$year/drivers.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand Down Expand Up @@ -377,7 +379,7 @@ class _ErgastApiCalls {

Future<List<DriverResult>> getDriverResults(String driverId) async {
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/drivers/$driverId/results.json',
'$defaultEndpoint/f1/${DateTime.now().year}/drivers/$driverId/results.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand Down Expand Up @@ -416,7 +418,7 @@ class _ErgastApiCalls {

Future<List<List<DriverResult>>> getTeamResults(String team) async {
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/constructors/$team/results.json',
'$defaultEndpoint/f1/${DateTime.now().year}/constructors/$team/results.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand Down Expand Up @@ -499,8 +501,9 @@ class _ErgastApiCalls {
scheduleLastSavedFormat == 'ergast') {
return formatLastSchedule(schedule, toCome);
} else {
var url =
Uri.parse('https://ergast.com/api/f1/${DateTime.now().year}.json');
var url = Uri.parse(
'$defaultEndpoint/f1/${DateTime.now().year}.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
List<Race> races = formatLastSchedule(responseAsJson, toCome);
Expand All @@ -513,7 +516,7 @@ class _ErgastApiCalls {

Future<Race> getRaceDetails(String round) async {
var url = Uri.parse(
'https://ergast.com/api/f1/${DateTime.now().year}/$round.json',
'$defaultEndpoint/f1/${DateTime.now().year}/$round.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson =
Expand Down Expand Up @@ -596,7 +599,7 @@ class _ErgastApiCalls {
return formatLastTeamsStandings(teamsStandings);
} else {
var url = Uri.parse(
'https://ergast.com/api/f1/current/constructorStandings.json',
'$defaultEndpoint/f1/current/constructorStandings.json',
);
var response = await http.get(url);
Map<String, dynamic> responseAsJson = jsonDecode(response.body);
Expand Down
7 changes: 4 additions & 3 deletions lib/api/event_tracker.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import 'dart:convert';

import 'package:boxbox/api/formulae.dart';
import 'package:boxbox/api/race_components.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart';
import 'package:intl/intl.dart';
Expand Down Expand Up @@ -71,9 +72,9 @@ class Session {
}

class EventTracker {
final String defaultEndpoint = "https://api.formula1.com";
final String apikey = "qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP";
final String feEndpoint = 'https://api.formula-e.pulselive.com';
final String defaultEndpoint = Constants().F1_API_URL;
final String apikey = Constants().F1_API_KEY;
final String feEndpoint = Constants().FE_API_URL;

bool isEventRunning(DateTime meetingStartDate, DateTime meetingEndDate) {
DateTime now = DateTime.now();
Expand Down
5 changes: 3 additions & 2 deletions lib/api/formula1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,15 @@ import 'dart:ui';
import 'package:boxbox/api/driver_components.dart';
import 'package:boxbox/api/race_components.dart';
import 'package:boxbox/api/team_components.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:boxbox/helpers/convert_ergast_and_formula_one.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';

class Formula1 {
final String defaultEndpoint = "https://api.formula1.com";
final String apikey = "qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP";
final String defaultEndpoint = Constants().F1_API_URL;
final String apikey = Constants().F1_API_KEY;

List<News> formatResponse(Map responseAsJson) {
bool useDataSaverMode = Hive.box('settings')
Expand Down
5 changes: 3 additions & 2 deletions lib/api/formulae.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,14 @@ import 'package:boxbox/api/formula1.dart';
import 'package:boxbox/api/race_components.dart';
import 'package:boxbox/api/team_components.dart';
import 'package:boxbox/api/videos.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:flutter/material.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;

class FormulaE {
final String defaultEndpoint = "https://api.formula-e.pulselive.com";
final String defaultF1Endpoint = "https://api.formula1.com";
final String defaultEndpoint = Constants().FE_API_URL;
final String defaultF1Endpoint = Constants().F1_API_URL;
late final String championshipId = Hive.box('settings').get(
'feChampionshipId',
defaultValue: '84467676-4d5d-4c97-ae07-0b7520bb95ea',
Expand Down
5 changes: 3 additions & 2 deletions lib/api/videos.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,13 @@
import 'dart:async';
import 'dart:convert';

import 'package:boxbox/helpers/constants.dart';
import 'package:hive_flutter/hive_flutter.dart';
import 'package:http/http.dart' as http;

class F1VideosFetcher {
final String defaultEndpoint = "https://api.formula1.com";
final String apikey = "qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP";
final String defaultEndpoint = Constants().F1_API_URL;
final String apikey = Constants().F1_API_KEY;

List<Video> formatResponse(Map responseAsJson) {
List finalJson = responseAsJson['videos'];
Expand Down
33 changes: 33 additions & 0 deletions lib/helpers/constants.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
/*
* This file is part of BoxBox (https://github.com/BrightDV/BoxBox).
*
* BoxBox is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* BoxBox is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with BoxBox. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright (c) 2022-2024, BrightDV
*/

class Constants {
final String F1_API_URL = "https://api.formula1.com";
final String FE_API_URL = "https://api.formula-e.pulselive.com";
final String ERGAST_API_URL = "https://ergast.com/api";
final String OFFICIAL_BBS_SERVER_URL =
"https://boxbox-server.netlify.app/api";
final String F1_API_KEY = "qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP";
final String F1_BRIGHTCOVE_PLAYER_ID = "6057949432001";
final String FE_BRIGHTCOVE_PLAYER_ID = "6275361344001";
final String F1_BRIGHTCOVE_PLAYER_KEY =
" application/json;pk=BCpkADawqM1hQVBuXkSlsl6hUsBZQMmrLbIfOjJQ3_n8zmPOhlNSwZhQBF6d5xggxm0t052lQjYyhqZR3FW2eP03YGOER9ihJkUnIhRZGBxuLhnL-QiFpvcDWIh_LvwN5j8zkjTtGKarhsdV";
final String FE_BRIGHTCOVE_PLAYER_KEY =
" application/json;pk=BCpkADawqM0CZElkVwfs62q-JTOc4CeZSNJRfxT923qzbMSqp6qn5VEgWV1iao1cEf2sXX9ce8achTuOfYKUvfchSis_rB5Sxz_ih70GYLYdf8bZgHi3Yq1VK_6v3mj29rxxcJjF3OX6Ri32";
}
5 changes: 3 additions & 2 deletions lib/helpers/download.dart
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,14 @@ import 'dart:io';
import 'package:background_downloader/background_downloader.dart';
import 'package:boxbox/api/brightcove.dart';
import 'package:boxbox/api/videos.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:hive_flutter/hive_flutter.dart';

class DownloadUtils {
final String f1Endpoint = 'https://api.formula1.com';
final String f1ApiKey = "qPgPPRJyGCIPxFT3el4MF7thXHyJCzAP";
final String f1Endpoint = Constants().F1_API_URL;
final String f1ApiKey = Constants().F1_API_KEY;
Future<String?> videoDownloadQualitySelector(
BuildContext context,
) async {
Expand Down
3 changes: 2 additions & 1 deletion lib/helpers/news_feed_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import 'package:boxbox/Screens/MixedNews/rss_feed.dart';
import 'package:boxbox/Screens/MixedNews/wordpress.dart';
import 'package:boxbox/api/rss.dart';
import 'package:boxbox/helpers/constants.dart';
import 'package:boxbox/helpers/loading_indicator_util.dart';
import 'package:boxbox/helpers/news.dart';
import 'package:boxbox/helpers/request_error.dart';
Expand Down Expand Up @@ -112,7 +113,7 @@ class NewsFeedWidget extends StatelessWidget {

@override
Widget build(BuildContext context) {
const String officialFeed = "https://api.formula1.com";
final String officialFeed = Constants().F1_API_URL;
List savedFeedUrl = Hive.box('settings')
.get('homeFeed', defaultValue: [officialFeed, 'api']) as List;
String savedServer = Hive.box('settings')
Expand Down
Loading

0 comments on commit 5532a29

Please sign in to comment.