switched to json storage to allow sharing
This commit is contained in:
@ -1,22 +1,27 @@
|
||||
import 'dart:convert';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
import 'dart:io';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import '../models/project.dart';
|
||||
|
||||
class StorageService {
|
||||
static const String _projectsKey = 'projects';
|
||||
static const String _currentPortKey = 'current_port';
|
||||
static const String _projectsFileName = 'unityudp_projects.json';
|
||||
|
||||
Future<String> get _localPath async {
|
||||
final directory = await getApplicationDocumentsDirectory();
|
||||
return directory.path;
|
||||
}
|
||||
|
||||
Future<File> get _projectsFile async {
|
||||
final path = await _localPath;
|
||||
return File('$path/$_projectsFileName');
|
||||
}
|
||||
|
||||
Future<void> saveProjects(List<Project> projects) async {
|
||||
try {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
final file = await _projectsFile;
|
||||
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();
|
||||
final jsonString = const JsonEncoder.withIndent(' ').convert(jsonList);
|
||||
await file.writeAsString(jsonString);
|
||||
} catch (e) {
|
||||
rethrow;
|
||||
}
|
||||
@ -24,31 +29,60 @@ class StorageService {
|
||||
|
||||
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);
|
||||
final file = await _projectsFile;
|
||||
|
||||
if (jsonString == null || jsonString.isEmpty) {
|
||||
// Check if file exists
|
||||
if (!await file.exists()) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final List<dynamic> jsonList = json.decode(jsonString);
|
||||
final contents = await file.readAsString();
|
||||
|
||||
if (contents.isEmpty) {
|
||||
return [];
|
||||
}
|
||||
|
||||
final List<dynamic> jsonList = json.decode(contents);
|
||||
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<String> getProjectsFilePath() async {
|
||||
final file = await _projectsFile;
|
||||
return file.path;
|
||||
}
|
||||
|
||||
Future<int> loadCurrentPort() async {
|
||||
final prefs = await SharedPreferences.getInstance();
|
||||
return prefs.getInt(_currentPortKey) ?? 8888;
|
||||
Future<bool> importProjectsFromFile(String filePath) async {
|
||||
try {
|
||||
final importFile = File(filePath);
|
||||
|
||||
if (!await importFile.exists()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
final contents = await importFile.readAsString();
|
||||
final List<dynamic> jsonList = json.decode(contents);
|
||||
final projects = jsonList.map((json) => Project.fromJson(json)).toList();
|
||||
|
||||
// Save the imported projects
|
||||
await saveProjects(projects);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
Future<bool> exportProjectsToFile(String filePath, List<Project> projects) async {
|
||||
try {
|
||||
final exportFile = File(filePath);
|
||||
final jsonList = projects.map((p) => p.toJson()).toList();
|
||||
final jsonString = const JsonEncoder.withIndent(' ').convert(jsonList);
|
||||
await exportFile.writeAsString(jsonString);
|
||||
return true;
|
||||
} catch (e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user