From e03d45c4c4d9c3cedd17348a3141fa2a880b9214 Mon Sep 17 00:00:00 2001 From: yousefyako Date: Wed, 11 Oct 2023 13:42:46 +0200 Subject: [PATCH] Changed Transaction class name Changed Transaction class name to OldTransaction to make it obvious that the Transaction object is outdated and should be replaced with the new one. --- ...{transaction.dart => old_transaction.dart} | 10 +++---- lib/services/local_data.dart | 4 +-- lib/services/transaction_api_service.dart | 30 +++++++++---------- lib/ui/screens/home_page/home_viewmodel.dart | 4 +-- .../snappingcheet_viewmodel.dart | 4 +-- lib/ui/sheets/top_sheet/top_sheet_view.dart | 2 +- .../top_sheet/top_sheet_view_model.dart | 20 +++++++------ 7 files changed, 38 insertions(+), 36 deletions(-) rename lib/models/{transaction.dart => old_transaction.dart} (89%) diff --git a/lib/models/transaction.dart b/lib/models/old_transaction.dart similarity index 89% rename from lib/models/transaction.dart rename to lib/models/old_transaction.dart index a4e6d97..1b8529e 100644 --- a/lib/models/transaction.dart +++ b/lib/models/old_transaction.dart @@ -1,6 +1,6 @@ /// The class is a model for a transaction. It has a bunch of properties, /// and a constructor that takes a map of strings and dynamic values -class Transaction { +class OldTransaction { int transactionID = 0; String userID = ""; int chargerID = 0; @@ -14,9 +14,9 @@ class Transaction { int currentChargePercentage = 0; String paymentID = ''; - Transaction(); + OldTransaction(); - Transaction.fromTransaction({ + OldTransaction.fromTransaction({ required this.transactionID, required this.userID, required this.chargerID, @@ -31,10 +31,10 @@ class Transaction { required this.paymentID, }); - Transaction.fromJson(Map json) { + OldTransaction.fromJson(Map json) { transactionID = json['transactionID'] ?? 0; userID = json['userID'] ?? ''; - chargerID = json['chargerID'] ?? 0; + chargerID = json['connectorID'] ?? 0; pricePerKwh = double.parse(json['pricePerKwh'] ?? '0.0'); sessionID = json['session_id'] ?? ""; clientToken = json['klarna_consumer_token'] ?? ""; diff --git a/lib/services/local_data.dart b/lib/services/local_data.dart index 0ae1e4c..61e2f16 100644 --- a/lib/services/local_data.dart +++ b/lib/services/local_data.dart @@ -3,7 +3,7 @@ import 'dart:async'; import 'package:flexicharge/enums/event_type.dart'; import 'package:flexicharge/models/charger.dart'; import 'package:flexicharge/models/charger_point.dart'; -import 'package:flexicharge/models/transaction.dart'; +import 'package:flexicharge/models/old_transaction.dart'; import 'package:google_maps_flutter/google_maps_flutter.dart'; /// It's a class that holds data that is used by the app @@ -13,7 +13,7 @@ class LocalData { List chargerPoints = []; LatLng userLocation = LatLng(0, 0); int chargingCharger = -1; - Transaction transactionSession = Transaction(); + OldTransaction transactionSession = OldTransaction(); bool isButtonActive = true; int chargingPercentage = 0; //late Timer timer; This is commented out because a temporary diff --git a/lib/services/transaction_api_service.dart b/lib/services/transaction_api_service.dart index 391d68a..d4a549a 100644 --- a/lib/services/transaction_api_service.dart +++ b/lib/services/transaction_api_service.dart @@ -1,7 +1,7 @@ import 'dart:convert'; import 'package:flexicharge/enums/error_codes.dart'; import 'package:flexicharge/models/api.dart'; -import 'package:flexicharge/models/transaction.dart'; +import 'package:flexicharge/models/old_transaction.dart'; import 'package:http/http.dart' as http; /// It's a class that makes requests to the server and returns a Transaction object @@ -16,13 +16,13 @@ class TransactionApiService { /// /// Returns: /// A Future - Future getTransactionById(int id) async { + Future getTransactionById(int id) async { var response = await client.get(Uri.parse('${API.url}/transactions/$id')); switch (response.statusCode) { case 200: var parsedTransaction = json.decode(response.body); - var transactionFromJson = Transaction.fromJson(parsedTransaction); + var transactionFromJson = OldTransaction.fromJson(parsedTransaction); return transactionFromJson; case 404: throw Exception(ErrorCodes.notFound); @@ -41,8 +41,8 @@ class TransactionApiService { /// /// Returns: /// A list of transactions. - Future> getTransactionsByUserId(int id) async { - var transactions = []; + Future> getTransactionsByUserId(int id) async { + var transactions = []; var response = await client .get(Uri.parse('${API.url}/transactions/userTransactions/$id')); @@ -50,7 +50,7 @@ class TransactionApiService { case 200: var parsedTransactions = json.decode(response.body) as List; for (var trans in parsedTransactions) { - transactions.add(Transaction.fromJson(trans)); + transactions.add(OldTransaction.fromJson(trans)); } return transactions; case 404: @@ -70,8 +70,8 @@ class TransactionApiService { /// /// Returns: /// A list of transactions. - Future> getTransactionsByChargerId(int id) async { - var transactions = []; + Future> getTransactionsByChargerId(int id) async { + var transactions = []; var response = await client .get(Uri.parse('${API.url}/transactions/chargerTransactions/$id')); @@ -79,7 +79,7 @@ class TransactionApiService { case 200: var parsedTransactions = json.decode(response.body) as List; for (var trans in parsedTransactions) { - transactions.add(Transaction.fromJson(trans)); + transactions.add(OldTransaction.fromJson(trans)); } return transactions; case 404: @@ -160,7 +160,7 @@ class TransactionApiService { /// /// Returns: /// The response is a JSON object containing the following: - Future createKlarnaPaymentSession( + Future createKlarnaPaymentSession( int? userId, int chargerId) async { var response = await client.post(Uri.parse('${API.url}/transactions'), headers: API.defaultRequestHeaders, @@ -174,7 +174,7 @@ class TransactionApiService { switch (response.statusCode) { case 201: var transaction = json.decode(response.body) as Map; - var parsedSession = Transaction.fromJson(transaction); + var parsedSession = OldTransaction.fromJson(transaction); return parsedSession; case 400: throw Exception(ErrorCodes.badRequest); @@ -189,7 +189,7 @@ class TransactionApiService { /// The request returns the updated transaction object, /// If everything goes as expected, it will contain a paymentId. - Future createKlarnaOrder( + Future createKlarnaOrder( int transactionId, String authToken, ) async { @@ -205,7 +205,7 @@ class TransactionApiService { case 200: var transaction = json.decode(response.body) as Map; if (transaction.isEmpty) throw Exception(ErrorCodes.emptyResponse); - var parsedSession = Transaction.fromJson(transaction); + var parsedSession = OldTransaction.fromJson(transaction); print("Klarna updatedSession paymentID: " + parsedSession.paymentID.toString()); @@ -226,7 +226,7 @@ class TransactionApiService { /// The request will return an updated transaction object which contains /// paymentConfirmed == true. - Future stopCharging(int transactionId) async { + Future stopCharging(int transactionId) async { var response = await client.put( Uri.parse('${API.url}/transactions/stop/$transactionId'), headers: API.defaultRequestHeaders); @@ -236,7 +236,7 @@ class TransactionApiService { // We get a List with a single Transaction object in response var list = json.decode(response.body) as List; if (list.isEmpty) throw Exception(ErrorCodes.emptyResponse); - var parsedSession = Transaction.fromJson(list.first); + var parsedSession = OldTransaction.fromJson(list.first); print("Klarna updatedSession paymentConfirmed : " + parsedSession.paymentConfirmed.toString()); return parsedSession; diff --git a/lib/ui/screens/home_page/home_viewmodel.dart b/lib/ui/screens/home_page/home_viewmodel.dart index c4c25c9..d8ce379 100644 --- a/lib/ui/screens/home_page/home_viewmodel.dart +++ b/lib/ui/screens/home_page/home_viewmodel.dart @@ -1,5 +1,5 @@ import 'dart:async'; -import 'package:flexicharge/models/transaction.dart'; +import 'package:flexicharge/models/old_transaction.dart'; import 'package:flexicharge/services/transaction_api_service.dart'; import 'package:flexicharge/app/app.router.dart'; import 'package:flexicharge/models/charger_point.dart'; @@ -121,7 +121,7 @@ class HomeViewModel extends BaseViewModel { /// The current charging percentage of the car. Future fetchChargingPercentage() async { try { - Transaction currentTransaction = await _transactionAPI + OldTransaction currentTransaction = await _transactionAPI .getTransactionById(localData.transactionSession.transactionID); int currentChargingPercentage = diff --git a/lib/ui/sheets/map_bottom_sheet/snappingcheet_viewmodel.dart b/lib/ui/sheets/map_bottom_sheet/snappingcheet_viewmodel.dart index 1b5efb9..074e375 100644 --- a/lib/ui/sheets/map_bottom_sheet/snappingcheet_viewmodel.dart +++ b/lib/ui/sheets/map_bottom_sheet/snappingcheet_viewmodel.dart @@ -2,7 +2,7 @@ import 'dart:async'; import 'package:flexicharge/app/app.locator.dart'; import 'package:flexicharge/models/charger.dart'; import 'package:flexicharge/models/charger_point.dart'; -import 'package:flexicharge/models/transaction.dart'; +import 'package:flexicharge/models/old_transaction.dart'; import 'package:flexicharge/services/charger_api_service.dart'; import 'package:flexicharge/services/local_data.dart'; import 'package:flexicharge/services/transaction_api_service.dart'; @@ -255,7 +255,7 @@ class CustomSnappingSheetViewModel extends BaseViewModel { print("starting the session.."); // Create a transaction session print("Trying to create a transaction session... "); - Transaction transactionSession = + OldTransaction transactionSession = await _transactionAPI.createKlarnaPaymentSession(null, id); localData.transactionSession = transactionSession; print("TransactionID: " + diff --git a/lib/ui/sheets/top_sheet/top_sheet_view.dart b/lib/ui/sheets/top_sheet/top_sheet_view.dart index ab22c81..321a84a 100644 --- a/lib/ui/sheets/top_sheet/top_sheet_view.dart +++ b/lib/ui/sheets/top_sheet/top_sheet_view.dart @@ -114,7 +114,7 @@ class TopSheetView extends StatelessWidget { if (model.chargingState == 4 && model.topSheetState == 3) Expanded( child: ChargingSummary( - time: model.stopTime, + time: model.getChargingStopTime(), chargingDuration: "1hr 41min", energyUsed: "${model.transactionSession.kwhTransferred.toStringAsFixed(2)}kWh @ ${model.transactionSession.pricePerKwh.toStringAsFixed(2)}kr", diff --git a/lib/ui/sheets/top_sheet/top_sheet_view_model.dart b/lib/ui/sheets/top_sheet/top_sheet_view_model.dart index 83179ca..b128e00 100644 --- a/lib/ui/sheets/top_sheet/top_sheet_view_model.dart +++ b/lib/ui/sheets/top_sheet/top_sheet_view_model.dart @@ -3,7 +3,7 @@ import 'dart:async'; import 'package:flexicharge/app/app.locator.dart'; import 'package:flexicharge/enums/event_type.dart'; import 'package:flexicharge/enums/top_sheet_strings.dart'; -import 'package:flexicharge/models/transaction.dart'; +import 'package:flexicharge/models/old_transaction.dart'; import 'package:flexicharge/services/charger_api_service.dart'; import 'package:flexicharge/services/local_data.dart'; import 'package:flexicharge/services/transaction_api_service.dart'; @@ -30,9 +30,6 @@ class TopSheetViewModel extends BaseViewModel { init() { startStreamListener(); - // This function is implemented temporarily until the stream line is used correctly in the app. - updateStopStime(); - timer = Timer.periodic(Duration(milliseconds: 200), (Timer t) => incrementChargingPercentage()); } @@ -83,7 +80,7 @@ class TopSheetViewModel extends BaseViewModel { changeTopSheetSize(); } - Transaction get transactionSession => localData.transactionSession; + OldTransaction get transactionSession => localData.transactionSession; /// It changes the size of the top sheet based on the state of the top sheet void changeTopSheetSize() { @@ -201,16 +198,21 @@ class TopSheetViewModel extends BaseViewModel { }); } - /// The function "updateStopStime" updates the "stopTime" variable with the current hour and minute in - /// the format "HH:MM". - void updateStopStime() { + /// The function returns the current time in the format "HH:MM". + /// + /// This method is temporarily used to retrieve the charging stop time until the stream listeners is + /// used correctly in the app. + /// + /// Returns: + /// The method is returning a string representing the current hour and minute in the format "HH:MM". + String getChargingStopTime() { int hour = DateTime.now().hour; int minute = DateTime.now().minute; String hourString = hour < 10 ? '0$hour' : '$hour'; String minuteString = minute < 10 ? '0$minute' : '$minute'; - stopTime = '$hourString:$minuteString'; + return '$hourString:$minuteString'; } }