37 lines
1.0 KiB
Dart
37 lines
1.0 KiB
Dart
import 'dart:convert';
|
|
import 'dart:io';
|
|
|
|
import 'package:mirror_protocol/mirror_protocol.dart';
|
|
|
|
class LayoutPersistence {
|
|
final String _path;
|
|
|
|
LayoutPersistence({String? path})
|
|
: _path = path ??
|
|
'${Platform.environment['HOME']}/.config/mirror-os/dashboard_layout.json';
|
|
|
|
Future<DashboardLayoutConfig> load() async {
|
|
final file = File(_path);
|
|
if (!await file.exists()) {
|
|
return DashboardLayoutConfig.defaultLayout();
|
|
}
|
|
try {
|
|
final content = await file.readAsString();
|
|
final json = jsonDecode(content) as Map<String, dynamic>;
|
|
return DashboardLayoutConfig.fromJson(json);
|
|
} catch (_) {
|
|
return DashboardLayoutConfig.defaultLayout();
|
|
}
|
|
}
|
|
|
|
Future<void> save(DashboardLayoutConfig layout) async {
|
|
final file = File(_path);
|
|
final dir = file.parent;
|
|
if (!await dir.exists()) {
|
|
await dir.create(recursive: true);
|
|
}
|
|
final content = const JsonEncoder.withIndent(' ').convert(layout.toJson());
|
|
await file.writeAsString(content);
|
|
}
|
|
}
|