Skip to content

Commit 4ebfbeb

Browse files
committed
docs: readme improvement
1 parent 828c56b commit 4ebfbeb

File tree

9 files changed

+710
-240
lines changed

9 files changed

+710
-240
lines changed

README.md

Lines changed: 134 additions & 183 deletions
Large diffs are not rendered by default.

example/README.md

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,6 @@
1-
# example
21

3-
A new Flutter project.
2+
# Simple Weather App Source Code
43

5-
## Getting Started
4+
This is the source code for the simple weather app using the [`trent`](https://pub.dev/packages/trent) package.
65

7-
This project is a starting point for a Flutter application.
8-
9-
A few resources to get you started if this is your first Flutter project:
10-
11-
- [Lab: Write your first Flutter app](https://docs.flutter.dev/get-started/codelab)
12-
- [Cookbook: Useful Flutter samples](https://docs.flutter.dev/cookbook)
13-
14-
For help getting started with Flutter development, view the
15-
[online documentation](https://docs.flutter.dev/), which offers tutorials,
16-
samples, guidance on mobile development, and a full API reference.
6+
<img src="https://raw.githubusercontent.com/mattrltrent/random_assets/refs/heads/main/trent_weather.gif" height="500px"/>

example/lib/main.dart

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'dart:math';
22

3+
import 'package:flutter/cupertino.dart';
34
import 'package:flutter/material.dart';
45
import 'package:trent/trent.dart';
56

@@ -48,15 +49,14 @@ class _HomeScreenState extends State<HomeScreen> {
4849

4950
return Scaffold(
5051
appBar: AppBar(
51-
title: map<WeatherTrent, WeatherTypes>(context, (mapper) {
52+
title: watchMap<WeatherTrent, WeatherTypes>(context, (mapper) {
5253
mapper
53-
..as<Data>((state) => Text("Weather for lat/long: ${state.location}"))
5454
..as<NoData>((state) => const Text("Enter Location"))
5555
..as<Loading>((state) => const Text("Fetching Weather..."))
5656
..orElse((state) => const Text("Weather App"));
5757
}),
5858
),
59-
floatingActionButton: watch<WeatherTrent>(context).currState is Data
59+
floatingActionButton: watch<WeatherTrent>(context).state is Data
6060
? FloatingActionButton(
6161
onPressed: () => weatherTrent.clear(),
6262
child: const Icon(Icons.clear),
@@ -75,6 +75,7 @@ class _HomeScreenState extends State<HomeScreen> {
7575
}),
7676
listenStates: (mapper) => mapper
7777
..as<NoData>((state) {
78+
print("HELLO!");
7879
_latitudeController.clear();
7980
_longitudeController.clear();
8081
}),
@@ -115,17 +116,20 @@ class _HomeScreenState extends State<HomeScreen> {
115116
child: const Text("Fetch Weather"),
116117
),
117118
const SizedBox(height: 16),
118-
Digester<WeatherTrent, WeatherTypes>(
119-
child: (mapper) {
120-
mapper
121-
..as<NoData>((state) => const Text("Please enter location to fetch weather."))
122-
..as<Loading>((state) => const CircularProgressIndicator())
123-
..as<Data>((state) => Text(
124-
"${state.location} has temperature ${state.temperature}°C",
125-
style: const TextStyle(fontSize: 18),
126-
))
127-
..orElse((state) => const Text("Unknown state"));
128-
},
119+
Container(
120+
constraints: BoxConstraints(minHeight: 200),
121+
child: Digester<WeatherTrent, WeatherTypes>(
122+
child: (mapper) {
123+
mapper
124+
..as<NoData>((state) => const Text("Please enter location to fetch weather."))
125+
..as<Loading>((state) => const CupertinoActivityIndicator())
126+
..as<Data>((state) => Text(
127+
"${state.location} has temperature ${state.temperature}°C",
128+
style: const TextStyle(fontSize: 18),
129+
))
130+
..orElse((state) => const Text("Unknown state"));
131+
},
132+
),
129133
),
130134
],
131135
),
@@ -138,9 +142,14 @@ class _HomeScreenState extends State<HomeScreen> {
138142

139143
// ====== trents/weather_trent.dart ======
140144

141-
class WeatherTypes extends Equatable {
145+
class WeatherTypes extends EquatableCopyable<WeatherTypes> {
142146
@override
143147
List<Object> get props => [];
148+
149+
@override
150+
WeatherTypes copyWith() {
151+
return this;
152+
}
144153
}
145154

146155
class NoData extends WeatherTypes {}
@@ -153,6 +162,11 @@ class Error extends WeatherTypes {
153162

154163
@override
155164
List<Object> get props => [message];
165+
166+
@override
167+
Error copyWith({String? message}) {
168+
return Error(message ?? this.message);
169+
}
156170
}
157171

158172
class Data extends WeatherTypes {

lib/src/logic/accessors.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@ T watch<T extends Trents>(BuildContext context) {
1313
return Provider.of<T>(context, listen: true);
1414
}
1515

16-
/// Retrieve a widget for the current state of a Trent reactively.
17-
Widget map<T extends Trents<S>, S>(BuildContext context, void Function(WidgetSubtypeMapper<S>) configure) {
16+
/// Retrieve a map over the current state of a Trent reactively.
17+
Widget watchMap<T extends Trents<S>, S>(BuildContext context, void Function(WidgetSubtypeMapper<S>) configure) {
1818
final trent = Provider.of<T>(context, listen: true);
19-
final mapper = WidgetSubtypeMapper<S>(trent.currState);
19+
final mapper = WidgetSubtypeMapper<S>(trent.state);
2020

2121
// Configure the mapper with handlers
2222
configure(mapper);

lib/src/logic/trent.dart

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@ abstract class Trents<Base> extends ChangeNotifier {
2929
final Map<Type, Option<Base>> _lastStates = {};
3030

3131
/// Getter method for the current state.
32-
Base get currState => _state;
32+
Base get state => _state;
3333

3434
/// Getter method for the current state by type.
35-
LogicSubTypeMapper<Base> get currStateMapper => LogicSubTypeMapper<Base>(_state);
35+
LogicSubTypeMapper<Base> get stateMap => LogicSubTypeMapper<Base>(_state);
3636

3737
/// Getter method for the state stream. Usually not used directly.
3838
Stream<Base> get stateStream => _stateSubject.stream;
@@ -122,9 +122,13 @@ abstract class Trents<Base> extends ChangeNotifier {
122122
}
123123
}
124124

125+
abstract class EquatableCopyable<T> extends Equatable implements Copyable<T> {}
126+
127+
abstract class Copyable<T> {
128+
T copyWith();
129+
}
130+
125131
/// A generic Trent that manages state transitions.
126-
abstract class Trent<Base extends Equatable> extends Trents<Base> {
127-
Trent(
128-
super.state,
129-
);
132+
abstract class Trent<Base extends EquatableCopyable<Base>> extends Trents<Base> {
133+
Trent(super.state);
130134
}

lib/src/widgets/alerter.dart

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,13 @@ class AlerterState<TrentType extends Trents<StateType>, StateType> extends State
3535
late final TrentType sm = get<TrentType>(context);
3636
late StateType _previousAlert; // Tracks the previous alert state
3737
late StateType _previousState; // Tracks the previous normal state
38+
bool _hasInitialStateTriggered = false; // Tracks if the initial state has been emitted
3839

3940
@override
4041
void initState() {
4142
super.initState();
42-
_previousAlert = sm.currState;
43-
_previousState = sm.currState;
43+
_previousAlert = sm.state;
44+
_previousState = sm.state;
4445

4546
// Listen to the alert stream
4647
sm.alertStream.listen((alert) {
@@ -56,6 +57,11 @@ class AlerterState<TrentType extends Trents<StateType>, StateType> extends State
5657

5758
// Listen to the state stream
5859
sm.stateStream.listen((state) {
60+
if (!_hasInitialStateTriggered) {
61+
_hasInitialStateTriggered = true; // Skip the first seeded state
62+
return;
63+
}
64+
5965
if (widget.listenStates != null) {
6066
final shouldTrigger = widget.listenStatesIf?.call(_previousState, state) ?? true;
6167
if (shouldTrigger) {

lib/src/widgets/digester.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,15 @@ class Digester<TrentType extends Trents<StateType>, StateType> extends Stateless
1515
@override
1616
Widget build(BuildContext context) {
1717
// Retrieve the Trent instance dynamically from the context
18-
final sm = get<TrentType>(context);
19-
final mapper = WidgetSubtypeMapper<StateType>(sm.currState);
18+
final sm = watch<TrentType>(context);
19+
final mapper = WidgetSubtypeMapper<StateType>(sm.state);
2020

2121
// Register handlers for each state type
2222
child(mapper);
2323

2424
return StreamBuilder<StateType>(
2525
stream: sm.stateStream, // Plug into the Trent's state stream
26-
initialData: sm.currState, // Provide the initial state
26+
initialData: sm.state, // Provide the initial state
2727
builder: (context, snapshot) {
2828
// Resolve the widget for the current state
2929
return mapper.resolve();

pubspec.yaml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ dependencies:
1515
flutter:
1616
sdk: flutter
1717
equatable: ^2.0.7
18-
get_it: ^8.0.3
1918
rxdart: ^0.28.0
2019
provider: ^6.1.2
2120

0 commit comments

Comments
 (0)