Advertisement
mrblab

lib/services/app_service.dart

Sep 24th, 2024
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Dart 7.62 KB | None | 0 0
  1. import 'dart:io';
  2. import 'package:easy_localization/easy_localization.dart';
  3. import 'package:firebase_core/firebase_core.dart';
  4. import 'package:flutter/services.dart';
  5. import 'package:flutter_displaymode/flutter_displaymode.dart';
  6. // ignore: depend_on_referenced_packages
  7. import 'package:html/parser.dart';
  8. import 'package:flutter/material.dart';
  9. import 'package:flutter_web_browser/flutter_web_browser.dart';
  10. import 'package:hive/hive.dart';
  11. import 'package:html_unescape/html_unescape.dart';
  12. import 'package:jiffy/jiffy.dart';
  13. import 'package:path_provider/path_provider.dart';
  14. import 'package:provider/provider.dart';
  15. import 'package:reading_time/reading_time.dart';
  16. import 'package:store_redirect/store_redirect.dart';
  17. import 'package:url_launcher/url_launcher.dart';
  18. import 'package:wordpress_app/blocs/settings_bloc.dart';
  19. import 'package:wordpress_app/blocs/theme_bloc.dart';
  20. import 'package:wordpress_app/config/config.dart';
  21. import 'package:wordpress_app/models/app_config_model.dart';
  22. import 'package:wordpress_app/models/article.dart';
  23. import 'package:wordpress_app/utils/toast.dart';
  24. import '../config/language_config.dart';
  25. import '../constants/constant.dart';
  26. import 'package:timeago/timeago.dart' as timeago;
  27.  
  28. class AppService {
  29.   Future<bool> checkInternet() async {
  30.     bool internet = false;
  31.     try {
  32.       final result = await InternetAddress.lookup('google.com');
  33.       if (result.isNotEmpty && result[0].rawAddress.isNotEmpty) {
  34.         debugPrint('connected');
  35.         internet = true;
  36.       }
  37.     } on SocketException catch (_) {
  38.       debugPrint('not connected');
  39.       internet = false;
  40.     }
  41.     return internet;
  42.   }
  43.  
  44.   Future addToRecentSearchList(String newSerchItem) async {
  45.     final hive = await Hive.openBox(Constants.resentSearchTag);
  46.     hive.add(newSerchItem);
  47.   }
  48.  
  49.   Future removeFromRecentSearchList(int selectedIndex) async {
  50.     final hive = await Hive.openBox(Constants.resentSearchTag);
  51.     hive.deleteAt(selectedIndex);
  52.   }
  53.  
  54.   Future openLink(context, String url) async {
  55.     final Uri uri = Uri.parse(url);
  56.     if (await canLaunchUrl(uri)) {
  57.       launchUrl(uri, mode: LaunchMode.externalApplication);
  58.     } else {
  59.       openToast1("Can't launch the url");
  60.     }
  61.   }
  62.  
  63.   Future openEmailSupport(context, String email) async {
  64.     final Uri uri = Uri(
  65.       scheme: 'mailto',
  66.       path: email.trim(),
  67.       query: 'subject=About ${Config.appName}&body=', //add subject and body here
  68.     );
  69.  
  70.     if (await canLaunchUrl(uri)) {
  71.       await launchUrl(uri);
  72.     } else {
  73.       openToast1("Can't open the email app");
  74.     }
  75.   }
  76.  
  77.   Future sendCommentReportEmail(context, String postTitle, String comment, String postLink, String userName, String supportEmail) async {
  78.     final String formattedComment = AppService.getNormalText(comment);
  79.     final Uri uri = Uri(
  80.       scheme: 'mailto',
  81.       path: supportEmail,
  82.       query:
  83.           'subject=${Config.appName} - Comment Report&body=$userName has reported on a comment on $postTitle.\nReported Comment: $formattedComment\nPost Link: $postLink', //add subject and body here
  84.     );
  85.     if (await canLaunchUrl(uri)) {
  86.       await launchUrl(uri);
  87.     } else {
  88.       openToast1("Can't open the email app");
  89.     }
  90.   }
  91.  
  92.   Future openLinkWithCustomTab(BuildContext context, String url) async {
  93.     try {
  94.       await FlutterWebBrowser.openWebPage(
  95.         url: url,
  96.         customTabsOptions: CustomTabsOptions(
  97.           colorScheme: context.read<ThemeBloc>().darkTheme! ? CustomTabsColorScheme.dark : CustomTabsColorScheme.light,
  98.           instantAppsEnabled: true,
  99.           showTitle: true,
  100.           urlBarHidingEnabled: true,
  101.         ),
  102.         safariVCOptions: const SafariViewControllerOptions(
  103.           barCollapsingEnabled: true,
  104.           dismissButtonStyle: SafariViewControllerDismissButtonStyle.close,
  105.           modalPresentationCapturesStatusBarAppearance: true,
  106.         ),
  107.       );
  108.     } catch (e) {
  109.       openToast1('Cant launch the url');
  110.       debugPrint(e.toString());
  111.     }
  112.   }
  113.  
  114.   Future launchAppReview(context) async {
  115.     final SettingsBloc sb = Provider.of<SettingsBloc>(context, listen: false);
  116.     StoreRedirect.redirect(androidAppId: sb.packageName, iOSAppId: Config.iOSAppID);
  117.     if (Platform.isIOS) {
  118.       if (Config.iOSAppID == '000000') {
  119.         openToast1('The iOS version is not available on the AppStore yet');
  120.       }
  121.     }
  122.   }
  123.  
  124.   static getNormalText(String text) {
  125.     return HtmlUnescape().convert(parse(text).documentElement!.text);
  126.   }
  127.  
  128.   static String getVimeoId(String videoUrl) {
  129.     RegExp regExp = RegExp(
  130.         r'(?:http|https)?:?\/?\/?(?:www\.)?(?:player\.)?vimeo\.com\/(?:channels\/(?:\w+\/)?|groups\/(?:[^\/]*)\/videos\/|video\/|)(\d+)(?:|\/\?)');
  131.     return regExp.firstMatch(videoUrl)!.group(1).toString();
  132.   }
  133.  
  134.   static bool isEmailValid(email) {
  135.     return RegExp(r"^[a-zA-Z0-9.a-zA-Z0-9.!#$%&'*+-/=?^_`{|}~]+@[a-zA-Z0-9]+\.[a-zA-Z]+").hasMatch(email);
  136.   }
  137.  
  138.   static String getTime(DateTime dateTime, BuildContext context) {
  139.     final currentLocale = EasyLocalization.of(context)!.currentLocale;
  140.     final data = timeago.format(dateTime, locale: currentLocale.toString());
  141.     return data;
  142.   }
  143.  
  144.   static String getDate(DateTime dateTime) {
  145.     final date = Jiffy.parseFromDateTime(dateTime).yMMMd;
  146.     return date;
  147.   }
  148.  
  149.   static bool isVideoPost(Article article) {
  150.     if (article.videoPost! && (article.videoUrl != '' || article.youtubeUrl != '' || article.viemoUrl != '')) {
  151.       return true;
  152.     } else {
  153.       return false;
  154.     }
  155.   }
  156.  
  157.   // configuring list to string for blocked categories
  158.   static String getIds(List<int> ids) {
  159.     if (ids.isEmpty) {
  160.       return '0';
  161.     } else {
  162.       final rawIds = ids.join(',');
  163.       return rawIds;
  164.     }
  165.   }
  166.  
  167.   static String getReadingTime(String text) {
  168.     if (text == '') {
  169.       return '';
  170.     } else {
  171.       var reader = readingTime(getNormalText(text));
  172.       return reader.msg;
  173.     }
  174.   }
  175.  
  176.   static bool nativeAdVisible(String placement, ConfigModel configs) {
  177.     if (configs.admobEnabled && configs.nativeAdsEnabled && configs.nativeAdPlacements.contains(placement)) {
  178.       return true;
  179.     } else {
  180.       return false;
  181.     }
  182.   }
  183.  
  184.   static bool customAdVisible(String placement, ConfigModel configs) {
  185.     if (!configs.nativeAdsEnabled && configs.customAdsEnabled && configs.customAdAssetUrl != '' && configs.customAdPlacements.contains(placement)) {
  186.       return true;
  187.     } else {
  188.       return false;
  189.     }
  190.   }
  191.  
  192.   static void setDisplayToHighRefreshRate() async {
  193.     if (Platform.isAndroid) {
  194.       try {
  195.         FlutterDisplayMode.setHighRefreshRate();
  196.       } catch (e) {
  197.         debugPrint('error on refresh rate');
  198.       }
  199.     } else {
  200.       debugPrint('Refresh rate not supported on ios');
  201.     }
  202.   }
  203.  
  204.   static Future appStartupFunctions() async {
  205.     WidgetsFlutterBinding.ensureInitialized();
  206.     await EasyLocalization.ensureInitialized();
  207.     await Firebase.initializeApp();
  208.  
  209.     Directory directory = await getApplicationDocumentsDirectory();
  210.     // Hive.init(directory.path);
  211.     Hive
  212.       ..init(directory.path)
  213.       ..openBox(Constants.bookmarkTag)
  214.       ..openBox(Constants.resentSearchTag)
  215.       ..openBox(Constants.notificationTag);
  216.     // await Hive.openBox(Constants.bookmarkTag);
  217.     // await Hive.openBox(Constants.resentSearchTag);
  218.     // await Hive.openBox(Constants.notificationTag);
  219.     LanguageConfig.setLocaleMessagesForTimeAgo();
  220.     AppService.setDisplayToHighRefreshRate();
  221.  
  222.     SystemChrome.setSystemUIOverlayStyle(const SystemUiOverlayStyle(
  223.       statusBarColor: Colors.transparent,
  224.       statusBarIconBrightness: Brightness.dark,
  225.     ));
  226.   }
  227. }
  228.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement