Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
103 changes: 16 additions & 87 deletions lib/data/repository/transaction_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import 'package:moneyplus/domain/repository/transaction_repository.dart';

import '../../domain/entity/currency.dart';


class TransactionRepositoryImpl implements TransactionRepository {
final SupabaseService service;

Expand Down Expand Up @@ -73,8 +72,22 @@ class TransactionRepositoryImpl implements TransactionRepository {
TransactionType? type,
TransactionCategory? category,
DateTime? date,
List<int>? categoriesId,
required int page,
}) async {
throw UnimplementedError('getTransactions not implemented');
final client = await service.getClient();
final response = await client.rpc(
RpcString.getTransactions,
params: {
'p_timestamp': date?.toIso8601String(),
'p_category_ids': categoriesId,
'p_transaction_type_id': type?.value,
'p_page': page,
},
);
return (response as List)
.map((transaction) => Transaction.fromJson(transaction))
.toList();
}

@override
Expand Down Expand Up @@ -174,94 +187,10 @@ class TransactionRepositoryImpl implements TransactionRepository {
}) async {
throw UnimplementedError('editExpenseCategory not implemented');
}

@override
Future<List<Transaction>> getAllTransactions() async {
await Future.delayed(const Duration(milliseconds: 500));
return [
Transaction(
id: 1,
amount: 50000,
currency: "IQD",
type: TransactionType.expense,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
Transaction(
id: 4,
amount: 5040,
currency: "IQD",
type: TransactionType.income,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
Transaction(
id: 2,
amount: 230000,
currency: "IQD",
type: TransactionType.income,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
Transaction(
id: 3,
amount: 530000,
currency: "IQD",
type: TransactionType.expense,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
];
}

@override
Future<List<Transaction>> getAllTransactionsByType(
TransactionType type,
) async {
await Future.delayed(const Duration(milliseconds: 500));
if (type == TransactionType.income) {
return [
Transaction(
id: 4,
amount: 5040,
currency: "IQD",
type: TransactionType.income,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
Transaction(
id: 2,
amount: 230000,
currency: "IQD",
type: TransactionType.income,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
];
} else {
return [
Transaction(
id: 1,
amount: 50000,
currency: "IQD",
type: TransactionType.expense,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
Transaction(
id: 3,
amount: 530000,
currency: "IQD",
type: TransactionType.expense,
date: DateTime(2024, 12, 2),
category: TransactionCategory(id: 1, name: "shopping"),
),
];
}
}
}

class RpcString {
static String deleteTransaction = 'delete_transaction';
static String getTransactionDetails = 'get_transaction_details';
static String getTransactions = 'get_transactions';
}
16 changes: 15 additions & 1 deletion lib/domain/entity/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,21 @@ class Transaction {
type: type ?? this.type,
date: date ?? this.date,
category: category ?? this.category,
note: note ?? this.note
note: note ?? this.note,
);
}

factory Transaction.fromJson(Map<String, dynamic> json) {
return Transaction(
id: json['id'] ?? 0,
amount: (json['amount'] as num).toDouble(),
currency: json['currency'] ?? '',
type: json['transaction_type'] == 'income'
? TransactionType.income
: TransactionType.expense,
date: DateTime.parse(json['date']),
category: TransactionCategory(id: json['category_id'], name: json['category']),
note: json['note'] ?? '',
);
}
}
8 changes: 3 additions & 5 deletions lib/domain/repository/transaction_repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,11 @@ abstract class TransactionRepository {
TransactionType? type,
TransactionCategory? category,
DateTime? date,
List<int> categoriesId = const[],
required int page,
});

Future<Result<Transaction>> getTransactionDetails(String id);
Future<Result<Transaction>> getTransactionDetails(String id);

Future<double> getTotalAmount({TransactionType? type});

Expand All @@ -46,8 +48,4 @@ abstract class TransactionRepository {
Future<bool> addExpenseCategory(String name);

Future<bool> editExpenseCategory({required int id, required String name});

Future<List<Transaction>> getAllTransactions();

Future<List<Transaction>> getAllTransactionsByType(TransactionType type,);
}
149 changes: 102 additions & 47 deletions lib/presentation/transactions/cubit/transaction_cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,57 @@ class TransactionCubit extends Cubit<TransactionState> {
void loadData() async {
emit(state.copyWith(status: TransactionStatus.loading));

final result = await transactionRepository.getAllTransactions();
final now = DateTime.now();
emit(
state.copyWith(
allTransactions: result,
filteredTransactions: _filterTransaction(now.month, now.year, result),
selectedYear: now.year,
selectedMonth: now.month,
status: TransactionStatus.success,
),
);
try {
final result = await transactionRepository.getTransactions(
page: 1,
date: now,
);
emit(
state.copyWith(
transactions: result,
selectedYear: now.year,
selectedMonth: now.month,
status: TransactionStatus.success,
hasMore: result.length == 20,
),
);
} catch (_) {
emit(state.copyWith(status: TransactionStatus.failure));
}
}

void onTabSelected(TransactionTabs tab) async {
if (state.selectedTab == tab) return;

emit(state.copyWith(status: TransactionStatus.loading, selectedTab: tab));

final result = await _getTransactionsByTab(tab);

emit(
state.copyWith(
status: TransactionStatus.success,
allTransactions: result,
filteredTransactions: _filterTransaction(
state.selectedMonth,
state.selectedYear,
result,
),
status: TransactionStatus.loading,
selectedTab: tab,
currentPage: 1,
hasMore: true,
),
);

try {
final result = await _getTransaction(
page: 1,
tab: tab,
year: state.selectedYear,
month: state.selectedMonth,
);

emit(
state.copyWith(
status: TransactionStatus.success,
transactions: result,
currentPage: 1,
hasMore: result.length == 20,
),
);
} catch (_) {
emit(state.copyWith(status: TransactionStatus.failure));
}
}

void setSelectedDate(int month, int year) async {
Expand All @@ -54,39 +74,74 @@ class TransactionCubit extends Cubit<TransactionState> {
selectedMonth: month,
selectedYear: year,
status: TransactionStatus.loading,
currentPage: 1,
hasMore: true,
),
);

emit(
state.copyWith(
status: TransactionStatus.success,
filteredTransactions: _filterTransaction(
month,
year,
state.allTransactions,
try {
final result = await _getTransaction(
page: 1,
tab: state.selectedTab,
year: year,
month: month,
);

emit(
state.copyWith(
status: TransactionStatus.success,
transactions: result,
currentPage: 1,
hasMore: result.length == 20,
),
),
);
);
} catch (_) {
emit(state.copyWith(status: TransactionStatus.failure));
}
}

Future<List<Transaction>> _getTransactionsByTab(TransactionTabs tab) async {
return await switch (tab) {
TransactionTabs.all => transactionRepository.getAllTransactions(),
TransactionTabs.incomes => transactionRepository.getAllTransactionsByType(
TransactionType.income,
),
TransactionTabs.expenses =>
transactionRepository.getAllTransactionsByType(TransactionType.expense),
};
void loadMore() async {
if (!state.hasMore || state.isLoadingMore) return;

emit(state.copyWith(isLoadingMore: true));

final nextPage = state.currentPage + 1;

try {
final result = await _getTransaction(
page: nextPage,
tab: state.selectedTab,
year: state.selectedYear,
month: state.selectedMonth,
);

emit(
state.copyWith(
transactions: [...state.transactions, ...result],
currentPage: nextPage,
hasMore: result.length == 20,
isLoadingMore: false,
),
);
} catch (_) {
emit(state.copyWith(status: TransactionStatus.failure));
}
}

List<Transaction> _filterTransaction(
int month,
int year,
List<Transaction> transactions,
) {
return transactions.where((transaction) {
return transaction.date.year == year && transaction.date.month == month;
}).toList();
Future<List<Transaction>> _getTransaction({
required int page,
required TransactionTabs tab,
required int year,
required int month,
}) async {
return await transactionRepository.getTransactions(
page: page,
type: tab == TransactionTabs.expenses
? TransactionType.expense
: tab == TransactionTabs.incomes
? TransactionType.income
: null,
date: DateTime(year, month),
);
}
}
Loading