Skip to content

Commit

Permalink
Encapsulate Url Launch with Toast
Browse files Browse the repository at this point in the history
  • Loading branch information
rubuy-74 authored and bdmendes committed Oct 10, 2023
1 parent 7dd89dd commit 6d9bd4f
Show file tree
Hide file tree
Showing 11 changed files with 50 additions and 16 deletions.
18 changes: 18 additions & 0 deletions uni/lib/controller/networking/url_launcher.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'dart:async';

import 'package:flutter/cupertino.dart';
import 'package:uni/generated/l10n.dart';
import 'package:uni/view/common_widgets/toast_message.dart';
import 'package:url_launcher/url_launcher.dart';

Future<void> launchUrlWithToast(BuildContext context, String url) async {
final validUrl = Uri.parse(url);
if (url != '' && canLaunchUrl(validUrl) as bool) {
await launchUrl(Uri.parse(url));
} else {
await ToastMessage.error(
context,
S.of(context).no_link,
);
}
}
2 changes: 2 additions & 0 deletions uni/lib/generated/intl/messages_en.dart
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,8 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("No favorite restaurants"),
"no_info": MessageLookupByLibrary.simpleMessage(
"There is no information to display"),
"no_link":
MessageLookupByLibrary.simpleMessage("We couldn\'t open the link"),
"no_menu_info": MessageLookupByLibrary.simpleMessage(
"There is no information available about meals"),
"no_menus": MessageLookupByLibrary.simpleMessage(
Expand Down
2 changes: 2 additions & 0 deletions uni/lib/generated/intl/messages_pt_PT.dart
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ class MessageLookup extends MessageLookupByLibrary {
MessageLookupByLibrary.simpleMessage("Sem restaurantes favoritos"),
"no_info": MessageLookupByLibrary.simpleMessage(
"Não existem informações para apresentar"),
"no_link": MessageLookupByLibrary.simpleMessage(
"Não conseguimos abrir o link"),
"no_menu_info": MessageLookupByLibrary.simpleMessage(
"Não há informação disponível sobre refeições"),
"no_menus": MessageLookupByLibrary.simpleMessage(
Expand Down
10 changes: 10 additions & 0 deletions uni/lib/generated/l10n.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions uni/lib/l10n/intl_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@
"@no_selected_exams": {},
"occurrence_type": "Type of occurrence",
"@occurrence_type": {},
"no_link": "We couldn't open the link",
"@no_link": {},
"other_links": "Other links",
"@other_links": {},
"pass_change_request": "For security reasons, passwords must be changed periodically.",
Expand Down
2 changes: 2 additions & 0 deletions uni/lib/l10n/intl_pt_PT.arb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,8 @@
"@no_selected_exams": {},
"occurrence_type": "Tipo de ocorrência",
"@occurrence_type": {},
"no_link": "Não conseguimos abrir o link",
"@no_link": {},
"other_links": "Outros links",
"@other_links": {},
"pass_change_request": "Por razões de segurança, as palavras-passe têm de ser alteradas periodicamente.",
Expand Down
12 changes: 5 additions & 7 deletions uni/lib/view/about/widgets/terms_and_conditions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,32 +2,30 @@ import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_markdown/flutter_markdown.dart';
import 'package:uni/controller/load_static/terms_and_conditions.dart';
import 'package:uni/controller/networking/url_launcher.dart';
import 'package:uni/generated/l10n.dart';
import 'package:url_launcher/url_launcher.dart';

class TermsAndConditions extends StatelessWidget {
const TermsAndConditions({super.key});

@override
Widget build(BuildContext context) {
var termsAndConditionsSaved = S.of(context).loading_terms;
String? termsAndConditionsSaved = S.of(context).loading_terms;
final termsAndConditionsFuture = fetchTermsAndConditions();
return FutureBuilder(
future: termsAndConditionsFuture,
builder:
(BuildContext context, AsyncSnapshot<String> termsAndConditions) {
if (termsAndConditions.connectionState == ConnectionState.done &&
termsAndConditions.hasData) {
termsAndConditionsSaved = termsAndConditions.data!;
termsAndConditionsSaved = termsAndConditions.data;
}
return MarkdownBody(
styleSheet: MarkdownStyleSheet(),
shrinkWrap: false,
data: termsAndConditionsSaved,
data: termsAndConditionsSaved!,
onTapLink: (text, url, title) async {
if (await canLaunchUrl(Uri.parse(url!))) {
await launchUrl(Uri.parse(url));
}
await launchUrlWithToast(context, url!);
},
);
},
Expand Down
8 changes: 4 additions & 4 deletions uni/lib/view/schedule/widgets/schedule_slot.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:uni/controller/networking/network_router.dart';
import 'package:uni/controller/networking/url_launcher.dart';
import 'package:uni/view/common_widgets/row_container.dart';
import 'package:url_launcher/url_launcher.dart';

class ScheduleSlot extends StatelessWidget {
const ScheduleSlot({
Expand Down Expand Up @@ -113,9 +113,9 @@ class SubjectButtonWidget extends StatelessWidget {
'UCURR_GERAL.FICHA_UC_VIEW?pv_ocorrencia_id=$occurrId';
}

Future<void> _launchURL() async {
Future<void> _launchURL(BuildContext context) async {
final url = toUcLink(occurrId);
await launchUrl(Uri.parse(url));
await launchUrlWithToast(context, url);
}

@override
Expand All @@ -133,7 +133,7 @@ class SubjectButtonWidget extends StatelessWidget {
color: Colors.grey,
alignment: Alignment.centerRight,
tooltip: 'Abrir página da UC no browser',
onPressed: _launchURL,
onPressed: () => _launchURL(context),
),
],
);
Expand Down
2 changes: 1 addition & 1 deletion uni/lib/view/terms_and_condition_dialog.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ class TermsAndConditionDialog {
),
),
],
)
),
],
),
);
Expand Down
4 changes: 2 additions & 2 deletions uni/lib/view/useful_info/widgets/link_button.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:uni/controller/networking/url_launcher.dart';

class LinkButton extends StatelessWidget {
const LinkButton({
Expand Down Expand Up @@ -27,7 +27,7 @@ class LinkButton extends StatelessWidget {
.headlineSmall!
.copyWith(decoration: TextDecoration.underline),
),
onTap: () => launchUrl(Uri.parse(link)),
onTap: () => launchUrlWithToast(context, link),
),
)
],
Expand Down
4 changes: 2 additions & 2 deletions uni/lib/view/useful_info/widgets/text_components.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:flutter/material.dart';
import 'package:url_launcher/url_launcher.dart';
import 'package:uni/controller/networking/url_launcher.dart';

Container h1(String text, BuildContext context, {bool initial = false}) {
final marginTop = initial ? 15.0 : 30.0;
Expand Down Expand Up @@ -44,7 +44,7 @@ Container infoText(
.bodyLarge!
.apply(color: Theme.of(context).colorScheme.tertiary),
),
onTap: () => link != '' ? launchUrl(Uri.parse(link)) : null,
onTap: () => launchUrlWithToast(context, link),
),
),
);
Expand Down

0 comments on commit 6d9bd4f

Please sign in to comment.