initial upload
This commit is contained in:
54
lib/services/storage_service.dart
Normal file
54
lib/services/storage_service.dart
Normal file
@ -0,0 +1,54 @@
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
42
lib/services/udp_service.dart
Normal file
42
lib/services/udp_service.dart
Normal file
@ -0,0 +1,42 @@
|
||||
import 'dart:convert';
|
||||
import 'dart:io';
|
||||
import 'package:udp/udp.dart';
|
||||
|
||||
class UdpService {
|
||||
Future<bool> sendPackage({
|
||||
required String data,
|
||||
required String ipAddress,
|
||||
required int port,
|
||||
}) async {
|
||||
UDP? sender;
|
||||
try {
|
||||
// Create UDP instance
|
||||
sender = await UDP.bind(Endpoint.any());
|
||||
|
||||
// Convert data to bytes
|
||||
final dataBytes = utf8.encode(data);
|
||||
|
||||
// Parse IP address
|
||||
final address = InternetAddress(ipAddress);
|
||||
|
||||
// Create endpoint
|
||||
final destination = Endpoint.unicast(address, port: Port(port));
|
||||
|
||||
// Send the packet
|
||||
final bytesSent = await sender.send(dataBytes, destination);
|
||||
|
||||
// Give a small delay to ensure packet is sent
|
||||
await Future.delayed(const Duration(milliseconds: 100));
|
||||
|
||||
return bytesSent > 0;
|
||||
} catch (e) {
|
||||
// Error occurred while sending UDP package
|
||||
return false;
|
||||
} finally {
|
||||
// Always close the socket
|
||||
sender?.close();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user