fixed nullable mess with editor ids
This commit is contained in:
@@ -7,7 +7,6 @@ import "package:provider/provider.dart";
|
||||
import "package:prod/models/globalModel.dart";
|
||||
|
||||
void main() {
|
||||
// DEAL WITH NULLS IN EDITOR LIST AND PROJECT's EDITOR LIST. SWITCH TO HIDDEN NONE EDITOR WITH "" AS ID.
|
||||
runApp(const MyApp());
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
|
||||
@@ -14,7 +14,7 @@ class EditorEditor extends StatelessWidget {
|
||||
return Scaffold(
|
||||
appBar: AppBar(title: Text("/ PROject Dashboard / Editors")),
|
||||
floatingActionButton: EditorFAB(),
|
||||
body: gm.lenEdt > 0
|
||||
body: gm.lenEdt - 1 > 0
|
||||
? LayoutBuilder(
|
||||
builder: (context, constraints) {
|
||||
int cols = (constraints.maxWidth / 250).floor();
|
||||
@@ -25,7 +25,7 @@ class EditorEditor extends StatelessWidget {
|
||||
mainAxisSpacing: 10,
|
||||
childAspectRatio: 1.5,
|
||||
),
|
||||
itemCount: gm.lenEdt,
|
||||
itemCount: gm.lenEdt - 1,
|
||||
itemBuilder: (context, index) => EditorCard(index),
|
||||
);
|
||||
},
|
||||
|
||||
@@ -7,7 +7,7 @@ import "package:yaru/yaru.dart";
|
||||
import "package:process_run/shell.dart";
|
||||
|
||||
class EditorCard extends StatelessWidget {
|
||||
const EditorCard(this.id, {super.key});
|
||||
const EditorCard(id, {super.key}) : id = id + 1;
|
||||
final int id;
|
||||
|
||||
@override
|
||||
|
||||
@@ -23,8 +23,8 @@ class EditorSelector extends StatelessWidget {
|
||||
quarterTurns: turns,
|
||||
),
|
||||
onSelected: (a) {
|
||||
print("$a");
|
||||
prj.setEditor(turns, a);
|
||||
// print("$a");
|
||||
prj.setEditor(turns, a ?? "");
|
||||
gm.updatePrj(id, prj);
|
||||
},
|
||||
|
||||
|
||||
@@ -2,20 +2,31 @@ import "package:flutter/material.dart";
|
||||
import "package:prod/models/editor.dart";
|
||||
import "package:prod/models/globalModel.dart";
|
||||
import "package:provider/provider.dart";
|
||||
import "package:process_run/shell.dart";
|
||||
|
||||
class LauncherButton extends StatelessWidget {
|
||||
const LauncherButton(this.eid, {super.key});
|
||||
final String? eid;
|
||||
const LauncherButton(this.eid, this.path, {super.key});
|
||||
final String eid;
|
||||
final String path;
|
||||
|
||||
@override
|
||||
Widget build(BuildContext context) {
|
||||
// print("EDITOR ID: $eid");
|
||||
GlobalModel gm = Provider.of<GlobalModel>(context, listen: false);
|
||||
final Editor edt = gm.nthEdt(gm.id2EdtMap[eid]!);
|
||||
return eid == null
|
||||
final Editor edt = gm.nthEdt(gm.getEdtPosFromID(eid));
|
||||
// print("GRABBED EDITOR: ${edt.name}");
|
||||
return eid == ""
|
||||
? Container()
|
||||
: Expanded(
|
||||
flex: 1,
|
||||
child: TextButton(child: Text("${edt.sname}"), onPressed: () {}),
|
||||
child: TextButton(
|
||||
child: Text("${edt.sname}"),
|
||||
onPressed: () {
|
||||
Shell().run(
|
||||
"${edt.commandTemplate.replaceAll('\$path', path)}",
|
||||
);
|
||||
},
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,10 +76,16 @@ class ProjectCard extends StatelessWidget {
|
||||
Row(
|
||||
children: [
|
||||
Column(
|
||||
children: [LauncherButton(prj.e0), LauncherButton(prj.e1)],
|
||||
children: [
|
||||
LauncherButton(prj.e0, prj.path.path),
|
||||
LauncherButton(prj.e1, prj.path.path),
|
||||
],
|
||||
),
|
||||
Column(
|
||||
children: [LauncherButton(prj.e2), LauncherButton(prj.e3)],
|
||||
children: [
|
||||
LauncherButton(prj.e2, prj.path.path),
|
||||
LauncherButton(prj.e3, prj.path.path),
|
||||
],
|
||||
),
|
||||
],
|
||||
),
|
||||
|
||||
Reference in New Issue
Block a user