fixed nullable mess with editor ids
This commit is contained in:
@@ -19,31 +19,30 @@ class GlobalModel extends ChangeNotifier {
|
||||
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);
|
||||
editors = [];
|
||||
id2EdtMap = {};
|
||||
for (var (i, d) in data.indexed) {
|
||||
var localEdt = Editor.fromJson(d);
|
||||
editors.add(localEdt);
|
||||
id2EdtMap[localEdt.id] = i;
|
||||
id2EdtMap[localEdt.id] = i + 1;
|
||||
}
|
||||
} else {
|
||||
editors = [];
|
||||
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);
|
||||
projects = [];
|
||||
for (var d in data) {
|
||||
projects.add(Project.fromJson(d));
|
||||
projects.add(Project.fromJson(d, idList));
|
||||
}
|
||||
} else {
|
||||
projects = [];
|
||||
prefs.setString(kProjectsKey, jsonEncode(projects));
|
||||
}
|
||||
print(projects);
|
||||
@@ -61,7 +60,11 @@ class GlobalModel extends ChangeNotifier {
|
||||
}
|
||||
|
||||
void saveEditorState() {
|
||||
var arst = editors.map((a) => a.toJson()).toList();
|
||||
var tempEditors = editors;
|
||||
tempEditors.removeWhere((a) => a.id == "");
|
||||
List<Map<String, dynamic>> arst = tempEditors
|
||||
.map((a) => a.toJson())
|
||||
.toList();
|
||||
prefs.setString(kEditorsKey, jsonEncode(arst));
|
||||
}
|
||||
|
||||
@@ -112,12 +115,16 @@ class GlobalModel extends ChangeNotifier {
|
||||
|
||||
void addEdt(Editor edt) {
|
||||
editors.add(edt);
|
||||
print(editors);
|
||||
id2EdtMap[edt.id] = editors.length - 1;
|
||||
saveEditorState();
|
||||
notifyListeners();
|
||||
}
|
||||
|
||||
void delEdt(int index) {
|
||||
editors.removeAt(index);
|
||||
Editor removed = editors.removeAt(index);
|
||||
int remmedID = id2EdtMap.remove(removed.id)!;
|
||||
assert(index == remmedID);
|
||||
saveEditorState();
|
||||
notifyListeners();
|
||||
}
|
||||
@@ -130,8 +137,8 @@ class GlobalModel extends ChangeNotifier {
|
||||
return editors[index];
|
||||
}
|
||||
|
||||
int? getFromID(String? id) {
|
||||
return id2EdtMap[id];
|
||||
int getEdtPosFromID(String id) {
|
||||
return id2EdtMap[id] ?? 0;
|
||||
}
|
||||
|
||||
// Editing controller
|
||||
|
||||
@@ -6,10 +6,10 @@ class Project {
|
||||
String name;
|
||||
String language;
|
||||
File path;
|
||||
String? e0;
|
||||
String? e1;
|
||||
String? e2;
|
||||
String? e3;
|
||||
String e0;
|
||||
String e1;
|
||||
String e2;
|
||||
String e3;
|
||||
bool isGit;
|
||||
|
||||
Project(
|
||||
@@ -17,10 +17,10 @@ class Project {
|
||||
this.language,
|
||||
this.path,
|
||||
this.isGit, {
|
||||
this.e0 = null,
|
||||
this.e1 = null,
|
||||
this.e2 = null,
|
||||
this.e3 = null,
|
||||
this.e0 = "",
|
||||
this.e1 = "",
|
||||
this.e2 = "",
|
||||
this.e3 = "",
|
||||
});
|
||||
|
||||
factory Project.newValidated(String name, String lang, String path) {
|
||||
@@ -43,27 +43,19 @@ class Project {
|
||||
"language": language,
|
||||
"path": path.path,
|
||||
"isGit": isGit,
|
||||
"e0": e0,
|
||||
"e1": e1,
|
||||
"e2": e2,
|
||||
"e3": e3,
|
||||
};
|
||||
if (e0 == null) {
|
||||
fin["e0"] = e0;
|
||||
}
|
||||
if (e1 == null) {
|
||||
fin["e1"] = e1;
|
||||
}
|
||||
if (e2 == null) {
|
||||
fin["e2"] = e2;
|
||||
}
|
||||
if (e3 == null) {
|
||||
fin["e3"] = e3;
|
||||
}
|
||||
return fin;
|
||||
}
|
||||
|
||||
factory Project.fromJson(Map<String, dynamic> data) {
|
||||
final String? e0 = data["e0"];
|
||||
final String? e1 = data["e1"];
|
||||
final String? e2 = data["e2"];
|
||||
final String? e3 = data["e3"];
|
||||
factory Project.fromJson(Map<String, dynamic> data, List<String> editorList) {
|
||||
final String e0 = editorList.contains(data["e0"]) ? data["e0"] : "";
|
||||
final String e1 = editorList.contains(data["e1"]) ? data["e1"] : "";
|
||||
final String e2 = editorList.contains(data["e2"]) ? data["e2"] : "";
|
||||
final String e3 = editorList.contains(data["e3"]) ? data["e3"] : "";
|
||||
return Project(
|
||||
data["name"] as String,
|
||||
data["language"] as String,
|
||||
@@ -76,7 +68,7 @@ class Project {
|
||||
);
|
||||
}
|
||||
|
||||
String? getEditor(enumb) {
|
||||
String getEditor(int enumb) {
|
||||
switch (enumb) {
|
||||
case 0:
|
||||
return e0;
|
||||
@@ -87,12 +79,11 @@ class Project {
|
||||
case 3:
|
||||
return e3;
|
||||
default:
|
||||
return null;
|
||||
break;
|
||||
return "";
|
||||
}
|
||||
}
|
||||
|
||||
void setEditor(int enumb, String? edt) {
|
||||
void setEditor(int enumb, String edt) {
|
||||
switch (enumb) {
|
||||
case 0:
|
||||
e0 = edt;
|
||||
|
||||
Reference in New Issue
Block a user