sync data

This commit is contained in:
efrilm
2025-09-20 05:02:01 +07:00
parent 72a464b4c0
commit a58d1040af
4 changed files with 678 additions and 1 deletions
@@ -282,4 +282,16 @@ class CategoryRepository {
return false;
}
}
Future<void> clearAllCategories() async {
try {
log('πŸ—‘οΈ Clearing all categories from repository...');
await _localDatasource.clearAllCategories();
clearCache();
log('βœ… All categories cleared successfully');
} catch (e) {
log('❌ Error clearing all categories: $e');
rethrow;
}
}
}
@@ -1,15 +1,18 @@
import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:enaklo_pos/data/datasources/product/product_local_datasource.dart';
import 'package:enaklo_pos/data/datasources/product_remote_datasource.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
class ProductRepository {
static ProductRepository? _instance;
final ProductLocalDatasource _localDatasource;
final ProductRemoteDatasource _remoteDatasource;
ProductRepository._internal()
: _localDatasource = ProductLocalDatasource.instance;
: _localDatasource = ProductLocalDatasource.instance,
_remoteDatasource = ProductRemoteDatasource();
static ProductRepository get instance {
_instance ??= ProductRepository._internal();
@@ -85,6 +88,66 @@ class ProductRepository {
}
}
// ========================================
// PRODUCT SYNC OPERATIONS
// ========================================
Future<Either<String, String>> syncAllProducts() async {
try {
log('πŸ”„ Starting manual sync of all products...');
int page = 1;
const limit = 50; // Higher limit for bulk sync
bool hasMore = true;
int totalSynced = 0;
// Clear local data first for fresh sync
await _localDatasource.clearAllProducts();
while (hasMore) {
log('πŸ“„ Syncing page $page...');
final result = await _remoteDatasource.getProducts(
page: page,
limit: limit,
);
await result.fold(
(failure) async {
log('❌ Sync failed at page $page: $failure');
throw Exception(failure);
},
(response) async {
final products = response.data?.products ?? [];
if (products.isNotEmpty) {
await _localDatasource.saveProductsBatch(
products,
clearFirst: false, // Don't clear on subsequent pages
);
totalSynced += products.length;
// Check if we have more pages
hasMore = page < (response.data?.totalPages ?? 0);
page++;
log('πŸ“¦ Page $page synced: ${products.length} products');
} else {
hasMore = false;
}
},
);
}
final message = 'Berhasil sinkronisasi $totalSynced produk';
log('βœ… $message');
return Right(message);
} catch (e) {
final error = 'Gagal sinkronisasi produk: $e';
log('❌ $error');
return Left(error);
}
}
// ========================================
// LOCAL DATABASE OPERATIONS
// ========================================
@@ -142,4 +205,16 @@ class ProductRepository {
return false;
}
}
Future<void> clearAllProducts() async {
try {
log('πŸ—‘οΈ Clearing all products from repository...');
await _localDatasource.clearAllProducts();
clearCache();
log('βœ… All products cleared successfully');
} catch (e) {
log('❌ Error clearing all products: $e');
rethrow;
}
}
}