mirror of
https://github.com/cyberbread-ru/notepad.git
synced 2026-07-14 12:51:01 +03:00
initial vibecoded release
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import 'package:dio/dio.dart';
|
||||
|
||||
class ApiClient {
|
||||
late final Dio _dio;
|
||||
|
||||
ApiClient({required String baseUrl, required String apiKey}) {
|
||||
_dio = Dio(
|
||||
BaseOptions(
|
||||
baseUrl: baseUrl,
|
||||
headers: {'X-API-Key': apiKey},
|
||||
contentType: 'application/json',
|
||||
connectTimeout: const Duration(seconds: 10),
|
||||
receiveTimeout: const Duration(seconds: 10),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
Future<Response> get(String path) => _dio.get(path);
|
||||
|
||||
Future<Response> post(String path, Map<String, dynamic> body) =>
|
||||
_dio.post(path, data: body);
|
||||
|
||||
Future<Response> put(String path, Map<String, dynamic> body) =>
|
||||
_dio.put(path, data: body);
|
||||
|
||||
Future<Response> delete(String path) => _dio.delete(path);
|
||||
}
|
||||
@@ -0,0 +1,53 @@
|
||||
import 'package:uuid/uuid.dart';
|
||||
|
||||
import '../models/note.dart';
|
||||
import '../repositories/local_note_repository.dart';
|
||||
import 'sync_service.dart';
|
||||
|
||||
class NoteService {
|
||||
final LocalNoteRepository _local;
|
||||
final SyncService? _sync;
|
||||
final _uuid = const Uuid();
|
||||
|
||||
NoteService({
|
||||
required LocalNoteRepository local,
|
||||
SyncService? sync,
|
||||
}) : _local = local,
|
||||
_sync = sync;
|
||||
|
||||
Future<List<Note>> getAllNotes() => _local.getAll();
|
||||
|
||||
Future<Note> createNote({String title = '', String content = ''}) async {
|
||||
final note = Note(
|
||||
id: _uuid.v4(),
|
||||
title: title,
|
||||
content: content,
|
||||
createdAt: DateTime.now(),
|
||||
updatedAt: DateTime.now(),
|
||||
);
|
||||
await _local.save(note);
|
||||
return note;
|
||||
}
|
||||
|
||||
Future<Note> updateNote(Note note) async {
|
||||
final updated = note.copyWith(updatedAt: DateTime.now());
|
||||
await _local.save(updated);
|
||||
return updated;
|
||||
}
|
||||
|
||||
Future<void> deleteNote(String id) async {
|
||||
await _local.delete(id);
|
||||
}
|
||||
|
||||
/// Manual backup — push everything to the server.
|
||||
Future<void> triggerBackup() async {
|
||||
if (_sync == null) throw Exception('Backup not configured');
|
||||
await _sync!.pushAll();
|
||||
}
|
||||
|
||||
/// Restore — pull everything from the server.
|
||||
Future<void> triggerRestore() async {
|
||||
if (_sync == null) throw Exception('Backup not configured');
|
||||
await _sync!.pullAll();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
import '../models/note.dart';
|
||||
import '../repositories/local_note_repository.dart';
|
||||
import '../repositories/remote_note_repository.dart';
|
||||
|
||||
class SyncService {
|
||||
final LocalNoteRepository _local;
|
||||
final RemoteNoteRepository _remote;
|
||||
|
||||
SyncService({
|
||||
required LocalNoteRepository local,
|
||||
required RemoteNoteRepository remote,
|
||||
}) : _local = local,
|
||||
_remote = remote;
|
||||
|
||||
/// Push all local notes to the server, overwriting whatever is there.
|
||||
Future<void> pushAll() async {
|
||||
final notes = await _local.getAll();
|
||||
for (final note in notes) {
|
||||
try {
|
||||
await _remote.update(note);
|
||||
} catch (_) {
|
||||
// Note doesn't exist on server yet — create it
|
||||
await _remote.save(note);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Pull all notes from the server, overwriting local storage.
|
||||
Future<void> pullAll() async {
|
||||
final notes = await _remote.getAll();
|
||||
for (final note in notes) {
|
||||
await _local.save(note);
|
||||
}
|
||||
}
|
||||
|
||||
/// Sync a single note by id — push local version to server.
|
||||
Future<void> syncNote(String id) async {
|
||||
final note = await _local.getById(id);
|
||||
if (note == null) return;
|
||||
try {
|
||||
await _remote.update(note);
|
||||
} catch (_) {
|
||||
await _remote.save(note);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user