Skip to content

Commit

Permalink
rebase
Browse files Browse the repository at this point in the history
  • Loading branch information
vasilich6107 committed Aug 8, 2024
1 parent 4c8e8c2 commit ccace5b
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 21 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import 'dart:async';

import 'package:example/helpers.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:reactive_forms_annotations/reactive_forms_annotations.dart';

part 'product.freezed.dart';

part 'product.gform.dart';

@freezed
@Rf()
class ProductDetails<P extends Product, C extends Cart> with _$ProductDetails<P, C> {
@Rf(output: false)
class ProductDetails<P extends Product, C extends Cart>
with _$ProductDetails<P, C> {
factory ProductDetails({
@RfControl() String? description,
@Rf() Id<P, C>? id,
Expand All @@ -16,10 +18,9 @@ class ProductDetails<P extends Product, C extends Cart> with _$ProductDetails<P,
}

@freezed
@Rf()
@Rf(output: false)
@RfGroup()
class Id<P extends Product, C extends Cart>
with _$Id<P, C> {
class Id<P extends Product, C extends Cart> with _$Id<P, C> {
factory Id({
@RfControl() String? companyName,
@RfControl() String? name,
Expand All @@ -31,7 +32,7 @@ class Id<P extends Product, C extends Cart>
@freezed
class Product with _$Product {
const factory Product({
String? companyName,
String? companyName,
String? name,
}) = _Product;

Expand All @@ -41,7 +42,7 @@ class Product with _$Product {
@freezed
class Cart with _$Cart {
const factory Cart({
Product? product,
Product? product,
String? description,
}) = _Cart;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// coverage:ignore-file
// GENERATED CODE - DO NOT MODIFY BY HAND
// ignore_for_file: type=lint
// ignore_for_file:
// ignore_for_file: unused_element, deprecated_member_use, deprecated_member_use_from_same_package, use_function_type_syntax_for_parameters, unnecessary_const, avoid_init_to_null, invalid_override_different_default_values_named, prefer_expression_function_bodies, annotate_overrides, invalid_annotation_target, unnecessary_question_mark

part of 'product.dart';

Expand Down Expand Up @@ -144,6 +144,8 @@ class _ProductDetailsFormBuilderState<P extends Product, C extends Cart>
extends State<ProductDetailsFormBuilder<P, C>> {
late ProductDetailsForm<P, C> _formModel;

StreamSubscription<LogRecord>? _logSubscription;

@override
void initState() {
_formModel = ProductDetailsForm<P, C>(
Expand All @@ -155,6 +157,34 @@ class _ProductDetailsFormBuilderState<P extends Product, C extends Cart>

widget.initState?.call(context, _formModel);

_logSubscription = _logProductDetailsForm.onRecord.listen((LogRecord e) {
// use `dumpErrorToConsole` for severe messages to ensure that severe
// exceptions are formatted consistently with other Flutter examples and
// avoids printing duplicate exceptions
if (e.level >= Level.SEVERE) {
final Object? error = e.error;
FlutterError.dumpErrorToConsole(
FlutterErrorDetails(
exception: error is Exception ? error : Exception(error),
stack: e.stackTrace,
library: e.loggerName,
context: ErrorDescription(e.message),
),
);
} else {
log(
e.message,
time: e.time,
sequenceNumber: e.sequenceNumber,
level: e.level.value,
name: e.loggerName,
zone: e.zone,
error: e.error,
stackTrace: e.stackTrace,
);
}
});

super.initState();
}

Expand All @@ -170,6 +200,7 @@ class _ProductDetailsFormBuilderState<P extends Product, C extends Cart>
@override
void dispose() {
_formModel.form.dispose();
_logSubscription?.cancel();
super.dispose();
}

Expand All @@ -192,8 +223,10 @@ class _ProductDetailsFormBuilderState<P extends Product, C extends Cart>
}
}

final _logProductDetailsForm = Logger.detached('ProductDetailsForm<P, C>');

class ProductDetailsForm<P extends Product, C extends Cart>
implements FormModel<ProductDetails<P, C>> {
implements FormModel<ProductDetails<P, C>, ProductDetails<P, C>> {
ProductDetailsForm(
this.form,
this.path,
Expand Down Expand Up @@ -403,9 +436,11 @@ class ProductDetailsForm<P extends Product, C extends Cart>
final isValid = !currentForm.hasErrors && currentForm.errors.isEmpty;

if (!isValid) {
debugPrintStack(
label:
'[${path ?? 'ProductDetailsForm<P, C>'}]\n┗━ Avoid calling `model` on invalid form. Possible exceptions for non-nullable fields which should be guarded by `required` validator.');
_logProductDetailsForm.warning(
'Avoid calling `model` on invalid form.Possible exceptions for non-nullable fields which should be guarded by `required` validator.',
null,
StackTrace.current,
);
}
return ProductDetails<P, C>(description: _descriptionValue, id: _idValue);
}
Expand Down Expand Up @@ -453,6 +488,8 @@ class ProductDetailsForm<P extends Product, C extends Cart>
if (currentForm.valid) {
onValid(model);
} else {
_logProductDetailsForm.info('Errors');
_logProductDetailsForm.info('┗━━ ${form.errors}');
onNotValid?.call();
}
}
Expand Down Expand Up @@ -502,7 +539,10 @@ class ProductDetailsForm<P extends Product, C extends Cart>
disabled: false);
}

class IdForm<P extends Product, C extends Cart> implements FormModel<Id<P, C>> {
final _logIdForm = Logger.detached('IdForm<P, C>');

class IdForm<P extends Product, C extends Cart>
implements FormModel<Id<P, C>, Id<P, C>> {
IdForm(
this.form,
this.path,
Expand Down Expand Up @@ -709,9 +749,11 @@ class IdForm<P extends Product, C extends Cart> implements FormModel<Id<P, C>> {
final isValid = !currentForm.hasErrors && currentForm.errors.isEmpty;

if (!isValid) {
debugPrintStack(
label:
'[${path ?? 'IdForm<P, C>'}]\n┗━ Avoid calling `model` on invalid form. Possible exceptions for non-nullable fields which should be guarded by `required` validator.');
_logIdForm.warning(
'Avoid calling `model` on invalid form.Possible exceptions for non-nullable fields which should be guarded by `required` validator.',
null,
StackTrace.current,
);
}
return Id<P, C>(companyName: _companyNameValue, name: _nameValue);
}
Expand Down Expand Up @@ -757,6 +799,8 @@ class IdForm<P extends Product, C extends Cart> implements FormModel<Id<P, C>> {
if (currentForm.valid) {
onValid(model);
} else {
_logIdForm.info('Errors');
_logIdForm.info('┗━━ ${form.errors}');
onNotValid?.call();
}
}
Expand Down Expand Up @@ -1077,6 +1121,8 @@ class _IdFormBuilderState<P extends Product, C extends Cart>
extends State<IdFormBuilder<P, C>> {
late IdForm<P, C> _formModel;

StreamSubscription<LogRecord>? _logSubscription;

@override
void initState() {
_formModel = IdForm<P, C>(IdForm.formElements<P, C>(widget.model), null);
Expand All @@ -1087,6 +1133,34 @@ class _IdFormBuilderState<P extends Product, C extends Cart>

widget.initState?.call(context, _formModel);

_logSubscription = _logIdForm.onRecord.listen((LogRecord e) {
// use `dumpErrorToConsole` for severe messages to ensure that severe
// exceptions are formatted consistently with other Flutter examples and
// avoids printing duplicate exceptions
if (e.level >= Level.SEVERE) {
final Object? error = e.error;
FlutterError.dumpErrorToConsole(
FlutterErrorDetails(
exception: error is Exception ? error : Exception(error),
stack: e.stackTrace,
library: e.loggerName,
context: ErrorDescription(e.message),
),
);
} else {
log(
e.message,
time: e.time,
sequenceNumber: e.sequenceNumber,
level: e.level.value,
name: e.loggerName,
zone: e.zone,
error: e.error,
stackTrace: e.stackTrace,
);
}
});

super.initState();
}

Expand All @@ -1102,6 +1176,7 @@ class _IdFormBuilderState<P extends Product, C extends Cart>
@override
void dispose() {
_formModel.form.dispose();
_logSubscription?.cancel();
super.dispose();
}

Expand Down

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

0 comments on commit ccace5b

Please sign in to comment.