This commit is contained in:
2
lib/models/constants.dart
Normal file
2
lib/models/constants.dart
Normal file
@@ -0,0 +1,2 @@
|
||||
const String kProjectsKey = "projects";
|
||||
const String kEditorsKey = "editors";
|
||||
@@ -1,13 +1,19 @@
|
||||
import "package:flutter/material.dart";
|
||||
import "package:process_run/which.dart";
|
||||
import "package:prod/models/rand.dart";
|
||||
|
||||
class Editor {
|
||||
final String id;
|
||||
final String name;
|
||||
final String command;
|
||||
final String commandTemplate;
|
||||
// final Icon icon;
|
||||
|
||||
const Editor(this.name, this.command, this.commandTemplate);
|
||||
const Editor(this.name, this.command, this.commandTemplate, this.id);
|
||||
|
||||
factory Editor.create(String name, String command, String commandTemplate) {
|
||||
return Editor(name, command, commandTemplate, getRandomString(5));
|
||||
}
|
||||
|
||||
bool validateCommand() {
|
||||
final String? fullPath = whichSync(command);
|
||||
@@ -17,4 +23,29 @@ class Editor {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"name": name,
|
||||
"command": command,
|
||||
"commandTemplate": commandTemplate,
|
||||
"id": id,
|
||||
};
|
||||
}
|
||||
|
||||
factory Editor.fromJson(Map<String, dynamic> data) {
|
||||
if (!data.containsKey("name") ||
|
||||
!data.containsKey("command") ||
|
||||
!data.containsKey("commandTemplate") ||
|
||||
!data.containsKey("id")) {
|
||||
print("Found invalid editor config when parsing: $data");
|
||||
return Editor("null", "null", "null", "null");
|
||||
}
|
||||
return Editor(
|
||||
data["name"] as String,
|
||||
data["command"] as String,
|
||||
data["commandTemplate"] as String,
|
||||
data["id"] as String,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
|
||||
|
||||
@@ -6,7 +6,7 @@ class Project {
|
||||
final String name;
|
||||
final String language;
|
||||
final File path;
|
||||
final List<Editor> editors;
|
||||
final List<String> editors;
|
||||
final bool isGit;
|
||||
final bool enableTerminal;
|
||||
|
||||
@@ -19,14 +19,15 @@ class Project {
|
||||
this.enableTerminal,
|
||||
);
|
||||
|
||||
factory Project.validated(
|
||||
factory Project.newValidated(
|
||||
String name,
|
||||
String lang,
|
||||
String path,
|
||||
List<Editor> editors,
|
||||
List<String> editors,
|
||||
bool enableTerminal,
|
||||
) {
|
||||
final File fpath = File(path);
|
||||
print(fpath.absolute.path);
|
||||
if (fpath.existsSync()) {
|
||||
print("Project not found!!!");
|
||||
} else {
|
||||
@@ -38,6 +39,28 @@ class Project {
|
||||
return Project(name, lang, fpath, editors, isGit, enableTerminal);
|
||||
}
|
||||
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
"name": name,
|
||||
"language": language,
|
||||
"path": path.path,
|
||||
"editors": editors,
|
||||
"isGit": isGit,
|
||||
"enableTerminal": enableTerminal,
|
||||
};
|
||||
}
|
||||
|
||||
factory Project.fromJson(Map<String, dynamic> data) {
|
||||
return Project(
|
||||
data["name"] as String,
|
||||
data["language"] as String,
|
||||
File(data["path"] as String),
|
||||
(data["editors"] as List<dynamic>).map((a) => a.toString()).toList(),
|
||||
data["isGit"] as bool,
|
||||
data["enableTerminal"] as bool,
|
||||
);
|
||||
}
|
||||
|
||||
// bool validatePath(){
|
||||
// return File
|
||||
// }
|
||||
|
||||
11
lib/models/rand.dart
Normal file
11
lib/models/rand.dart
Normal file
@@ -0,0 +1,11 @@
|
||||
import 'dart:math';
|
||||
|
||||
const _chars = 'AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz1234567890';
|
||||
Random _rnd = Random();
|
||||
|
||||
String getRandomString(int length) => String.fromCharCodes(
|
||||
Iterable.generate(
|
||||
length,
|
||||
(_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length)),
|
||||
),
|
||||
);
|
||||
Reference in New Issue
Block a user