From 12da359de758e3caf7b7aad139dc5a6600663a78 Mon Sep 17 00:00:00 2001 From: Lukas Grossberger Date: Thu, 13 Jun 2024 21:41:31 +0200 Subject: [PATCH] fix address coordinates not updated on auto fetch --- lib/ui/profile/cubit.dart | 4 +- lib/ui/profile/page.dart | 95 +++++++++----------- lib/ui/widgets/address_coordinates_form.dart | 47 +++++----- 3 files changed, 73 insertions(+), 73 deletions(-) diff --git a/lib/ui/profile/cubit.dart b/lib/ui/profile/cubit.dart index d6de5bb..b64e284 100644 --- a/lib/ui/profile/cubit.dart +++ b/lib/ui/profile/cubit.dart @@ -54,7 +54,7 @@ class ProfileCubit extends Cubit { emit(state.copyWith(status: ProfileStatus.create)); final newContactId = (await FlutterContacts.openExternalInsert())?.id; if (newContactId != null) { - // TODO: Can we be more trageted here, to only update the one contact id? + // TODO: Can we be more targeted here, to only update the one contact id? await contactsRepository.updateFromSystemContacts(); } await setContact(newContactId); @@ -102,7 +102,7 @@ class ProfileCubit extends Cubit { updateCoordinates( iAddress, chosenLocation.longitude, chosenLocation.latitude); } on NoResultFoundException catch (e) { - // TODO: Proper error handling + // TODO: Proper error handling with corresponding error state print('${e} ${address}'); } } diff --git a/lib/ui/profile/page.dart b/lib/ui/profile/page.dart index 01747f4..dfde7b6 100644 --- a/lib/ui/profile/page.dart +++ b/lib/ui/profile/page.dart @@ -4,7 +4,6 @@ import 'dart:async'; import 'dart:io'; -import 'package:awesome_extensions/awesome_extensions.dart'; import 'package:fast_immutable_collections/fast_immutable_collections.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; @@ -17,6 +16,7 @@ import '../widgets/address_coordinates_form.dart'; import '../widgets/avatar.dart'; import '../widgets/circles/cubit.dart'; import '../widgets/circles/widget.dart'; +// import 'address_location/widget.dart'; import 'cubit.dart'; Future showPickCirclesBottomSheet( @@ -231,11 +231,12 @@ Widget addressesWithForms(BuildContext context, List
addresses, const SizedBox(height: 8), // TODO: This is not updated when fetch coordinates emits new state AddressCoordinatesForm( - lng: locations + i: i, + longitude: locations .where((l) => labelDoesMatch(l.name, e)) .firstOrNull ?.longitude, - lat: locations + latitude: locations .where((l) => labelDoesMatch(l.name, e)) .firstOrNull ?.latitude, @@ -465,55 +466,53 @@ Widget buildProfileScrollView( ])) ])); +Widget buildUninitializedProfileScreen( + BuildContext context, bool permissionsGranted) => + Column(mainAxisAlignment: MainAxisAlignment.center, children: [ + Container( + padding: const EdgeInsets.only(left: 16, right: 16, bottom: 28), + child: const Text( + 'Welcome to Coagulate. To start sharing your ' + 'contact details with others, create a new ' + 'profile or pick an existing contact that ' + 'contains your data from the address book.', + textScaler: TextScaler.linear(1.2))), + if (permissionsGranted) ...[ + // Re-enable for Android when fixed: https://github.com/QuisApp/flutter_contacts/issues/100 + if (Platform.isIOS) + TextButton( + onPressed: context.read().promptCreate, + child: const Text('Create Profile', + textScaler: TextScaler.linear(1.2))), + if (Platform.isIOS) + Container( + padding: const EdgeInsets.all(8), + child: const Text('or', textScaler: TextScaler.linear(1.2))), + TextButton( + onPressed: context.read().promptPick, + child: const Text('Pick Contact as Profile', + textScaler: TextScaler.linear(1.2))), + ], + // TODO: Check if read access is enough / ensure + // TODO: Check if a re-request permissions button here is possible (doesn't seem to work reliably) + if (!permissionsGranted) + Container( + padding: const EdgeInsets.only(left: 16, right: 16, bottom: 28), + child: const Text( + 'Please go to your permissions settings and grant Coagulate access to your address book.')) + ]); + class ProfileViewState extends State { Widget _scaffoldBody(BuildContext context, ProfileState state) { switch (state.status) { case ProfileStatus.initial: return Center( - child: Column(mainAxisAlignment: MainAxisAlignment.center, children: [ - Container( - padding: const EdgeInsets.only(left: 16, right: 16, bottom: 28), - child: const Text( - 'Welcome to Coagulate. To start sharing your ' - 'contact details with others, create a new ' - 'profile or pick an existing contact that ' - 'contains your data from the address book.', - textScaler: TextScaler.linear(1.2))), - if (state.permissionsGranted) ...[ - // Re-enable for Android when fixed: https://github.com/QuisApp/flutter_contacts/issues/100 - if (Platform.isIOS) - TextButton( - onPressed: context.read().promptCreate, - child: const Text('Create Profile', - textScaler: TextScaler.linear(1.2))), - if (Platform.isIOS) - Container( - padding: const EdgeInsets.all(8), - child: - const Text('or', textScaler: TextScaler.linear(1.2))), - TextButton( - onPressed: context.read().promptPick, - child: const Text('Pick Contact as Profile', - textScaler: TextScaler.linear(1.2))), - ], - // TODO: Check if read access is enough / ensure - // TODO: Check if a re-request permissions button here is possible (doesn't seem to work reliably) - if (!state.permissionsGranted) - Container( - padding: - const EdgeInsets.only(left: 16, right: 16, bottom: 28), - child: const Text( - 'Please go to your permissions settings and grant Coagulate access to your address book.')) - ]), - ); + child: buildUninitializedProfileScreen( + context, state.permissionsGranted)); case ProfileStatus.create: - return const Center( - child: CircularProgressIndicator(), - ); + return const Center(child: CircularProgressIndicator()); case ProfileStatus.pick: - return const Center( - child: CircularProgressIndicator(), - ); + return const Center(child: CircularProgressIndicator()); case ProfileStatus.success: return Center( child: buildProfileScrollView( @@ -540,12 +539,6 @@ class ProfileViewState extends State { // TODO: Add generate QR code for sharing with someone who I haven't as a contact yet // TODO: Add update action; use system update view actions: [ - // IconButton( - // icon: const Icon(Icons.add_task_rounded), - // onPressed: () { - // // TODO: Manage profile sharing settings - // }, - // ), if (state.status == ProfileStatus.success && state.profileContact?.systemContact?.id != null) IconButton( diff --git a/lib/ui/widgets/address_coordinates_form.dart b/lib/ui/widgets/address_coordinates_form.dart index 1b89595..1f2c5b7 100644 --- a/lib/ui/widgets/address_coordinates_form.dart +++ b/lib/ui/widgets/address_coordinates_form.dart @@ -5,11 +5,17 @@ import 'package:flutter/material.dart'; typedef UpdateLngLatCallback = void Function(num, num); class AddressCoordinatesForm extends StatefulWidget { - final num? lng; - final num? lat; - final UpdateLngLatCallback? callback; + const AddressCoordinatesForm( + {required this.i, + super.key, + this.longitude, + this.latitude, + this.callback}); - AddressCoordinatesForm({super.key, this.lng, this.lat, this.callback}); + final int i; + final num? longitude; + final num? latitude; + final UpdateLngLatCallback? callback; @override _AddressCoordinatesFormState createState() => _AddressCoordinatesFormState(); @@ -22,10 +28,19 @@ class _AddressCoordinatesFormState extends State { @override void initState() { super.initState(); - _lngController = TextEditingController( - text: (widget.lng != null) ? widget.lng.toString() : ''); - _latController = TextEditingController( - text: (widget.lat != null) ? widget.lat.toString() : ''); + _lngController = TextEditingController(text: '${widget.longitude}'); + _latController = TextEditingController(text: '${widget.latitude}'); + } + + @override + void didUpdateWidget(covariant AddressCoordinatesForm oldWidget) { + super.didUpdateWidget(oldWidget); + if (oldWidget.longitude != widget.longitude) { + _lngController.text = '${widget.longitude}'; + } + if (oldWidget.latitude != widget.latitude) { + _latController.text = '${widget.latitude}'; + } } @override @@ -33,33 +48,25 @@ class _AddressCoordinatesFormState extends State { children: [ Expanded( child: TextFormField( + key: Key('addressCoordinatesForm_${widget.i}Longitude'), controller: _lngController, + keyboardType: TextInputType.number, decoration: const InputDecoration( labelText: 'Longitude', border: OutlineInputBorder(), isDense: true), - onChanged: (value) => setState(() { - _lngController - ..text = value - ..selection = TextSelection.fromPosition( - TextPosition(offset: value.length)); - }), ), ), const SizedBox(width: 10), Expanded( child: TextFormField( + key: Key('addressCoordinatesForm_${widget.i}Latitude'), controller: _latController, + keyboardType: TextInputType.number, decoration: const InputDecoration( labelText: 'Latitude', border: OutlineInputBorder(), isDense: true), - onChanged: (value) => setState(() { - _latController - ..text = value - ..selection = TextSelection.fromPosition( - TextPosition(offset: value.length)); - }), ), ), TextButton(