-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathsighting.dart
More file actions
284 lines (235 loc) · 8.72 KB
/
Copy pathsighting.dart
File metadata and controls
284 lines (235 loc) · 8.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
// SPDX-License-Identifier: AGPL-3.0-or-later
import 'package:app/ui/widgets/location_field.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:app/io/files.dart';
import 'package:app/models/local_names.dart';
import 'package:app/models/sightings.dart';
import 'package:app/models/species.dart';
import 'package:app/models/taxonomy_species.dart';
import 'package:app/ui/colors.dart';
import 'package:app/ui/widgets/autocomplete.dart';
import 'package:app/ui/widgets/error_card.dart';
import 'package:app/ui/widgets/hive_location_field.dart';
import 'package:app/ui/widgets/image_carousel.dart';
import 'package:app/ui/widgets/local_name_field.dart';
import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:app/ui/widgets/scaffold.dart';
import 'package:app/ui/widgets/sighting_popup_menu.dart';
import 'package:app/ui/widgets/species_field.dart';
import 'package:app/ui/widgets/text_field.dart';
import 'package:app/ui/widgets/used_for_field.dart';
class SightingScreen extends StatefulWidget {
final String documentId;
const SightingScreen({super.key, required this.documentId});
@override
State<SightingScreen> createState() => _SightingScreenState();
}
class _SightingScreenState extends State<SightingScreen> {
@override
Widget build(BuildContext context) {
return Query(
options: QueryOptions(document: gql(sightingQuery(widget.documentId))),
builder: (result, {VoidCallback? refetch, FetchMore? fetchMore}) {
Sighting? sighting;
if (!result.hasException && !result.isLoading) {
sighting = Sighting.fromJson(
result.data?['sighting'] as Map<String, dynamic>);
}
return MeliScaffold(
title: AppLocalizations.of(context)!.sightingScreenTitle,
backgroundColor: MeliColors.electric,
appBarColor: MeliColors.electric,
actionRight: sighting != null
? SightingPopupMenu(sighting: sighting)
: null,
body: SingleChildScrollView(
child: Builder(builder: (BuildContext context) {
if (result.hasException) {
return ErrorCard(message: result.exception.toString());
}
if (result.isLoading) {
return const Center(
child: SizedBox(
width: 50,
height: 50,
child: CircularProgressIndicator(
color: MeliColors.black)),
);
}
return SightingProfile(sighting!);
}),
));
});
}
}
class SightingProfile extends StatefulWidget {
final Sighting initialValue;
const SightingProfile(this.initialValue, {super.key});
@override
State<SightingProfile> createState() => _SightingProfileState();
}
class _SightingProfileState extends State<SightingProfile> {
/// Mutable sighting instance. Can be changed by this stateful widget.
late Sighting sighting;
@override
void initState() {
sighting = widget.initialValue;
super.initState();
}
void _setUpdateFlag() {
// Set flag for other widgets to tell them that they might need to re-render
// their data. This will make sure that our updates are reflected in the UI
RefreshProvider.of(context).setDirty(RefreshKeys.UpdatedSighting);
}
Future<void> _updateLocalName(AutocompleteItem? item) async {
List<LocalName> localNames = [];
if (item == null) {
// Remove local name from sighting
} else if (item.documentId == null) {
// Create new local name to assign it then to sighting. This method checks
// for existing local names with the same value before
localNames.add(await createDeduplicatedLocalName(item.value));
} else if (item.documentId != null) {
// Assign existing local name to sighting
localNames.add(LocalName(
id: item.documentId!, viewId: item.viewId!, name: item.value));
}
await sighting.update(localNames: localNames);
_setUpdateFlag();
setState(() {});
}
void _updateHiveLocation() {
_setUpdateFlag();
}
void _updateUsedFor() {
_setUpdateFlag();
}
Future<void> _updateSpecies(TaxonomySpecies? taxon) async {
if (sighting.species?.species?.id == taxon?.id) {
// Nothing has changed
return;
}
if (taxon == null) {
// Remove species assignment
await sighting.update(species: []);
} else {
// Assign species, create it before if it doesn't exist yet
final species = await Species.upsert(taxon);
await sighting.update(species: [species]);
}
_setUpdateFlag();
setState(() {});
}
Future<void> _updateComment(String? comment) async {
if (sighting.comment == comment) {
// Nothing has changed
return;
}
await sighting.update(comment: comment);
setState(() {});
}
void _updateLocation(Coordinates coordinates) async {
if (sighting.latitude == coordinates.latitude &&
sighting.longitude == coordinates.longitude) {
// Nothing has changed
return;
}
await sighting.update(
latitude: coordinates.latitude, longitude: coordinates.longitude);
setState(() {});
}
@override
Widget build(BuildContext context) {
final imagePaths =
sighting.images.map((image) => '$BLOBS_BASE_PATH/${image.id}').toList();
return Container(
padding: const EdgeInsets.only(
left: 20.0, right: 20.0, top: 80.0, bottom: 20.0),
decoration: const SeaWavesBackground(),
child: Wrap(runSpacing: 20.0, children: [
SightingProfileTitle(sighting),
ImageCarousel(imagePaths: imagePaths),
LocalNameField(
sighting.localName,
onUpdate: _updateLocalName,
),
SpeciesField(
sighting.species?.species,
onUpdate: _updateSpecies,
),
UsedForField(sightingId: sighting.id, onUpdate: _updateUsedFor),
HiveLocationField(
sightingId: sighting.id, onUpdate: _updateHiveLocation),
EditableTextField(sighting.comment,
title: AppLocalizations.of(context)!.noteCardTitle,
onUpdate: _updateComment),
LocationField(
// Not the best way to check if a position has not been set, but works for now
coordinates: sighting.latitude == 0 && sighting.longitude == 0
? null
: (latitude: sighting.latitude, longitude: sighting.longitude),
onUpdate: _updateLocation),
]),
);
}
}
class SightingProfileTitle extends StatelessWidget {
final Sighting sighting;
const SightingProfileTitle(this.sighting, {super.key});
@override
Widget build(BuildContext context) {
List<String> title = [];
if (sighting.species != null && sighting.species!.species != null) {
title.add(sighting.species!.species!.name);
}
if (sighting.localName != null) {
title.add('"${sighting.localName!.name}"');
}
if (title.isEmpty) {
title.add(AppLocalizations.of(context)!.sightingUnspecified);
}
final id = sighting.id.substring(sighting.id.length - 4);
final datetime = sighting.datetime;
final date = '${datetime.day}.${datetime.month}.${datetime.year}';
final time = '${datetime.hour}:${datetime.minute} ${datetime.timeZoneName}';
return Center(
child: Column(children: [
Text(title.join(' '),
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
maxLines: 2,
style: const TextStyle(
height: 1.1, fontFamily: 'Staatliches', fontSize: 24.0)),
const SizedBox(height: 10.0),
Text('$date | $time | #$id', style: const TextStyle(fontSize: 16.0))
]));
}
}
class SeaWavesBackground extends Decoration {
const SeaWavesBackground();
@override
BoxPainter createBoxPainter([VoidCallback? onChanged]) {
return _SeaWavesPainer();
}
}
class _SeaWavesPainer extends BoxPainter {
@override
void paint(Canvas canvas, Offset offset, ImageConfiguration configuration) {
final Size? bounds = configuration.size;
final paint = Paint()
..color = MeliColors.sea
..style = PaintingStyle.fill;
final path = Path();
path.moveTo(0, 0);
path.lineTo((bounds!.width / 4) * 1, -50.0);
path.lineTo((bounds.width / 4) * 2, 0.0);
path.lineTo((bounds.width / 4) * 3, -50.0);
path.lineTo((bounds.width / 4) * 4, 0.0);
path.lineTo(bounds.width, bounds.height);
path.lineTo(0, bounds.height);
path.close();
canvas.drawPath(path.shift(offset).shift(const Offset(0, 50.0)), paint);
}
}