68 lines
2.2 KiB
Dart
68 lines
2.2 KiB
Dart
/// Invoices Provider
|
|
///
|
|
/// Riverpod providers for managing invoices state.
|
|
library;
|
|
|
|
import 'package:riverpod_annotation/riverpod_annotation.dart';
|
|
import 'package:worker/core/network/dio_client.dart';
|
|
import 'package:worker/features/invoices/data/datasources/invoice_remote_datasource.dart';
|
|
import 'package:worker/features/invoices/data/repositories/invoice_repository_impl.dart';
|
|
import 'package:worker/features/invoices/domain/entities/invoice.dart';
|
|
import 'package:worker/features/invoices/domain/repositories/invoice_repository.dart';
|
|
|
|
part 'invoices_provider.g.dart';
|
|
|
|
/// Invoice Repository Provider
|
|
@riverpod
|
|
Future<InvoiceRepository> invoiceRepository(Ref ref) async {
|
|
final dioClient = await ref.watch(dioClientProvider.future);
|
|
final remoteDataSource = InvoiceRemoteDataSource(dioClient);
|
|
return InvoiceRepositoryImpl(remoteDataSource);
|
|
}
|
|
|
|
/// Invoices Provider
|
|
///
|
|
/// Provides list of all invoices from repository.
|
|
@riverpod
|
|
class Invoices extends _$Invoices {
|
|
@override
|
|
Future<List<Invoice>> build() async {
|
|
try {
|
|
final repository = await ref.read(invoiceRepositoryProvider.future);
|
|
final invoices = await repository.getInvoicesList(
|
|
limitStart: 0,
|
|
limitPageLength: 0, // 0 = get all
|
|
);
|
|
// Sort by posting date (newest first)
|
|
invoices.sort((a, b) => b.postingDate.compareTo(a.postingDate));
|
|
return invoices;
|
|
} catch (e) {
|
|
throw Exception('Failed to load invoices: $e');
|
|
}
|
|
}
|
|
|
|
/// Refresh invoices
|
|
Future<void> refresh() async {
|
|
state = const AsyncValue.loading();
|
|
state = await AsyncValue.guard(() async {
|
|
final repository = await ref.read(invoiceRepositoryProvider.future);
|
|
final invoices = await repository.getInvoicesList(
|
|
limitStart: 0,
|
|
limitPageLength: 0,
|
|
);
|
|
// Sort by posting date (newest first)
|
|
invoices.sort((a, b) => b.postingDate.compareTo(a.postingDate));
|
|
return invoices;
|
|
});
|
|
}
|
|
}
|
|
|
|
/// Invoice Detail Provider
|
|
///
|
|
/// Provides invoice detail by ID.
|
|
@riverpod
|
|
Future<Invoice> invoiceDetail(Ref ref, String name) async {
|
|
final repository = await ref.watch(invoiceRepositoryProvider.future);
|
|
return await repository.getInvoiceDetail(name);
|
|
}
|