Skip to content

Commit

Permalink
refract Pulsar Tenant(Aka Namespaces) page to MVVM
Browse files Browse the repository at this point in the history
  • Loading branch information
shoothzj committed Jul 31, 2021
1 parent d4002a6 commit c2d9112
Show file tree
Hide file tree
Showing 12 changed files with 221 additions and 131 deletions.
4 changes: 2 additions & 2 deletions lib/main.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/module/pulsar/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.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';
Expand All @@ -10,6 +9,7 @@ import 'package:paas_dashboard_flutter/ui/pulsar/pulsar_page.dart';
import 'package:paas_dashboard_flutter/vm/bk/bk_instance_list_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_tenant_view_model.dart';
import 'package:provider/provider.dart';

void main() {
Expand Down Expand Up @@ -52,7 +52,7 @@ class MyApp extends StatelessWidget {
return RouteGen.pulsarInstance(args);
}
if (settings.name == PageRouteConst.PulsarTenant) {
final args = settings.arguments as TenantPageContext;
final args = settings.arguments as PulsarTenantViewModel;
return RouteGen.pulsarTenant(args);
}
if (settings.name == PageRouteConst.PulsarNamespace) {
Expand Down
16 changes: 10 additions & 6 deletions lib/module/pulsar/pulsar_namespace.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_namespace_view_model.dart';

class PulsarNamespaceModule {
final String namespaceName;
Expand All @@ -7,21 +7,21 @@ class PulsarNamespaceModule {
}

class NamespacePageContext {
final TenantPageContext tenantPageContext;
final PulsarNamespaceViewModel pulsarNamespaceViewModel;
final PulsarNamespaceModule namespaceModule;

NamespacePageContext(this.tenantPageContext, this.namespaceModule);
NamespacePageContext(this.pulsarNamespaceViewModel, this.namespaceModule);

String get host {
return tenantPageContext.host;
return pulsarNamespaceViewModel.host;
}

int get port {
return tenantPageContext.port;
return pulsarNamespaceViewModel.port;
}

String get tenantName {
return tenantPageContext.tenantModule.tenantName;
return pulsarNamespaceViewModel.tenantName;
}

String get namespaceName {
Expand All @@ -34,6 +34,10 @@ class NamespaceResp {

NamespaceResp(this.namespaceName);

NamespaceResp deepCopy() {
return new NamespaceResp(namespaceName);
}

factory NamespaceResp.fromJson(String name) {
var split = name.split("/");
return NamespaceResp(split[split.length - 1]);
Expand Down
22 changes: 4 additions & 18 deletions lib/module/pulsar/pulsar_tenant.dart
Original file line number Diff line number Diff line change
@@ -1,26 +1,12 @@
class PulsarTenantModule {
final String tenantName;

PulsarTenantModule(this.tenantName);
}

class TenantPageContext {
final String host;
final int port;
final PulsarTenantModule tenantModule;

TenantPageContext(this.host, this.port, this.tenantModule);

String get tenantName {
return tenantModule.tenantName;
}
}

class TenantResp {
final String tenantName;

TenantResp(this.tenantName);

TenantResp deepCopy() {
return new TenantResp(this.tenantName);
}

factory TenantResp.fromJson(String name) {
return TenantResp(name);
}
Expand Down
6 changes: 3 additions & 3 deletions lib/module/pulsar/pulsar_topic.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ class TopicPageContext {
TopicPageContext(this.namespacePageContext, this.topicModule);

String get host {
return namespacePageContext.tenantPageContext.host;
return namespacePageContext.host;
}

int get port {
return namespacePageContext.tenantPageContext.port;
return namespacePageContext.port;
}

String get tenantName {
return namespacePageContext.tenantPageContext.tenantModule.tenantName;
return namespacePageContext.tenantName;
}

String get namespaceName {
Expand Down
14 changes: 7 additions & 7 deletions lib/route/route_gen.dart
Original file line number Diff line number Diff line change
@@ -1,30 +1,30 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.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_tenant_view_model.dart';
import 'package:provider/provider.dart';

class RouteGen {
static Route pulsarInstance(PulsarInstanceViewModel viewModel) {
// deep copy view model
return MaterialPageRoute(
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel.deepCopy(),
create: (context) => viewModel,
child: PulsarInstanceScreen(),
));
}

static Route pulsarTenant(TenantPageContext args) {
static Route pulsarTenant(PulsarTenantViewModel viewModel) {
return MaterialPageRoute(
builder: (context) {
return PulsarTenantScreen(args);
},
);
builder: (context) => ChangeNotifierProvider(
create: (context) => viewModel,
child: PulsarTenantScreen(),
));
}

static Route pulsarNamespace(NamespacePageContext args) {
Expand Down
6 changes: 2 additions & 4 deletions lib/ui/pulsar/pulsar_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class _PulsarPageState extends State<PulsarPage> {
onSelectChanged: (bool? selected) {
Navigator.pushNamed(
context, PageRouteConst.PulsarInstance,
arguments: itemRow);
arguments: itemRow.deepCopy());
},
cells: [
DataCell(Text(itemRow.id.toString())),
Expand All @@ -57,9 +57,7 @@ class _PulsarPageState extends State<PulsarPage> {
var formButton = createInstanceButton(context);
var refreshButton = TextButton(
onPressed: () {
setState(() {
vm.fetchPulsarInstances();
});
vm.fetchPulsarInstances();
},
child: Text('Refresh'));
var body = ListView(
Expand Down
129 changes: 55 additions & 74 deletions lib/ui/pulsar/screen/pulsar_tenant.dart
Original file line number Diff line number Diff line change
@@ -1,45 +1,54 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/api/pulsar/pulsar_namespace_api.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_namespace.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.dart';
import 'package:paas_dashboard_flutter/route/page_route_const.dart';
import 'package:paas_dashboard_flutter/ui/pulsar/pulsar_const.dart';
import 'package:paas_dashboard_flutter/ui/util/alert_util.dart';
import 'package:paas_dashboard_flutter/ui/util/form_util.dart';
import 'package:paas_dashboard_flutter/ui/util/spinner_util.dart';
import 'package:paas_dashboard_flutter/vm/pulsar/pulsar_tenant_view_model.dart';
import 'package:provider/provider.dart';

class PulsarTenantScreen extends StatefulWidget {
final TenantPageContext tenantPageContext;

PulsarTenantScreen(this.tenantPageContext);
PulsarTenantScreen();

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

class PulsarTenantScreenState extends State<PulsarTenantScreen> {
final TenantPageContext tenantPageContext;

late Future<List<NamespaceResp>> _func;

PulsarTenantScreenState(this.tenantPageContext);

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

@override
Widget build(BuildContext context) {
final vm = Provider.of<PulsarTenantViewModel>(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 formButton = createNamespace(context);
var refreshButton = TextButton(
onPressed: () {
setState(() {
loadData();
});
vm.fetchNamespaces();
},
child: Text('Refresh'));
var listView = ListView(
Expand All @@ -56,52 +65,34 @@ class PulsarTenantScreenState extends State<PulsarTenantScreen> {
'Namespaces',
style: TextStyle(fontSize: 22),
),
FutureBuilder(
future: _func,
builder: (ctx, snapshot) {
if (snapshot.hasData) {
List<NamespaceResp> data = snapshot.data as List<NamespaceResp>;
return SingleChildScrollView(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text('Namespace Name')),
DataColumn(label: Text('Delete namespace')),
],
rows: data
.map((data) => DataRow(
onSelectChanged: (bool? selected) {
Navigator.pushNamed(
context, PageRouteConst.PulsarNamespace,
arguments: new NamespacePageContext(
tenantPageContext,
new PulsarNamespaceModule(
data.namespaceName)));
},
cells: [
DataCell(
Text(data.namespaceName),
),
DataCell(TextButton(
child: Text('Delete'),
onPressed: () {
PulsarNamespaceAPi.deleteNamespace(
tenantPageContext.host,
tenantPageContext.port,
tenantPageContext.tenantName,
data.namespaceName);
loadData();
},
)),
]))
.toList()),
);
} else if (snapshot.hasError) {
return AlertUtil.create(snapshot.error, context);
}
// By default, show a loading spinner.
return SpinnerUtil.create();
})
SingleChildScrollView(
child: DataTable(
showCheckboxColumn: false,
columns: [
DataColumn(label: Text('Namespace Name')),
DataColumn(label: Text('Delete namespace')),
],
rows: vm.namespaces
.map((data) => DataRow(
onSelectChanged: (bool? selected) {
Navigator.pushNamed(
context, PageRouteConst.PulsarNamespace,
arguments: new NamespacePageContext(data,
new PulsarNamespaceModule(data.namespace)));
},
cells: [
DataCell(
Text(data.namespace),
),
DataCell(TextButton(
child: Text('Delete'),
onPressed: () {
vm.deleteNamespace(data.namespace);
},
)),
]))
.toList()),
),
],
);
return Scaffold(
Expand All @@ -112,22 +103,12 @@ class PulsarTenantScreenState extends State<PulsarTenantScreen> {
);
}

loadData() {
_func = PulsarNamespaceAPi.getNamespaces(tenantPageContext.host,
tenantPageContext.port, tenantPageContext.tenantName);
}

ButtonStyleButton createNamespace(BuildContext context) {
var list = [FormFieldDef('Namespace Name')];
return FormUtil.createButton1("Pulsar Namespace", list, context,
(namespace) async {
try {
await PulsarNamespaceAPi.createNamespace(tenantPageContext.host,
tenantPageContext.port, tenantPageContext.tenantName, namespace);
loadData();
} on Exception catch (e) {
AlertUtil.exceptionDialog(e, context);
}
final vm = Provider.of<PulsarTenantViewModel>(context, listen: false);
vm.createNamespace(namespace);
});
}
}
6 changes: 1 addition & 5 deletions lib/ui/pulsar/tab/pulsar_details.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'package:flutter/material.dart';
import 'package:paas_dashboard_flutter/module/pulsar/pulsar_tenant.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 @@ -76,12 +75,9 @@ class PulsarTenantsState extends State<PulsarTenantsWidget> {
rows: vm.tenants
.map((data) => DataRow(
onSelectChanged: (bool? selected) {
String host = vm.host;
int port = vm.port;
Navigator.pushNamed(
context, PageRouteConst.PulsarTenant,
arguments: new TenantPageContext(host, port,
new PulsarTenantModule(data.tenantName)));
arguments: data.deepCopy());
},
cells: [
DataCell(
Expand Down
9 changes: 9 additions & 0 deletions lib/vm/base_load_view_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import 'package:flutter/material.dart';

class BaseLoadViewModel extends ChangeNotifier {
bool loading = true;

Exception? loadException;

Exception? opException;
}
Loading

0 comments on commit c2d9112

Please sign in to comment.