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

Fix missing link #121

Merged
merged 10 commits into from
Jun 9, 2024
1 change: 1 addition & 0 deletions lib/class/class_WarnMessage.dart
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,7 @@ class WarnMessage {
'instruction': instruction,
'areaList': areaList,
'contact': contact,
'web': web,
'notified': notified,
'read': read
};
Expand Down
2 changes: 1 addition & 1 deletion lib/class/class_userPreferences.dart
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ class UserPreferences {
bool showAllWarnings = false;
bool areWarningsFromCache = false;

String versionNumber = "0.7.0"; // shown in the about view
String versionNumber = "0.7.1"; // shown in the about view

bool activateAlertSwiss = false;
bool isFirstStart = true;
Expand Down
86 changes: 47 additions & 39 deletions lib/services/urlLauncher.dart
Original file line number Diff line number Diff line change
@@ -1,51 +1,46 @@
import "package:url_launcher/url_launcher.dart";

String extractWebAddress(String text) {
String? extractWebAddress(String text) {
if (text.startsWith("<a")) {
// extract address from HTML-formatted tag
int beginIndex = text.indexOf("href=\"") + 6;
int endIndex = text.indexOf("\"", beginIndex);
text= text.substring(beginIndex, endIndex);
text = text.substring(beginIndex, endIndex);
}

final RegExp webAddressRegEx = RegExp(r"((http|https)://)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)");
final RegExp webAddressRegEx = RegExp(
r"((http|https)://)?(www\.)?[-a-zA-Z0-9@:%._+~#=]{1,256}\.[a-zA-Z0-9()]{1,6}\b([-a-zA-Z0-9()@:%_+.~#?&/=]*)");

final RegExpMatch? match = webAddressRegEx.firstMatch(text);
if(match != null && match.start == 0 && match.end == text.length) {
if (match != null && match.start == 0 && match.end == text.length) {
return text;
}

return "invalid";
return null;
}

Future<void> launchUrlInBrowser(String url) async {
Uri webAddress = Uri.parse(extractWebAddress(url));
if (await canLaunchUrl(webAddress)) {
await launchUrl(webAddress, mode: LaunchMode.externalApplication);
} else {
throw "Could not launch $webAddress";
}
}

Future<void> makePhoneCall(String url) async {
String phoneNumber = extractPhoneNumber(url);
Uri uri = Uri.parse("tel:$phoneNumber");
String? webAddress = extractWebAddress(url);

print("Extracted phone number: $phoneNumber");
if (webAddress == null) {
return;
}

if (await canLaunchUrl(uri)) {
await launchUrl(uri);
Uri webUri = Uri.parse(webAddress);
if (await canLaunchUrl(webUri)) {
await launchUrl(webUri, mode: LaunchMode.externalApplication);
} else {
throw "Could not launch ${uri.toString()}";
throw "Could not launch ${webUri.toString()}";
}
}

String extractPhoneNumber(String text) {
try {
// remove some chars to detect even weird formatted phone numbers
RegExp expToRemove = RegExp(r"[\s/-]");
text = text.replaceAll(expToRemove, "");
String? extractPhoneNumber(String text) {
// remove some chars to detect even weird formatted phone numbers
RegExp expToRemove = RegExp(r"[\s/-]");
text = text.replaceAll(expToRemove, "");

// @todo this regex can certainly be further improved
/*
// @todo this regex can certainly be further improved
/*
* (+\d{1,3}\s?)? - This part recognizes an optional country code starting with a plus sign (+) followed by 1 to 3 digits and an optional space.
* ((\d{1,3})\s?)? - This part recognizes an optional prefix in parentheses, starting with an opening parenthesis "(", followed by 1 to 3 digits, a closing parenthesis ")" and an optional space.
* \d{1,4} - This part recognizes 1 to 4 digits for the main number.
Expand All @@ -54,21 +49,34 @@ String extractPhoneNumber(String text) {
* [\s.-]? - This part again recognizes an optional space, a hyphen "-" or a period "." as a separator.
* \d{1,9} - This part recognizes 1 to 9 digits for the third number group.
*/
RegExp phoneNumberRegex = RegExp(
r"(\+\d{1,3}\s?)?(\(\d{1,3}\)\s?)?\d{1,4}[\s.-]?\d{1,4}[\s.-]?\d{1,9}");
RegExp phoneNumberRegex = RegExp(
r"(\+\d{1,3}\s?)?(\(\d{1,3}\)\s?)?\d{1,4}[\s.-]?\d{1,4}[\s.-]?\d{1,9}");

List<String> phoneNumbers = text.split(phoneNumberRegex);
List<String?> result =
phoneNumberRegex.allMatches(text).map((e) => e.group(0)).toList();
final RegExpMatch? match = phoneNumberRegex.firstMatch(text);
if (match != null && match.start != -1 && match.end != -1) {
return text.substring(match.start, match.end);
}

return null;
}

if (result[0] != null) {
return result[0]!;
}
/// open the telephone app with the extracted phone number
/// returns true if it was successful and
/// false if there is no valid telephone number to launch
Future<bool> makePhoneCall(String url) async {
String? phoneNumber = extractPhoneNumber(url);

return phoneNumbers[0];
} catch (e) {
print("No valid phone number found: " + e.toString());
return "invalid";
if (phoneNumber == null) {
return false;
}

Uri uri = Uri.parse("tel:$phoneNumber");
print("Extracted phone number: $phoneNumber");
if (await canLaunchUrl(uri)) {
await launchUrl(uri);
return true;
} else {
throw "Could not launch ${uri.toString()}";
}
}

Expand Down
25 changes: 22 additions & 3 deletions lib/views/WarningDetailView.dart
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ class _DetailScreenState extends State<DetailScreen> {
// save places List to store new read state
saveMyPlacesList();
// cancel the notification
NotificationService.cancelOneNotification(widget._warnMessage.identifier.hashCode);
NotificationService.cancelOneNotification(
widget._warnMessage.identifier.hashCode);

List<String> generateAreaDescList(int length) {
List<String> tempList = [];
Expand Down Expand Up @@ -861,8 +862,26 @@ class _DetailScreenState extends State<DetailScreen> {
Flexible(
fit: FlexFit.loose,
child: TextButton(
onPressed: () =>
makePhoneCall(widget._warnMessage.contact),
onPressed: () async {
bool success = await makePhoneCall(
widget._warnMessage.contact);
// display error message in snackBar if
// launch was not successful
if (!success) {
final snackBar = SnackBar(
content: const Text(
'Keine Telefonnummer gefunden',
//@todo translate
style: TextStyle(color: Colors.black),
),
backgroundColor: Colors.red[100],
);
// Find the ScaffoldMessenger in the widget tree
// and use it to show a SnackBar.
ScaffoldMessenger.of(context)
.showSnackBar(snackBar);
}
},
child: Text(
replaceHTMLTags(widget._warnMessage.contact),
style: TextStyle(
Expand Down
5 changes: 5 additions & 0 deletions lib/widgets/dialogs/ChangeLogDialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@ class ChangeLogDialog extends StatelessWidget {
crossAxisAlignment: CrossAxisAlignment.start,
mainAxisSize: MainAxisSize.min,
children: [
Text(
"0.7.1 (beta)",
style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
),
Text("* Fehlerbehebungen \n"),
Text(
"0.7.0 (beta)",
style: TextStyle(fontSize: 15, fontWeight: FontWeight.bold),
Expand Down
Loading