Skip to content

Commit

Permalink
adding a plugin system
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-min committed Mar 31, 2024
1 parent c2f46d8 commit eeefb94
Show file tree
Hide file tree
Showing 9 changed files with 90 additions and 70 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ Please see the announcement here: https://alex-min.fr/live-view-native-flutter-r
- Basic Images support
- Live Components
- Confirm modals
- Plugins to add your own custom components

## What is missing?

Expand Down
15 changes: 1 addition & 14 deletions example/README.md
Original file line number Diff line number Diff line change
@@ -1,16 +1,3 @@
# example

A new Flutter project.

## Getting Started

This project is a starting point for a Flutter application.

A few resources to get you started if this is your first Flutter project:

- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)

For help getting started with Flutter development, view the
[online documentation](https://docs.flutter.dev/), which offers tutorials,
samples, guidance on mobile development, and a full API reference.
A basic demo project for the live view client
48 changes: 36 additions & 12 deletions example/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,30 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.0.1+1"
leak_tracker:
dependency: transitive
description:
name: leak_tracker
sha256: "78eb209deea09858f5269f5a5b02be4049535f568c07b275096836f01ea323fa"
url: "https://pub.dev"
source: hosted
version: "10.0.0"
leak_tracker_flutter_testing:
dependency: transitive
description:
name: leak_tracker_flutter_testing
sha256: b46c5e37c19120a8a01918cfaf293547f47269f7cb4b0058f21531c2465d6ef0
url: "https://pub.dev"
source: hosted
version: "2.0.1"
leak_tracker_testing:
dependency: transitive
description:
name: leak_tracker_testing
sha256: a597f72a664dbd293f3bfc51f9ba69816f84dcd403cdac7066cb3f6003f3ab47
url: "https://pub.dev"
source: hosted
version: "2.0.1"
lints:
dependency: transitive
description:
Expand Down Expand Up @@ -307,26 +331,26 @@ packages:
dependency: transitive
description:
name: matcher
sha256: "1803e76e6653768d64ed8ff2e1e67bea3ad4b923eb5c56a295c3e634bad5960e"
sha256: d2323aa2060500f906aa31a895b4030b6da3ebdcc5619d14ce1aada65cd161cb
url: "https://pub.dev"
source: hosted
version: "0.12.16"
version: "0.12.16+1"
material_color_utilities:
dependency: transitive
description:
name: material_color_utilities
sha256: "9528f2f296073ff54cb9fee677df673ace1218163c3bc7628093e7eed5203d41"
sha256: "0e0a020085b65b6083975e499759762399b4475f766c21668c4ecca34ea74e5a"
url: "https://pub.dev"
source: hosted
version: "0.5.0"
version: "0.8.0"
meta:
dependency: transitive
description:
name: meta
sha256: a6e590c838b18133bb482a2745ad77c5bb7715fb0451209e1a7567d416678b8e
sha256: d584fa6707a52763a52446f02cc621b077888fb63b93bbcb1143a7be5a0c0c04
url: "https://pub.dev"
source: hosted
version: "1.10.0"
version: "1.11.0"
nested:
dependency: transitive
description:
Expand All @@ -347,10 +371,10 @@ packages:
dependency: transitive
description:
name: path
sha256: "8829d8a55c13fc0e37127c29fedf290c102f4e40ae94ada574091fe0ff96c917"
sha256: "087ce49c3f0dc39180befefc60fdb4acd8f8620e5682fe2476afd0b3688bb4af"
url: "https://pub.dev"
source: hosted
version: "1.8.3"
version: "1.9.0"
path_provider:
dependency: transitive
description:
Expand Down Expand Up @@ -668,14 +692,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "2.1.4"
web:
vm_service:
dependency: transitive
description:
name: web
sha256: afe077240a270dcfd2aafe77602b4113645af95d0ad31128cc02bce5ac5d5152
name: vm_service
sha256: b3d56ff4341b8f182b96aceb2fa20e3dcb336b9f867bc0eafc0de10f1048e957
url: "https://pub.dev"
source: hosted
version: "0.3.0"
version: "13.0.0"
web_socket_channel:
dependency: transitive
description:
Expand Down
30 changes: 0 additions & 30 deletions example/test/widget_test.dart

This file was deleted.

14 changes: 7 additions & 7 deletions lib/exec/flutter_exec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,17 @@ class FlutterExecAction {
)
: null,
);
})
}, triggers: ['tap'])
..add(['live-patch'], (value, attributes) {
return ExecLivePatch(url: value!['name']);
})
}, triggers: ['tap'])
..add(['phx-href'], (value, attributes) {
return ExecPhxHref(url: value!['name']);
})
}, triggers: ['tap'])
..add(['phx-href-modal'], (value, attributes) {
return ExecPhxHrefModal(url: value!['name']);
})
..add(['goBack'], (_, __) => ExecGoBack())
}, triggers: ['tap'])
..add(['goBack'], (_, __) => ExecGoBack(), triggers: ['tap'])
..add(['switchTheme'], (value, attributes) {
return ExecSwitchTheme(
theme: value!['theme'],
Expand Down Expand Up @@ -116,8 +116,8 @@ class FlutterExec {
return [];
}

List? actionList = tryJsonDecode(attribute);
if (actionList == null) {
dynamic actionList = tryJsonDecode(attribute);
if (actionList == null || actionList is num || actionList is String) {
return [
FlutterExecAction(name: attributeName, value: {'name': attribute})
.parse(attributeName, attributes)
Expand Down
15 changes: 14 additions & 1 deletion lib/exec/live_view_exec_registry.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,26 @@ class LiveViewExecRegistry {
LiveViewExecRegistry._internal();

final Map<String, ExecBuilder> _execs = {};
final Map<String, List<String>> _execsByTriggers = {};

static LiveViewExecRegistry get instance => _instance;

void add(List<String> execNames, ExecBuilder execBuilder) {
List<String> execsByTrigger(String trigger) =>
_execsByTriggers[trigger] ?? [];

void add(List<String> execNames, ExecBuilder execBuilder,
{List<String> triggers = const []}) {
for (var execName in execNames) {
_execs[execName] = execBuilder;
}
for (var trigger in triggers) {
_execsByTriggers[trigger] ??= [];
for (var execName in execNames) {
if (!_execsByTriggers[trigger]!.contains(execName)) {
_execsByTriggers[trigger]!.add(execName);
}
}
}
}

Exec? exec(
Expand Down
12 changes: 12 additions & 0 deletions lib/live_view/live_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import 'package:http/http.dart' as http;
import 'package:http_query_string/http_query_string.dart' as qs;
import 'package:liveview_flutter/exec/exec_live_event.dart';
import 'package:liveview_flutter/exec/flutter_exec.dart';
import 'package:liveview_flutter/exec/live_view_exec_registry.dart';
import 'package:liveview_flutter/live_view/live_view_fallback_pages.dart';
import 'package:liveview_flutter/live_view/plugin.dart';
import 'package:liveview_flutter/live_view/reactive/live_connection_notifier.dart';
import 'package:liveview_flutter/live_view/reactive/live_go_back_notifier.dart';
import 'package:liveview_flutter/live_view/reactive/state_notifier.dart';
Expand All @@ -25,6 +27,7 @@ import 'package:liveview_flutter/live_view/ui/components/live_floating_action_bu
import 'package:liveview_flutter/live_view/ui/components/live_navigation_rail.dart';
import 'package:liveview_flutter/live_view/ui/components/live_persistent_footer_button.dart';
import 'package:liveview_flutter/live_view/ui/dynamic_component.dart';
import 'package:liveview_flutter/live_view/ui/live_view_ui_registry.dart';
import 'package:liveview_flutter/live_view/ui/root_view/internal_view.dart';
import 'package:liveview_flutter/live_view/ui/root_view/root_view.dart';
import 'package:liveview_flutter/live_view/webdocs.dart';
Expand Down Expand Up @@ -71,6 +74,7 @@ class LiveSocket {
enum ClientType { liveView, httpOnly, webDocs }

class LiveView {
List<Plugin> _installedPlugins = [];
bool catchExceptions = true;
bool disableAnimations = false;
ClientType clientType = ClientType.liveView;
Expand Down Expand Up @@ -567,4 +571,12 @@ class LiveView {
router.navigatorKey?.currentState?.maybePop();
router.notify();
}

Future<void> installPlugins(List<Plugin> plugins) async {
for (var plugin in plugins) {
plugin.registerWidgets(LiveViewUiRegistry.instance);
plugin.registerExecs(LiveViewExecRegistry.instance);
}
_installedPlugins.addAll(plugins);
}
}
11 changes: 11 additions & 0 deletions lib/live_view/plugin.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import 'package:flutter/widgets.dart';
import 'package:liveview_flutter/exec/live_view_exec_registry.dart';
import 'package:liveview_flutter/live_view/ui/live_view_ui_registry.dart';

abstract class Plugin {
String get name;

void registerWidgets(LiveViewUiRegistry registry);

void registerExecs(LiveViewExecRegistry registry);
}
14 changes: 8 additions & 6 deletions lib/live_view/ui/components/state_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:flutter/material.dart';
import 'package:liveview_flutter/exec/exec.dart';
import 'package:liveview_flutter/exec/exec_visibility_action.dart';
import 'package:liveview_flutter/exec/flutter_exec.dart';
import 'package:liveview_flutter/exec/live_view_exec_registry.dart';
import 'package:liveview_flutter/live_view/live_view.dart';
import 'package:liveview_flutter/live_view/mapping/text_replacement.dart';
import 'package:liveview_flutter/live_view/reactive/state_notifier.dart';
Expand Down Expand Up @@ -204,12 +205,13 @@ abstract class StateWidget<T extends LiveStateWidget> extends State<T>
void gatherAllTapEvents(
List<EventHandler> events, {
Map<String, dynamic>? fromAttributes,
}) =>
gatherAllEvents(
['phx-click', 'live-patch', 'phx-href', 'phx-href-modal'],
events,
fromAttributes: fromAttributes,
);
}) {
return gatherAllEvents(
LiveViewExecRegistry.instance.execsByTrigger('tap'),
events,
fromAttributes: fromAttributes,
);
}

List<Exec> convertAttributesToExecs(
List<String> attributes,
Expand Down

0 comments on commit eeefb94

Please sign in to comment.