50 lines
1.4 KiB
Dart
50 lines
1.4 KiB
Dart
import 'dart:convert';
|
|
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
|
|
/*
|
|
* Class to save the devices pinned by the user
|
|
*/
|
|
class DevicesSaved {
|
|
String deviceName = '';
|
|
String deviceAddress = '';
|
|
String remoteId = '';
|
|
bool connected = false;
|
|
|
|
DevicesSaved(
|
|
{required this.deviceName,
|
|
required this.deviceAddress,
|
|
required this.remoteId});
|
|
|
|
DevicesSaved.fromJson(Map<String, dynamic> json) {
|
|
deviceName = json['deviceName'];
|
|
deviceAddress = json['deviceAddress'];
|
|
remoteId = json['remoteId'];
|
|
}
|
|
|
|
Map<String, dynamic> toJson() {
|
|
final Map<String, dynamic> data = <String, dynamic>{};
|
|
data['deviceName'] = deviceName;
|
|
data['deviceAddress'] = deviceAddress;
|
|
data['remoteId'] = remoteId;
|
|
return data;
|
|
}
|
|
}
|
|
|
|
Future<List<DevicesSaved>> restoreLocal() async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final String? encodedData = prefs.getString('deviceSaved');
|
|
if (encodedData != null) {
|
|
final List<dynamic> decodedData = jsonDecode(encodedData);
|
|
return decodedData.map((json) => DevicesSaved.fromJson(json)).toList();
|
|
}
|
|
return [];
|
|
}
|
|
|
|
Future<void> saveLocal(List<DevicesSaved> deviceSaved) async {
|
|
final prefs = await SharedPreferences.getInstance();
|
|
final String encodedData =
|
|
jsonEncode(deviceSaved.map((obj) => obj.toJson()).toList());
|
|
await prefs.setString('deviceSaved', encodedData);
|
|
}
|