Skip to content

Commit 9935d43

Browse files
committed
Begins charts page
1 parent 8309be2 commit 9935d43

File tree

10 files changed

+348
-26
lines changed

10 files changed

+348
-26
lines changed

lib/api_requests/api_calls.dart

+23
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,26 @@ Future<SateliteInfo> fetchSateliteInfo(Satelites satelite) async {
2727
rethrow; // TODO: add a properly catch
2828
}
2929
}
30+
31+
Future<List<SateliteInfo>> fetchSateliteHistory(
32+
Satelites satelite, ChartRanges range) async {
33+
try {
34+
final res = await http.get(
35+
Uri.parse("${MorazanApi.apiBase}${satelite.name.capitalize()}/$range"));
36+
if (res.statusCode == 200) {
37+
var dataList = jsonDecode(res.body) as List<Map<String, dynamic>>;
38+
final sateliteInfo = dataList.map((data) => SateliteInfo.fromJson(data));
39+
return sateliteInfo.toList();
40+
} else {
41+
if (kDebugMode) {
42+
print("error en el fetch por ${res.statusCode}");
43+
}
44+
throw Exception(""); // TODO: add a properly exception
45+
}
46+
} catch (e) {
47+
if (kDebugMode) {
48+
print("error en el fetch por $e");
49+
}
50+
rethrow; // TODO: add a properly catch
51+
}
52+
}

lib/components/wind_widget.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,11 @@ class Wind extends StatelessWidget {
4141
fontWeight: FontWeight.w900,
4242
fontSize: 10,
4343
),
44-
),TweenAnimationBuilder(
45-
tween: Tween<double>(begin: 0, end: _direction+0.0),
46-
duration: const Duration(milliseconds: 500), // Duración de la animación
44+
),
45+
TweenAnimationBuilder(
46+
tween: Tween<double>(begin: 0, end: _direction + 0.0),
47+
duration:
48+
const Duration(milliseconds: 500), // Duración de la animación
4749
builder: (context, double angle, child) {
4850
return Transform.rotate(
4951
angle: angle * math.pi / 180,

lib/pages/base/base_widget.dart

+15-17
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import 'package:morazan/util/constants.dart';
55
import 'package:string_capitalize/string_capitalize.dart';
66

77
class MorazanApp extends StatefulWidget {
8-
const MorazanApp({Key? key}) : super(key: key);
8+
const MorazanApp({super.key});
99

1010
@override
1111
State<MorazanApp> createState() => _MorazanAppState();
@@ -19,9 +19,8 @@ class _MorazanAppState extends State<MorazanApp> {
1919
@override
2020
Widget build(BuildContext context) {
2121
var colorScheme = Theme.of(context).colorScheme;
22-
var themeIcon = _themeMode == ThemeMode.light
23-
? Icons.dark_mode
24-
: Icons.light_mode;
22+
var themeIcon =
23+
_themeMode == ThemeMode.light ? Icons.dark_mode : Icons.light_mode;
2524

2625
return MaterialApp(
2726
title: 'Morazan',
@@ -36,11 +35,11 @@ class _MorazanAppState extends State<MorazanApp> {
3635
Text("Detalles de ${_actualSatelite.name.capitalize()}"),
3736
TextButton(
3837
onPressed: () {
39-
setState(() {
40-
_themeMode = _themeMode == ThemeMode.light
38+
setState(
39+
() => _themeMode = _themeMode == ThemeMode.light
4140
? ThemeMode.dark
42-
: ThemeMode.light;
43-
});
41+
: ThemeMode.light,
42+
);
4443
},
4544
child: Icon(themeIcon, size: 20, color: colorScheme.onPrimary),
4645
),
@@ -56,16 +55,15 @@ class _MorazanAppState extends State<MorazanApp> {
5655
value: _actualSatelite,
5756
icon: Icon(Icons.expand_more,
5857
size: 24, color: colorScheme.outline),
59-
onChanged: (satelite) {
60-
setState(() {
61-
_actualSatelite = satelite!;
62-
});
63-
},
58+
onChanged: (satelite) =>
59+
setState(() => _actualSatelite = satelite!),
6460
items: _satelites
65-
.map((satelite) => DropdownMenuItem(
66-
value: satelite,
67-
child: Text(satelite.name.capitalize()),
68-
))
61+
.map(
62+
(satelite) => DropdownMenuItem(
63+
value: satelite,
64+
child: Text(satelite.name.capitalize()),
65+
),
66+
)
6967
.toList(),
7068
),
7169
),

lib/pages/charts/charts_widget.dart

+104
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,104 @@
1+
import 'package:flutter/material.dart';
2+
import 'package:intl/intl.dart';
3+
import 'package:morazan/api_requests/api_calls.dart';
4+
import 'package:morazan/pages/satelites/satelites_model.dart';
5+
import 'package:morazan/util/constants.dart';
6+
import 'package:string_capitalize/string_capitalize.dart';
7+
8+
class ChartsPage extends StatefulWidget {
9+
final Satelites _satelite;
10+
final Metrics _metric;
11+
12+
const ChartsPage({
13+
super.key,
14+
required Satelites satelite,
15+
required Metrics metric,
16+
}) : _satelite = satelite,
17+
_metric = metric;
18+
19+
@override
20+
State<ChartsPage> createState() => _ChartsPageState();
21+
}
22+
23+
class _ChartsPageState extends State<ChartsPage> {
24+
static final dayFormat = DateFormat("dd-MM-yyyy");
25+
ChartRanges _range = ChartRanges.days;
26+
DateTime _actualDate = DateTime.now();
27+
late Future<List<SateliteInfo>> _futureSateliteHistory;
28+
29+
void updateSateliteInfo() {
30+
_futureSateliteHistory = fetchSateliteHistory(widget._satelite, _range);
31+
}
32+
33+
@override
34+
void initState() {
35+
super.initState();
36+
updateSateliteInfo();
37+
}
38+
39+
@override
40+
void didUpdateWidget(covariant ChartsPage oldWidget) {
41+
super.didUpdateWidget(oldWidget);
42+
updateSateliteInfo();
43+
}
44+
45+
Future<void> _selectDate(BuildContext context) async {
46+
final DateTime? picked = await showDatePicker(
47+
context: context,
48+
initialDate: _actualDate,
49+
firstDate: DateTime(2022, 8),
50+
lastDate: DateTime.now(),
51+
);
52+
53+
if (picked != null && picked != _actualDate) {
54+
setState(() => _actualDate = picked);
55+
}
56+
}
57+
58+
@override
59+
Widget build(BuildContext context) {
60+
var colorScheme = Theme.of(context).colorScheme;
61+
62+
return Scaffold(
63+
appBar: AppBar(title: Text(widget._metric.name.capitalize())),
64+
body: Padding(
65+
padding: const EdgeInsets.all(8.0),
66+
child: Column(
67+
children: [
68+
switch (_range) {
69+
ChartRanges.days => ElevatedButton(
70+
onPressed: () => _selectDate(context),
71+
child: Text(dayFormat.format(_actualDate)),
72+
),
73+
ChartRanges.weeks => const Text('Last week'),
74+
},
75+
Row(
76+
children: [
77+
ElevatedButton(
78+
onPressed: () => _range = ChartRanges.days,
79+
child: const Text("Dia", textAlign: TextAlign.center),
80+
),
81+
ElevatedButton(
82+
onPressed: () => _range = ChartRanges.weeks,
83+
child: const Text("Semana", textAlign: TextAlign.center),
84+
),
85+
],
86+
),
87+
Row(
88+
children: [
89+
ElevatedButton(
90+
onPressed: () => _range = ChartRanges.days,
91+
child: const Text("Dia", textAlign: TextAlign.center),
92+
),
93+
ElevatedButton(
94+
onPressed: () => _range = ChartRanges.weeks,
95+
child: const Text("Semana", textAlign: TextAlign.center),
96+
),
97+
],
98+
)
99+
],
100+
),
101+
),
102+
);
103+
}
104+
}

lib/pages/satelites/satelites_widget.dart

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import 'dart:async';
2+
13
import 'package:flutter/material.dart';
24
import 'package:morazan/api_requests/api_calls.dart';
35
import 'package:morazan/components/humidity_widget.dart';
@@ -20,17 +22,25 @@ class SatelitesPage extends StatefulWidget {
2022

2123
class _SatelitesPageState extends State<SatelitesPage> {
2224
late Future<SateliteInfo> _futureSateliteInfo;
25+
late Timer _timer;
26+
27+
void updateSateliteInfo() {
28+
_futureSateliteInfo = fetchSateliteInfo(widget._satelite);
29+
_timer = Timer.periodic(
30+
const Duration(minutes: 5), (timer) => updateSateliteInfo());
31+
}
2332

2433
@override
2534
void initState() {
2635
super.initState();
27-
_futureSateliteInfo = fetchSateliteInfo(widget._satelite);
36+
updateSateliteInfo();
2837
}
2938

3039
@override
3140
void didUpdateWidget(covariant SatelitesPage oldWidget) {
3241
super.didUpdateWidget(oldWidget);
33-
_futureSateliteInfo = fetchSateliteInfo(widget._satelite);
42+
_timer.cancel();
43+
updateSateliteInfo();
3444
}
3545

3646
@override

lib/util/constants.dart

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
enum Satelites {
2-
cunoc, cantel, conce
3-
}
1+
enum Satelites { cunoc, cantel, conce }
2+
3+
enum Metrics { floorHumidity, solarRadiation, temperature, wind, precipitation }
4+
5+
enum ChartRanges { days, weeks }
46

57
class MorazanApi {
68
static const apiBase = "https://cyt.cunoc.edu.gt/index.php/Ultimo-Registro/";

mockoon/station.json

+1
Large diffs are not rendered by default.

mockoon/station2.json

+156
Large diffs are not rendered by default.

pubspec.lock

+25-1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,14 @@ packages:
4949
url: "https://pub.dev"
5050
source: hosted
5151
version: "1.0.8"
52+
equatable:
53+
dependency: transitive
54+
description:
55+
name: equatable
56+
sha256: c2b87cb7756efdf69892005af546c56c0b5037f54d2a88269b4f347a505e3ca2
57+
url: "https://pub.dev"
58+
source: hosted
59+
version: "2.0.5"
5260
fake_async:
5361
dependency: transitive
5462
description:
@@ -57,6 +65,14 @@ packages:
5765
url: "https://pub.dev"
5866
source: hosted
5967
version: "1.3.1"
68+
fl_chart:
69+
dependency: "direct main"
70+
description:
71+
name: fl_chart
72+
sha256: d0f0d49112f2f4b192481c16d05b6418bd7820e021e265a3c22db98acf7ed7fb
73+
url: "https://pub.dev"
74+
source: hosted
75+
version: "0.68.0"
6076
flutter:
6177
dependency: "direct main"
6278
description: flutter
@@ -99,6 +115,14 @@ packages:
99115
url: "https://pub.dev"
100116
source: hosted
101117
version: "4.0.2"
118+
intl:
119+
dependency: "direct main"
120+
description:
121+
name: intl
122+
sha256: d6f56758b7d3014a48af9701c085700aac781a92a87a62b1333b46d8879661cf
123+
url: "https://pub.dev"
124+
source: hosted
125+
version: "0.19.0"
102126
leak_tracker:
103127
dependency: transitive
104128
description:
@@ -266,4 +290,4 @@ packages:
266290
version: "0.5.1"
267291
sdks:
268292
dart: ">=3.3.3 <4.0.0"
269-
flutter: ">=3.7.0"
293+
flutter: ">=3.16.0"

pubspec.yaml

+2
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ dependencies:
3939
flutter_carousel_widget: ^2.2.0
4040
material_symbols_icons: ^4.2719.3
4141
http: ^1.2.1
42+
fl_chart: ^0.68.0
43+
intl: ^0.19.0
4244

4345
dev_dependencies:
4446
flutter_test:

0 commit comments

Comments
 (0)