From bbd7c6cc1322785213b845b81e682f169e35c4c7 Mon Sep 17 00:00:00 2001 From: Camille Brulotte Date: Wed, 20 Sep 2023 13:46:24 -0400 Subject: [PATCH] Clean up --- lib/core/viewmodels/faq_viewmodel.dart | 27 ++++++++++++++-- .../viewmodels/web_link_card_viewmodel.dart | 2 +- lib/ui/views/faq_view.dart | 32 ++----------------- test/viewmodels/faq_viewmodel_test.dart | 2 +- 4 files changed, 30 insertions(+), 33 deletions(-) diff --git a/lib/core/viewmodels/faq_viewmodel.dart b/lib/core/viewmodels/faq_viewmodel.dart index 0698d0ed7..74da49568 100644 --- a/lib/core/viewmodels/faq_viewmodel.dart +++ b/lib/core/viewmodels/faq_viewmodel.dart @@ -1,13 +1,18 @@ // FLUTTER / DART / THIRD-PARTIES -import 'dart:ui'; +import 'package:flutter/material.dart'; import 'package:notredame/locator.dart'; import 'package:stacked/stacked.dart'; +import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // MANAGERS import 'package:notredame/core/managers/settings_manager.dart'; // SERVICES import 'package:notredame/core/services/launch_url_service.dart'; +import 'package:notredame/core/services/analytics_service.dart'; + +// CONSTANTS +import 'package:notredame/core/constants/app_info.dart'; class FaqViewModel extends BaseViewModel { final SettingsManager _settingsManager = locator(); @@ -20,8 +25,26 @@ class FaqViewModel extends BaseViewModel { return 'mailto:$email?subject=$subject'; } - /// used to open a website or the security view Future launchWebsite(String link, Brightness brightness) async { await _launchUrlService.launchInBrowser(link, brightness); } + + Future openMail(String addressEmail, BuildContext context) async { + var email = ""; + if (addressEmail == AppInfo.email) { + email = + mailtoStr(addressEmail, AppIntl.of(context).email_subject); + } else { + email = + mailtoStr(addressEmail, ""); + } + + final urlLaunchable = await _launchUrlService.canLaunch(email); + + if (urlLaunchable) { + await _launchUrlService.launch(email); + } else { + locator().logError("login_view", "Cannot send email."); + } + } } diff --git a/lib/core/viewmodels/web_link_card_viewmodel.dart b/lib/core/viewmodels/web_link_card_viewmodel.dart index 0f6037e51..27e285bb2 100644 --- a/lib/core/viewmodels/web_link_card_viewmodel.dart +++ b/lib/core/viewmodels/web_link_card_viewmodel.dart @@ -40,6 +40,6 @@ class WebLinkCardViewModel extends BaseViewModel { } Future launchWebView(QuickLink link) async { - _navigationService.pushNamed(RouterPaths.webView); + _navigationService.pushNamed(RouterPaths.webView, arguments: link); } } diff --git a/lib/ui/views/faq_view.dart b/lib/ui/views/faq_view.dart index 7820d7d66..c725b27ca 100644 --- a/lib/ui/views/faq_view.dart +++ b/lib/ui/views/faq_view.dart @@ -1,11 +1,7 @@ // FLUTTER / DART / THIRD-PARTIES import 'package:carousel_slider/carousel_slider.dart'; import 'package:flutter/material.dart'; -import 'package:notredame/locator.dart'; -import 'package:notredame/ui/utils/app_theme.dart'; import 'package:stacked/stacked.dart'; - -// UTILS import 'package:flutter_gen/gen_l10n/app_localizations.dart'; // VIEWMODEL @@ -14,13 +10,8 @@ import 'package:notredame/core/viewmodels/faq_viewmodel.dart'; // MODELS import 'package:notredame/core/models/faq_actions.dart'; -// SERVICES -import 'package:notredame/core/services/launch_url_service.dart'; -import 'package:notredame/core/services/analytics_service.dart'; - // CONSTANTS import 'package:notredame/core/constants/faq.dart'; -import 'package:notredame/core/constants/app_info.dart'; class FaqView extends StatefulWidget { final Color backgroundColor; @@ -34,8 +25,6 @@ class FaqView extends StatefulWidget { class _FaqViewState extends State { final Faq faq = Faq(); - final LaunchUrlService _launchUrlService = locator(); - @override Widget build(BuildContext context) => ViewModelBuilder.reactive( @@ -212,7 +201,7 @@ class _FaqViewState extends State { if (type.name == ActionType.webview.name) { openWebview(model, link); } else if (type.name == ActionType.email.name) { - openMail(model, link); + openMail(model, context, link); } }, style: ButtonStyle( @@ -287,22 +276,7 @@ class _FaqViewState extends State { model.launchWebsite(link, Theme.of(context).brightness); } - Future openMail(FaqViewModel model, String addressEmail) async { - var email = ""; - if (addressEmail == AppInfo.email) { - email = - model.mailtoStr(addressEmail, AppIntl.of(context).email_subject); - } else { - email = - model.mailtoStr(addressEmail, ""); - } - - final urlLaunchable = await _launchUrlService.canLaunch(email); - - if (urlLaunchable) { - await _launchUrlService.launch(email); - } else { - locator().logError("login_view", "Cannot send email."); - } + Future openMail(FaqViewModel model, BuildContext context, String addressEmail) async { + model.openMail(addressEmail, context); } } diff --git a/test/viewmodels/faq_viewmodel_test.dart b/test/viewmodels/faq_viewmodel_test.dart index f4b719ef0..aba4e57c4 100644 --- a/test/viewmodels/faq_viewmodel_test.dart +++ b/test/viewmodels/faq_viewmodel_test.dart @@ -2,10 +2,10 @@ import 'package:flutter/material.dart'; import 'package:flutter_test/flutter_test.dart'; import 'package:mockito/mockito.dart'; -import 'package:notredame/core/managers/settings_manager.dart'; // SERVICES / MANAGERS import 'package:notredame/core/services/launch_url_service.dart'; +import 'package:notredame/core/managers/settings_manager.dart'; // VIEW MODEL import 'package:notredame/core/viewmodels/faq_viewmodel.dart';