Skip to content

Commit

Permalink
fix address coordinates not updated on auto fetch
Browse files Browse the repository at this point in the history
  • Loading branch information
LGro committed Jun 13, 2024
1 parent 542ec5b commit 12da359
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 73 deletions.
4 changes: 2 additions & 2 deletions lib/ui/profile/cubit.dart
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ class ProfileCubit extends Cubit<ProfileState> {
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);
Expand Down Expand Up @@ -102,7 +102,7 @@ class ProfileCubit extends Cubit<ProfileState> {
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}');
}
}
Expand Down
95 changes: 44 additions & 51 deletions lib/ui/profile/page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<void> showPickCirclesBottomSheet(
Expand Down Expand Up @@ -231,11 +231,12 @@ Widget addressesWithForms(BuildContext context, List<Address> 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,
Expand Down Expand Up @@ -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<ProfileCubit>().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<ProfileCubit>().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<ProfileView> {
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<ProfileCubit>().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<ProfileCubit>().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(
Expand All @@ -540,12 +539,6 @@ class ProfileViewState extends State<ProfileView> {
// 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(
Expand Down
47 changes: 27 additions & 20 deletions lib/ui/widgets/address_coordinates_form.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -22,44 +28,45 @@ class _AddressCoordinatesFormState extends State<AddressCoordinatesForm> {
@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
Widget build(BuildContext context) => Row(
children: <Widget>[
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(
Expand Down

0 comments on commit 12da359

Please sign in to comment.