Skip to content

Commit

Permalink
Introduce global state to keep track if data has been actually changed
Browse files Browse the repository at this point in the history
  • Loading branch information
adzialocha committed May 30, 2024
1 parent 82bfabe commit ba24807
Show file tree
Hide file tree
Showing 11 changed files with 154 additions and 48 deletions.
8 changes: 5 additions & 3 deletions packages/app/lib/app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:graphql_flutter/graphql_flutter.dart';
import 'package:shared_preferences/shared_preferences.dart';

import 'package:app/io/graphql/graphql.dart' as graphql;
import 'package:app/router.dart';
import 'package:app/ui/widgets/image_provider.dart';
import 'package:app/io/graphql/graphql.dart' as graphql;
import 'package:app/ui/widgets/refresh_provider.dart';

class MeliApp extends StatefulWidget {
const MeliApp({super.key});
Expand Down Expand Up @@ -50,7 +51,8 @@ class MeliAppState extends State<MeliApp> {

return GraphQLProvider(
client: client,
child: MeliCameraProvider(MaterialApp.router(
child: RefreshProvider(
child: MeliCameraProvider(MaterialApp.router(
// Register router for navigation
routerDelegate: router.routerDelegate,
routeInformationProvider: router.routeInformationProvider,
Expand All @@ -73,6 +75,6 @@ class MeliAppState extends State<MeliApp> {

// Disable "debug" banner shown in top right corner during development
debugShowCheckedModeBanner: false,
)));
))));
}
}
9 changes: 5 additions & 4 deletions packages/app/lib/ui/screens/all_sightings.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import 'package:app/models/sightings.dart';
import 'package:app/router.dart';
import 'package:app/ui/colors.dart';
import 'package:app/ui/widgets/fab.dart';
import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:app/ui/widgets/scaffold.dart';
import 'package:app/ui/widgets/sightings_list.dart';

Expand All @@ -31,10 +32,10 @@ class AllSightingsScreen extends StatelessWidget {
onPressed: () {
router.push(RoutePaths.createSighting.path).then((value) {
// Refresh list after returning from creating a new sighting
if (value == true) {
if (paginator.refresh != null) {
paginator.refresh!();
}
if (RefreshProvider.of(context)
.isDirty(RefreshKeys.CreatedSighting) &&
paginator.refresh != null) {
paginator.refresh!();
}
});
}),
Expand Down
5 changes: 4 additions & 1 deletion packages/app/lib/ui/screens/all_species.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:app/ui/widgets/pagination_list.dart';
import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand Down Expand Up @@ -61,7 +62,9 @@ class _SpeciesListState extends State<SpeciesList> {
router.pushNamed(RoutePaths.species.name,
pathParameters: {'documentId': species.id}).then((value) {
// Refresh list after returning from updating a species
if (widget.paginator.refresh != null) {
if (RefreshProvider.of(context)
.isDirty(RefreshKeys.UpdatedSpecies) &&
widget.paginator.refresh != null) {
widget.paginator.refresh!();
}
})
Expand Down
10 changes: 8 additions & 2 deletions packages/app/lib/ui/screens/create_sighting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import 'dart:io';

import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';

Expand Down Expand Up @@ -101,6 +102,7 @@ class _CreateSightingScreenState extends State<CreateSightingScreen> {
void _createSighting() async {
final messenger = ScaffoldMessenger.of(context);
final t = AppLocalizations.of(context)!;
final refreshProvider = RefreshProvider.of(context);

_overlayKey.currentState!.show();

Expand Down Expand Up @@ -135,8 +137,12 @@ class _CreateSightingScreenState extends State<CreateSightingScreen> {
// .. wait until sighting got materialized on node
await untilDocumentViewAvailable(SchemaIds.bee_sighting, viewId);

// Set flag to indicate to other widgets that they need to refresh their
// data to include this new sighting in their listings
refreshProvider.setDirty(RefreshKeys.CreatedSighting);

// Go back to sightings overview
router.pop(true);
router.pop();

// Show notification
messenger.showSnackBar(SnackBar(
Expand Down Expand Up @@ -171,7 +177,7 @@ class _CreateSightingScreenState extends State<CreateSightingScreen> {
_addImage(file);
} else {
// If no file was captured navigate back to all sightings screen
router.pop(false);
router.pop();
}
});
}
Expand Down
27 changes: 24 additions & 3 deletions packages/app/lib/ui/screens/sighting.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,11 @@ 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/hive_location_field.dart';
import 'package:app/ui/widgets/note_field.dart';
import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:app/ui/widgets/scaffold.dart';
import 'package:app/ui/widgets/species_field.dart';
import 'package:app/ui/widgets/used_for_field.dart';
Expand Down Expand Up @@ -83,6 +84,12 @@ class _SightingProfileState extends State<SightingProfile> {
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);
}

void _updateLocalName(AutocompleteItem? item) async {
List<LocalName> localNames = [];
if (item == null) {
Expand All @@ -97,9 +104,20 @@ class _SightingProfileState extends State<SightingProfile> {
}

await sighting.update(localNames: localNames);

_setUpdateFlag();

setState(() {});
}

void _updateHiveLocation() {
_setUpdateFlag();
}

void _updateUsedFor() {
_setUpdateFlag();
}

void _updateSpecies(TaxonomySpecies? taxon) async {
if (sighting.species?.species.id == taxon?.id) {
// Nothing has changed
Expand All @@ -115,6 +133,8 @@ class _SightingProfileState extends State<SightingProfile> {
await sighting.update(species: [species]);
}

_setUpdateFlag();

setState(() {});
}

Expand Down Expand Up @@ -149,8 +169,9 @@ class _SightingProfileState extends State<SightingProfile> {
sighting.species?.species,
onUpdate: _updateSpecies,
),
UsedForField(sightingId: sighting.id),
HiveLocationField(sightingId: sighting.id),
UsedForField(sightingId: sighting.id, onUpdate: _updateUsedFor),
HiveLocationField(
sightingId: sighting.id, onUpdate: _updateHiveLocation),
NoteField(sighting.comment, onUpdate: _updateComment),
]),
);
Expand Down
12 changes: 11 additions & 1 deletion packages/app/lib/ui/screens/species.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:app/models/taxonomy_species.dart';
import 'package:app/router.dart';
import 'package:app/ui/colors.dart';
import 'package:app/ui/widgets/error_card.dart';
import 'package:app/ui/widgets/refresh_provider.dart';
import 'package:app/ui/widgets/scaffold.dart';
import 'package:app/ui/widgets/sightings_tiles.dart';
import 'package:app/ui/widgets/species_field.dart';
Expand Down Expand Up @@ -84,12 +85,19 @@ class _SpeciesProfileState extends State<SpeciesProfile> {
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.UpdatedSpecies);
}

void _updateTaxon(TaxonomySpecies? taxon) async {
if (species.species.id == taxon?.id) {
// Nothing has changed
return;
} else if (taxon != null) {
await species.update(species: taxon);
_setUpdateFlag();
setState(() {});
}
}
Expand Down Expand Up @@ -133,7 +141,9 @@ class _SpeciesProfileState extends State<SpeciesProfile> {
// Force loading the species again after we've returned from
// an updated sighting, to make sure that aggregated data over
// all sightings is up-to-date
if (widget.refetch != null) {
if (RefreshProvider.of(context)
.isDirty(RefreshKeys.UpdatedSighting) &&
widget.refetch != null) {
widget.refetch!();
}
});
Expand Down
Loading

0 comments on commit ba24807

Please sign in to comment.