pull/2/head
parent
e819ba46e0
commit
094d74b746
@ -0,0 +1,21 @@
|
||||
export 'local_solutions_web.dart' if (dart.library.io) 'local_solutions_linux.dart';
|
||||
|
||||
import 'package:dde_gesture_manager/models/solution.dart';
|
||||
|
||||
abstract class LocalSolutionEntry {
|
||||
Solution solution;
|
||||
DateTime lastModifyTime;
|
||||
String path;
|
||||
|
||||
LocalSolutionEntry({
|
||||
required this.path,
|
||||
required this.solution,
|
||||
required this.lastModifyTime,
|
||||
});
|
||||
|
||||
save();
|
||||
}
|
||||
|
||||
abstract class LocalSolutionsInterface<T extends LocalSolutionEntry> {
|
||||
Future<List<T>> get solutionEntries;
|
||||
}
|
@ -0,0 +1,68 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:dde_gesture_manager/builder/provider_annotation.dart';
|
||||
import 'package:dde_gesture_manager/extensions.dart';
|
||||
import 'package:dde_gesture_manager/models/solution.dart';
|
||||
import 'package:path/path.dart' show join;
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
|
||||
import 'local_solutions.dart';
|
||||
|
||||
export 'local_solutions.dart';
|
||||
|
||||
@ProviderModel()
|
||||
class LocalSolutions implements LocalSolutionsInterface<LocalSolutionEntryLinux> {
|
||||
LocalSolutions() {
|
||||
solutionEntries.then((value) => solutions = value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<LocalSolutionEntryLinux>> get solutionEntries async {
|
||||
var _supportDirectory = await getApplicationSupportDirectory();
|
||||
var directory = Directory(join(_supportDirectory.path, 'solutions'));
|
||||
if (!directory.existsSync()) directory.createSync();
|
||||
directory.path.sout();
|
||||
return directory
|
||||
.list()
|
||||
.map<LocalSolutionEntryLinux?>((f) {
|
||||
LocalSolutionEntryLinux? entry;
|
||||
try {
|
||||
var content = File(f.path).readAsStringSync();
|
||||
entry = LocalSolutionEntryLinux(
|
||||
path: f.path, solution: Solution.parse(content), lastModifyTime: f.statSync().modified);
|
||||
} catch (e) {
|
||||
e.sout();
|
||||
}
|
||||
return entry;
|
||||
})
|
||||
.where((e) => e != null)
|
||||
.cast<LocalSolutionEntryLinux>()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@ProviderModelProp()
|
||||
List<LocalSolutionEntry>? solutions;
|
||||
}
|
||||
|
||||
class LocalSolutionEntryLinux implements LocalSolutionEntry {
|
||||
@override
|
||||
String path;
|
||||
|
||||
@override
|
||||
Solution solution;
|
||||
|
||||
@override
|
||||
DateTime lastModifyTime;
|
||||
|
||||
LocalSolutionEntryLinux({
|
||||
required this.path,
|
||||
required this.solution,
|
||||
required this.lastModifyTime,
|
||||
});
|
||||
|
||||
@override
|
||||
save() {
|
||||
// TODO: implement save
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
export 'local_solutions_web.provider.dart' if (dart.library.io) 'local_solutions_linux.provider.dart';
|
@ -0,0 +1,67 @@
|
||||
import 'dart:convert';
|
||||
|
||||
import 'package:dde_gesture_manager/builder/provider_annotation.dart';
|
||||
import 'package:dde_gesture_manager/models/solution.dart';
|
||||
import 'package:dde_gesture_manager/extensions.dart';
|
||||
import 'dart:html';
|
||||
|
||||
import 'local_solutions.dart';
|
||||
export 'local_solutions.dart';
|
||||
|
||||
@ProviderModel()
|
||||
class LocalSolutions implements LocalSolutionsInterface<LocalSolutionEntryWeb> {
|
||||
LocalSolutions() {
|
||||
solutionEntries.then((value) => solutions = value);
|
||||
}
|
||||
|
||||
@override
|
||||
Future<List<LocalSolutionEntryWeb>> get solutionEntries async {
|
||||
return window.localStorage.keys
|
||||
.map<LocalSolutionEntryWeb?>((key) {
|
||||
if (key.startsWith('solutions.')) {
|
||||
LocalSolutionEntryWeb? entry;
|
||||
try {
|
||||
var content = window.localStorage[key] ?? '';
|
||||
var solutionJson = json.decode(content);
|
||||
entry = LocalSolutionEntryWeb(
|
||||
path: key,
|
||||
solution: Solution.parse(solutionJson),
|
||||
lastModifyTime: DateTime.parse(solutionJson['modified_at']),
|
||||
);
|
||||
} catch (e) {
|
||||
e.sout();
|
||||
}
|
||||
return entry;
|
||||
}
|
||||
})
|
||||
.where((e) => e != null)
|
||||
.cast<LocalSolutionEntryWeb>()
|
||||
.toList();
|
||||
}
|
||||
|
||||
@ProviderModelProp()
|
||||
List<LocalSolutionEntry>? solutions;
|
||||
}
|
||||
|
||||
class LocalSolutionEntryWeb implements LocalSolutionEntry {
|
||||
@override
|
||||
String path;
|
||||
|
||||
@override
|
||||
Solution solution;
|
||||
|
||||
@override
|
||||
DateTime lastModifyTime;
|
||||
|
||||
LocalSolutionEntryWeb({
|
||||
required this.path,
|
||||
required this.solution,
|
||||
required this.lastModifyTime,
|
||||
});
|
||||
|
||||
@override
|
||||
save() {
|
||||
// TODO: implement save
|
||||
throw UnimplementedError();
|
||||
}
|
||||
}
|
Loading…
Reference in new issue