Skip to content

Commit b927cff

Browse files
committed
fix: typos
1 parent 5633599 commit b927cff

File tree

5 files changed

+63
-46
lines changed

5 files changed

+63
-46
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.0.3
2+
3+
- README updates.
4+
15
## 0.0.2
26

37
- Improves documentation.

README.md

Lines changed: 50 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
Define custom state classes, then use them in your Trent state manager:
4242

4343
```dart
44-
4544
//
4645
// Classes A, B, and C defined here
4746
//
@@ -87,9 +86,9 @@ class AuthTrent extends Trent<AuthTypes> {
8786
8887
// Map over the current state and do things based on the type
8988
// (not all routes need to be defined)
90-
currStateMapper
91-
..all((state) {
92-
// Do something
89+
stateMap
90+
..orElse((state) {
91+
// Do something (doElse run if nothing else more specific hit)
9392
})
9493
..as<A>((state) {
9594
// Do something
@@ -102,7 +101,7 @@ class AuthTrent extends Trent<AuthTypes> {
102101
});
103102
104103
// Simply access the raw state for custom manipulation
105-
print(currState);
104+
print(state);
106105
}
107106
108107
/// ... More business functions ...
@@ -113,26 +112,48 @@ class AuthTrent extends Trent<AuthTypes> {
113112

114113
### UI Layer: Built-in Widgets
115114

116-
- `Alerter` widget that listens to one-time state `alert(...)`s from your business logic layer. This is good if your business logic needs to "quickly send off a state without saving it". An example would be you having `Loading`, `Data`, and `WarningNotification` states. You may be in `Data` state, but want to send off a quick `WarningNotification` state without having to throw away your `Data` state. This is what an `alert(WarningNotification(...))` is good for.
115+
- `Alerter` widget that listens to one-time state `alert(...)`s from your business logic layer in `listenAlerts`. This is good if your business logic needs to "quickly send off a state without saving it". An example would be you having `Loading`, `Data`, and `WarningNotification` states. You may be in `Data` state, but want to send off a quick `WarningNotification` state without having to throw away your `Data` state. This is what an `alert(WarningNotification(...))` is good for. `Alerter` can also can listen to regular state updates in `listenStates`. Both can have their listeners programmatically toggled on/off with `listenAlertsIf` and `listenStatesIf` respectively.
117116

118117
```dart
119118
// AuthTrent is where your business logic is defined, AuthTrentTypes is
120119
// the type all your business logic types extend from (in this example `A`, `B`, and `C` states)
121120
Alerter<AuthTrent, AuthTrentTypes>(
122121
// Not all handlers need to be defined
123-
handlers: (mapper) => mapper
124-
..all((state) {
125-
// Always called if defined
126-
})
127-
..as<A>((state) {
128-
// Called if `A` is alerted
129-
})
130-
..as<B>((state) {
131-
// Called if `B` is alerted
132-
})
133-
..as<C>((_) {
134-
// Called if `C` is alerted
135-
}),
122+
//
123+
// This only listens to alerts
124+
listenAlerts: (mapper) => mapper
125+
..orElse((state) {
126+
// Triggered if nothing more specific is defined
127+
})
128+
..as<A>((state) {
129+
// Called if `A` is alerted
130+
})
131+
..as<B>((state) {
132+
// Called if `B` is alerted
133+
})
134+
..as<C>((_) {
135+
// Called if `C` is alerted
136+
}),
137+
// Not all handlers need to be defined
138+
//
139+
// This only listens to states emitted
140+
listenStates: (mapper) => mapper
141+
..orElse((state) {
142+
// Triggered if nothing more specific is defined
143+
})
144+
..as<A>((state) {
145+
// Called if `A` is alerted
146+
})
147+
..as<B>((state) {
148+
// Called if `B` is alerted
149+
})
150+
..as<C>((_) {
151+
// Called if `C` is alerted
152+
}),
153+
154+
// Only trigger listens if...
155+
listenAlertsIf: (oldAlert, newAlert) => true,
156+
listenStatesIf: (oldState, newState) => true,
136157
child: Container(),
137158
);
138159
```
@@ -144,9 +165,9 @@ class AuthTrent extends Trent<AuthTypes> {
144165
// the type all your business logic types extend from (in this example `A`, `B`, and `C` states)
145166
Digester<AuthTrent, AuthTrentTypes>(
146167
// Not all handlers need to be defined
147-
handlers: (mapper) {
168+
child: (mapper) {
148169
mapper
149-
..all((state) => const Text("Rendered if no more specific type is defined"))
170+
..orElse((state) => const Text("Rendered if no more specific type is defined"))
150171
..as<A>((state) => Text("State is A"))
151172
..as<B>((state) => const Text("State is B"))
152173
..as<C>((state) => const Text("State is C"));
@@ -182,7 +203,7 @@ class AuthTrent extends Trent<AuthTypes> {
182203
}
183204
```
184205

185-
- `alert(state)`: Alert a temporary state WITHOUT setting it, but being able to listen to it from the `Alerter` widget (for things like notifications).
206+
- `alert(state)`: Alert a temporary state WITHOUT setting/saving it, but being able to listen to it from the `Alerter` widget (for things like notifications).
186207

187208
```dart
188209
class CalculatorTrent extends Trent<CalculatorStates> {
@@ -195,7 +216,7 @@ class AuthTrent extends Trent<AuthTypes> {
195216
}
196217
```
197218

198-
- `getExStateAs<T>()`: This will return the last state of type `T`. Useful for accessing a state you transitioned away from.
219+
- `getExStateAs<T>()`: This will return the last state of type `T`. Useful for accessing a state you transitioned away from. For example, if you transitioned from `Division` to `Multiplication`, you can still access the last value of the `Division` state after transitioning away from it.
199220

200221
```dart
201222
class CalculatorTrent extends Trent<CalculatorStates> {
@@ -235,17 +256,17 @@ class AuthTrent extends Trent<AuthTypes> {
235256
}
236257
```
237258

238-
- `currStateMapper`: Maps over the current state and performs actions based on its type.
259+
- `stateMap`: Maps over the current state and performs actions based on its type.
239260

240261
```dart
241262
class CalculatorTrent extends Trent<CalculatorStates> {
242263
CalculatorTrent() : super(BlankScreen());
243264
244265
// Perform different actions depending on the current state type
245266
void handleState() {
246-
currStateMapper
247-
..all((state) {
248-
print("Generic state handler.");
267+
stateMap
268+
..orElse((state) {
269+
print("Generic state handler. Called if nothing more specific defined.");
249270
})
250271
..as<BlankScreen>((_) {
251272
print("Calculator is blank.");
@@ -260,15 +281,15 @@ class AuthTrent extends Trent<AuthTypes> {
260281
}
261282
```
262283

263-
- `currState`: Access the raw state for custom manipulation.
284+
- `state`: Access the raw state for custom manipulation.
264285

265286
```dart
266287
class CalculatorTrent extends Trent<CalculatorStates> {
267288
CalculatorTrent() : super(BlankScreen());
268289
269290
// Print the raw state for debugging or custom handling
270291
void printRawState() {
271-
print("Raw state: $currState");
292+
print("Raw state: $state");
272293
}
273294
}
274295
```

example/lib/main.dart

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ class _HomeScreenState extends State<HomeScreen> {
7979
_latitudeController.clear();
8080
_longitudeController.clear();
8181
}),
82+
8283
child: Padding(
8384
padding: const EdgeInsets.all(16.0),
8485
child: Column(
@@ -105,8 +106,7 @@ class _HomeScreenState extends State<HomeScreen> {
105106
TextButton(
106107
onPressed: () {
107108
final latitude = double.tryParse(_latitudeController.text);
108-
final longitude =
109-
double.tryParse(_longitudeController.text);
109+
final longitude = double.tryParse(_longitudeController.text);
110110

111111
if (latitude != null && longitude != null) {
112112
weatherTrent.fetchWeather(latitude, longitude);
@@ -122,10 +122,8 @@ class _HomeScreenState extends State<HomeScreen> {
122122
child: Digester<WeatherTrent, WeatherTypes>(
123123
child: (mapper) {
124124
mapper
125-
..as<NoData>((state) => const Text(
126-
"Please enter location to fetch weather."))
127-
..as<Loading>(
128-
(state) => const CupertinoActivityIndicator())
125+
..as<NoData>((state) => const Text("Please enter location to fetch weather."))
126+
..as<Loading>((state) => const CupertinoActivityIndicator())
129127
..as<Data>((state) => Text(
130128
"${state.location} has temperature ${state.temperature}°C",
131129
style: const TextStyle(fontSize: 18),
@@ -194,9 +192,7 @@ class WeatherTrent extends Trent<WeatherTypes> {
194192
emit(Loading());
195193
Future.delayed(const Duration(milliseconds: 500), () {
196194
final mockData = {
197-
"temperature": double.parse(
198-
(15 + (latitude % 10) + Random().nextDouble() * 5)
199-
.toStringAsFixed(1)),
195+
"temperature": double.parse((15 + (latitude % 10) + Random().nextDouble() * 5).toStringAsFixed(1)),
200196
"location": "$latitude, $longitude",
201197
};
202198

lib/src/logic/trent.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -96,18 +96,15 @@ abstract class Trents<Base> extends ChangeNotifier {
9696
/// to return to D(value: 10) instead of D(value: some_value_you_must_define).
9797
Option<T> getExStateAs<T extends Base>() {
9898
return _lastStates[T] != null
99-
? _lastStates[T]!.match(
100-
some: (v) => Option.some(v as T), none: () => Option<T>.none())
99+
? _lastStates[T]!.match(some: (v) => Option.some(v as T), none: () => Option<T>.none())
101100
: Option<T>.none();
102101
}
103102

104103
/// Retrieve the current state as a specific type.
105104
///
106105
/// Will return None if the current state is not of the specified type.
107106
Option<T> getCurrStateAs<T extends Base>() {
108-
return _state.runtimeType == T
109-
? Option.some(_state as T)
110-
: Option<T>.none();
107+
return _state.runtimeType == T ? Option.some(_state as T) : Option<T>.none();
111108
}
112109

113110
/// Dispose of the Trent.
@@ -132,7 +129,6 @@ abstract class Copyable<T> {
132129
}
133130

134131
/// A generic Trent that manages state transitions.
135-
abstract class Trent<Base extends EquatableCopyable<Base>>
136-
extends Trents<Base> {
132+
abstract class Trent<Base extends EquatableCopyable<Base>> extends Trents<Base> {
137133
Trent(super.state);
138134
}

pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name: trent
22
description: A Flutter package for simple, scalable, and reactive state management with built-in dependency injection and efficient stream-based state handling.
3-
version: 0.0.2
3+
version: 0.0.3
44

55
homepage: https://matthewtrent.me
66
repository: https://github.com/mattrltrent/trent

0 commit comments

Comments
 (0)