Skip to content

Commit

Permalink
Changed Transaction class name
Browse files Browse the repository at this point in the history
Changed Transaction class name to OldTransaction to make it obvious that the Transaction object is outdated and should be replaced with the new one.
  • Loading branch information
yousefyako committed Oct 11, 2023
1 parent 46e6334 commit e03d45c
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 36 deletions.
10 changes: 5 additions & 5 deletions lib/models/transaction.dart → lib/models/old_transaction.dart
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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,
Expand All @@ -31,10 +31,10 @@ class Transaction {
required this.paymentID,
});

Transaction.fromJson(Map<String, dynamic> json) {
OldTransaction.fromJson(Map<String, dynamic> 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'] ?? "";
Expand Down
4 changes: 2 additions & 2 deletions lib/services/local_data.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,7 +13,7 @@ class LocalData {
List<ChargerPoint> 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
Expand Down
30 changes: 15 additions & 15 deletions lib/services/transaction_api_service.dart
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -16,13 +16,13 @@ class TransactionApiService {
///
/// Returns:
/// A Future<Transaction>
Future<Transaction> getTransactionById(int id) async {
Future<OldTransaction> 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);
Expand All @@ -41,16 +41,16 @@ class TransactionApiService {
///
/// Returns:
/// A list of transactions.
Future<List<Transaction>> getTransactionsByUserId(int id) async {
var transactions = <Transaction>[];
Future<List<OldTransaction>> getTransactionsByUserId(int id) async {
var transactions = <OldTransaction>[];
var response = await client
.get(Uri.parse('${API.url}/transactions/userTransactions/$id'));

switch (response.statusCode) {
case 200:
var parsedTransactions = json.decode(response.body) as List<dynamic>;
for (var trans in parsedTransactions) {
transactions.add(Transaction.fromJson(trans));
transactions.add(OldTransaction.fromJson(trans));
}
return transactions;
case 404:
Expand All @@ -70,16 +70,16 @@ class TransactionApiService {
///
/// Returns:
/// A list of transactions.
Future<List<Transaction>> getTransactionsByChargerId(int id) async {
var transactions = <Transaction>[];
Future<List<OldTransaction>> getTransactionsByChargerId(int id) async {
var transactions = <OldTransaction>[];
var response = await client
.get(Uri.parse('${API.url}/transactions/chargerTransactions/$id'));

switch (response.statusCode) {
case 200:
var parsedTransactions = json.decode(response.body) as List<dynamic>;
for (var trans in parsedTransactions) {
transactions.add(Transaction.fromJson(trans));
transactions.add(OldTransaction.fromJson(trans));
}
return transactions;
case 404:
Expand Down Expand Up @@ -160,7 +160,7 @@ class TransactionApiService {
///
/// Returns:
/// The response is a JSON object containing the following:
Future<Transaction> createKlarnaPaymentSession(
Future<OldTransaction> createKlarnaPaymentSession(
int? userId, int chargerId) async {
var response = await client.post(Uri.parse('${API.url}/transactions'),
headers: API.defaultRequestHeaders,
Expand All @@ -174,7 +174,7 @@ class TransactionApiService {
switch (response.statusCode) {
case 201:
var transaction = json.decode(response.body) as Map<String, dynamic>;
var parsedSession = Transaction.fromJson(transaction);
var parsedSession = OldTransaction.fromJson(transaction);
return parsedSession;
case 400:
throw Exception(ErrorCodes.badRequest);
Expand All @@ -189,7 +189,7 @@ class TransactionApiService {

/// The request returns the updated transaction object,
/// If everything goes as expected, it will contain a paymentId.
Future<Transaction> createKlarnaOrder(
Future<OldTransaction> createKlarnaOrder(
int transactionId,
String authToken,
) async {
Expand All @@ -205,7 +205,7 @@ class TransactionApiService {
case 200:
var transaction = json.decode(response.body) as Map<String, dynamic>;
if (transaction.isEmpty) throw Exception(ErrorCodes.emptyResponse);
var parsedSession = Transaction.fromJson(transaction);
var parsedSession = OldTransaction.fromJson(transaction);

print("Klarna updatedSession paymentID: " +
parsedSession.paymentID.toString());
Expand All @@ -226,7 +226,7 @@ class TransactionApiService {

/// The request will return an updated transaction object which contains
/// paymentConfirmed == true.
Future<Transaction> stopCharging(int transactionId) async {
Future<OldTransaction> stopCharging(int transactionId) async {
var response = await client.put(
Uri.parse('${API.url}/transactions/stop/$transactionId'),
headers: API.defaultRequestHeaders);
Expand All @@ -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<dynamic>;
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;
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/screens/home_page/home_viewmodel.dart
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -121,7 +121,7 @@ class HomeViewModel extends BaseViewModel {
/// The current charging percentage of the car.
Future<int> fetchChargingPercentage() async {
try {
Transaction currentTransaction = await _transactionAPI
OldTransaction currentTransaction = await _transactionAPI
.getTransactionById(localData.transactionSession.transactionID);

int currentChargingPercentage =
Expand Down
4 changes: 2 additions & 2 deletions lib/ui/sheets/map_bottom_sheet/snappingcheet_viewmodel.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -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: " +
Expand Down
2 changes: 1 addition & 1 deletion lib/ui/sheets/top_sheet/top_sheet_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
20 changes: 11 additions & 9 deletions lib/ui/sheets/top_sheet/top_sheet_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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());
}
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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';
}
}

Expand Down

0 comments on commit e03d45c

Please sign in to comment.