fluffychat/lib/pages/chat/send_location_dialog.dart

147 lines
4.3 KiB
Dart
Raw Normal View History

2021-08-01 09:53:43 +02:00
import 'dart:async';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
2021-10-26 18:50:34 +02:00
2021-08-01 09:53:43 +02:00
import 'package:flutter_gen/gen_l10n/l10n.dart';
import 'package:future_loading_dialog/future_loading_dialog.dart';
2021-10-26 18:50:34 +02:00
import 'package:geolocator/geolocator.dart';
import 'package:matrix/matrix.dart';
2021-08-01 09:53:43 +02:00
2021-11-09 21:32:16 +01:00
import 'package:fluffychat/pages/chat/events/map_bubble.dart';
2021-10-26 18:50:34 +02:00
import 'package:fluffychat/utils/platform_infos.dart';
2021-08-01 09:53:43 +02:00
class SendLocationDialog extends StatefulWidget {
final Room room;
const SendLocationDialog({
2022-01-29 12:35:03 +01:00
required this.room,
Key? key,
2021-08-01 09:53:43 +02:00
}) : super(key: key);
@override
_SendLocationDialogState createState() => _SendLocationDialogState();
}
class _SendLocationDialogState extends State<SendLocationDialog> {
bool disabled = false;
bool denied = false;
bool isSending = false;
2022-01-29 12:35:03 +01:00
Position? position;
Object? error;
2021-08-01 09:53:43 +02:00
@override
void initState() {
super.initState();
requestLocation();
}
Future<void> requestLocation() async {
if (!(await Geolocator.isLocationServiceEnabled())) {
setState(() => disabled = true);
return;
}
var permission = await Geolocator.checkPermission();
if (permission == LocationPermission.denied) {
permission = await Geolocator.requestPermission();
if (permission == LocationPermission.denied) {
setState(() => denied = true);
return;
}
}
if (permission == LocationPermission.deniedForever) {
setState(() => denied = true);
return;
}
try {
Position _position;
try {
_position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.best,
2021-10-14 18:09:30 +02:00
timeLimit: const Duration(seconds: 30),
2021-08-01 09:53:43 +02:00
);
} on TimeoutException {
_position = await Geolocator.getCurrentPosition(
desiredAccuracy: LocationAccuracy.medium,
2021-10-14 18:09:30 +02:00
timeLimit: const Duration(seconds: 30),
2021-08-01 09:53:43 +02:00
);
}
setState(() => position = _position);
} catch (e) {
setState(() => error = e);
}
}
void sendAction() async {
setState(() => isSending = true);
final body =
2022-01-29 12:35:03 +01:00
'https://www.openstreetmap.org/?mlat=${position!.latitude}&mlon=${position!.longitude}#map=16/${position!.latitude}/${position!.longitude}';
2021-08-01 09:53:43 +02:00
final uri =
2022-01-29 12:35:03 +01:00
'geo:${position!.latitude},${position!.longitude};u=${position!.accuracy}';
2021-08-01 09:53:43 +02:00
await showFutureLoadingDialog(
context: context,
future: () => widget.room.sendLocation(body, uri),
);
Navigator.of(context, rootNavigator: false).pop();
}
@override
Widget build(BuildContext context) {
Widget contentWidget;
if (position != null) {
contentWidget = MapBubble(
2022-01-29 12:35:03 +01:00
latitude: position!.latitude,
longitude: position!.longitude,
2021-08-01 09:53:43 +02:00
);
} else if (disabled) {
2022-01-29 12:35:03 +01:00
contentWidget = Text(L10n.of(context)!.locationDisabledNotice);
2021-08-01 09:53:43 +02:00
} else if (denied) {
2022-01-29 12:35:03 +01:00
contentWidget = Text(L10n.of(context)!.locationPermissionDeniedNotice);
2021-08-01 09:53:43 +02:00
} else if (error != null) {
contentWidget =
2022-01-29 12:35:03 +01:00
Text(L10n.of(context)!.errorObtainingLocation(error.toString()));
2021-08-01 09:53:43 +02:00
} else {
contentWidget = Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.center,
children: [
2021-10-14 18:09:30 +02:00
const CupertinoActivityIndicator(),
const SizedBox(width: 12),
2022-01-29 12:35:03 +01:00
Text(L10n.of(context)!.obtainingLocation),
2021-08-01 09:53:43 +02:00
],
);
}
if (PlatformInfos.isCupertinoStyle) {
return CupertinoAlertDialog(
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.shareLocation),
2021-08-01 09:53:43 +02:00
content: contentWidget,
actions: [
CupertinoDialogAction(
onPressed: Navigator.of(context, rootNavigator: false).pop,
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.cancel),
2021-08-01 09:53:43 +02:00
),
CupertinoDialogAction(
onPressed: isSending ? null : sendAction,
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.send),
2021-08-01 09:53:43 +02:00
),
],
);
}
return AlertDialog(
2022-01-29 12:35:03 +01:00
title: Text(L10n.of(context)!.shareLocation),
2021-08-01 09:53:43 +02:00
content: contentWidget,
actions: [
TextButton(
onPressed: Navigator.of(context, rootNavigator: false).pop,
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.cancel),
2021-08-01 09:53:43 +02:00
),
if (position != null)
TextButton(
onPressed: isSending ? null : sendAction,
2022-01-29 12:35:03 +01:00
child: Text(L10n.of(context)!.send),
2021-08-01 09:53:43 +02:00
),
],
);
}
}