158 lines
6.5 KiB
Dart
158 lines
6.5 KiB
Dart
import 'package:oc_front/core/sections/header/header.dart';
|
|
import 'package:oc_front/main.dart';
|
|
import 'package:oc_front/pages/abstract_page.dart';
|
|
import 'package:oc_front/pages/catalog.dart';
|
|
import 'package:oc_front/pages/catalog_item.dart';
|
|
import 'package:oc_front/pages/datacenter.dart';
|
|
import 'package:oc_front/pages/map.dart';
|
|
import 'package:oc_front/pages/scheduler.dart';
|
|
import 'package:oc_front/pages/shared.dart';
|
|
import 'package:oc_front/pages/workflow.dart';
|
|
import 'package:shared_preferences/shared_preferences.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:go_router/go_router.dart';
|
|
|
|
GlobalKey<RouterWidgetState> routerKey = GlobalKey<RouterWidgetState>();
|
|
|
|
class RouterWidget extends StatefulWidget {
|
|
const RouterWidget({Key? key}) : super(key: key);
|
|
@override RouterWidgetState createState() => RouterWidgetState();
|
|
}
|
|
|
|
class RouterWidgetState extends State<RouterWidget> {
|
|
@override Widget build(BuildContext context) {
|
|
return Padding( padding: const EdgeInsets.only(right: 20), child: Row(children: [
|
|
IconButton(onPressed: () async => AppRouter.realHistory.length > 1 ? AppRouter.back() : null, icon: Icon(Icons.arrow_back, color: AppRouter.realHistory.length > 1 ? Colors.white : Theme.of(context).splashColor)),
|
|
IconButton(onPressed: () async => AppRouter.canForward() ? AppRouter.forward() : null, icon: Icon(Icons.arrow_forward, color: AppRouter.canForward() ? Colors.white : Theme.of(context).splashColor)),
|
|
],));
|
|
}
|
|
}
|
|
|
|
class RouterItem {
|
|
final IconData? icon;
|
|
final String? label;
|
|
final String route;
|
|
final String? description;
|
|
final String? help;
|
|
final AbstractFactory factory;
|
|
List<String> args = [];
|
|
RouterItem({this.icon, this.label, this.description, this.help,
|
|
required this.route, required this.factory, this.args = const []});
|
|
String get path => "/${route == AppRouter.home ? "" : route}";
|
|
|
|
void go(BuildContext context, Map<String, String> params) {
|
|
AppRouter.currentRoute = this;
|
|
var newPath = "$path";
|
|
for (var arg in args) { newPath = newPath.replaceAll(":$arg", params[arg] ?? ""); }
|
|
Future.delayed( const Duration(seconds: 1), () {
|
|
HeaderConstants.setTitle(null);
|
|
HeaderConstants.setDescription(null);
|
|
});
|
|
context.go(newPath);
|
|
}
|
|
}
|
|
|
|
class AppRouter {
|
|
static String home = "catalog";
|
|
static final RouterItem workflowItem = RouterItem(icon: Icons.rebase_edit, label: "workflow manager", route: "workflow",
|
|
factory: WorkflowFactory());
|
|
static final RouterItem workflowIDItem = RouterItem(description: "", help: "", route: "workflow/:id", factory: WorkflowFactory(), args: ["id"]);
|
|
static final RouterItem catalogItem = RouterItem(description: "", help: "", route: "catalog/:id", factory: CatalogItemFactory(), args: ["id"]);
|
|
static final RouterItem catalog= RouterItem(icon: Icons.book_outlined, label: "catalog searcher", route: "catalog", factory: CatalogFactory());
|
|
static final RouterItem scheduler = RouterItem(icon: Icons.schedule, label: "scheduled tasks", route: "scheduler", factory: SchedulerFactory());
|
|
static final RouterItem compute = RouterItem(icon: Icons.dns_outlined, label: "my compute", route: "compute", factory: DatacenterFactory());
|
|
static final RouterItem shared = RouterItem(icon: Icons.share_rounded, label: "collaborative area", route: "shared", factory: SharedFactory());
|
|
|
|
static List<RouterItem> zones = [
|
|
catalog,
|
|
workflowItem,
|
|
scheduler,
|
|
compute,
|
|
RouterItem(icon: Icons.public_outlined, label: "localisations", route: "map", factory: MapFactory()),
|
|
shared,
|
|
workflowIDItem,
|
|
catalogItem,
|
|
];
|
|
static List<String> history = [];
|
|
static List<String> realHistory = [];
|
|
static final AppRouter _instance = AppRouter._internal();
|
|
factory AppRouter() { return _instance; }
|
|
AppRouter._internal() {
|
|
SharedPreferences.getInstance().then((prefs) {
|
|
if (prefs.containsKey("history")) {
|
|
realHistory = prefs.getString("history")!.split(",");
|
|
history = prefs.getString("history")!.split(",");
|
|
routerKey.currentState?.setState(() { });
|
|
}
|
|
});
|
|
}
|
|
|
|
static Future<String?> getRouteCookie() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
return prefs.getString("url") != "" ? prefs.getString("url") : null;
|
|
}
|
|
|
|
static removeRouteCookie() async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
prefs.remove("url");
|
|
}
|
|
|
|
static setRouteCookie( String path , BuildContext context ) async {
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
prefs.setString("url", path);
|
|
if (realHistory.isNotEmpty && realHistory.last != path || realHistory.isEmpty) {
|
|
try {
|
|
var index = history.indexOf(realHistory.last);
|
|
history = history.sublist(0, index + 1);
|
|
} catch (e) { /* */ }
|
|
realHistory.add(path);
|
|
history.add(path);
|
|
if (history.length > 10) {
|
|
realHistory.removeAt(0);
|
|
history.removeAt(0);
|
|
}
|
|
prefs.setString("history", realHistory.join(","));
|
|
routerKey.currentState?.setState(() { });
|
|
}
|
|
}
|
|
|
|
static back() async {
|
|
if (realHistory.length <= 1) { return; }
|
|
realHistory.removeLast();
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
prefs.setString("url", realHistory.last);
|
|
prefs.setString("history", realHistory.join(","));
|
|
var splitted = realHistory.last.split(":");
|
|
routerKey.currentState?.setState(() { });
|
|
scaffoldKey.currentState?.setState(() {});
|
|
}
|
|
static bool canForward() {
|
|
try {
|
|
var index = history.indexOf(realHistory.last);
|
|
return (index + 1) < history.length;
|
|
} catch (e) { return false; }
|
|
|
|
}
|
|
static forward() async {
|
|
if (canForward()) {
|
|
var index = history.indexOf(realHistory.last);
|
|
realHistory.add(history[index + 1]);
|
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
prefs.setString("url", realHistory.last);
|
|
var splitted = realHistory.last.split(":");
|
|
prefs.setString("history", realHistory.join(","));
|
|
routerKey.currentState?.setState(() { });
|
|
scaffoldKey.currentState?.setState(() {});
|
|
}
|
|
}
|
|
|
|
static RouterItem currentRoute = zones.first;
|
|
static List<RouteBase> get routes => zones.map( (k) => GoRoute(
|
|
name: k.route,
|
|
path: k.path,
|
|
builder: (BuildContext context, GoRouterState state) {
|
|
return MainPage(page: k.factory.factory(state, k.args));
|
|
},
|
|
)).toList();
|
|
}
|
|
// ROUTER SHOULD INVOKE MAIN TO ACCESS VIEW, VIEW ARE MENU SECTION |