This commit is contained in:
@@ -1,27 +1,75 @@
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:prod/models/editor.dart';
|
||||
import 'package:prod/models/project.dart';
|
||||
import "package:shared_preferences/shared_preferences.dart";
|
||||
import "package:prod/models/constants.dart";
|
||||
// import "package:json_annotation/json_annotation.dart";
|
||||
import "dart:convert";
|
||||
|
||||
class GlobalModel extends ChangeNotifier {
|
||||
late List<Project> projects;
|
||||
late List<bool> hoverShow;
|
||||
late List<Editor> editors;
|
||||
late SharedPreferences prefs;
|
||||
bool importedData = false;
|
||||
|
||||
GlobalModel() {
|
||||
projects = [];
|
||||
editors = [];
|
||||
hoverShow = List.filled(projects.length, false, growable: true);
|
||||
SharedPreferences.getInstance().then((a) {
|
||||
print("Loaded sp");
|
||||
prefs = a;
|
||||
if (prefs.containsKey(kProjectsKey)) {
|
||||
String prjData = prefs.getString(kProjectsKey)!;
|
||||
List<dynamic> data = jsonDecode(prjData);
|
||||
projects = [];
|
||||
for (var d in data) {
|
||||
projects.add(Project.fromJson(d));
|
||||
}
|
||||
} else {
|
||||
projects = [];
|
||||
prefs.setString(kProjectsKey, jsonEncode(projects));
|
||||
}
|
||||
print(projects);
|
||||
|
||||
if (prefs.containsKey(kEditorsKey)) {
|
||||
String edtData = prefs.getString(kEditorsKey)!;
|
||||
List<dynamic> data = jsonDecode(edtData);
|
||||
editors = [];
|
||||
for (var d in data) {
|
||||
editors.add(Editor.fromJson(d));
|
||||
}
|
||||
} else {
|
||||
editors = [];
|
||||
prefs.setString(kEditorsKey, jsonEncode(editors));
|
||||
}
|
||||
|
||||
hoverShow = List.filled(projects.length, false, growable: true);
|
||||
print("loaded data");
|
||||
importedData = true;
|
||||
notifyListeners();
|
||||
});
|
||||
}
|
||||
|
||||
void saveProjectState() {
|
||||
var arst = projects.map((a) => a.toJson()).toList();
|
||||
prefs.setString(kProjectsKey, jsonEncode(arst));
|
||||
}
|
||||
|
||||
void saveEditorState() {
|
||||
var arst = editors.map((a) => a.toJson()).toList();
|
||||
prefs.setString(kEditorsKey, jsonEncode(arst));
|
||||
}
|
||||
|
||||
void addPrj(Project prj) {
|
||||
projects.add(prj);
|
||||
hoverShow.add(false);
|
||||
saveProjectState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delPrj(int index) {
|
||||
projects.removeAt(index);
|
||||
hoverShow.removeAt(index);
|
||||
saveProjectState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
@@ -50,11 +98,13 @@ class GlobalModel extends ChangeNotifier {
|
||||
|
||||
void addEdt(Editor edt) {
|
||||
editors.add(edt);
|
||||
saveEditorState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delEdt(int index) {
|
||||
editors.removeAt(index);
|
||||
saveEditorState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user