Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Account for null taxonomies in species #155

Merged
merged 1 commit into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/app/lib/models/species.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Species {
final DocumentId id;
DocumentViewId viewId;

TaxonomySpecies species;
TaxonomySpecies? species;
String description;

Species(
Expand All @@ -27,8 +27,10 @@ class Species {
return Species(
id: result['meta']['documentId'] as DocumentId,
viewId: result['meta']['viewId'] as DocumentViewId,
species: TaxonomySpecies.fromJson(
result['fields']['species'] as Map<String, dynamic>),
species: result['fields']['species'] != null
? TaxonomySpecies.fromJson(
result['fields']['species'] as Map<String, dynamic>)
: null,
description: result['fields']['description'] as String);
}

Expand Down Expand Up @@ -68,8 +70,10 @@ class Species {
}

Future<void> delete() async {
// Delete the taxonomy_species document this species relates to first
await species.delete();
if (species != null) {
// Delete the taxonomy_species document this species relates to first
await species!.delete();
}
// Then delete the bee_species document
await deleteSpecies(viewId);
}
Expand Down
6 changes: 3 additions & 3 deletions packages/app/lib/ui/screens/sighting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ class _SightingProfileState extends State<SightingProfile> {
}

Future<void> _updateSpecies(TaxonomySpecies? taxon) async {
if (sighting.species?.species.id == taxon?.id) {
if (sighting.species?.species?.id == taxon?.id) {
// Nothing has changed
return;
}
Expand Down Expand Up @@ -199,8 +199,8 @@ class SightingProfileTitle extends StatelessWidget {
Widget build(BuildContext context) {
List<String> title = [];

if (sighting.species != null) {
title.add(sighting.species!.species.name);
if (sighting.species != null && sighting.species!.species != null) {
title.add(sighting.species!.species!.name);
}

if (sighting.localName != null) {
Expand Down
8 changes: 6 additions & 2 deletions packages/app/lib/ui/screens/species.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class _SpeciesProfileState extends State<SpeciesProfile> {
}

Future<void> _updateTaxon(TaxonomySpecies? taxon) async {
if (species.species.id == taxon?.id) {
if (species.species?.id == taxon?.id) {
// Nothing has changed
return;
} else if (taxon != null) {
Expand All @@ -120,14 +120,18 @@ class _SpeciesProfileState extends State<SpeciesProfile> {

@override
Widget build(BuildContext context) {
final t = AppLocalizations.of(context)!;

return Container(
padding: const EdgeInsets.only(top: 0.0, right: 20.0, left: 20.0),
decoration: const SeaWavesBackground(),
child: CustomScrollView(
slivers: [
SliverList(
delegate: SliverChildListDelegate([
SpeciesProfileTitle(species.species.name),
SpeciesProfileTitle(species.species != null
? species.species!.name
: t.sightingUnspecified),
const SizedBox(height: 100.0),
SpeciesField(
species.species,
Expand Down
4 changes: 2 additions & 2 deletions packages/app/lib/ui/widgets/sighting_card.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ class _SightingCardState extends State<SightingCard> {
Widget get _title {
String title = AppLocalizations.of(context)!.sightingUnspecified;

if (widget.species != null) {
title = widget.species!.species.name;
if (widget.species != null && widget.species!.species != null) {
title = widget.species!.species!.name;
} else if (widget.localName != null) {
title = widget.localName!.name;
}
Expand Down
13 changes: 9 additions & 4 deletions packages/app/lib/ui/widgets/species_card.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: AGPL-3.0-or-later

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/p2panda/publish.dart';
Expand All @@ -13,7 +14,7 @@ import 'package:app/ui/widgets/image.dart';

class SpeciesCard extends StatefulWidget {
final DocumentId id;
final TaxonomySpecies taxonomySpecies;
final TaxonomySpecies? taxonomySpecies;

final VoidCallback onTap;

Expand All @@ -30,9 +31,13 @@ class SpeciesCard extends StatefulWidget {
class _SpeciesCardState extends State<SpeciesCard> {
bool isSelected = false;

Widget get _title {
Widget _title(BuildContext context) {
final t = AppLocalizations.of(context)!;

return Text(
widget.taxonomySpecies.name,
widget.taxonomySpecies != null
? widget.taxonomySpecies!.name
: t.sightingUnspecified,
textAlign: TextAlign.center,
overflow: TextOverflow.ellipsis,
style: const TextStyle(
Expand Down Expand Up @@ -111,7 +116,7 @@ class _SpeciesCardState extends State<SpeciesCard> {
padding: const EdgeInsets.only(
top: 8.0, right: 6.0, bottom: 10.0, left: 6.0),
alignment: AlignmentDirectional.center,
child: _title,
child: _title(context),
),
])),
);
Expand Down