refactor: Move external app url launching to its own function
Encapsulating logic and error handling into UiHelpers.launchExternalApplicationURLerror-handling
parent
0639ee4d57
commit
0df5940c68
|
@ -19,7 +19,6 @@ import 'package:easy_localization/easy_localization.dart';
|
|||
import 'package:selfprivacy/ui/pages/services/service_page.dart';
|
||||
import 'package:selfprivacy/utils/route_transitions/basic.dart';
|
||||
import 'package:selfprivacy/utils/ui_helpers.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
const switchableServices = [
|
||||
'bitwarden',
|
||||
|
@ -36,18 +35,6 @@ class ServicesPage extends StatefulWidget {
|
|||
State<ServicesPage> createState() => _ServicesPageState();
|
||||
}
|
||||
|
||||
void _launchURL(final url) async {
|
||||
try {
|
||||
final Uri uri = Uri.parse(url);
|
||||
await launchUrl(
|
||||
uri,
|
||||
mode: LaunchMode.externalApplication,
|
||||
);
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
}
|
||||
|
||||
class _ServicesPageState extends State<ServicesPage> {
|
||||
@override
|
||||
Widget build(final BuildContext context) {
|
||||
|
@ -181,7 +168,8 @@ class _Card extends StatelessWidget {
|
|||
Column(
|
||||
children: [
|
||||
GestureDetector(
|
||||
onTap: () => _launchURL(
|
||||
onTap: () async =>
|
||||
UiHelpers.launchExternalApplicationURL(
|
||||
'https://${service.url}',
|
||||
),
|
||||
child: Text(
|
||||
|
|
|
@ -1,8 +1,27 @@
|
|||
import 'package:selfprivacy/logic/cubit/server_installation/server_installation_cubit.dart';
|
||||
import 'package:url_launcher/url_launcher.dart';
|
||||
|
||||
/// it's ui helpers use only for ui components, don't use for logic components.
|
||||
|
||||
class UiHelpers {
|
||||
static String getDomainName(final ServerInstallationState config) =>
|
||||
config.isDomainSelected ? config.serverDomain!.domainName : 'example.com';
|
||||
|
||||
static Future<bool> launchExternalApplicationURL(final url) async {
|
||||
bool launched = false;
|
||||
|
||||
try {
|
||||
final Uri uri = Uri.parse(url);
|
||||
if (await canLaunchUrl(uri)) {
|
||||
launched = await launchUrl(
|
||||
uri,
|
||||
mode: LaunchMode.externalApplication,
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
print(e);
|
||||
}
|
||||
|
||||
return launched;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue