Skip to content

Commit

Permalink
Refactor several classes for code readability and upgrade package ver…
Browse files Browse the repository at this point in the history
…sions

This commit refactors multiple classes in the mobx, flutter_mobx, and mobx_example libraries to simplify and improve parameter passing. It also upgrades the versions of mobx and flutter_mobx packages. JSON serialization behaviour in the Todo List example of mobx_example is updated to exclude certain attributes from toJSON and fromJSON methods.
  • Loading branch information
pavanpodila committed Mar 28, 2024
1 parent 13178f1 commit af5bf90
Show file tree
Hide file tree
Showing 25 changed files with 64 additions and 79 deletions.
4 changes: 4 additions & 0 deletions flutter_mobx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.2.1+1

- Analyzer fixes

## 2.2.1

- Upgrading packages and sdk
Expand Down
6 changes: 3 additions & 3 deletions flutter_mobx/lib/src/multi_reaction_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,8 @@ import 'package:provider/provider.dart';
class MultiReactionBuilder extends MultiProvider {
/// {@macro multi_reaction_builder}
MultiReactionBuilder({
Key? key,
super.key,
required List<ReactionBuilder> builders,
required Widget child,
}) : super(key: key, providers: builders, child: child);
required Widget super.child,
}) : super(providers: builders);
}
26 changes: 8 additions & 18 deletions flutter_mobx/lib/src/observer.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,36 +19,26 @@ bool debugAddStackTraceInObserverName = true;
class Observer extends StatelessObserverWidget {
// ignore: prefer_const_constructors_in_immutables
Observer({
Key? key,
super.key,
required this.builder,
String? name,
bool? warnWhenNoObservables,
super.name,
super.warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builderWithChild = null,
child = null,
assert(builder != null),
super(
key: key,
name: name,
warnWhenNoObservables: warnWhenNoObservables,
);
assert(builder != null);

/// Observer which excludes the child branch from being rebuilt
// ignore: prefer_const_constructors_in_immutables
Observer.withBuiltChild({
Key? key,
super.key,
required this.builderWithChild,
required this.child,
String? name,
bool? warnWhenNoObservables,
super.name,
super.warnWhenNoObservables,
}) : debugConstructingStackFrame = debugFindConstructingStackFrame(),
builder = null,
assert(builderWithChild != null && child != null),
super(
key: key,
name: name,
warnWhenNoObservables: warnWhenNoObservables,
);
assert(builderWithChild != null && child != null);

/// regular builder, suitable for most cases
final WidgetBuilder? builder;
Expand Down
3 changes: 1 addition & 2 deletions flutter_mobx/lib/src/reaction_builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,7 @@ typedef ReactionBuilderFunction = ReactionDisposer Function(
class ReactionBuilder extends SingleChildStatefulWidget {
final ReactionBuilderFunction builder;

const ReactionBuilder({Key? key, Widget? child, required this.builder})
: super(key: key, child: child);
const ReactionBuilder({super.key, super.child, required this.builder});

@override
ReactionBuilderState createState() => ReactionBuilderState();
Expand Down
7 changes: 3 additions & 4 deletions flutter_mobx/lib/src/stateful_observer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ abstract class StatefulObserverWidget extends StatefulWidget
with ObserverWidgetMixin {
/// Initializes [key], [context] and [name] for subclasses.
const StatefulObserverWidget(
{Key? key, ReactiveContext? context, String? name})
{super.key, ReactiveContext? context, String? name})
: _name = name,
_context = context,
super(key: key);
_context = context;

final String? _name;
final ReactiveContext? _context;
Expand All @@ -36,7 +35,7 @@ abstract class StatefulObserverWidget extends StatefulWidget
class StatefulObserverElement extends StatefulElement
with ObserverElementMixin {
/// Creates an element that uses the given widget as its configuration.
StatefulObserverElement(StatefulObserverWidget widget) : super(widget);
StatefulObserverElement(StatefulObserverWidget super.widget);

@override
StatefulObserverWidget get widget => super.widget as StatefulObserverWidget;
Expand Down
7 changes: 3 additions & 4 deletions flutter_mobx/lib/src/stateless_observer_widget.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ abstract class StatelessObserverWidget extends StatelessWidget
with ObserverWidgetMixin {
/// Initializes [key], [context] and [name] for subclasses.
const StatelessObserverWidget({
Key? key,
super.key,
ReactiveContext? context,
String? name,
this.warnWhenNoObservables,
}) : _name = name,
_context = context,
super(key: key);
_context = context;

final String? _name;
final ReactiveContext? _context;
Expand All @@ -41,7 +40,7 @@ abstract class StatelessObserverWidget extends StatelessWidget
class StatelessObserverElement extends StatelessElement
with ObserverElementMixin {
/// Creates an element that uses the given widget as its configuration.
StatelessObserverElement(StatelessObserverWidget widget) : super(widget);
StatelessObserverElement(StatelessObserverWidget super.widget);

@override
StatelessObserverWidget get widget => super.widget as StatelessObserverWidget;
Expand Down
2 changes: 1 addition & 1 deletion flutter_mobx/lib/version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!!

/// The current version as per `pubspec.yaml`.
const version = '2.2.1';
const version = '2.2.1+1';
2 changes: 1 addition & 1 deletion flutter_mobx/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ name: flutter_mobx
description:
Flutter integration for MobX. It provides a set of Observer widgets that automatically rebuild
when the tracked observables change.
version: 2.2.1
version: 2.2.1+1

repository: https://github.com/mobxjs/mobx.dart
issue_tracker: https://github.com/mobxjs/mobx.dart/issues
Expand Down
15 changes: 6 additions & 9 deletions flutter_mobx/test/flutter_mobx_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -419,7 +419,7 @@ Future<_ErrorWrapper> _testThrowingObserverWithStackTrace(

class ConstObserver extends StatelessObserverWidget {
// const keyword compiles
const ConstObserver({Key? key}) : super(key: key);
const ConstObserver({super.key});

@override
Widget build(BuildContext context) => Container();
Expand All @@ -430,7 +430,7 @@ class ConstObserver extends StatelessObserverWidget {

class ConstStatefulObserver extends StatefulObserverWidget {
// const keyword compiles
const ConstStatefulObserver({Key? key}) : super(key: key);
const ConstStatefulObserver({super.key});

@override
State<ConstStatefulObserver> createState() => _ConstStatefulObserverState();
Expand All @@ -442,7 +442,7 @@ class _ConstStatefulObserverState extends State<ConstStatefulObserver> {
}

class _ObserverRebuildTestMyApp extends StatefulWidget {
const _ObserverRebuildTestMyApp({Key? key}) : super(key: key);
const _ObserverRebuildTestMyApp();

@override
State<_ObserverRebuildTestMyApp> createState() =>
Expand Down Expand Up @@ -471,8 +471,7 @@ class _ObserverRebuildTestMyAppState extends State<_ObserverRebuildTestMyApp> {
class _ObserverRebuildTestOne extends StatelessWidget {
final _ObserverRebuildTestMyStore store;

const _ObserverRebuildTestOne({Key? key, required this.store})
: super(key: key);
const _ObserverRebuildTestOne({required this.store});

@override
Widget build(BuildContext context) {
Expand All @@ -494,8 +493,7 @@ class _ObserverRebuildTestOne extends StatelessWidget {
class _ObserverRebuildTestTwo extends StatelessWidget {
final _ObserverRebuildTestMyStore store;

const _ObserverRebuildTestTwo({Key? key, required this.store})
: super(key: key);
const _ObserverRebuildTestTwo({required this.store});

@override
Widget build(BuildContext context) {
Expand All @@ -512,8 +510,7 @@ class _ObserverRebuildTestTwo extends StatelessWidget {
class _ObserverRebuildTestTwoChild extends StatefulWidget {
final _ObserverRebuildTestMyStore store;

const _ObserverRebuildTestTwoChild({Key? key, required this.store})
: super(key: key);
const _ObserverRebuildTestTwoChild({required this.store});

@override
_ObserverRebuildTestTwoChildState createState() =>
Expand Down
24 changes: 9 additions & 15 deletions flutter_mobx/test/helpers.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,7 @@ class MockReaction extends Mock implements ReactionImpl {

// ignore: must_be_immutable
class TestObserver extends Observer {
TestObserver(this.reaction, {Key? key, required WidgetBuilder builder})
: super(builder: builder, key: key);
TestObserver(this.reaction, {super.key, required WidgetBuilder super.builder});

final Reaction reaction;

Expand All @@ -38,14 +37,10 @@ class TestObserver extends Observer {
class LoggingObserver extends Observer {
// ignore: prefer_const_constructors_in_immutables
LoggingObserver({
required WidgetBuilder builder,
bool? warnWhenNoObservables,
Key? key,
}) : super(
key: key,
builder: builder,
warnWhenNoObservables: warnWhenNoObservables,
);
required WidgetBuilder super.builder,
super.warnWhenNoObservables,
super.key,
});

String? previousLog;

Expand All @@ -59,10 +54,10 @@ class LoggingObserver extends Observer {
class FlutterErrorThrowingObserver extends Observer {
// ignore: prefer_const_constructors_in_immutables
FlutterErrorThrowingObserver({
required WidgetBuilder builder,
required WidgetBuilder super.builder,
required this.errorToThrow,
Key? key,
}) : super(key: key, builder: builder);
super.key,
});

final Object errorToThrow;

Expand All @@ -72,8 +67,7 @@ class FlutterErrorThrowingObserver extends Observer {
}

class FlutterErrorThrowingObserverElement extends StatelessObserverElement {
FlutterErrorThrowingObserverElement(StatelessObserverWidget widget)
: super(widget);
FlutterErrorThrowingObserverElement(super.widget);

@override
FlutterErrorThrowingObserver get widget =>
Expand Down
2 changes: 1 addition & 1 deletion flutter_mobx/test/reaction_builder_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import 'package:mobx/mobx.dart';
class _TestWrapper extends StatefulWidget {
final Observable<int> counter;

const _TestWrapper({Key? key, required this.counter}) : super(key: key);
const _TestWrapper({required this.counter});

@override
State<_TestWrapper> createState() => _TestWrapperState();
Expand Down
4 changes: 4 additions & 0 deletions mobx/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
## 2.3.3+1

- Analyzer fixes

## 2.3.3

- Upgrading packages and sdk
Expand Down
1 change: 1 addition & 0 deletions mobx/lib/src/api/annotations.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
/// Declares configuration of a Store class.
/// Currently the only configuration used is boolean to indicate generation of toString method (true), or not (false)
library;

class StoreConfig {
const StoreConfig({this.hasToString = true});
Expand Down
2 changes: 1 addition & 1 deletion mobx/lib/src/core.dart
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ class MobXException extends Error implements Exception {
/// This exception would be fired when an reaction has a cycle and does
/// not stabilize in [ReactiveConfig.maxIterations] iterations
class MobXCyclicReactionException extends MobXException {
MobXCyclicReactionException(String message) : super(message);
MobXCyclicReactionException(super.message);
}

/// This captures the stack trace when user-land code throws an exception
Expand Down
4 changes: 2 additions & 2 deletions mobx/lib/src/core/computed.dart
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ class Computed<T> extends Atom implements Derivation, ObservableValue<T> {
Computed._(context ?? mainContext, fn,
name: name, equals: equals, keepAlive: keepAlive);

Computed._(ReactiveContext context, this._fn,
Computed._(super.context, this._fn,
{String? name, this.equals, bool? keepAlive})
: _keepAlive = keepAlive ?? false,
super._(context, name: name ?? context.nameFor('Computed'));
super._(name: name ?? context.nameFor('Computed'));

final EqualityComparer<T>? equals;

Expand Down
4 changes: 2 additions & 2 deletions mobx/lib/src/core/observable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ class Observable<T> extends Atom
Observable._(context ?? mainContext, initialValue,
name: name, equals: equals);

Observable._(ReactiveContext context, this._value,
Observable._(super.context, this._value,
{String? name, this.equals})
: _interceptors = Interceptors(context),
_listeners = Listeners(context),
super._(context, name: name ?? context.nameFor('Observable')) {
super._(name: name ?? context.nameFor('Observable')) {
if (_context.isSpyEnabled) {
_context.spyReport(ObservableValueSpyEvent(this,
newValue: _value, name: this.name, isEnd: true));
Expand Down
12 changes: 5 additions & 7 deletions mobx/lib/src/core/spy.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,9 @@ abstract class SpyEvent {

/// Used for reporting value changes on an Observable
class ObservableValueSpyEvent extends SpyEvent {
ObservableValueSpyEvent(dynamic object,
{this.newValue, this.oldValue, required String name, bool isEnd = false})
: super._(object,
type: 'observable', name: name, isStart: true, isEnd: isEnd);
ObservableValueSpyEvent(super.object,
{this.newValue, this.oldValue, required super.name, super.isEnd})
: super._(type: 'observable', isStart: true);

final dynamic newValue;
final dynamic oldValue;
Expand All @@ -52,9 +51,8 @@ class ObservableValueSpyEvent extends SpyEvent {
}

class ComputedValueSpyEvent extends SpyEvent {
ComputedValueSpyEvent(object, {required String name})
: super._(object,
type: 'computed', name: name, isStart: true, isEnd: true);
ComputedValueSpyEvent(super.object, {required super.name})
: super._(type: 'computed', isStart: true, isEnd: true);
}

class ReactionSpyEvent extends SpyEvent {
Expand Down
2 changes: 1 addition & 1 deletion mobx/lib/src/interceptable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ abstract class Interceptable<T> {
/// is stored inside the `Interceptors<T>`.
/// This is an internal class and should not be used directly.
class Interceptors<T> extends NotificationHandlers<WillChangeNotification<T>> {
Interceptors(ReactiveContext context) : super(context);
Interceptors(super.context);

@override
Dispose add(Interceptor<T> handler) => super.add(handler);
Expand Down
2 changes: 1 addition & 1 deletion mobx/lib/src/listenable.dart
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ abstract class Listenable<TNotification> {
/// Stores the handler functions that have been attached via [Observable.observe] method
/// This is an internal class and should not be used directly.
class Listeners<TNotification> extends NotificationHandlers<TNotification> {
Listeners(ReactiveContext context) : super(context);
Listeners(super.context);

@override
Dispose add(Listener<TNotification> handler) => super.add(handler);
Expand Down
2 changes: 1 addition & 1 deletion mobx/lib/version.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Generated via set_version.dart. !!!DO NOT MODIFY BY HAND!!!

/// The current version as per `pubspec.yaml`.
const version = '2.3.3';
const version = '2.3.3+1';
2 changes: 1 addition & 1 deletion mobx/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: mobx
version: 2.3.3
version: 2.3.3+1
description: "MobX is a library for reactively managing the state of your applications. Use the power of observables, actions, and reactions to supercharge your Dart and Flutter apps."

repository: https://github.com/mobxjs/mobx.dart
Expand Down
2 changes: 1 addition & 1 deletion mobx/test/autorun_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ void main() {
fakeAsync((async) {
dispose = autorun((_) {
value = x.value + 1;
}, delay: delayMs);
}, delay: delayMs).call;

async.elapse(const Duration(milliseconds: 2500));

Expand Down
2 changes: 1 addition & 1 deletion mobx/test/observable_map_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ void main() {
<String, void Function(ObservableMap<String, int>)>{
'[]': (m) => m['a'],
'containsKey': (m) => m.containsKey('a'),
'containsValue': (m) => m.containsKey(1),
'containsValue': (m) => m.containsValue(1),
'forEach': (m) => m.forEach((_, __) {}),
'putIfAbsent': (m) => m.putIfAbsent('a', () => 1),
'length': (m) => m.length,
Expand Down
4 changes: 2 additions & 2 deletions mobx_examples/lib/hackernews/news_widgets.dart
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,9 @@ class FeedItemsView extends StatelessWidget {

switch (future.status) {
case FutureStatus.pending:
return Column(
return const Column(
mainAxisAlignment: MainAxisAlignment.center,
children: const [
children: [
CircularProgressIndicator(),
Text('Loading items...'),
],
Expand Down

0 comments on commit af5bf90

Please sign in to comment.