Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: Add support to action registration #169

Merged
merged 11 commits into from
Aug 3, 2023
101 changes: 88 additions & 13 deletions docs/docs/basics/parsers.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ sidebar_position: 1

Now that we have installed Mirai, let's talk about the "parsers".

Mirai Parser are the most important part of the Mirai framework. A parser is the class which will parse your json data into Flutter widget.
Mirai Parser are the most important part of the Mirai framework. A parser is the class which will parse your json data into Flutter widget and actions.

## Creating a Parser
## Creating a Widget Parser

To create a parser, simply extend a class with `MiraiParser`.
To create a widget parser, simply extend a class with `MiraiParser`.

```dart
import 'package:flutter/material.dart';
Expand Down Expand Up @@ -44,24 +44,24 @@ class MiraiTextParser extends MiraiParser<MiraiText> {
}
```

The above code snippet contains 3 main componets
The above code snippet contains 3 main components

- `type`: This is where you define the widget type, which is a uniquie identifier for the widget.
- `type`: This is where you define the widget type, which is a unique identifier for the widget.

- `getModel`: This is where you will define the model for your widget, which you will receive in `parse` method. The ideal way is to create a model class and returnt he fromJson function.
- `getModel`: This is where you will define the model for your widget, which you will receive in `parse` method. The ideal way is to create a model class and return the fromJson function.

- `parse`: This is where the json is parsed into widget. The `parse` method provides you with `context` and the `model` and returns a widget.

## Registering a Parser
## Registering a Widget Parser

There are two ways to register a parser:

1. Register in MiraiApp
2. Register through MiraiRegistry
1. Register in `MiraiApp`
2. Register through `MiraiRegistry`

### Register in MiraiApp

You can register your parser my passing it on MiraiApp.
You can register your parser my passing it on `MiraiApp`.

```dart
MiraiApp(
Expand All @@ -73,19 +73,94 @@ You can register your parser my passing it on MiraiApp.

### Register through MiraiRegistry

MiraiRegistry provides you with two method to register the parser.
`MiraiRegistry` provides you with two method to register the widget parser.

1.`register`: register method takes a `MiraiParser` and register it.
1.`register`: this method takes a `MiraiParser` and register it.

```dart
MiraiRegistry.instance.register(parser);
```

2.`registerAll`: registerAll method takes a list of `MiraiParser` and regsiter it.
2.`registerAll`: this method takes a list of `MiraiParser` and register it.

```dart
MiraiRegistry.instance.registerAll([
MiraiTextParser(),
MiraiButtonParser(),
]);
```

## Creating an Action Parser

To create an action parser, simply extend a class with `MiraiActionParser`.

```dart
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:mirai/mirai.dart';

class MiraiShareParser extends MiraiActionParser<MiraiShare> {
const MiraiShareParser();

@override
MiraiShare getModel(Map<String, dynamic> json) => MiraiShare.fromJson(json);

@override
String get type => 'share';

@override
FutureOr<dynamic> onCall(BuildContext context, MiraiShare model) {
return MiraiShare.share(
subject: model.subject,
text: model.text,
);
}
}
```

The above code snippet contains 3 main components

- `type`: This is where you define the action type, which is a unique identifier for the action.

- `getModel`: This is where you will define the model for your action, which you will receive in `onCall` method. The ideal way is to create a model class and return the fromJson function.

- `onCall`: This is where the JSON is parsed into an action. The `onCall` method provides you with `context` and the `model` and returns the result of the action.

## Registering an Action Parser

Like the Widget Parser registration, there are two ways to register a parser:

1. Register in `MiraiApp`
2. Register through `MiraiRegistry`

### Register in MiraiApp

You can register your parser my passing it on `MiraiApp`.

```dart
MiraiApp(
actionParsers: [
MiraiShareParser(),
],
);
```

### Register through MiraiRegistry

`MiraiRegistry` provides you with two method to register the action parser.

1.`registerAction`: this method takes a `MiraiActionParser` and register it.

```dart
MiraiRegistry.instance.registerAction(parser);
```

2.`registerAllActions`: this method takes a list of `MiraiActionParser` and register it.

```dart
MiraiRegistry.instance.registerAllActions([
MiraiShareParser(),
MiraiBluetoothParser(),
]);
```
3 changes: 3 additions & 0 deletions docs/docs/overview.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ void main() async {
parsers: const [
ExampleScreenParser(),
],
actionParsers: const [
ExampleActionParser(),
],
dio: dio,
);

Expand Down
2 changes: 1 addition & 1 deletion packages/mirai/lib/mirai.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
library mirai;

export 'package:mirai/src/action/action.dart';
export 'package:mirai/src/action_parsers/action_parsers.dart';
export 'package:mirai/src/framework/framework.dart';
export 'package:mirai/src/navigation/navigation.dart';
export 'package:mirai/src/network/network.dart';
Expand Down
4 changes: 0 additions & 4 deletions packages/mirai/lib/src/action/action.dart

This file was deleted.

26 changes: 0 additions & 26 deletions packages/mirai/lib/src/action/mirai_action.dart

This file was deleted.

73 changes: 0 additions & 73 deletions packages/mirai/lib/src/action/mirai_action_parser.dart

This file was deleted.

3 changes: 3 additions & 0 deletions packages/mirai/lib/src/action_parsers/action_parsers.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export 'package:mirai/src/action_parsers/mirai_navigate_action/mirai_navigate_action_parser.dart';
export 'package:mirai/src/action_parsers/mirai_none_action/mirai_none_action_parser.dart';
export 'package:mirai/src/action_parsers/mirai_request_action/mirai_request_action_parser.dart';
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import 'dart:async';

import 'package:flutter/widgets.dart';
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:mirai/src/framework/framework.dart';
import 'package:mirai/src/navigation/mirai_navigator.dart';
import 'package:mirai/src/network/mirai_request.dart';

part 'mirai_navigate_action.freezed.dart';
part 'mirai_navigate_action.g.dart';

@freezed
class MiraiNavigateAction with _$MiraiNavigateAction {
const MiraiNavigateAction._();

factory MiraiNavigateAction({
MiraiRequest? request,
Map<String, dynamic>? widgetJson,
String? assetPath,
String? routeName,
NavigationType? navigationType,
NavigationStyle? navigationStyle,
Map<String, dynamic>? result,
Map<String, dynamic>? arguments,
}) = _MiraiNavigateAction;

factory MiraiNavigateAction.fromJson(Map<String, dynamic> json) =>
_$MiraiNavigateActionFromJson(json);

FutureOr<dynamic>? onCall(BuildContext context) async {
Widget? widget;
if (widgetJson != null) {
widget = Mirai.fromJson(widgetJson, context);

if (widget != null) {
return MiraiNavigator.navigate(
context: context,
navigationType: navigationType ?? NavigationType.screen,
navigationStyle: navigationStyle ?? NavigationStyle.push,
widget: widget,
result: result,
arguments: arguments,
);
}
} else if (request != null) {
widget = Mirai.fromNetwork(request!);

return MiraiNavigator.navigate(
context: context,
navigationType: navigationType ?? NavigationType.screen,
navigationStyle: navigationStyle ?? NavigationStyle.push,
widget: widget,
result: result,
arguments: arguments,
);
} else if (assetPath != null) {
widget = await Mirai.fromAssets(assetPath!, context);

if (context.mounted && widget != null) {
return MiraiNavigator.navigate(
context: context,
navigationType: navigationType ?? NavigationType.screen,
navigationStyle: navigationStyle ?? NavigationStyle.push,
widget: widget,
result: result,
arguments: arguments,
);
}
} else {
if (context.mounted) {
return MiraiNavigator.navigate(
context: context,
navigationType: navigationType ?? NavigationType.screen,
navigationStyle: navigationStyle ?? NavigationStyle.push,
routeName: routeName,
result: result,
arguments: arguments,
);
}
}
}
}
Loading
Loading