Implement offline mode
This commit is contained in:
@@ -6,6 +6,7 @@ import 'package:http/http.dart' as http;
|
||||
import 'package:logging/logging.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import '../models/change.dart';
|
||||
import '../models/sheet.dart';
|
||||
|
||||
/// HTTP client for communicating with the Sheetless API server.
|
||||
@@ -217,6 +218,46 @@ class ApiClient {
|
||||
});
|
||||
_log.info('Annotation uploaded for sheet $sheetUuid page $page');
|
||||
}
|
||||
|
||||
// ---------------------------------------------------------------------------
|
||||
// Change Sync Operations
|
||||
// ---------------------------------------------------------------------------
|
||||
|
||||
/// Uploads a batch of changes to the server.
|
||||
///
|
||||
/// The server will apply changes based on their [createdAt] timestamps,
|
||||
/// using the newest change for each field when resolving conflicts.
|
||||
///
|
||||
/// Returns the list of change indices that were successfully applied.
|
||||
/// Throws [ApiException] if the request fails (e.g., offline).
|
||||
Future<List<int>> uploadChanges(List<Change> changes) async {
|
||||
if (changes.isEmpty) return [];
|
||||
|
||||
final response = await post('/api/changes/sync', {
|
||||
'changes': changes.map((c) => c.toJson()).toList(),
|
||||
});
|
||||
|
||||
final data = jsonDecode(response.body);
|
||||
final applied = (data['applied'] as List<dynamic>).cast<int>();
|
||||
_log.info('Uploaded ${changes.length} changes, ${applied.length} applied');
|
||||
return applied;
|
||||
}
|
||||
|
||||
/// Checks if the server is reachable.
|
||||
///
|
||||
/// Returns true if the server responds, false otherwise.
|
||||
Future<bool> checkConnection() async {
|
||||
try {
|
||||
final url = Uri.parse('$baseUrl/api/health');
|
||||
final response = await http
|
||||
.get(url, headers: _buildHeaders())
|
||||
.timeout(const Duration(seconds: 5));
|
||||
return response.statusCode == 200;
|
||||
} catch (e) {
|
||||
_log.fine('Connection check failed: $e');
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents an annotation from the server.
|
||||
|
||||
Reference in New Issue
Block a user