fix: add default value of serde

This commit is contained in:
appflowy 2022-03-01 11:22:39 +08:00
parent b6de0caad6
commit 156d38179a
11 changed files with 70 additions and 42 deletions

View File

@ -17,8 +17,6 @@ abstract class Plugin {
String get pluginId; String get pluginId;
bool get enable;
void dispose(); void dispose();
PluginDisplay get display; PluginDisplay get display;
@ -27,13 +25,17 @@ abstract class Plugin {
abstract class PluginBuilder { abstract class PluginBuilder {
Plugin build(dynamic data); Plugin build(dynamic data);
String get pluginName; String get name;
PluginType get pluginType; PluginType get pluginType;
ViewDataType get dataType => ViewDataType.PlainText; ViewDataType get dataType => ViewDataType.PlainText;
} }
abstract class PluginConfig {
bool get creatable => true;
}
abstract class PluginDisplay with NavigationItem { abstract class PluginDisplay with NavigationItem {
@override @override
Widget get leftBarItem; Widget get leftBarItem;
@ -46,8 +48,8 @@ abstract class PluginDisplay with NavigationItem {
Widget buildWidget(); Widget buildWidget();
} }
void registerPlugin({required PluginBuilder builder}) { void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
getIt<PluginSandbox>().registerPlugin(builder.pluginType, builder); getIt<PluginSandbox>().registerPlugin(builder.pluginType, builder, config: config);
} }
Plugin makePlugin({required PluginType pluginType, dynamic data}) { Plugin makePlugin({required PluginType pluginType, dynamic data}) {
@ -55,6 +57,18 @@ Plugin makePlugin({required PluginType pluginType, dynamic data}) {
return plugin; return plugin;
} }
List<PluginBuilder> pluginBuilders() {
final pluginBuilders = getIt<PluginSandbox>().builders;
final pluginConfigs = getIt<PluginSandbox>().pluginConfigs;
return pluginBuilders.where(
(builder) {
final config = pluginConfigs[builder.pluginType]?.creatable;
return config ?? true;
},
).toList();
}
enum FlowyPluginException { enum FlowyPluginException {
invalidData, invalidData,
} }

View File

@ -6,7 +6,8 @@ import '../plugin.dart';
import 'runner.dart'; import 'runner.dart';
class PluginSandbox { class PluginSandbox {
final LinkedHashMap<PluginType, PluginBuilder> _pluginMap = LinkedHashMap(); final LinkedHashMap<PluginType, PluginBuilder> _pluginBuilders = LinkedHashMap();
final Map<PluginType, PluginConfig> _pluginConfigs = <PluginType, PluginConfig>{};
late PluginRunner pluginRunner; late PluginRunner pluginRunner;
PluginSandbox() { PluginSandbox() {
@ -14,7 +15,7 @@ class PluginSandbox {
} }
int indexOf(PluginType pluginType) { int indexOf(PluginType pluginType) {
final index = _pluginMap.keys.toList().indexWhere((ty) => ty == pluginType); final index = _pluginBuilders.keys.toList().indexWhere((ty) => ty == pluginType);
if (index == -1) { if (index == -1) {
throw PlatformException(code: '-1', message: "Can't find the flowy plugin type: $pluginType"); throw PlatformException(code: '-1', message: "Can't find the flowy plugin type: $pluginType");
} }
@ -22,18 +23,24 @@ class PluginSandbox {
} }
Plugin buildPlugin(PluginType pluginType, dynamic data) { Plugin buildPlugin(PluginType pluginType, dynamic data) {
final plugin = _pluginMap[pluginType]!.build(data); final plugin = _pluginBuilders[pluginType]!.build(data);
return plugin; return plugin;
} }
void registerPlugin(PluginType pluginType, PluginBuilder builder) { void registerPlugin(PluginType pluginType, PluginBuilder builder, {PluginConfig? config}) {
if (_pluginMap.containsKey(pluginType)) { if (_pluginBuilders.containsKey(pluginType)) {
throw PlatformException(code: '-1', message: "$pluginType was registered before"); throw PlatformException(code: '-1', message: "$pluginType was registered before");
} }
_pluginMap[pluginType] = builder; _pluginBuilders[pluginType] = builder;
if (config != null) {
_pluginConfigs[pluginType] = config;
}
} }
List<int> get supportPluginTypes => _pluginMap.keys.toList(); List<int> get supportPluginTypes => _pluginBuilders.keys.toList();
List<PluginBuilder> get builders => _pluginMap.values.toList(); List<PluginBuilder> get builders => _pluginBuilders.values.toList();
Map<PluginType, PluginConfig> get pluginConfigs => _pluginConfigs;
} }

View File

@ -23,18 +23,14 @@ extension FlowyDefaultPluginExt on DefaultPlugin {
} }
} }
bool isDefaultPlugin(PluginType pluginType) {
return DefaultPlugin.values.map((e) => e.type()).contains(pluginType);
}
class PluginLoadTask extends LaunchTask { class PluginLoadTask extends LaunchTask {
@override @override
LaunchTaskType get type => LaunchTaskType.dataProcessing; LaunchTaskType get type => LaunchTaskType.dataProcessing;
@override @override
Future<void> initialize(LaunchContext context) async { Future<void> initialize(LaunchContext context) async {
registerPlugin(builder: BlankPluginBuilder()); registerPlugin(builder: BlankPluginBuilder(), config: BlankPluginConfig());
registerPlugin(builder: TrashPluginBuilder()); registerPlugin(builder: TrashPluginBuilder(), config: TrashPluginConfig());
registerPlugin(builder: DocumentPluginBuilder()); registerPlugin(builder: DocumentPluginBuilder());
} }
} }

View File

@ -22,7 +22,12 @@ class AppBloc extends Bloc<AppEvent, AppState> {
); );
await _fetchViews(emit); await _fetchViews(emit);
}, createView: (CreateView value) async { }, createView: (CreateView value) async {
final viewOrFailed = await repo.createView(name: value.name, desc: value.desc, dataType: value.dataType); final viewOrFailed = await repo.createView(
name: value.name,
desc: value.desc,
dataType: value.dataType,
pluginType: value.pluginType,
);
viewOrFailed.fold( viewOrFailed.fold(
(view) => emit(state.copyWith( (view) => emit(state.copyWith(
latestCreatedView: view, latestCreatedView: view,
@ -96,7 +101,12 @@ class AppBloc extends Bloc<AppEvent, AppState> {
@freezed @freezed
class AppEvent with _$AppEvent { class AppEvent with _$AppEvent {
const factory AppEvent.initial() = Initial; const factory AppEvent.initial() = Initial;
const factory AppEvent.createView(String name, String desc, PluginDataType dataType) = CreateView; const factory AppEvent.createView(
String name,
String desc,
PluginDataType dataType,
PluginType pluginType,
) = CreateView;
const factory AppEvent.delete() = Delete; const factory AppEvent.delete() = Delete;
const factory AppEvent.rename(String newName) = Rename; const factory AppEvent.rename(String newName) = Rename;
const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews; const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews;

View File

@ -28,12 +28,14 @@ class AppRepository {
required String name, required String name,
required String desc, required String desc,
required PluginDataType dataType, required PluginDataType dataType,
required PluginType pluginType,
}) { }) {
final request = CreateViewPayload.create() final request = CreateViewPayload.create()
..belongToId = appId ..belongToId = appId
..name = name ..name = name
..desc = desc ..desc = desc
..dataType = dataType; ..dataType = dataType
..pluginType = pluginType;
return FolderEventCreateView(request).send(); return FolderEventCreateView(request).send();
} }

View File

@ -14,12 +14,17 @@ class BlankPluginBuilder extends PluginBuilder {
} }
@override @override
String get pluginName => "Blank"; String get name => "Blank";
@override @override
PluginType get pluginType => DefaultPlugin.blank.type(); PluginType get pluginType => DefaultPlugin.blank.type();
} }
class BlankPluginConfig implements PluginConfig {
@override
bool get creatable => false;
}
class BlankPagePlugin implements Plugin { class BlankPagePlugin implements Plugin {
final PluginType _pluginType; final PluginType _pluginType;
BlankPagePlugin({ BlankPagePlugin({
@ -32,9 +37,6 @@ class BlankPagePlugin implements Plugin {
@override @override
PluginDisplay get display => BlankPagePluginDisplay(); PluginDisplay get display => BlankPagePluginDisplay();
@override
bool get enable => true;
@override @override
String get pluginId => "BlankStack"; String get pluginId => "BlankStack";

View File

@ -25,7 +25,7 @@ import 'package:provider/provider.dart';
import 'document_page.dart'; import 'document_page.dart';
class DocumentPluginBuilder implements PluginBuilder { class DocumentPluginBuilder extends PluginBuilder {
@override @override
Plugin build(dynamic data) { Plugin build(dynamic data) {
if (data is View) { if (data is View) {
@ -36,7 +36,7 @@ class DocumentPluginBuilder implements PluginBuilder {
} }
@override @override
String get pluginName => "Doc"; String get name => "Doc";
@override @override
PluginType get pluginType => DefaultPlugin.quillEditor.type(); PluginType get pluginType => DefaultPlugin.quillEditor.type();
@ -74,9 +74,6 @@ class DocumentPlugin implements Plugin {
@override @override
PluginDisplay get display => DocumentPluginDisplay(view: _view); PluginDisplay get display => DocumentPluginDisplay(view: _view);
@override
bool get enable => true;
@override @override
PluginType get pluginType => _pluginType; PluginType get pluginType => _pluginType;

View File

@ -28,12 +28,17 @@ class TrashPluginBuilder extends PluginBuilder {
} }
@override @override
String get pluginName => "Trash"; String get name => "Trash";
@override @override
PluginType get pluginType => DefaultPlugin.trash.type(); PluginType get pluginType => DefaultPlugin.trash.type();
} }
class TrashPluginConfig implements PluginConfig {
@override
bool get creatable => false;
}
class TrashPlugin implements Plugin { class TrashPlugin implements Plugin {
final PluginType _pluginType; final PluginType _pluginType;
@ -45,9 +50,6 @@ class TrashPlugin implements Plugin {
@override @override
PluginDisplay get display => TrashPluginDisplay(); PluginDisplay get display => TrashPluginDisplay();
@override
bool get enable => true;
@override @override
String get pluginId => "TrashStack"; String get pluginId => "TrashStack";

View File

@ -44,12 +44,7 @@ class ActionList {
const ActionList({required this.anchorContext, required this.onSelected}); const ActionList({required this.anchorContext, required this.onSelected});
void show(BuildContext buildContext) { void show(BuildContext buildContext) {
final items = getIt<PluginSandbox>() final items = pluginBuilders().map(
.builders
.where(
(builder) => !isDefaultPlugin(builder.pluginType),
)
.map(
(pluginBuilder) { (pluginBuilder) {
return CreateItem( return CreateItem(
pluginBuilder: pluginBuilder, pluginBuilder: pluginBuilder,
@ -94,7 +89,7 @@ class CreateItem extends StatelessWidget {
return GestureDetector( return GestureDetector(
onTap: () => onSelected(pluginBuilder), onTap: () => onSelected(pluginBuilder),
child: FlowyText.medium( child: FlowyText.medium(
pluginBuilder.pluginName, pluginBuilder.name,
color: theme.textColor, color: theme.textColor,
fontSize: 12, fontSize: 12,
).padding(horizontal: 10, vertical: 6), ).padding(horizontal: 10, vertical: 6),

View File

@ -107,6 +107,7 @@ class MenuAppHeader extends StatelessWidget {
LocaleKeys.menuAppHeader_defaultNewPageName.tr(), LocaleKeys.menuAppHeader_defaultNewPageName.tr(),
"", "",
pluginBuilder.dataType, pluginBuilder.dataType,
pluginBuilder.pluginType,
)); ));
}, },
).padding(right: MenuAppSizes.headerPadding), ).padding(right: MenuAppSizes.headerPadding),

View File

@ -44,9 +44,11 @@ pub struct View {
pub create_time: i64, pub create_time: i64,
#[pb(index = 10)] #[pb(index = 10)]
#[serde(default)]
pub ext_data: String, pub ext_data: String,
#[pb(index = 11)] #[pb(index = 11)]
#[serde(default)]
pub thumbnail: String, pub thumbnail: String,
#[pb(index = 12)] #[pb(index = 12)]