sync product to local
This commit is contained in:
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user