Skip to content

Commit

Permalink
increase type support
Browse files Browse the repository at this point in the history
  • Loading branch information
ragokan committed Mar 31, 2021
1 parent c71b410 commit 7fe8d22
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 20 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ linux
web
# It is for testing app with current lib
lib/main.dart
*.stamp
*.stamp
.vscode/settings.json
8 changes: 7 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,10 @@

- New feature: OkitoWatcher (Check test/okito_watcher_test.dart)
- More examples
- More tests
- More tests


## 1.0.4

- Document for OkitoWatcher
- Document for all variables and parameters
8 changes: 5 additions & 3 deletions analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@ include: package:pedantic/analysis_options.yaml

linter:
rules:
- prefer_relative_imports
- prefer_const_constructors
- directives_ordering
- annotate_overrides
- await_only_futures
- camel_case_types
Expand All @@ -16,14 +19,13 @@ linter:
- close_sinks
- comment_references
- control_flow_in_finally
- constant_identifier_names
- non_constant_identifier_names
- empty_statements
- prefer_const_constructors
- directives_ordering
- lines_longer_than_80_chars
- curly_braces_in_flow_control_structures
- implementation_imports
- avoid_relative_lib_imports
- prefer_relative_imports
- prefer_adjacent_string_concatenation
- prefer_interpolation_to_compose_strings
- unnecessary_brace_in_string_interps
Expand Down
2 changes: 1 addition & 1 deletion lib/bin/extensions/context_extensions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import 'package:flutter/material.dart';

final flutterDialog = showDialog;

extension BuildContextUtilities on BuildContext {
extension BuildContextExtensions on BuildContext {
/// The horizontal extent of this size.
double get width => MediaQuery.of(this).size.width;

Expand Down
20 changes: 19 additions & 1 deletion lib/bin/state_management/builder.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,24 @@ import '../../types/callback_types.dart';
import 'controller.dart';
import 'modules/communication.dart';

/// [OkitoBuilder] is the easiest way to track state changes and
/// re-render state on change.
///
/// Instead of having a stateful widget that re-renders on any change
/// which causes all the dependencies to re-render no matter if they
/// are changed or not, StatelessWidget with OkitoBuilder just re-renders
/// where did you use OkitoBuilder. If your widget don't need a dynamic
/// state, just don't put it inside builder. It won't re-built.
///
/// Example
///
/// ```dart
/// OkitoBuilder(
/// controller: counterController,
/// builder: () => Text('${counterController.count}',
/// ),
/// );
/// ```
class OkitoBuilder<T extends OkitoController> extends StatefulWidget {
/// [controller] should be a class that extends or mixs [OkitoController].
final T controller;
Expand All @@ -15,7 +33,7 @@ class OkitoBuilder<T extends OkitoController> extends StatefulWidget {
/// Builder callback is called whenever your state changes.
///
/// You have to return a Widget that you want to re-build on state changes.
final ControllerCallback builder;
final BuilderCallback builder;

/// OkitoBuilder is the way to use OkitoController.
///
Expand Down
11 changes: 7 additions & 4 deletions lib/bin/state_management/modules/communication.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,20 @@ class _ControllerCommunication<Okito> {
final _watchers = <Watcher>{};

/// [notify] method is the method called when you use 'update' or 'setState'.
void notify<T extends Okito>(T type) {
void notify<T extends Okito>(T controller) {
for (var watcher in _watchers) {
if (watcher.event == type) {
if (watcher.controller == controller) {
watcher.callback();
}
}
}

/// With [watch] method, state watches changes that are coming from notify.
Function watch<T extends Okito>(T type, VoidCallback<Object> callback) {
var watcher = Watcher(type, callback);
Function watch<T extends Okito>(
T controller,
VoidCallback callback,
) {
var watcher = Watcher(controller, callback);
_watchers.add(watcher);

return () => _watchers.remove(watcher);
Expand Down
4 changes: 2 additions & 2 deletions lib/bin/state_management/modules/watcher_model.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import '../../../types/callback_types.dart';

class Watcher<T> {
final T event;
final T controller;
final VoidCallback callback;

const Watcher(this.event, this.callback);
const Watcher(this.controller, this.callback);
}
26 changes: 26 additions & 0 deletions lib/bin/state_management/watcher.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,35 @@
import '../../okito.dart';
import '../../types/callback_types.dart';
import 'controller.dart';
import 'modules/communication.dart';

/// [OkitoWatcher] is the easiest and the most lightweight way to observe
/// controller changes outside of [OkitoBuilder].
///
/// It differs from [OkitoBuilder] in one way, [OkitoBuilder] creates a
/// StatefulWidget to observe changes, so that you can display your widget, but
/// [OkitoWatcher] doesn't create any widget, it is just a callback function
/// that gives you access to the state and watch state changes of your
/// [OkitoController].
///
/// Example
///
/// ```dart
/// // You can give a type here optionally to receive maximum type support.
/// OkitoWatcher<CounterController>(
/// watch: counterController,
/// onChange: (controller) {
/// print('Count is changed to ${controller.count}');
/// },
/// )
/// ```
// ignore: non_constant_identifier_names
Function OkitoWatcher<T extends OkitoController>({
/// Controller to watch
required T watch,

/// The function that called whenever controller state changes.
/// onChange: (controller) => /* Your code here */
required WatcherCallback<T> onChange,
}) {
return controllerXview.watch(
Expand Down
4 changes: 2 additions & 2 deletions lib/okito.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
/// need context to have a state or update state.
library okito;

export 'package:okito/bin/state_management/index.dart';
export 'package:okito/bin/okito_instance.dart';
export 'bin/okito_instance.dart';
export 'bin/state_management/index.dart';
11 changes: 9 additions & 2 deletions lib/types/callback_types.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import 'package:flutter/material.dart';

typedef VoidCallback<T> = void Function();
typedef ControllerCallback = Widget Function();
/// Regular callback type for the whole library.
typedef VoidCallback = void Function();

/// A callback that returns Widget.
/// It is current used only for OkitoBuilder.
typedef BuilderCallback = Widget Function();

/// It is used for OkitoWatcher right now,
/// Returns a void function that gives the [controller] with type [T].
typedef WatcherCallback<T> = void Function(T controller);
7 changes: 4 additions & 3 deletions pubspec.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
name: okito
description: Okito is the simplest state management solution ever, at least I think so. It depends on nothing, works really fast with minimum code usage.
version: 1.0.3
description: Okito is the simplest state management solution ever, at least I
think so. It depends on nothing, works really fast with minimum code usage.
version: 1.0.4
homepage: https://github.com/ragokan/okito
repository: https://github.com/ragokan/okito

Expand All @@ -12,6 +13,6 @@ dependencies:
sdk: flutter

dev_dependencies:
pedantic: ^1.9.0
flutter_test:
sdk: flutter
pedantic: ^1.11.0

0 comments on commit 7fe8d22

Please sign in to comment.