155 lines
3.6 KiB
Dart
155 lines
3.6 KiB
Dart
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;
|
|
late Map<String, int> id2EdtMap;
|
|
bool importedData = false;
|
|
bool edited = false;
|
|
|
|
GlobalModel() {
|
|
SharedPreferences.getInstance().then((a) {
|
|
print("Loaded sp");
|
|
prefs = a;
|
|
editors = [Editor("", "None", "NANII", "", "")];
|
|
id2EdtMap = {"NANII": 0};
|
|
if (prefs.containsKey(kEditorsKey)) {
|
|
String edtData = prefs.getString(kEditorsKey)!;
|
|
List<dynamic> data = jsonDecode(edtData);
|
|
for (var (i, d) in data.indexed) {
|
|
var localEdt = Editor.fromJson(d);
|
|
editors.add(localEdt);
|
|
id2EdtMap[localEdt.id] = i + 1;
|
|
}
|
|
} else {
|
|
prefs.setString(kEditorsKey, jsonEncode(editors));
|
|
}
|
|
print(id2EdtMap);
|
|
final List<String> idList = id2EdtMap.keys.toList();
|
|
|
|
projects = [];
|
|
if (prefs.containsKey(kProjectsKey)) {
|
|
String prjData = prefs.getString(kProjectsKey)!;
|
|
List<dynamic> data = jsonDecode(prjData);
|
|
for (var d in data) {
|
|
projects.add(Project.fromJson(d, idList));
|
|
}
|
|
} else {
|
|
prefs.setString(kProjectsKey, jsonEncode(projects));
|
|
}
|
|
print(projects);
|
|
|
|
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 tempEditors = editors;
|
|
tempEditors.removeWhere((a) => a.id == "");
|
|
List<Map<String, dynamic>> arst = tempEditors
|
|
.map((a) => a.toJson())
|
|
.toList();
|
|
prefs.setString(kEditorsKey, jsonEncode(arst));
|
|
}
|
|
|
|
// Project management
|
|
|
|
void addPrj(Project prj) {
|
|
projects.add(prj);
|
|
hoverShow.add(false);
|
|
saveProjectState();
|
|
notifyListeners();
|
|
}
|
|
|
|
void delPrj(int index) {
|
|
projects.removeAt(index);
|
|
hoverShow.removeAt(index);
|
|
saveProjectState();
|
|
notifyListeners();
|
|
}
|
|
|
|
List<Project> get lsPrj {
|
|
return projects;
|
|
}
|
|
|
|
int get lenPrj {
|
|
return projects.length;
|
|
}
|
|
|
|
Project nthPrj(int index) {
|
|
return projects[index];
|
|
}
|
|
|
|
void setHoverShow(int index, bool state) {
|
|
hoverShow[index] = state;
|
|
notifyListeners();
|
|
}
|
|
|
|
bool getHoverShow(int index) {
|
|
return hoverShow[index];
|
|
}
|
|
|
|
void updatePrj(int index, Project prj) {
|
|
projects[index] = prj;
|
|
saveProjectState();
|
|
notifyListeners();
|
|
}
|
|
|
|
// Editor List Management.
|
|
|
|
void addEdt(Editor edt) {
|
|
editors.add(edt);
|
|
print(editors);
|
|
id2EdtMap[edt.id] = editors.length - 1;
|
|
saveEditorState();
|
|
notifyListeners();
|
|
}
|
|
|
|
void delEdt(int index) {
|
|
Editor removed = editors.removeAt(index);
|
|
int remmedID = id2EdtMap.remove(removed.id)!;
|
|
assert(index == remmedID);
|
|
saveEditorState();
|
|
notifyListeners();
|
|
}
|
|
|
|
int get lenEdt {
|
|
return editors.length;
|
|
}
|
|
|
|
Editor nthEdt(int index) {
|
|
return editors[index];
|
|
}
|
|
|
|
int getEdtPosFromID(String id) {
|
|
return id2EdtMap[id] ?? 0;
|
|
}
|
|
|
|
// Editing controller
|
|
|
|
bool get isEdited {
|
|
return edited;
|
|
}
|
|
|
|
void updateEdited(bool a) {
|
|
edited = a;
|
|
notifyListeners();
|
|
}
|
|
}
|