Skip to content

Commit

Permalink
Create the models
Browse files Browse the repository at this point in the history
  • Loading branch information
SepehrFakoori committed Jul 11, 2024
1 parent 1398995 commit deb8744
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ class CryptoBloc extends Bloc<CryptoEvent, CryptoState> {
CryptoBloc() : super(CryptoInitState()) {
on<CryptoInitializeEvent>((event, emit) async {
emit(CryptoLoadingState());
var response = await _repository.getCryptoCurrencies();
// var response = await _repository.getCryptoCurrencies();
var response = await _repository.getCurrencies();
emit(CryptoResponseState(response));
});
}
Expand Down
File renamed without changes.
Original file line number Diff line number Diff line change
@@ -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 {}
Expand All @@ -8,7 +9,8 @@ class CryptoInitState extends CryptoState {}
class CryptoLoadingState extends CryptoState {}

class CryptoResponseState extends CryptoState {
Either<String, List<CryptoCurrency>> response;
// Either<String, List<CryptoCurrency>> cryptoCurrencyResponse;
Either<String, List<Currency>> currencyResponse;

CryptoResponseState(this.response);
CryptoResponseState(this.currencyResponse);
}
Original file line number Diff line number Diff line change
@@ -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<List<CryptoCurrency>> getCryptoCurrencies();

Future<List<Currency>> getCurrencies();
}

class CryptoCurrencyRemoteDatasource extends ICryptoCurrencyDataSource {
Expand All @@ -28,4 +32,23 @@ class CryptoCurrencyRemoteDatasource extends ICryptoCurrencyDataSource {
throw ApiException(0, "Unknown Error!");
}
}

@override
Future<List<Currency>> getCurrencies() async {
try {
Map<String, dynamic> qParams = {
"token": "440714:668d2eb4c7b96",
"action": "tgju",
};
var response = await _dio.get("price/", queryParameters: qParams);
var result = convertMap(response.data, "currencies");
return result
.map<Currency>((jsonObject) => Currency.fromMapJson(jsonObject))
.toList();
} on DioException catch (ex) {
throw ApiException(ex.response!.statusCode, ex.response!.statusMessage);
} catch (ex) {
throw ApiException(0, "Unknown Error!");
}
}
}
31 changes: 31 additions & 0 deletions lib/data/model/coin.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> jsonObject) {
return Coin(
jsonObject["name"],
jsonObject["p"],
jsonObject["h"],
jsonObject["l"],
jsonObject["d"],
jsonObject["dp"].toString(),
jsonObject["ts"],
);
}
}
25 changes: 0 additions & 25 deletions lib/data/model/crypto_currency.dart

This file was deleted.

31 changes: 31 additions & 0 deletions lib/data/model/currency.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> jsonObject) {
return Currency(
jsonObject["name"],
jsonObject["p"],
jsonObject["h"],
jsonObject["l"],
jsonObject["d"],
jsonObject["dp"].toString(),
jsonObject["ts"],
);
}
}
31 changes: 31 additions & 0 deletions lib/data/model/gold.dart
Original file line number Diff line number Diff line change
@@ -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<String, dynamic> jsonObject) {
return Gold(
jsonObject["name"],
jsonObject["p"],
jsonObject["h"],
jsonObject["l"],
jsonObject["d"],
jsonObject["dp"].toString(),
jsonObject["ts"],
);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
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';


abstract class ICryptoCurrencyRepository {
Future<Either<String, List<CryptoCurrency>>> getCryptoCurrencies();

Future<Either<String, List<Currency>>> getCurrencies();
}

class CryptoCurrencyRepository extends ICryptoCurrencyRepository {
Expand All @@ -21,4 +24,14 @@ class CryptoCurrencyRepository extends ICryptoCurrencyRepository {
return left(ex.message ?? "Text Error We Got!");
}
}

@override
Future<Either<String, List<Currency>>> getCurrencies() async {
try {
var response = await _dataSource.getCurrencies();
return right(response);
} on ApiException catch (ex) {
return left(ex.message ?? "Text Error We Got!");
}
}
}

0 comments on commit deb8744

Please sign in to comment.