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;
bool get enable;
void dispose();
PluginDisplay get display;
@ -27,13 +25,17 @@ abstract class Plugin {
abstract class PluginBuilder {
Plugin build(dynamic data);
String get pluginName;
String get name;
PluginType get pluginType;
ViewDataType get dataType => ViewDataType.PlainText;
}
abstract class PluginConfig {
bool get creatable => true;
}
abstract class PluginDisplay with NavigationItem {
@override
Widget get leftBarItem;
@ -46,8 +48,8 @@ abstract class PluginDisplay with NavigationItem {
Widget buildWidget();
}
void registerPlugin({required PluginBuilder builder}) {
getIt<PluginSandbox>().registerPlugin(builder.pluginType, builder);
void registerPlugin({required PluginBuilder builder, PluginConfig? config}) {
getIt<PluginSandbox>().registerPlugin(builder.pluginType, builder, config: config);
}
Plugin makePlugin({required PluginType pluginType, dynamic data}) {
@ -55,6 +57,18 @@ Plugin makePlugin({required PluginType pluginType, dynamic data}) {
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 {
invalidData,
}

View File

@ -6,7 +6,8 @@ import '../plugin.dart';
import 'runner.dart';
class PluginSandbox {
final LinkedHashMap<PluginType, PluginBuilder> _pluginMap = LinkedHashMap();
final LinkedHashMap<PluginType, PluginBuilder> _pluginBuilders = LinkedHashMap();
final Map<PluginType, PluginConfig> _pluginConfigs = <PluginType, PluginConfig>{};
late PluginRunner pluginRunner;
PluginSandbox() {
@ -14,7 +15,7 @@ class PluginSandbox {
}
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) {
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) {
final plugin = _pluginMap[pluginType]!.build(data);
final plugin = _pluginBuilders[pluginType]!.build(data);
return plugin;
}
void registerPlugin(PluginType pluginType, PluginBuilder builder) {
if (_pluginMap.containsKey(pluginType)) {
void registerPlugin(PluginType pluginType, PluginBuilder builder, {PluginConfig? config}) {
if (_pluginBuilders.containsKey(pluginType)) {
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 {
@override
LaunchTaskType get type => LaunchTaskType.dataProcessing;
@override
Future<void> initialize(LaunchContext context) async {
registerPlugin(builder: BlankPluginBuilder());
registerPlugin(builder: TrashPluginBuilder());
registerPlugin(builder: BlankPluginBuilder(), config: BlankPluginConfig());
registerPlugin(builder: TrashPluginBuilder(), config: TrashPluginConfig());
registerPlugin(builder: DocumentPluginBuilder());
}
}

View File

@ -22,7 +22,12 @@ class AppBloc extends Bloc<AppEvent, AppState> {
);
await _fetchViews(emit);
}, 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(
(view) => emit(state.copyWith(
latestCreatedView: view,
@ -96,7 +101,12 @@ class AppBloc extends Bloc<AppEvent, AppState> {
@freezed
class AppEvent with _$AppEvent {
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.rename(String newName) = Rename;
const factory AppEvent.didReceiveViews(List<View> views) = ReceiveViews;

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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