From deb8744577c411e71e51ccea8d3beacee9e3616a Mon Sep 17 00:00:00 2001 From: Sepehr Date: Thu, 11 Jul 2024 16:13:07 +0330 Subject: [PATCH] Create the models --- .../crypto_bloc.dart => home/home_bloc.dart} | 3 +- .../home_event.dart} | 0 .../home_state.dart} | 6 ++-- ..._datasource.dart => price_datasource.dart} | 23 ++++++++++++++ lib/data/model/coin.dart | 31 +++++++++++++++++++ lib/data/model/crypto_currency.dart | 25 --------------- lib/data/model/currency.dart | 31 +++++++++++++++++++ lib/data/model/gold.dart | 31 +++++++++++++++++++ ..._repository.dart => price_repository.dart} | 13 ++++++++ 9 files changed, 135 insertions(+), 28 deletions(-) rename lib/bloc/{crypto/crypto_bloc.dart => home/home_bloc.dart} (84%) rename lib/bloc/{crypto/crypto_event.dart => home/home_event.dart} (100%) rename lib/bloc/{crypto/crypto_state.dart => home/home_state.dart} (55%) rename lib/data/datasource/{crypto_currency_datasource.dart => price_datasource.dart} (56%) create mode 100644 lib/data/model/coin.dart delete mode 100644 lib/data/model/crypto_currency.dart create mode 100644 lib/data/model/currency.dart create mode 100644 lib/data/model/gold.dart rename lib/data/repository/{crypto_currency_repository.dart => price_repository.dart} (68%) diff --git a/lib/bloc/crypto/crypto_bloc.dart b/lib/bloc/home/home_bloc.dart similarity index 84% rename from lib/bloc/crypto/crypto_bloc.dart rename to lib/bloc/home/home_bloc.dart index 605e865..f116d75 100644 --- a/lib/bloc/crypto/crypto_bloc.dart +++ b/lib/bloc/home/home_bloc.dart @@ -10,7 +10,8 @@ class CryptoBloc extends Bloc { CryptoBloc() : super(CryptoInitState()) { on((event, emit) async { emit(CryptoLoadingState()); - var response = await _repository.getCryptoCurrencies(); + // var response = await _repository.getCryptoCurrencies(); + var response = await _repository.getCurrencies(); emit(CryptoResponseState(response)); }); } diff --git a/lib/bloc/crypto/crypto_event.dart b/lib/bloc/home/home_event.dart similarity index 100% rename from lib/bloc/crypto/crypto_event.dart rename to lib/bloc/home/home_event.dart diff --git a/lib/bloc/crypto/crypto_state.dart b/lib/bloc/home/home_state.dart similarity index 55% rename from lib/bloc/crypto/crypto_state.dart rename to lib/bloc/home/home_state.dart index 6429edc..5d243ad 100644 --- a/lib/bloc/crypto/crypto_state.dart +++ b/lib/bloc/home/home_state.dart @@ -1,4 +1,5 @@ import 'package:currency_exchange_rate_app_flutter/data/model/crypto_currency.dart'; +import 'package:currency_exchange_rate_app_flutter/data/model/currency.dart'; import 'package:dartz/dartz.dart'; abstract class CryptoState {} @@ -8,7 +9,8 @@ class CryptoInitState extends CryptoState {} class CryptoLoadingState extends CryptoState {} class CryptoResponseState extends CryptoState { - Either> response; + // Either> cryptoCurrencyResponse; + Either> currencyResponse; - CryptoResponseState(this.response); + CryptoResponseState(this.currencyResponse); } diff --git a/lib/data/datasource/crypto_currency_datasource.dart b/lib/data/datasource/price_datasource.dart similarity index 56% rename from lib/data/datasource/crypto_currency_datasource.dart rename to lib/data/datasource/price_datasource.dart index 5f82890..d3b1553 100644 --- a/lib/data/datasource/crypto_currency_datasource.dart +++ b/lib/data/datasource/price_datasource.dart @@ -1,10 +1,14 @@ import 'package:currency_exchange_rate_app_flutter/data/model/crypto_currency.dart'; +import 'package:currency_exchange_rate_app_flutter/data/model/currency.dart'; import 'package:currency_exchange_rate_app_flutter/di/di.dart'; import 'package:currency_exchange_rate_app_flutter/util/api_exception.dart'; +import 'package:currency_exchange_rate_app_flutter/util/map_to_list_convertor.dart'; import 'package:dio/dio.dart'; abstract class ICryptoCurrencyDataSource { Future> getCryptoCurrencies(); + + Future> getCurrencies(); } class CryptoCurrencyRemoteDatasource extends ICryptoCurrencyDataSource { @@ -28,4 +32,23 @@ class CryptoCurrencyRemoteDatasource extends ICryptoCurrencyDataSource { throw ApiException(0, "Unknown Error!"); } } + + @override + Future> getCurrencies() async { + try { + Map qParams = { + "token": "440714:668d2eb4c7b96", + "action": "tgju", + }; + var response = await _dio.get("price/", queryParameters: qParams); + var result = convertMap(response.data, "currencies"); + return result + .map((jsonObject) => Currency.fromMapJson(jsonObject)) + .toList(); + } on DioException catch (ex) { + throw ApiException(ex.response!.statusCode, ex.response!.statusMessage); + } catch (ex) { + throw ApiException(0, "Unknown Error!"); + } + } } diff --git a/lib/data/model/coin.dart b/lib/data/model/coin.dart new file mode 100644 index 0000000..0c9152c --- /dev/null +++ b/lib/data/model/coin.dart @@ -0,0 +1,31 @@ +class Coin { + String? name; + String? currentPrice; + String? high; + String? low; + String? change; + String? percent; + String? time; + + Coin( + this.name, + this.currentPrice, + this.high, + this.low, + this.change, + this.percent, + this.time, + ); + + factory Coin.fromMapJson(Map jsonObject) { + return Coin( + jsonObject["name"], + jsonObject["p"], + jsonObject["h"], + jsonObject["l"], + jsonObject["d"], + jsonObject["dp"].toString(), + jsonObject["ts"], + ); + } +} diff --git a/lib/data/model/crypto_currency.dart b/lib/data/model/crypto_currency.dart deleted file mode 100644 index b53d2c3..0000000 --- a/lib/data/model/crypto_currency.dart +++ /dev/null @@ -1,25 +0,0 @@ -class CryptoCurrency { - String? keyName; - String? persianName; - String? englishName; - String? rank; - String? price; - - CryptoCurrency( - this.keyName, - this.persianName, - this.englishName, - this.rank, - this.price, - ); - - factory CryptoCurrency.fromMapJson(Map jsonObject) { - return CryptoCurrency( - jsonObject["key"], - jsonObject["name"], - jsonObject["name_en"], - jsonObject["rank"].toString(), - jsonObject["price"].toString(), - ); - } -} diff --git a/lib/data/model/currency.dart b/lib/data/model/currency.dart new file mode 100644 index 0000000..f0d4f52 --- /dev/null +++ b/lib/data/model/currency.dart @@ -0,0 +1,31 @@ +class Currency { + String? name; + String? currentPrice; + String? high; + String? low; + String? change; + String? percent; + String? time; + + Currency( + this.name, + this.currentPrice, + this.high, + this.low, + this.change, + this.percent, + this.time, + ); + + factory Currency.fromMapJson(Map jsonObject) { + return Currency( + jsonObject["name"], + jsonObject["p"], + jsonObject["h"], + jsonObject["l"], + jsonObject["d"], + jsonObject["dp"].toString(), + jsonObject["ts"], + ); + } +} diff --git a/lib/data/model/gold.dart b/lib/data/model/gold.dart new file mode 100644 index 0000000..36f684c --- /dev/null +++ b/lib/data/model/gold.dart @@ -0,0 +1,31 @@ +class Gold { + String? name; + String? currentPrice; + String? high; + String? low; + String? change; + String? percent; + String? time; + + Gold( + this.name, + this.currentPrice, + this.high, + this.low, + this.change, + this.percent, + this.time, + ); + + factory Gold.fromMapJson(Map jsonObject) { + return Gold( + jsonObject["name"], + jsonObject["p"], + jsonObject["h"], + jsonObject["l"], + jsonObject["d"], + jsonObject["dp"].toString(), + jsonObject["ts"], + ); + } +} diff --git a/lib/data/repository/crypto_currency_repository.dart b/lib/data/repository/price_repository.dart similarity index 68% rename from lib/data/repository/crypto_currency_repository.dart rename to lib/data/repository/price_repository.dart index c909e01..f8944c2 100644 --- a/lib/data/repository/crypto_currency_repository.dart +++ b/lib/data/repository/price_repository.dart @@ -1,5 +1,6 @@ import 'package:currency_exchange_rate_app_flutter/data/datasource/crypto_currency_datasource.dart'; import 'package:currency_exchange_rate_app_flutter/data/model/crypto_currency.dart'; +import 'package:currency_exchange_rate_app_flutter/data/model/currency.dart'; import 'package:currency_exchange_rate_app_flutter/di/di.dart'; import 'package:currency_exchange_rate_app_flutter/util/api_exception.dart'; import 'package:dartz/dartz.dart'; @@ -7,6 +8,8 @@ import 'package:dartz/dartz.dart'; abstract class ICryptoCurrencyRepository { Future>> getCryptoCurrencies(); + + Future>> getCurrencies(); } class CryptoCurrencyRepository extends ICryptoCurrencyRepository { @@ -21,4 +24,14 @@ class CryptoCurrencyRepository extends ICryptoCurrencyRepository { return left(ex.message ?? "Text Error We Got!"); } } + + @override + Future>> getCurrencies() async { + try { + var response = await _dataSource.getCurrencies(); + return right(response); + } on ApiException catch (ex) { + return left(ex.message ?? "Text Error We Got!"); + } + } }