Skip to content

Commit

Permalink
feat: Routes system (#186)
Browse files Browse the repository at this point in the history
* feat: Lenra Application Routes (#178)

* This should fix a lot

* This is working !!!

* update deps

* Update application runner

* feat: Update dependencies

* ci: Releases alpha

* feat: Update dependencies

* feat: Update dependencies

* feat: Update dependencies

* fix: Error when building client (no tree shake)

* feat: Client on port 4000 and API on 4001 (#187)

* feat: Open external links on navTo (#188)

* feat: Update dependencies (#189)

* build(deps): beta deps

---------

Co-authored-by: Thomas DA ROCHA <[email protected]>
  • Loading branch information
jonas-martinez and taorepoara committed Oct 8, 2023
1 parent fccfde9 commit f173b5c
Show file tree
Hide file tree
Showing 23 changed files with 305 additions and 113 deletions.
3 changes: 2 additions & 1 deletion .github/workflows/semantic_release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ on:
branches:
- main
- beta
- alpha
- "*.x"
env:
flutter_version: "3.3.x"
Expand Down Expand Up @@ -45,7 +46,7 @@ jobs:
- name: Build Flutter Web
run: |
cd client
flutter build web
flutter build web --no-tree-shake-icons
cd ..
- name: Deps + compile elixir
run: |
Expand Down
2 changes: 2 additions & 0 deletions .releaserc.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ branches:
- main
- name: beta
prerelease: true
- name: alpha
prerelease: true
plugins:
- - "@semantic-release/commit-analyzer"
- preset: conventionalcommits
Expand Down
63 changes: 63 additions & 0 deletions client/lib/appNavigator.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import 'package:client/main.dart';
import 'package:client_common/navigator/common_navigator.dart';
import 'package:flutter/material.dart';
import 'package:go_router/go_router.dart';
import 'package:lenra_ui_runner/app.dart';
import 'package:lenra_ui_runner/io_components/lenra_route.dart';
import 'package:url_launcher/url_launcher.dart';

class AppNavigator extends CommonNavigator {
static GoRoute appRoutes = GoRoute(
name: "appRoutes",
path: "/:path(.*)",
pageBuilder: (_, state) {
return NoTransitionPage(
child: App(
appName: DevTools.appName,
httpEndpoint: "http://localhost:4001",
accessToken: "",
wsEndpoint: "ws://localhost:4001/socket/websocket",
baseRoute: "/",
routeWidget: LenraRoute(
"/${state.params['path']!}",
// Use UniqueKey to make sure that the LenraRoute Widget is properly reloaded with the new route when navigating.
key: UniqueKey(),
),
navTo: (context, route) {
// This regex matches http:// and https:// urls
RegExp exp = RegExp(r"^https?://");
if (exp.hasMatch(route)) {
_launchURL(route);
} else {
GoRouter.of(context).go(route);
}
},
),
);
},
);

static GoRouter router = GoRouter(
initialLocation: "/",
routes: [appRoutes],
);
}

class NoTransitionPage extends CustomTransitionPage {
NoTransitionPage({required Widget child, LocalKey? key})
: super(
child: child,
key: key,
transitionDuration: Duration.zero,
transitionsBuilder: (context, animation, secondaryAnimation, child) {
return child;
},
);
}

_launchURL(String url) async {
final Uri uri = Uri.parse(url);
if (!await launchUrl(uri)) {
throw Exception("Could not launch url: $url");
}
}
33 changes: 0 additions & 33 deletions client/lib/homePage.dart

This file was deleted.

56 changes: 29 additions & 27 deletions client/lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import 'package:client/homePage.dart';
import 'package:client/appNavigator.dart';
import 'package:client/models/dev_tools_socket_model.dart';
import 'package:flutter/material.dart';
import 'package:lenra_components/lenra_components.dart';
import 'package:lenra_ui_runner/models/socket_model.dart';
import 'package:url_strategy/url_strategy.dart';
import 'package:provider/provider.dart';

void main() async {
setPathUrlStrategy();
Expand All @@ -14,37 +17,36 @@ void main() async {
}

class DevTools extends StatelessWidget {
static const String appName = "00000000-0000-0000-0000-000000000000";

int getUserId() {
if (!Uri.base.queryParameters.containsKey("user")) return 1;
String userIdStr = Uri.base.queryParameters["user"]!;
return int.tryParse(userIdStr) ?? 1;
}

@override
Widget build(BuildContext context) {
var themeData = LenraThemeData();

return LenraTheme(
themeData: themeData,
child: MaterialApp(
title: 'Lenra - DevTools',
theme: ThemeData(
textTheme: TextTheme(bodyText2: themeData.lenraTextThemeData.bodyText),
return MultiProvider(
providers: [
ChangeNotifierProvider<SocketModel>(
create: (context) {
return DevToolsSocketModel(getUserId(), appName);
},
),
// locale: DevicePreview.locale(context),
// builder: DevicePreview.appBuilder,
onGenerateRoute: (RouteSettings settings) {
Widget? pageView;
if (settings.name != null) {
var uriData = Uri.parse(settings.name!);
//uriData.path will be your path and uriData.queryParameters will hold query-params values

switch (uriData.path) {
case '/':
pageView = HomePage();
break;
}
}

if (pageView == null) return null;

return MaterialPageRoute(builder: (BuildContext context) => pageView!);
},
),
],
builder: (BuildContext context, _) => LenraTheme(
themeData: themeData,
child: MaterialApp.router(
routerConfig: AppNavigator.router,
title: 'Lenra - Devtool',
theme: ThemeData(
textTheme:
TextTheme(bodyText2: themeData.lenraTextThemeData.bodyText),
),
)),
);
}
}
2 changes: 1 addition & 1 deletion client/lib/models/dev_tools_socket_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class DevToolsSocketModel extends SocketModel {

DevToolsSocketModel(int userId, String appName)
: this._socket = createPhoenixSocket(
"ws://localhost:4000/socket/websocket",
"ws://localhost:4001/socket/websocket",
{"userId": userId.toString(), "app": appName},
) {
this._socket.connect();
Expand Down
2 changes: 2 additions & 0 deletions client/macos/Flutter/GeneratedPluginRegistrant.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import Foundation

import flutter_secure_storage_macos
import shared_preferences_foundation
import url_launcher_macos

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
FlutterSecureStoragePlugin.register(with: registry.registrar(forPlugin: "FlutterSecureStoragePlugin"))
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
}
Loading

0 comments on commit f173b5c

Please sign in to comment.