fixed nullable mess with editor ids
Some checks failed
Build CI / AMD64 Build (push) Has been cancelled
Build CI / ARM64 Build (push) Has been cancelled

This commit is contained in:
2026-02-28 15:20:26 +05:30
parent 07f222a87e
commit 4f0503ca50
8 changed files with 67 additions and 53 deletions

View File

@@ -7,7 +7,6 @@ import "package:provider/provider.dart";
import "package:prod/models/globalModel.dart"; import "package:prod/models/globalModel.dart";
void main() { void main() {
// DEAL WITH NULLS IN EDITOR LIST AND PROJECT's EDITOR LIST. SWITCH TO HIDDEN NONE EDITOR WITH "" AS ID.
runApp(const MyApp()); runApp(const MyApp());
} }

View File

@@ -19,31 +19,30 @@ class GlobalModel extends ChangeNotifier {
SharedPreferences.getInstance().then((a) { SharedPreferences.getInstance().then((a) {
print("Loaded sp"); print("Loaded sp");
prefs = a; prefs = a;
editors = [Editor("", "None", "NANII", "", "")];
id2EdtMap = {"NANII": 0};
if (prefs.containsKey(kEditorsKey)) { if (prefs.containsKey(kEditorsKey)) {
String edtData = prefs.getString(kEditorsKey)!; String edtData = prefs.getString(kEditorsKey)!;
List<dynamic> data = jsonDecode(edtData); List<dynamic> data = jsonDecode(edtData);
editors = [];
id2EdtMap = {};
for (var (i, d) in data.indexed) { for (var (i, d) in data.indexed) {
var localEdt = Editor.fromJson(d); var localEdt = Editor.fromJson(d);
editors.add(localEdt); editors.add(localEdt);
id2EdtMap[localEdt.id] = i; id2EdtMap[localEdt.id] = i + 1;
} }
} else { } else {
editors = [];
prefs.setString(kEditorsKey, jsonEncode(editors)); prefs.setString(kEditorsKey, jsonEncode(editors));
} }
print(id2EdtMap); print(id2EdtMap);
final List<String> idList = id2EdtMap.keys.toList();
projects = [];
if (prefs.containsKey(kProjectsKey)) { if (prefs.containsKey(kProjectsKey)) {
String prjData = prefs.getString(kProjectsKey)!; String prjData = prefs.getString(kProjectsKey)!;
List<dynamic> data = jsonDecode(prjData); List<dynamic> data = jsonDecode(prjData);
projects = [];
for (var d in data) { for (var d in data) {
projects.add(Project.fromJson(d)); projects.add(Project.fromJson(d, idList));
} }
} else { } else {
projects = [];
prefs.setString(kProjectsKey, jsonEncode(projects)); prefs.setString(kProjectsKey, jsonEncode(projects));
} }
print(projects); print(projects);
@@ -61,7 +60,11 @@ class GlobalModel extends ChangeNotifier {
} }
void saveEditorState() { 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)); prefs.setString(kEditorsKey, jsonEncode(arst));
} }
@@ -112,12 +115,16 @@ class GlobalModel extends ChangeNotifier {
void addEdt(Editor edt) { void addEdt(Editor edt) {
editors.add(edt); editors.add(edt);
print(editors);
id2EdtMap[edt.id] = editors.length - 1;
saveEditorState(); saveEditorState();
notifyListeners(); notifyListeners();
} }
void delEdt(int index) { void delEdt(int index) {
editors.removeAt(index); Editor removed = editors.removeAt(index);
int remmedID = id2EdtMap.remove(removed.id)!;
assert(index == remmedID);
saveEditorState(); saveEditorState();
notifyListeners(); notifyListeners();
} }
@@ -130,8 +137,8 @@ class GlobalModel extends ChangeNotifier {
return editors[index]; return editors[index];
} }
int? getFromID(String? id) { int getEdtPosFromID(String id) {
return id2EdtMap[id]; return id2EdtMap[id] ?? 0;
} }
// Editing controller // Editing controller

View File

@@ -6,10 +6,10 @@ class Project {
String name; String name;
String language; String language;
File path; File path;
String? e0; String e0;
String? e1; String e1;
String? e2; String e2;
String? e3; String e3;
bool isGit; bool isGit;
Project( Project(
@@ -17,10 +17,10 @@ class Project {
this.language, this.language,
this.path, this.path,
this.isGit, { this.isGit, {
this.e0 = null, this.e0 = "",
this.e1 = null, this.e1 = "",
this.e2 = null, this.e2 = "",
this.e3 = null, this.e3 = "",
}); });
factory Project.newValidated(String name, String lang, String path) { factory Project.newValidated(String name, String lang, String path) {
@@ -43,27 +43,19 @@ class Project {
"language": language, "language": language,
"path": path.path, "path": path.path,
"isGit": isGit, "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; return fin;
} }
factory Project.fromJson(Map<String, dynamic> data) { factory Project.fromJson(Map<String, dynamic> data, List<String> editorList) {
final String? e0 = data["e0"]; final String e0 = editorList.contains(data["e0"]) ? data["e0"] : "";
final String? e1 = data["e1"]; final String e1 = editorList.contains(data["e1"]) ? data["e1"] : "";
final String? e2 = data["e2"]; final String e2 = editorList.contains(data["e2"]) ? data["e2"] : "";
final String? e3 = data["e3"]; final String e3 = editorList.contains(data["e3"]) ? data["e3"] : "";
return Project( return Project(
data["name"] as String, data["name"] as String,
data["language"] as String, data["language"] as String,
@@ -76,7 +68,7 @@ class Project {
); );
} }
String? getEditor(enumb) { String getEditor(int enumb) {
switch (enumb) { switch (enumb) {
case 0: case 0:
return e0; return e0;
@@ -87,12 +79,11 @@ class Project {
case 3: case 3:
return e3; return e3;
default: default:
return null; return "";
break;
} }
} }
void setEditor(int enumb, String? edt) { void setEditor(int enumb, String edt) {
switch (enumb) { switch (enumb) {
case 0: case 0:
e0 = edt; e0 = edt;

View File

@@ -14,7 +14,7 @@ class EditorEditor extends StatelessWidget {
return Scaffold( return Scaffold(
appBar: AppBar(title: Text("/ PROject Dashboard / Editors")), appBar: AppBar(title: Text("/ PROject Dashboard / Editors")),
floatingActionButton: EditorFAB(), floatingActionButton: EditorFAB(),
body: gm.lenEdt > 0 body: gm.lenEdt - 1 > 0
? LayoutBuilder( ? LayoutBuilder(
builder: (context, constraints) { builder: (context, constraints) {
int cols = (constraints.maxWidth / 250).floor(); int cols = (constraints.maxWidth / 250).floor();
@@ -25,7 +25,7 @@ class EditorEditor extends StatelessWidget {
mainAxisSpacing: 10, mainAxisSpacing: 10,
childAspectRatio: 1.5, childAspectRatio: 1.5,
), ),
itemCount: gm.lenEdt, itemCount: gm.lenEdt - 1,
itemBuilder: (context, index) => EditorCard(index), itemBuilder: (context, index) => EditorCard(index),
); );
}, },

View File

@@ -7,7 +7,7 @@ import "package:yaru/yaru.dart";
import "package:process_run/shell.dart"; import "package:process_run/shell.dart";
class EditorCard extends StatelessWidget { class EditorCard extends StatelessWidget {
const EditorCard(this.id, {super.key}); const EditorCard(id, {super.key}) : id = id + 1;
final int id; final int id;
@override @override

View File

@@ -23,8 +23,8 @@ class EditorSelector extends StatelessWidget {
quarterTurns: turns, quarterTurns: turns,
), ),
onSelected: (a) { onSelected: (a) {
print("$a"); // print("$a");
prj.setEditor(turns, a); prj.setEditor(turns, a ?? "");
gm.updatePrj(id, prj); gm.updatePrj(id, prj);
}, },

View File

@@ -2,20 +2,31 @@ import "package:flutter/material.dart";
import "package:prod/models/editor.dart"; import "package:prod/models/editor.dart";
import "package:prod/models/globalModel.dart"; import "package:prod/models/globalModel.dart";
import "package:provider/provider.dart"; import "package:provider/provider.dart";
import "package:process_run/shell.dart";
class LauncherButton extends StatelessWidget { class LauncherButton extends StatelessWidget {
const LauncherButton(this.eid, {super.key}); const LauncherButton(this.eid, this.path, {super.key});
final String? eid; final String eid;
final String path;
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
// print("EDITOR ID: $eid");
GlobalModel gm = Provider.of<GlobalModel>(context, listen: false); GlobalModel gm = Provider.of<GlobalModel>(context, listen: false);
final Editor edt = gm.nthEdt(gm.id2EdtMap[eid]!); final Editor edt = gm.nthEdt(gm.getEdtPosFromID(eid));
return eid == null // print("GRABBED EDITOR: ${edt.name}");
return eid == ""
? Container() ? Container()
: Expanded( : Expanded(
flex: 1, flex: 1,
child: TextButton(child: Text("${edt.sname}"), onPressed: () {}), child: TextButton(
child: Text("${edt.sname}"),
onPressed: () {
Shell().run(
"${edt.commandTemplate.replaceAll('\$path', path)}",
);
},
),
); );
} }
} }

View File

@@ -76,10 +76,16 @@ class ProjectCard extends StatelessWidget {
Row( Row(
children: [ children: [
Column( Column(
children: [LauncherButton(prj.e0), LauncherButton(prj.e1)], children: [
LauncherButton(prj.e0, prj.path.path),
LauncherButton(prj.e1, prj.path.path),
],
), ),
Column( Column(
children: [LauncherButton(prj.e2), LauncherButton(prj.e3)], children: [
LauncherButton(prj.e2, prj.path.path),
LauncherButton(prj.e3, prj.path.path),
],
), ),
], ],
), ),