From 961d3e919cf9144014f2b0593ce949adb3c3d736 Mon Sep 17 00:00:00 2001 From: Mairon Lucas Date: Mon, 10 Jun 2024 21:33:57 -0300 Subject: [PATCH] Domain structure and dio configuration --- .env_example | 4 + .gitignore | 2 + README.md | 21 ++-- domain/lib/repository/game_repository.dart | 0 domain/lib/use_case/use_case.dart | 0 domain/pubspec.lock | 5 + domain/pubspec.yaml | 3 + lib/common/utils.dart | 1 + lib/data/remote/data_sources/game_rds.dart | 14 +++ lib/data/remote/infraestructure/my_dio.dart | 30 +++++ lib/main.dart | 116 ++------------------ pubspec.lock | 47 +++++--- pubspec.yaml | 78 +------------ 13 files changed, 111 insertions(+), 210 deletions(-) create mode 100644 .env_example create mode 100644 domain/lib/repository/game_repository.dart create mode 100644 domain/lib/use_case/use_case.dart create mode 100644 domain/pubspec.lock create mode 100644 domain/pubspec.yaml create mode 100644 lib/common/utils.dart create mode 100644 lib/data/remote/data_sources/game_rds.dart create mode 100644 lib/data/remote/infraestructure/my_dio.dart diff --git a/.env_example b/.env_example new file mode 100644 index 0000000..77a1a1c --- /dev/null +++ b/.env_example @@ -0,0 +1,4 @@ +{ + "API_KEY": "YOUR_API_KEY", + "API_HOST": "NBA_API_HOST" +} \ No newline at end of file diff --git a/.gitignore b/.gitignore index 29a3a50..7684330 100644 --- a/.gitignore +++ b/.gitignore @@ -41,3 +41,5 @@ app.*.map.json /android/app/debug /android/app/profile /android/app/release + +.env \ No newline at end of file diff --git a/README.md b/README.md index 672fbdb..1bf19c0 100644 --- a/README.md +++ b/README.md @@ -1,16 +1,11 @@ -# nbapp +# NBApp +This app is designed to consume the [API-NBA](https://rapidapi.com/api-sports/api/api-nba) with the purpose of showing the NBA games and their details like teams, players, standing, etc. -A new Flutter project. +Also, this app is designed to demonstrate my knowledge on developing mobile apps using Flutter. -## Getting Started +## How to run +To run, put this additional arg to you flutter run: -This project is a starting point for a Flutter application. - -A few resources to get you started if this is your first Flutter project: - -- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab) -- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook) - -For help getting started with Flutter development, view the -[online documentation](https://docs.flutter.dev/), which offers tutorials, -samples, guidance on mobile development, and a full API reference. +``` + --dart-define-from-file=.env +``` diff --git a/domain/lib/repository/game_repository.dart b/domain/lib/repository/game_repository.dart new file mode 100644 index 0000000..e69de29 diff --git a/domain/lib/use_case/use_case.dart b/domain/lib/use_case/use_case.dart new file mode 100644 index 0000000..e69de29 diff --git a/domain/pubspec.lock b/domain/pubspec.lock new file mode 100644 index 0000000..56b06b9 --- /dev/null +++ b/domain/pubspec.lock @@ -0,0 +1,5 @@ +# Generated by pub +# See https://dart.dev/tools/pub/glossary#lockfile +packages: {} +sdks: + dart: ">=3.4.1 <4.0.0" diff --git a/domain/pubspec.yaml b/domain/pubspec.yaml new file mode 100644 index 0000000..7114962 --- /dev/null +++ b/domain/pubspec.yaml @@ -0,0 +1,3 @@ +name: domain +environment: + sdk: '>=3.4.1 <4.0.0' diff --git a/lib/common/utils.dart b/lib/common/utils.dart new file mode 100644 index 0000000..d81c808 --- /dev/null +++ b/lib/common/utils.dart @@ -0,0 +1 @@ +final baseUrl = 'https://${const String.fromEnvironment('API_HOST')}'; \ No newline at end of file diff --git a/lib/data/remote/data_sources/game_rds.dart b/lib/data/remote/data_sources/game_rds.dart new file mode 100644 index 0000000..e52876e --- /dev/null +++ b/lib/data/remote/data_sources/game_rds.dart @@ -0,0 +1,14 @@ +import 'package:dio/dio.dart'; +import 'package:nbapp/common/utils.dart'; + +class GameRDS { + GameRDS({ + required Dio dio, +}) : _dio = dio; + + final Dio _dio; + + Future getLiveGames() async { + _dio.get('${baseUrl}/games?live=all'); + } +} \ No newline at end of file diff --git a/lib/data/remote/infraestructure/my_dio.dart b/lib/data/remote/infraestructure/my_dio.dart new file mode 100644 index 0000000..6c9a91a --- /dev/null +++ b/lib/data/remote/infraestructure/my_dio.dart @@ -0,0 +1,30 @@ +import 'package:dio/dio.dart'; +import 'package:dio/io.dart'; + +class MyDio extends DioForNative { + @override + Future> request( + String path, { + Object? data, + Map? queryParameters, + CancelToken? cancelToken, + Options? options, + ProgressCallback? onSendProgress, + ProgressCallback? onReceiveProgress, + }) { + const apiKey = const String.fromEnvironment('API_KEY'); + const apiHost = const String.fromEnvironment('API_HOST'); + options?.headers = { + 'x-rapidapi-key': apiKey, + 'x-rapidapi-host': apiHost, + }; + return super.request( + path, + queryParameters: queryParameters, + data: data, + options: options, + cancelToken: cancelToken, + onReceiveProgress: onReceiveProgress, + ); + } +} diff --git a/lib/main.dart b/lib/main.dart index 8e94089..daf3136 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,125 +1,25 @@ import 'package:flutter/material.dart'; void main() { - runApp(const MyApp()); + runApp( + const MyApp(), + ); } class MyApp extends StatelessWidget { - const MyApp({super.key}); + const MyApp({ + super.key, + }); - // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( - title: 'Flutter Demo', + title: 'NBApp', theme: ThemeData( - // This is the theme of your application. - // - // TRY THIS: Try running your application with "flutter run". You'll see - // the application has a purple toolbar. Then, without quitting the app, - // try changing the seedColor in the colorScheme below to Colors.green - // and then invoke "hot reload" (save your changes or press the "hot - // reload" button in a Flutter-supported IDE, or press "r" if you used - // the command line to start the app). - // - // Notice that the counter didn't reset back to zero; the application - // state is not lost during the reload. To reset the state, use hot - // restart instead. - // - // This works for code too, not just values: Most code changes can be - // tested with just a hot reload. colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple), useMaterial3: true, ), - home: const MyHomePage(title: 'Flutter Demo Home Page'), - ); - } -} - -class MyHomePage extends StatefulWidget { - const MyHomePage({super.key, required this.title}); - - // This widget is the home page of your application. It is stateful, meaning - // that it has a State object (defined below) that contains fields that affect - // how it looks. - - // This class is the configuration for the state. It holds the values (in this - // case the title) provided by the parent (in this case the App widget) and - // used by the build method of the State. Fields in a Widget subclass are - // always marked "final". - - final String title; - - @override - State createState() => _MyHomePageState(); -} - -class _MyHomePageState extends State { - int _counter = 0; - - void _incrementCounter() { - setState(() { - // This call to setState tells the Flutter framework that something has - // changed in this State, which causes it to rerun the build method below - // so that the display can reflect the updated values. If we changed - // _counter without calling setState(), then the build method would not be - // called again, and so nothing would appear to happen. - _counter++; - }); - } - - @override - Widget build(BuildContext context) { - // This method is rerun every time setState is called, for instance as done - // by the _incrementCounter method above. - // - // The Flutter framework has been optimized to make rerunning build methods - // fast, so that you can just rebuild anything that needs updating rather - // than having to individually change instances of widgets. - return Scaffold( - appBar: AppBar( - // TRY THIS: Try changing the color here to a specific color (to - // Colors.amber, perhaps?) and trigger a hot reload to see the AppBar - // change color while the other colors stay the same. - backgroundColor: Theme.of(context).colorScheme.inversePrimary, - // Here we take the value from the MyHomePage object that was created by - // the App.build method, and use it to set our appbar title. - title: Text(widget.title), - ), - body: Center( - // Center is a layout widget. It takes a single child and positions it - // in the middle of the parent. - child: Column( - // Column is also a layout widget. It takes a list of children and - // arranges them vertically. By default, it sizes itself to fit its - // children horizontally, and tries to be as tall as its parent. - // - // Column has various properties to control how it sizes itself and - // how it positions its children. Here we use mainAxisAlignment to - // center the children vertically; the main axis here is the vertical - // axis because Columns are vertical (the cross axis would be - // horizontal). - // - // TRY THIS: Invoke "debug painting" (choose the "Toggle Debug Paint" - // action in the IDE, or press "p" in the console), to see the - // wireframe for each widget. - mainAxisAlignment: MainAxisAlignment.center, - children: [ - const Text( - 'You have pushed the button this many times:', - ), - Text( - '$_counter', - style: Theme.of(context).textTheme.headlineMedium, - ), - ], - ), - ), - floatingActionButton: FloatingActionButton( - onPressed: _incrementCounter, - tooltip: 'Increment', - child: const Icon(Icons.add), - ), // This trailing comma makes auto-formatting nicer for build methods. + home: Placeholder(), ); } } diff --git a/pubspec.lock b/pubspec.lock index 5a8354a..a72b931 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -49,6 +49,21 @@ packages: url: "https://pub.dev" source: hosted version: "1.0.8" + dio: + dependency: "direct main" + description: + name: dio + sha256: "11e40df547d418cc0c4900a9318b26304e665da6fa4755399a9ff9efd09034b5" + url: "https://pub.dev" + source: hosted + version: "5.4.3+1" + domain: + dependency: "direct main" + description: + path: domain + relative: true + source: path + version: "0.0.0" fake_async: dependency: transitive description: @@ -62,19 +77,19 @@ packages: description: flutter source: sdk version: "0.0.0" - flutter_lints: - dependency: "direct dev" - description: - name: flutter_lints - sha256: "9e8c3858111da373efc5aa341de011d9bd23e2c5c5e0c62bccf32438e192d7b1" - url: "https://pub.dev" - source: hosted - version: "3.0.2" flutter_test: dependency: "direct dev" description: flutter source: sdk version: "0.0.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "2aa08ce0341cc9b354a498388e30986515406668dbcc4f7c950c3e715496693b" + url: "https://pub.dev" + source: hosted + version: "4.0.2" leak_tracker: dependency: transitive description: @@ -99,14 +114,6 @@ packages: url: "https://pub.dev" source: hosted version: "3.0.1" - lints: - dependency: transitive - description: - name: lints - sha256: cbf8d4b858bb0134ef3ef87841abdf8d63bfc255c266b7bf6b39daa1085c4290 - url: "https://pub.dev" - source: hosted - version: "3.0.0" matcher: dependency: transitive description: @@ -192,6 +199,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.0" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: facc8d6582f16042dd49f2463ff1bd6e2c9ef9f3d5da3d9b087e244a7b564b3c + url: "https://pub.dev" + source: hosted + version: "1.3.2" vector_math: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index 376e785..c8582ad 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,90 +1,22 @@ name: nbapp -description: "A new Flutter project." -# The following line prevents the package from being accidentally published to -# pub.dev using `flutter pub publish`. This is preferred for private packages. -publish_to: 'none' # Remove this line if you wish to publish to pub.dev - -# The following defines the version and build number for your application. -# A version number is three numbers separated by dots, like 1.2.43 -# followed by an optional build number separated by a +. -# Both the version and the builder number may be overridden in flutter -# build by specifying --build-name and --build-number, respectively. -# In Android, build-name is used as versionName while build-number used as versionCode. -# Read more about Android versioning at https://developer.android.com/studio/publish/versioning -# In iOS, build-name is used as CFBundleShortVersionString while build-number is used as CFBundleVersion. -# Read more about iOS versioning at -# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html -# In Windows, build-name is used as the major, minor, and patch parts -# of the product and file versions while build-number is used as the build suffix. +description: "To follow NBA games and stats" +publish_to: 'none' version: 1.0.0+1 environment: sdk: '>=3.4.1 <4.0.0' -# Dependencies specify other packages that your package needs in order to work. -# To automatically upgrade your package dependencies to the latest versions -# consider running `flutter pub upgrade --major-versions`. Alternatively, -# dependencies can be manually updated by changing the version numbers below to -# the latest version available on pub.dev. To see which dependencies have newer -# versions available, run `flutter pub outdated`. dependencies: flutter: sdk: flutter - - - # The following adds the Cupertino Icons font to your application. - # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.6 + dio: ^5.4.3+1 + domain: + path: ./domain dev_dependencies: flutter_test: sdk: flutter - # The "flutter_lints" package below contains a set of recommended lints to - # encourage good coding practices. The lint set provided by the package is - # activated in the `analysis_options.yaml` file located at the root of your - # package. See that file for information about deactivating specific lint - # rules and activating additional ones. - flutter_lints: ^3.0.0 - -# For information on the generic Dart part of this file, see the -# following page: https://dart.dev/tools/pub/pubspec - -# The following section is specific to Flutter packages. flutter: - - # The following line ensures that the Material Icons font is - # included with your application, so that you can use the icons in - # the material Icons class. uses-material-design: true - - # To add assets to your application, add an assets section, like this: - # assets: - # - images/a_dot_burr.jpeg - # - images/a_dot_ham.jpeg - - # An image asset can refer to one or more resolution-specific "variants", see - # https://flutter.dev/assets-and-images/#resolution-aware - - # For details regarding adding assets from package dependencies, see - # https://flutter.dev/assets-and-images/#from-packages - - # To add custom fonts to your application, add a fonts section here, - # in this "flutter" section. Each entry in this list should have a - # "family" key with the font family name, and a "fonts" key with a - # list giving the asset and other descriptors for the font. For - # example: - # fonts: - # - family: Schyler - # fonts: - # - asset: fonts/Schyler-Regular.ttf - # - asset: fonts/Schyler-Italic.ttf - # style: italic - # - family: Trajan Pro - # fonts: - # - asset: fonts/TrajanPro.ttf - # - asset: fonts/TrajanPro_Bold.ttf - # weight: 700 - # - # For details regarding fonts from package dependencies, - # see https://flutter.dev/custom-fonts/#from-packages