create submission

This commit is contained in:
Phuoc Nguyen
2025-11-27 16:56:01 +07:00
parent ba04576750
commit b6cb9e865a
18 changed files with 1445 additions and 138 deletions

View File

@@ -3,8 +3,11 @@
/// Implements the submissions repository interface with caching support.
library;
import 'package:worker/features/projects/data/datasources/project_progress_local_datasource.dart';
import 'package:worker/features/projects/data/datasources/project_status_local_datasource.dart';
import 'package:worker/features/projects/data/datasources/submissions_remote_datasource.dart';
import 'package:worker/features/projects/data/models/project_submission_request.dart';
import 'package:worker/features/projects/domain/entities/project_progress.dart';
import 'package:worker/features/projects/domain/entities/project_status.dart';
import 'package:worker/features/projects/domain/entities/project_submission.dart';
import 'package:worker/features/projects/domain/repositories/submissions_repository.dart';
@@ -16,10 +19,12 @@ class SubmissionsRepositoryImpl implements SubmissionsRepository {
const SubmissionsRepositoryImpl(
this._remoteDataSource,
this._statusLocalDataSource,
this._progressLocalDataSource,
);
final SubmissionsRemoteDataSource _remoteDataSource;
final ProjectStatusLocalDataSource _statusLocalDataSource;
final ProjectProgressLocalDataSource _progressLocalDataSource;
/// Get project status list with cache-first pattern
///
@@ -69,6 +74,54 @@ class SubmissionsRepositoryImpl implements SubmissionsRepository {
}
}
/// Get project progress list with cache-first pattern
///
/// 1. Return cached data if available
/// 2. Fetch from API in background and update cache
/// 3. If no cache, wait for API response
@override
Future<List<ProjectProgress>> getProjectProgressList({
bool forceRefresh = false,
}) async {
// Check cache first (unless force refresh)
if (!forceRefresh && _progressLocalDataSource.hasCachedData()) {
final cachedProgress = _progressLocalDataSource.getCachedProgressList();
if (cachedProgress.isNotEmpty) {
// Return cached data immediately
// Also refresh cache in background (fire and forget)
_refreshProgressCache();
return cachedProgress.map((model) => model.toEntity()).toList();
}
}
// No cache or force refresh - fetch from API
try {
final progressModels = await _remoteDataSource.getProjectProgressList();
// Cache the result
await _progressLocalDataSource.cacheProgressList(progressModels);
return progressModels.map((model) => model.toEntity()).toList();
} catch (e) {
// If API fails, try to return cached data as fallback
final cachedProgress = _progressLocalDataSource.getCachedProgressList();
if (cachedProgress.isNotEmpty) {
return cachedProgress.map((model) => model.toEntity()).toList();
}
rethrow;
}
}
/// Refresh progress cache in background
Future<void> _refreshProgressCache() async {
try {
final progressModels = await _remoteDataSource.getProjectProgressList();
await _progressLocalDataSource.cacheProgressList(progressModels);
} catch (e) {
// Silently fail - we already returned cached data
}
}
@override
Future<List<ProjectSubmission>> getSubmissions({
int limitStart = 0,
@@ -84,4 +137,28 @@ class SubmissionsRepositoryImpl implements SubmissionsRepository {
rethrow;
}
}
@override
Future<String> saveSubmission(ProjectSubmissionRequest request) async {
try {
return await _remoteDataSource.saveSubmission(request);
} catch (e) {
rethrow;
}
}
@override
Future<String> uploadProjectFile({
required String projectName,
required String filePath,
}) async {
try {
return await _remoteDataSource.uploadProjectFile(
projectName: projectName,
filePath: filePath,
);
} catch (e) {
rethrow;
}
}
}