Skip to content

Commit

Permalink
Update dependencies, refactor error view and add new network plugin
Browse files Browse the repository at this point in the history
This commit updates the project's dependencies, refactors how error views are managed and adds a new HTTP network plugin. It also adjusts some file and variable names for improved clarity. Coding conventions and organization have been improved for greater code readability and maintainability. Additionally, several functionalities have been enhanced, such as actions now being able to return Future or void.
  • Loading branch information
pavanpodila committed Mar 27, 2024
1 parent cd38563 commit 46961a6
Show file tree
Hide file tree
Showing 30 changed files with 540 additions and 57 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:vyuh_core/vyuh_core.dart';

final class HttpNetworkPlugin extends NetworkPlugin {
late Client _client;
var _initialized = false;

HttpNetworkPlugin()
: super(name: 'vyuh.plugin.network.http', title: 'HTTP Network Plugin');

@override
Future<void> init() async {
if (_initialized) {
return;
}

_client = Client();
_initialized = true;
}

@override
Future<void> dispose() async {
if (!_initialized) {
return;
}

_client.close();
_initialized = false;
}

@override
Future<Response> get(Uri url, {Map<String, String>? headers}) =>
_client.get(url, headers: headers);

@override
Future<Response> head(Uri url, {Map<String, String>? headers}) =>
_client.head(url, headers: headers);

@override
Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_client.post(url, headers: headers, body: body, encoding: encoding);

@override
Future<Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_client.put(url, headers: headers, body: body, encoding: encoding);

@override
Future<Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_client.delete(url, headers: headers, body: body, encoding: encoding);

@override
Future<Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding}) =>
_client.patch(url, headers: headers, body: body, encoding: encoding);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import 'dart:convert';

import 'package:http/http.dart';
import 'package:vyuh_core/vyuh_core.dart';

abstract base class NetworkPlugin extends Plugin {
NetworkPlugin({required super.name, required super.title})
: super(pluginType: PluginType.network);

Future<Response> get(Uri url, {Map<String, String>? headers});

Future<Response> head(Uri url, {Map<String, String>? headers});

Future<Response> post(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding});

Future<Response> put(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding});

Future<Response> delete(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding});

Future<Response> patch(Uri url,
{Map<String, String>? headers, Object? body, Encoding? encoding});
}
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ final class DefaultVyuhPlatform extends VyuhPlatform {
debugLogDiagnostics: kDebugMode,
observers: analytics.observers,
errorBuilder: (_, state) => widgetBuilder.routeErrorView(
path: state.matchedLocation,
title: 'Failed to load route',
subtitle: state.matchedLocation,
error: state.error,
onRetry: () {
vyuh.tracker.init(tracker.currentState.value);
Expand Down Expand Up @@ -230,6 +230,7 @@ extension on PluginType {
AnalyticsPlugin(providers: [NoOpAnalyticsProvider()]),
PluginType.content => NoOpContentPlugin(),
PluginType.di => GetItDIPlugin(),
PluginType.network => HttpNetworkPlugin(),
_ => null
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,15 @@ final defaultPlatformWidgetBuilder = PlatformWidgetBuilder(
error: error,
retryLabel: retryLabel,
onRetry: onRetry,
showRestart: showRestart,
),
routeErrorView: ({
required path,
required title,
onRetry,
retryLabel,
subtitle,
error,
}) =>
ErrorView(
ErrorViewScaffold(
title: title,
subtitle: subtitle,
error: error,
Expand Down
78 changes: 76 additions & 2 deletions packages/system/vyuh_core/lib/runtime/platform/error_view.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import 'package:flutter/material.dart';
import 'package:vyuh_core/vyuh_core.dart';

class ErrorView extends StatelessWidget {
class ErrorViewScaffold extends StatelessWidget {
final String title;
final String? subtitle;
final dynamic error;
Expand All @@ -10,7 +10,7 @@ class ErrorView extends StatelessWidget {

final String? retryLabel;

const ErrorView({
const ErrorViewScaffold({
super.key,
this.title = 'Something is not right!',
this.subtitle,
Expand Down Expand Up @@ -42,6 +42,7 @@ class ErrorView extends StatelessWidget {
color: textColor,
size: 64,
),
Text(title, textAlign: TextAlign.center),
if (subtitle != null)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
Expand Down Expand Up @@ -90,3 +91,76 @@ class ErrorView extends StatelessWidget {
);
}
}

class ErrorView extends StatelessWidget {
final String title;
final String? subtitle;
final dynamic error;
final VoidCallback? onRetry;
final String? retryLabel;

const ErrorView({
super.key,
this.title = 'Something is not right!',
this.subtitle,
this.error,
this.onRetry,
this.retryLabel,
});

@override
Widget build(BuildContext context) {
final theme = Theme.of(context);

final textColor = theme.colorScheme.onSurface;
return ConstrainedBox(
constraints: const BoxConstraints(minHeight: 100, maxHeight: 200),
child: Card(
child: Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: [
Icon(
Icons.hide_source_rounded,
color: textColor,
size: 32,
),
Text(title, textAlign: TextAlign.center),
if (subtitle != null)
Padding(
padding: const EdgeInsets.symmetric(vertical: 8.0),
child: Text(subtitle!,
textAlign: TextAlign.center,
style:
theme.textTheme.titleMedium?.apply(color: textColor)),
),
Expanded(
child: SingleChildScrollView(
padding: const EdgeInsets.symmetric(horizontal: 8),
child: Column(
children: [
if (error != null)
Text(
error.toString(),
style: theme.textTheme.bodyMedium?.apply(
fontFamily: 'Courier',
fontWeightDelta: 2,
fontSizeDelta: 1,
color: textColor,
),
),
],
),
)),
if (onRetry != null)
Padding(
padding: const EdgeInsets.only(left: 20.0, right: 20, top: 20),
child: FilledButton(
onPressed: onRetry, child: Text(retryLabel ?? 'Retry')),
),
],
),
),
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,13 @@ class _FrameworkInitViewState extends State<FrameworkInitView> {

final child = status == null || status == FutureStatus.pending
? vyuh.widgetBuilder.appLoader()
: vyuh.widgetBuilder.errorView(
: vyuh.widgetBuilder.routeErrorView(
title: 'Failed to load app',
error: vyuh.tracker.error,
retryLabel: 'Try Again',
onRetry: () {
vyuh.tracker.init();
},
showRestart: false,
);
final pendingApp = MaterialApp(home: child);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ abstract class VyuhPlatform {
PluginType.content,
PluginType.di,
PluginType.analytics,
PluginType.network,
// PluginType.storage,
];

Expand Down Expand Up @@ -58,6 +59,8 @@ extension NamedPlugins on VyuhPlatform {
AnalyticsPlugin get analytics =>
ensurePlugin<AnalyticsPlugin>(PluginType.analytics);

NetworkPlugin get network => ensurePlugin<NetworkPlugin>(PluginType.network);

T ensurePlugin<T>(PluginType type, {bool mustExist = true}) {
final plugin = getPlugin(type) as T;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ typedef RouteLoader = Widget Function([Uri? url, String? routeId]);
typedef ImagePlaceholderBuilder = Widget Function(
{double? width, double? height});
typedef RouteErrorViewBuilder = Widget Function({
required String path,
required String title,
String? retryLabel,
VoidCallback? onRetry,
Expand Down
10 changes: 6 additions & 4 deletions packages/system/vyuh_core/lib/vyuh_core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,16 @@ export 'feature_descriptor.dart';
export 'plugin_types/analytics/analytics_plugin.dart';
export 'plugin_types/analytics/analytics_provider.dart';
export 'plugin_types/analytics/noop_analytics_provider.dart';
export 'plugin_types/console_logger_plugin.dart';
export 'plugin_types/content/content_plugin.dart';
export 'plugin_types/content/content_provider.dart';
export 'plugin_types/content/noop_content_provider.dart';
export 'plugin_types/di_plugin.dart';
export 'plugin_types/logger_plugin.dart';
export 'plugin_types/di/di_plugin.dart';
export 'plugin_types/di/plugin_di_get_it.dart';
export 'plugin_types/logger/console_logger_plugin.dart';
export 'plugin_types/logger/logger_plugin.dart';
export 'plugin_types/network/http_network_plugin.dart';
export 'plugin_types/network/network_plugin.dart';
export 'plugin_types/plugin.dart';
export 'plugin_types/plugin_di_get_it.dart';
export 'runtime/cms_route.dart';
export 'runtime/init_tracker.dart';
export 'runtime/platform/default_platform_widget_builder.dart';
Expand Down
1 change: 1 addition & 0 deletions packages/system/vyuh_core/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ dependencies:
get_it: ^7.6.4
logger: ^2.1.0
flutter_sanity_portable_text: ^1.0.0-beta.4
http: ^1.2.1

dev_dependencies:
flutter_test:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:async';

import 'package:flutter/material.dart';
import 'package:json_annotation/json_annotation.dart';
import 'package:vyuh_core/vyuh_core.dart';
Expand All @@ -14,9 +16,9 @@ final class Action {
static configurationList(dynamic json) =>
listFromJson<ActionConfiguration>(json);

void execute(BuildContext context) {
FutureOr<void> execute(BuildContext context) async {
for (final config in configurations ?? []) {
config.execute(context);
await config.execute(context);
}
}

Expand All @@ -37,5 +39,5 @@ abstract class ActionConfiguration {
this.title,
});

void execute(BuildContext context);
FutureOr<void> execute(BuildContext context);
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,22 +24,30 @@ final class ContentExtensionBuilder extends ExtensionBuilder {
.expand((element) => element.contents ?? <ContentDescriptor>[])
.groupListsBy((element) => element.schemaType);

// Collect the builders
for (final entry in contentBuilders.entries) {
assert(entry.value.length == 1,
'There can be only one ContentBuilder for a content-type. We found ${entry.value.length} for ${entry.key}');

_contentBuilderMap[entry.key] = entry.value.first;
}

// Ensure every ContentDescriptor has a ContentBuilder
for (final entry in contents.entries) {
final schemaType = entry.key;
final descriptors = entry.value;
final builder = _contentBuilderMap[schemaType];

assert(builder != null,
'Missing ContentBuilder for ContentDescriptor of schemaType: $schemaType');
}

// Setup the builders
for (final entry in _contentBuilderMap.entries) {
final schemaType = entry.key;
final builder = entry.value;
final descriptors = contents[schemaType] ?? [];

builder?.init(descriptors);
builder.init(descriptors);
}

_initTypeRegistrations(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ class _RouteFutureBuilderState extends State<RouteFutureBuilder> {

vyuh.analytics.reportError(exception);

return vyuh.widgetBuilder.errorView(
return vyuh.widgetBuilder.routeErrorView(
title: 'Failed to load route from CMS',
error: exception,
onRetry: _refresh,
Expand All @@ -88,7 +88,7 @@ class _RouteFutureBuilderState extends State<RouteFutureBuilder> {
case FutureStatus.rejected:
vyuh.analytics.reportError(_tracker.value?.error);

return vyuh.widgetBuilder.errorView(
return vyuh.widgetBuilder.routeErrorView(
title: errorMsg,
error: _tracker.value?.error,
onRetry: _refresh,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class ConditionalAction extends ActionConfiguration {
_$ConditionalActionFromJson(json);

@override
void execute(flutter.BuildContext context) async {
Future<void> execute(flutter.BuildContext context) async {
final value = (await condition?.execute()) ?? defaultCase;

if (context.mounted) {
Expand Down
Loading

0 comments on commit 46961a6

Please sign in to comment.