Skip to content

Commit

Permalink
refract Pulsar Partitioned Topics to MVVM
Browse files Browse the repository at this point in the history
  • Loading branch information
shoothzj committed Jul 31, 2021
1 parent 37bbb9b commit 48aa0a5
Show file tree
Hide file tree
Showing 12 changed files with 117 additions and 161 deletions.
1 change: 1 addition & 0 deletions lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"appName": MessageLookupByLibrary.simpleMessage("Paas Dashboard"),
"clearBacklog": MessageLookupByLibrary.simpleMessage("Clear Backlog"),
"refresh": MessageLookupByLibrary.simpleMessage("Refresh")
};
}
1 change: 1 addition & 0 deletions lib/generated/intl/messages_zh.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class MessageLookup extends MessageLookupByLibrary {
final messages = _notInlinedMessages(_notInlinedMessages);
static Map<String, Function> _notInlinedMessages(_) => <String, Function>{
"appName": MessageLookupByLibrary.simpleMessage("Paas 仪表盘"),
"clearBacklog": MessageLookupByLibrary.simpleMessage("清理积压"),
"refresh": MessageLookupByLibrary.simpleMessage("刷新")
};
}
10 changes: 10 additions & 0 deletions lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"appName": "Paas Dashboard",
"refresh": "Refresh"
"refresh": "Refresh",
"clearBacklog": "Clear Backlog"
}
3 changes: 2 additions & 1 deletion lib/l10n/intl_zh.arb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"appName": "Paas 仪表盘",
"refresh": "刷新"
"refresh": "刷新",
"clearBacklog": "清理积压"
}
4 changes: 2 additions & 2 deletions lib/main.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import 'package:flutter/material.dart';
import 'package:flutter_localizations/flutter_localizations.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/route/route_gen.dart';
import 'package:paas_dashboard_flutter/ui/bk/bk_page.dart';
Expand All @@ -13,6 +12,7 @@ import 'package:paas_dashboard_flutter/vm/general/settings_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_list_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_tenant_view_model.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -75,7 +75,7 @@ class MyApp extends StatelessWidget {
return RouteGen.pulsarNamespace(args);
}
if (settings.name == PageRouteConst.PulsarTopic) {
final args = settings.arguments as TopicPageContext;
final args = settings.arguments as PulsarTopicViewModel;
return RouteGen.pulsarPartitionedTopic(args);
}
},
Expand Down
31 changes: 0 additions & 31 deletions lib/module/pulsar/pulsar_namespace.dart
Original file line number Diff line number Diff line change
@@ -1,34 +1,3 @@
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';

class PulsarNamespaceModule {
final String namespaceName;

PulsarNamespaceModule(this.namespaceName);
}

class NamespacePageContext {
final PulsarNamespaceViewModel pulsarNamespaceViewModel;
final PulsarNamespaceModule namespaceModule;

NamespacePageContext(this.pulsarNamespaceViewModel, this.namespaceModule);

String get host {
return pulsarNamespaceViewModel.host;
}

int get port {
return pulsarNamespaceViewModel.port;
}

String get tenantName {
return pulsarNamespaceViewModel.tenantName;
}

String get namespaceName {
return namespaceModule.namespaceName;
}
}

class NamespaceResp {
final String namespaceName;

Expand Down
40 changes: 5 additions & 35 deletions lib/module/pulsar/pulsar_subscription.dart
Original file line number Diff line number Diff line change
@@ -1,42 +1,12 @@
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';

class PulsarSubscriptionModule {
final String subscriptionName;

PulsarSubscriptionModule(this.subscriptionName);
}

class SubscriptionPageContext {
final TopicPageContext topicPageContext;
final PulsarSubscriptionModule subscriptionModule;

SubscriptionPageContext(this.topicPageContext, this.subscriptionModule);

String get host {
return topicPageContext.host;
}

int get port {
return topicPageContext.port;
}

String get tenantName {
return topicPageContext.tenantName;
}

String get namespaceName {
return topicPageContext.namespaceName;
}

String get topicName {
return topicPageContext.topicName;
}
}

class SubscriptionResp {
final String subscriptionName;
final int backlog;
final double rateOut;

SubscriptionResp(this.subscriptionName, this.backlog, this.rateOut);

SubscriptionResp deepCopy() {
return new SubscriptionResp(
this.subscriptionName, this.backlog, this.rateOut);
}
}
12 changes: 6 additions & 6 deletions lib/route/route_gen.dart
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/pulsar_instance.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_partitioned_topic.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/screen/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_instance_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_view_model.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_tenant_view_model.dart';
import 'package:provider/provider.dart';

Expand Down Expand Up @@ -35,11 +35,11 @@ class RouteGen {
));
}

static Route pulsarPartitionedTopic(TopicPageContext args) {
static Route pulsarPartitionedTopic(PulsarTopicViewModel viewModel) {
return MaterialPageRoute(
builder: (context) {
return PulsarPartitionedTopicScreen(args);
},
);
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: PulsarPartitionedTopicScreen(),
));
}
}
4 changes: 1 addition & 3 deletions lib/ui/pulsar/screen/pulsar_namespace.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/ui/util/alert_util.dart';
import 'package:paas_dashboard_flutter/ui/util/form_util.dart';
Expand Down Expand Up @@ -68,8 +67,7 @@ class PulsarNamespaceScreenState extends State<PulsarNamespaceScreen> {
.map((data) => DataRow(
onSelectChanged: (bool? selected) {
Navigator.pushNamed(context, PageRouteConst.PulsarTopic,
arguments: new TopicPageContext(
data, new PulsarTopicModule(data.topic)));
arguments: data.deepCopy());
},
cells: [
DataCell(
Expand Down
135 changes: 57 additions & 78 deletions lib/ui/pulsar/screen/pulsar_partitioned_topic.dart
Original file line number Diff line number Diff line change
@@ -1,94 +1,82 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_topic_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_subscription.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_topic.dart';
import 'package:paas_dashboard_flutter/generated/l10n.dart';
import 'package:paas_dashboard_flutter/ui/util/alert_util.dart';
import 'package:paas_dashboard_flutter/ui/util/spinner_util.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_partitioned_topic_view_model.dart';
import 'package:provider/provider.dart';

class PulsarPartitionedTopicScreen extends StatefulWidget {
final TopicPageContext topicPageContext;

PulsarPartitionedTopicScreen(this.topicPageContext);
PulsarPartitionedTopicScreen();

@override
State<StatefulWidget> createState() {
return new PulsarPartitionedTopicScreenState(this.topicPageContext);
return new PulsarPartitionedTopicScreenState();
}
}

class PulsarPartitionedTopicScreenState
extends State<PulsarPartitionedTopicScreen> {
final TopicPageContext topicPageContext;

PulsarPartitionedTopicScreenState(this.topicPageContext);

late Future<List<SubscriptionResp>> _func;

@override
void initState() {
loadData();
super.initState();
final vm = Provider.of<PulsarTopicViewModel>(context, listen: false);
vm.fetchSubscriptions();
}

@override
Widget build(BuildContext context) {
var topicsFuture = FutureBuilder(
future: _func,
builder: (ctx, snapshot) {
if (snapshot.hasData) {
List<SubscriptionResp> data =
snapshot.data as List<SubscriptionResp>;
return SingleChildScrollView(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text('Subscription Name')),
DataColumn(label: Text('MsgBacklog')),
DataColumn(label: Text('MsgRateOut')),
DataColumn(label: Text('Clear Backlog')),
],
rows: data
.map((data) => DataRow(cells: [
DataCell(
Text(data.subscriptionName),
),
DataCell(
Text(data.backlog.toString()),
),
DataCell(
Text(data.rateOut.toString()),
),
DataCell(TextButton(
child: Text('clear-backlog'),
onPressed: () {
PulsarTopicAPi.clearBacklog(
topicPageContext.host,
topicPageContext.port,
topicPageContext.tenantName,
topicPageContext.namespaceName,
topicPageContext.topicName,
data.subscriptionName);
setState(() {
loadData();
});
},
)),
]))
.toList()),
);
} else if (snapshot.hasError) {
return AlertUtil.create(snapshot.error, context);
}
// By default, show a loading spinner.
return SpinnerUtil.create();
});
final vm = Provider.of<PulsarTopicViewModel>(context);
if (vm.loading) {
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
SpinnerUtil.create();
});
}
if (vm.loadException != null) {
Exception ex = vm.loadException!;
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
AlertUtil.exceptionDialog(ex, context);
});
}
if (vm.opException != null) {
Exception ex = vm.opException!;
WidgetsBinding.instance!.addPostFrameCallback((timeStamp) {
AlertUtil.exceptionDialog(ex, context);
});
}
var topicsFuture = SingleChildScrollView(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text('Subscription Name')),
DataColumn(label: Text('MsgBacklog')),
DataColumn(label: Text('MsgRateOut')),
DataColumn(label: Text('Clear Backlog')),
],
rows: vm.displayList
.map((data) => DataRow(cells: [
DataCell(
Text(data.subscriptionName),
),
DataCell(
Text(data.backlog.toString()),
),
DataCell(
Text(data.rateOut.toString()),
),
DataCell(TextButton(
child: Text('clear-backlog'),
onPressed: () {
vm.clearBacklog(data.subscriptionName);
},
)),
]))
.toList()),
);
var refreshButton = TextButton(
onPressed: () {
setState(() {
loadData();
});
vm.fetchSubscriptions();
},
child: Text('Refresh'));
child: Text(S.of(context).refresh));
var body = ListView(
children: <Widget>[
Container(
Expand All @@ -100,7 +88,7 @@ class PulsarPartitionedTopicScreenState
),
),
Text(
'Partitioned Topics',
'Subscriptions',
style: TextStyle(fontSize: 22),
),
topicsFuture
Expand All @@ -114,13 +102,4 @@ class PulsarPartitionedTopicScreenState
body: body,
);
}

void loadData() {
_func = PulsarTopicAPi.getSubscription(
topicPageContext.host,
topicPageContext.port,
topicPageContext.tenantName,
topicPageContext.namespaceName,
topicPageContext.topicName);
}
}
Loading

0 comments on commit 48aa0a5

Please sign in to comment.