plugin_framework, plugin manager

Signed-off-by: fufesou <shuanglongchen@yeah.net>
This commit is contained in:
fufesou
2023-04-22 22:21:02 +08:00
parent b1b867dd9d
commit ae789ff5f1
7 changed files with 421 additions and 70 deletions

View File

@@ -10,6 +10,8 @@ import 'package:flutter_hbb/desktop/pages/desktop_home_page.dart';
import 'package:flutter_hbb/desktop/pages/desktop_tab_page.dart';
import 'package:flutter_hbb/models/platform_model.dart';
import 'package:flutter_hbb/models/server_model.dart';
import 'package:flutter_hbb/plugin/desc.dart';
import 'package:flutter_hbb/plugin/model.dart';
import 'package:get/get.dart';
import 'package:provider/provider.dart';
import 'package:url_launcher/url_launcher.dart';
@@ -71,16 +73,6 @@ class DesktopSettingPage extends StatefulWidget {
class _DesktopSettingPageState extends State<DesktopSettingPage>
with TickerProviderStateMixin, AutomaticKeepAliveClientMixin {
final List<_TabInfo> settingTabs = <_TabInfo>[
_TabInfo('General', Icons.settings_outlined, Icons.settings),
_TabInfo('Security', Icons.enhanced_encryption_outlined,
Icons.enhanced_encryption),
_TabInfo('Network', Icons.link_outlined, Icons.link),
_TabInfo('Display', Icons.desktop_windows_outlined, Icons.desktop_windows),
_TabInfo('Account', Icons.person_outline, Icons.person),
_TabInfo('About', Icons.info_outline, Icons.info)
];
late PageController controller;
late RxInt selectedIndex;
@@ -104,6 +96,39 @@ class _DesktopSettingPageState extends State<DesktopSettingPage>
Get.delete<RxInt>(tag: _kSettingPageIndexTag);
}
List<_TabInfo> _settingTabs() {
final List<_TabInfo> settingTabs = <_TabInfo>[
_TabInfo('General', Icons.settings_outlined, Icons.settings),
_TabInfo('Security', Icons.enhanced_encryption_outlined,
Icons.enhanced_encryption),
_TabInfo('Network', Icons.link_outlined, Icons.link),
_TabInfo(
'Display', Icons.desktop_windows_outlined, Icons.desktop_windows),
_TabInfo('Account', Icons.person_outline, Icons.person),
_TabInfo('About', Icons.info_outline, Icons.info)
];
if (bind.pluginFeatureIsEnabled()) {
settingTabs.insert(
4, _TabInfo('Plugin', Icons.extension_outlined, Icons.extension));
}
return settingTabs;
}
List<Widget> _children() {
final children = [
_General(),
_Safety(),
_Network(),
_Display(),
_Account(),
_About(),
];
if (bind.pluginFeatureIsEnabled()) {
children.insert(4, _Plugin());
}
return children;
}
@override
Widget build(BuildContext context) {
super.build(context);
@@ -116,7 +141,7 @@ class _DesktopSettingPageState extends State<DesktopSettingPage>
child: Column(
children: [
_header(),
Flexible(child: _listView(tabs: settingTabs)),
Flexible(child: _listView(tabs: _settingTabs())),
],
),
),
@@ -129,14 +154,7 @@ class _DesktopSettingPageState extends State<DesktopSettingPage>
child: PageView(
controller: controller,
physics: DraggableNeverScrollableScrollPhysics(),
children: const [
_General(),
_Safety(),
_Network(),
_Display(),
_Account(),
_About(),
],
children: _children(),
)),
),
)
@@ -1376,6 +1394,98 @@ class _AccountState extends State<_Account> {
}
}
class _Plugin extends StatefulWidget {
const _Plugin({Key? key}) : super(key: key);
@override
State<_Plugin> createState() => _PluginState();
}
class _PluginState extends State<_Plugin> {
// temp checkbox widget
Widget _checkbox(
String label,
bool Function() getValue,
Future<void> Function(bool) setValue,
) {
final value = getValue();
onChanged(bool b) async {
await setValue(b);
setState(() {});
}
return GestureDetector(
child: Row(
children: [
Checkbox(
value: bind.pluginIsEnabled(),
onChanged: (_) => onChanged(!value),
).marginOnly(right: 5),
Expanded(
child: Text(translate(label)),
)
],
).marginOnly(left: _kCheckBoxLeftMargin),
onTap: () => onChanged(!value));
}
@override
Widget build(BuildContext context) {
final scrollController = ScrollController();
buildCards(DescModel model) {
final cards = <Widget>[
_Card(title: 'Plugin', children: [
_checkbox('Enable', bind.pluginIsEnabled, (bool v) async {
if (!v) {
clearLocations();
}
await bind.pluginEnable(v: v);
}),
]),
];
model.all.forEach((key, value) {
cards.add(_Card(title: key, children: [
_Button('Reload', () {
bind.pluginReload(id: key);
}),
_checkbox('Enable', () => bind.pluginIdIsEnabled(id: key),
(bool v) async {
if (!v) {
clearPlugin(key);
}
await bind.pluginIdEnable(id: key, v: v);
}),
]));
});
return cards;
}
return DesktopScrollWrapper(
scrollController: scrollController,
child: ChangeNotifierProvider.value(
value: DescModel.instance,
child: Consumer<DescModel>(builder: (context, model, child) {
return ListView(
physics: DraggableNeverScrollableScrollPhysics(),
controller: scrollController,
children: buildCards(model),
).marginOnly(bottom: _kListViewBottomMargin);
}),
),
);
}
Widget accountAction() {
return Obx(() => _Button(
gFFI.userModel.userName.value.isEmpty ? 'Login' : 'Logout',
() => {
gFFI.userModel.userName.value.isEmpty
? loginDialog()
: gFFI.userModel.logOut()
}));
}
}
class _About extends StatefulWidget {
const _About({Key? key}) : super(key: key);

View File

@@ -1,4 +1,5 @@
import 'dart:collection';
import 'package:flutter/foundation.dart';
const String kValueTrue = '1';
const String kValueFalse = '0';
@@ -154,13 +155,27 @@ class Desc {
.toList());
}
final mapPluginDesc = <String, Desc>{};
class DescModel with ChangeNotifier {
final data = <String, Desc>{};
void updateDesc(Map<String, dynamic> desc) {
Desc d = Desc.fromJson(desc);
mapPluginDesc[d.id] = d;
DescModel._();
void _updateDesc(Map<String, dynamic> desc) {
Desc d = Desc.fromJson(desc);
data[d.id] = d;
notifyListeners();
}
Desc? _getDesc(String id) {
return data[id];
}
Map<String, Desc> get all => data;
static final DescModel _instance = DescModel._();
static DescModel get instance => _instance;
}
Desc? getDesc(String id) {
return mapPluginDesc[id];
}
void updateDesc(Map<String, dynamic> desc) =>
DescModel.instance._updateDesc(desc);
Desc? getDesc(String id) => DescModel.instance._getDesc(id);

View File

@@ -2,8 +2,8 @@ import 'package:flutter/material.dart';
import './common.dart';
import './desc.dart';
final Map<String, LocationModel> locationModels = {};
final Map<String, OptionModel> optionModels = {};
final Map<String, LocationModel> _locationModels = {};
final Map<String, OptionModel> _optionModels = {};
class OptionModel with ChangeNotifier {
String? v;
@@ -46,31 +46,48 @@ class LocationModel with ChangeNotifier {
}
}
void clear() {
pluginModels.clear();
notifyListeners();
}
bool get isEmpty => pluginModels.isEmpty;
}
void addLocationUi(String location, PluginId id, UiType ui) {
locationModels[location]?.add(id, ui);
_locationModels[location]?.add(id, ui);
}
LocationModel addLocation(String location) {
if (locationModels[location] == null) {
locationModels[location] = LocationModel();
if (_locationModels[location] == null) {
_locationModels[location] = LocationModel();
}
return _locationModels[location]!;
}
void clearPlugin(PluginId pluginId) {
for (var element in _locationModels.values) {
element.pluginModels.remove(pluginId);
}
}
void clearLocations() {
for (var element in _locationModels.values) {
element.clear();
}
return locationModels[location]!;
}
OptionModel addOptionModel(
String location, PluginId pluginId, String peer, String key) {
final k = OptionModel.key(location, pluginId, peer, key);
if (optionModels[k] == null) {
optionModels[k] = OptionModel();
if (_optionModels[k] == null) {
_optionModels[k] = OptionModel();
}
return optionModels[k]!;
return _optionModels[k]!;
}
void updateOption(
String location, PluginId id, String peer, String key, String value) {
final k = OptionModel.key(location, id, peer, key);
optionModels[k]?.value = value;
_optionModels[k]?.value = value;
}