55 lines
1.5 KiB
Dart
55 lines
1.5 KiB
Dart
import 'dart:convert';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import '../models/project.dart';
|
|
|
|
class StorageService {
|
|
static const String _projectsKey = 'projects';
|
|
static const String _currentPortKey = 'current_port';
|
|
|
|
Future<void> saveProjects(List<Project> projects) async {
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final jsonList = projects.map((p) => p.toJson()).toList();
|
|
final jsonString = json.encode(jsonList);
|
|
final success = await prefs.setString(_projectsKey, jsonString);
|
|
if (!success) {
|
|
throw Exception('Failed to save projects to storage');
|
|
}
|
|
// Force a commit on web
|
|
await prefs.reload();
|
|
} catch (e) {
|
|
rethrow;
|
|
}
|
|
}
|
|
|
|
Future<List<Project>> loadProjects() async {
|
|
try {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
// Reload to ensure we get the latest data
|
|
await prefs.reload();
|
|
final jsonString = prefs.getString(_projectsKey);
|
|
|
|
if (jsonString == null || jsonString.isEmpty) {
|
|
return [];
|
|
}
|
|
|
|
final List<dynamic> jsonList = json.decode(jsonString);
|
|
return jsonList.map((json) => Project.fromJson(json)).toList();
|
|
} catch (e) {
|
|
return [];
|
|
}
|
|
}
|
|
|
|
Future<void> saveCurrentPort(int port) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
await prefs.setInt(_currentPortKey, port);
|
|
}
|
|
|
|
Future<int> loadCurrentPort() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
return prefs.getInt(_currentPortKey) ?? 8888;
|
|
}
|
|
}
|
|
|
|
|