sync product to local

This commit is contained in:
efrilm
2025-09-20 03:10:05 +07:00
parent 3022d8de9f
commit f104390141
21 changed files with 4461 additions and 514 deletions
@@ -0,0 +1,145 @@
import 'dart:developer';
import 'package:dartz/dartz.dart';
import 'package:enaklo_pos/data/datasources/product/product_local_datasource.dart';
import 'package:enaklo_pos/data/models/response/product_response_model.dart';
class ProductRepository {
static ProductRepository? _instance;
final ProductLocalDatasource _localDatasource;
ProductRepository._internal()
: _localDatasource = ProductLocalDatasource.instance;
static ProductRepository get instance {
_instance ??= ProductRepository._internal();
return _instance!;
}
// ========================================
// PURE LOCAL DATABASE OPERATIONS
// ========================================
Future<Either<String, ProductResponseModel>> getProducts({
int page = 1,
int limit = 10,
String? categoryId,
String? search,
bool forceRefresh = false, // Ignored - kept for compatibility
}) async {
try {
log('πŸ“± Getting products from local database - page: $page, categoryId: $categoryId, search: $search');
// Clean expired cache for optimal performance
_localDatasource.clearExpiredCache();
// Use cached query for maximum performance
final cachedProducts = await _localDatasource.getCachedProducts(
page: page,
limit: limit,
categoryId: categoryId,
search: search,
);
final totalCount = await _localDatasource.getTotalCount(
categoryId: categoryId,
search: search,
);
final productData = ProductData(
products: cachedProducts,
totalCount: totalCount,
page: page,
limit: limit,
totalPages: totalCount > 0 ? (totalCount / limit).ceil() : 0,
);
final response = ProductResponseModel(
success: true,
data: productData,
errors: null,
);
log('βœ… Returned ${cachedProducts.length} local products (${totalCount} total)');
return Right(response);
} catch (e) {
log('❌ Error getting local products: $e');
return Left('Gagal memuat produk dari database lokal: $e');
}
}
// ========================================
// OPTIMIZED LOCAL SEARCH
// ========================================
Future<Either<String, List<Product>>> searchProductsOptimized(
String query) async {
try {
log('πŸ” Local optimized search for: "$query"');
final products = await _localDatasource.searchProductsOptimized(query);
log('βœ… Local search completed: ${products.length} results');
return Right(products);
} catch (e) {
log('❌ Error in local search: $e');
return Left('Pencarian lokal gagal: $e');
}
}
// ========================================
// LOCAL DATABASE OPERATIONS
// ========================================
// Refresh just cleans cache and reloads from local
Future<Either<String, ProductResponseModel>> refreshProducts({
String? categoryId,
String? search,
}) async {
log('πŸ”„ Refreshing local products...');
// Clear cache for fresh local data
clearCache();
return await getProducts(
page: 1,
limit: 10,
categoryId: categoryId,
search: search,
);
}
Future<Product?> getProductById(String id) async {
log('πŸ” Getting product by ID from local: $id');
return await _localDatasource.getProductById(id);
}
Future<bool> hasLocalProducts() async {
final hasProducts = await _localDatasource.hasProducts();
log('πŸ“Š Has local products: $hasProducts');
return hasProducts;
}
Future<Map<String, dynamic>> getDatabaseStats() async {
final stats = await _localDatasource.getDatabaseStats();
log('πŸ“Š Database stats: $stats');
return stats;
}
void clearCache() {
log('🧹 Clearing local cache');
_localDatasource.clearCache();
}
// Helper method to check if local database is populated
Future<bool> isLocalDatabaseReady() async {
try {
final stats = await getDatabaseStats();
final productCount = stats['total_products'] ?? 0;
final isReady = productCount > 0;
log('πŸ” Local database ready: $isReady ($productCount products)');
return isReady;
} catch (e) {
log('❌ Error checking database readiness: $e');
return false;
}
}
}