From d1e35c87bc990f35372dd878a23774731c98af46 Mon Sep 17 00:00:00 2001 From: primozratej Date: Fri, 17 May 2024 13:44:45 +0200 Subject: [PATCH] Refactor, prepare for flavored overrides --- .../animated_padding_component.dart | 7 +-- lib/components/bottom_navigation_bar.dart | 4 +- lib/components/language_switcher.dart | 6 +-- lib/components/rotating_globe.dart | 2 +- lib/models/channel_message.g.dart | 54 +++++++++---------- lib/models/event.dart | 42 +++++++-------- lib/models/event.g.dart | 15 +++--- lib/models/hum_hub.dart | 2 +- lib/pages/help/components/first_page.dart | 12 +++-- lib/pages/help/components/second_page.dart | 10 ++-- lib/pages/help/components/third_page.dart | 13 ++--- lib/pages/opener.dart | 4 +- lib/pages/web_view.dart | 3 +- lib/util/connectivity_plugin.dart | 10 ++-- lib/util/const.dart | 15 +++--- lib/util/extensions.dart | 5 +- lib/util/form_helper.dart | 2 +- lib/util/intent/intent_plugin.dart | 2 +- lib/util/log.dart | 20 +++---- lib/util/notifications/channel.dart | 2 +- lib/util/{ => openers}/opener_controller.dart | 10 ++-- .../universal_opener_controller.dart | 4 +- lib/util/override_locale.dart | 6 +-- lib/util/providers.dart | 34 +----------- lib/util/push/provider.dart | 33 ++++++++++++ lib/util/push/push_plugin.dart | 2 +- lib/util/push/register_token_plugin.dart | 17 +++--- test/opener_test.dart | 2 +- 28 files changed, 165 insertions(+), 173 deletions(-) rename lib/util/{ => openers}/opener_controller.dart (95%) rename lib/util/{ => openers}/universal_opener_controller.dart (97%) create mode 100644 lib/util/push/provider.dart diff --git a/lib/components/animated_padding_component.dart b/lib/components/animated_padding_component.dart index e055ee9..44d4e6d 100644 --- a/lib/components/animated_padding_component.dart +++ b/lib/components/animated_padding_component.dart @@ -11,13 +11,8 @@ class AnimatedPaddingComponent extends StatefulWidget { } class AnimatedPaddingComponentState extends State { - @override Widget build(BuildContext context) { - return AnimatedPadding( - duration: const Duration(milliseconds: 500), - padding: widget.padding, - child: widget.child - ); + return AnimatedPadding(duration: const Duration(milliseconds: 500), padding: widget.padding, child: widget.child); } } diff --git a/lib/components/bottom_navigation_bar.dart b/lib/components/bottom_navigation_bar.dart index f489a3d..925dea2 100644 --- a/lib/components/bottom_navigation_bar.dart +++ b/lib/components/bottom_navigation_bar.dart @@ -84,7 +84,9 @@ class BottomNavigationState extends State with TickerProviderS child: TextButton( onPressed: () => navigateForth(), child: Text( - selectedIndex != widget.pageCount - 1 ? AppLocalizations.of(context)!.next : AppLocalizations.of(context)!.connect_now, + selectedIndex != widget.pageCount - 1 + ? AppLocalizations.of(context)!.next + : AppLocalizations.of(context)!.connect_now, style: const TextStyle(color: Colors.grey), ), ), diff --git a/lib/components/language_switcher.dart b/lib/components/language_switcher.dart index c07ffe2..8eaf647 100644 --- a/lib/components/language_switcher.dart +++ b/lib/components/language_switcher.dart @@ -35,7 +35,7 @@ class _LanguageSwitcherState extends State { const SizedBox(width: 20), Text( locale.toUpperCase(), - style: TextStyle(color: primaryColor, fontSize: 16), + style: TextStyle(color: HumhubTheme.primaryColor, fontSize: 16), ), ], ), @@ -56,7 +56,7 @@ class _LanguageSwitcherState extends State { focusedBorder: InputBorder.none, ), value: _value(context), - icon: Icon(Icons.arrow_drop_down, color: primaryColor), + icon: Icon(Icons.arrow_drop_down, color: HumhubTheme.primaryColor), items: _items .mapIndexed( (localeString, index) => DropdownMenuItem( @@ -67,7 +67,7 @@ class _LanguageSwitcherState extends State { const SizedBox(width: 20), Text( localeString.toUpperCase(), - style: TextStyle(color: primaryColor, fontSize: 16), + style: TextStyle(color: HumhubTheme.primaryColor, fontSize: 16), ), ], ), diff --git a/lib/components/rotating_globe.dart b/lib/components/rotating_globe.dart index bdc5eed..da77b25 100644 --- a/lib/components/rotating_globe.dart +++ b/lib/components/rotating_globe.dart @@ -29,7 +29,7 @@ class _RotatingGlobeState extends State with TickerProviderStateM @override Widget build(BuildContext context) { - if(_controller.isCompleted){ + if (_controller.isCompleted) { _animation = widget.rotationDirection == Direction.left ? _animationSec : _animationFir; _controller.reset(); } diff --git a/lib/models/channel_message.g.dart b/lib/models/channel_message.g.dart index 9c5290d..dff8270 100644 --- a/lib/models/channel_message.g.dart +++ b/lib/models/channel_message.g.dart @@ -1,30 +1,24 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - -part of 'channel_message.dart'; - -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - -ChannelMessage _$ChannelMessageFromJson(String json) { - if (json == "humhub.mobile.hideOpener") { - return ChannelMessage( - "hideOpener", - null, - null, - ); - } else if (json == "humhub.mobile.showOpener") { - return ChannelMessage( - "showOpener", - null, - null, - ); - } else { - var jsonMap = jsonDecode(json) as Map; - return ChannelMessage( - jsonMap['type'] as String, - jsonMap['url'] as String?, - jsonMap['count'] as int?, - ); - } -} +part of 'channel_message.dart'; + +ChannelMessage _$ChannelMessageFromJson(String json) { + if (json == "humhub.mobile.hideOpener") { + return ChannelMessage( + "hideOpener", + null, + null, + ); + } else if (json == "humhub.mobile.showOpener") { + return ChannelMessage( + "showOpener", + null, + null, + ); + } else { + var jsonMap = jsonDecode(json) as Map; + return ChannelMessage( + jsonMap['type'] as String, + jsonMap['url'] as String?, + jsonMap['count'] as int?, + ); + } +} diff --git a/lib/models/event.dart b/lib/models/event.dart index 2f1935d..1b425f0 100644 --- a/lib/models/event.dart +++ b/lib/models/event.dart @@ -7,20 +7,20 @@ class PushEvent extends RemoteMessage { PushEvent(RemoteMessage message) : parsedData = PushEventData.fromJson(message.data), super( - senderId: message.senderId, - category: message.category, - collapseKey: message.collapseKey, - contentAvailable: message.contentAvailable, - data: message.data, - from: message.from, - messageId: message.messageId, - messageType: message.messageType, - mutableContent: message.mutableContent, - notification: message.notification, - sentTime: message.sentTime, - threadId: message.threadId, - ttl: message.ttl, - ); + senderId: message.senderId, + category: message.category, + collapseKey: message.collapseKey, + contentAvailable: message.contentAvailable, + data: message.data, + from: message.from, + messageId: message.messageId, + messageType: message.messageType, + mutableContent: message.mutableContent, + notification: message.notification, + sentTime: message.sentTime, + threadId: message.threadId, + ttl: message.ttl, + ); } class PushEventData { @@ -32,13 +32,13 @@ class PushEventData { final String? notificationCount; PushEventData( - this.notificationTitle, - this.notificationBody, - this.channel, - this.channelPayload, - this.redirectUrl, - this.notificationCount, - ); + this.notificationTitle, + this.notificationBody, + this.channel, + this.channelPayload, + this.redirectUrl, + this.notificationCount, + ); factory PushEventData.fromJson(Map json) => _$PushEventDataFromJson(json); } diff --git a/lib/models/event.g.dart b/lib/models/event.g.dart index 374d64b..becfc11 100644 --- a/lib/models/event.g.dart +++ b/lib/models/event.g.dart @@ -1,14 +1,13 @@ -// GENERATED CODE - DO NOT MODIFY BY HAND - part of 'event.dart'; -// ************************************************************************** -// JsonSerializableGenerator -// ************************************************************************** - PushEventData _$PushEventDataFromJson(Map json) { - return PushEventData(json['notification_title'] as String?, json['notification_body'] as String?, json['channel'] as String?, - json['channel_payload'] as String?, json['url'] as String?, json['notification_count'] as String?); + return PushEventData( + json['notification_title'] as String?, + json['notification_body'] as String?, + json['channel'] as String?, + json['channel_payload'] as String?, + json['url'] as String?, + json['notification_count'] as String?); } SimpleNotification _$SimpleNotificationFromJson(Map json) { diff --git a/lib/models/hum_hub.dart b/lib/models/hum_hub.dart index 19873a0..8203dc9 100644 --- a/lib/models/hum_hub.dart +++ b/lib/models/hum_hub.dart @@ -1,6 +1,6 @@ import 'dart:math'; import 'package:humhub/models/manifest.dart'; -import 'package:humhub/util/universal_opener_controller.dart'; +import 'package:humhub/util/openers/universal_opener_controller.dart'; enum RedirectAction { opener, webView } diff --git a/lib/pages/help/components/first_page.dart b/lib/pages/help/components/first_page.dart index cb3177a..13eb670 100644 --- a/lib/pages/help/components/first_page.dart +++ b/lib/pages/help/components/first_page.dart @@ -3,10 +3,12 @@ import 'package:humhub/components/rotating_globe.dart'; import 'package:humhub/util/const.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart'; - class FirstPage extends StatelessWidget { final bool fadeIn; - const FirstPage({Key? key, required this.fadeIn,}) : super(key: key); + const FirstPage({ + Key? key, + required this.fadeIn, + }) : super(key: key); @override Widget build(BuildContext context) { @@ -22,7 +24,7 @@ class FirstPage extends StatelessWidget { alignment: Alignment.centerLeft, child: Text( AppLocalizations.of(context)!.help_title, - style: getHeaderStyle(context), + style: HumhubTheme.getHeaderStyle(context), ), ), ), @@ -30,12 +32,12 @@ class FirstPage extends StatelessWidget { padding: const EdgeInsets.symmetric(vertical: 10), child: Text( AppLocalizations.of(context)!.help_first_par, - style: paragraphStyle, + style: HumhubTheme.paragraphStyle, ), ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.help_second_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.help_second_par, style: HumhubTheme.paragraphStyle), ), Center( child: RotatingGlobe( diff --git a/lib/pages/help/components/second_page.dart b/lib/pages/help/components/second_page.dart index 94b5a9e..2d9deb4 100644 --- a/lib/pages/help/components/second_page.dart +++ b/lib/pages/help/components/second_page.dart @@ -21,13 +21,13 @@ class SecondPage extends StatelessWidget { alignment: Alignment.centerLeft, child: Text( AppLocalizations.of(context)!.how_to_connect_title, - style: getHeaderStyle(context), + style: HumhubTheme.getHeaderStyle(context), ), ), ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.how_to_connect_first_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.how_to_connect_first_par, style: HumhubTheme.paragraphStyle), ), EaseOutContainer( fadeIn: fadeIn, @@ -79,7 +79,7 @@ class SecondPage extends StatelessWidget { ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.how_to_connect_sec_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.how_to_connect_sec_par, style: HumhubTheme.paragraphStyle), ), EaseOutContainer( fadeIn: fadeIn, @@ -94,7 +94,7 @@ class SecondPage extends StatelessWidget { style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (Set states) { - return primaryColor; + return HumhubTheme.primaryColor; }, ), ), @@ -114,7 +114,7 @@ class SecondPage extends StatelessWidget { ), Padding( padding: const EdgeInsets.symmetric(vertical: 20), - child: Text(AppLocalizations.of(context)!.how_to_connect_third_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.how_to_connect_third_par, style: HumhubTheme.paragraphStyle), ) ], ), diff --git a/lib/pages/help/components/third_page.dart b/lib/pages/help/components/third_page.dart index 6781c1f..465d551 100644 --- a/lib/pages/help/components/third_page.dart +++ b/lib/pages/help/components/third_page.dart @@ -22,21 +22,21 @@ class ThirdPage extends StatelessWidget { alignment: Alignment.centerLeft, child: Text( AppLocalizations.of(context)!.more_info_title, - style: getHeaderStyle(context), + style: HumhubTheme.getHeaderStyle(context), ), ), ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.more_info_first_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.more_info_first_par, style: HumhubTheme.paragraphStyle), ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.more_info_second_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.more_info_second_par, style: HumhubTheme.paragraphStyle), ), Padding( padding: const EdgeInsets.symmetric(vertical: 10), - child: Text(AppLocalizations.of(context)!.more_info_third_par, style: paragraphStyle), + child: Text(AppLocalizations.of(context)!.more_info_third_par, style: HumhubTheme.paragraphStyle), ), const SizedBox( height: 40, @@ -54,12 +54,13 @@ class ThirdPage extends StatelessWidget { style: ButtonStyle( backgroundColor: MaterialStateProperty.resolveWith( (Set states) { - return primaryColor; + return HumhubTheme.primaryColor; }, ), ), onPressed: () { - launchUrl(Uri.parse(AppLocalizations.of(context)!.more_info_pro_edition_url), mode: LaunchMode.platformDefault); + launchUrl(Uri.parse(AppLocalizations.of(context)!.more_info_pro_edition_url), + mode: LaunchMode.platformDefault); }, child: Center( child: Text( diff --git a/lib/pages/opener.dart b/lib/pages/opener.dart index ddbcf7c..2f0b7ad 100644 --- a/lib/pages/opener.dart +++ b/lib/pages/opener.dart @@ -8,7 +8,7 @@ import 'package:humhub/util/const.dart'; import 'package:humhub/util/form_helper.dart'; import 'package:humhub/util/intent/intent_plugin.dart'; import 'package:humhub/util/notifications/channel.dart'; -import 'package:humhub/util/opener_controller.dart'; +import 'package:humhub/util/openers/opener_controller.dart'; import 'package:humhub/util/providers.dart'; import 'package:rive/rive.dart'; import 'help/help_android.dart'; @@ -171,7 +171,7 @@ class OpenerState extends ConsumerState with SingleTickerProviderStateMi onPressed: _connectInstance, child: Text( AppLocalizations.of(context)!.connect, - style: TextStyle(color: primaryColor, fontSize: 20), + style: TextStyle(color: HumhubTheme.primaryColor, fontSize: 20), ), ), ), diff --git a/lib/pages/web_view.dart b/lib/pages/web_view.dart index bba9395..d0f73a8 100644 --- a/lib/pages/web_view.dart +++ b/lib/pages/web_view.dart @@ -16,7 +16,8 @@ import 'package:humhub/util/connectivity_plugin.dart'; import 'package:humhub/util/extensions.dart'; import 'package:humhub/util/notifications/channel.dart'; import 'package:humhub/util/providers.dart'; -import 'package:humhub/util/universal_opener_controller.dart'; +import 'package:humhub/util/openers/universal_opener_controller.dart'; +import 'package:humhub/util/push/provider.dart'; import 'package:humhub/util/router.dart'; import 'package:loggy/loggy.dart'; import 'package:permission_handler/permission_handler.dart'; diff --git a/lib/util/connectivity_plugin.dart b/lib/util/connectivity_plugin.dart index 6052083..1059957 100644 --- a/lib/util/connectivity_plugin.dart +++ b/lib/util/connectivity_plugin.dart @@ -15,11 +15,11 @@ class NoConnectionDialog extends StatelessWidget { @override Widget build(BuildContext context) { return AlertDialog( - title: Text(AppLocalizations.of(context)!.connectivity_popup_title), - content: Text(AppLocalizations.of(context)!.connectivity_popup_content), + title: Text(AppLocalizations.of(context)!.connectivity_popup_title), + content: Text(AppLocalizations.of(context)!.connectivity_popup_content), actions: [ TextButton( - child: Text(AppLocalizations.of(context)!.ok.toUpperCase()), + child: Text(AppLocalizations.of(context)!.ok.toUpperCase()), onPressed: () { Navigator.of(context).pop(); // Close the dialog }, @@ -32,7 +32,9 @@ class NoConnectionDialog extends StatelessWidget { showDialog( context: context, builder: (BuildContext context) { - return const NoConnectionDialog(); + return NoConnectionDialog( + key: context.widget.key, + ); }, ); } diff --git a/lib/util/const.dart b/lib/util/const.dart index dfb7957..71137f3 100644 --- a/lib/util/const.dart +++ b/lib/util/const.dart @@ -3,7 +3,6 @@ import 'package:flutter/material.dart'; class StorageKeys { static String humhubInstance = "humHubInstance"; static String lastInstanceUrl = "humHubLastUrl"; - } class Assets { @@ -13,11 +12,13 @@ class Assets { static String openerAnimationReverse = "assets/opener_animation_reverse.riv"; } -Color primaryColor = const Color(0xFF21a1b3); +class HumhubTheme { + static Color primaryColor = const Color(0xFF21a1b3); -TextStyle? getHeaderStyle(context) { - return Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.w600); -} + static TextStyle? getHeaderStyle(context) { + return Theme.of(context).textTheme.titleLarge?.copyWith(fontWeight: FontWeight.w600); + } -TextStyle paragraphStyle = - const TextStyle(letterSpacing: 0.5, fontWeight: FontWeight.normal, color: Colors.black, fontSize: 15); + static TextStyle paragraphStyle = + const TextStyle(letterSpacing: 0.5, fontWeight: FontWeight.normal, color: Colors.black, fontSize: 15); +} diff --git a/lib/util/extensions.dart b/lib/util/extensions.dart index f752787..17d23a5 100644 --- a/lib/util/extensions.dart +++ b/lib/util/extensions.dart @@ -18,8 +18,7 @@ extension MyWebViewController on InAppWebViewController { final exitConfirmed = await showDialog( context: context, builder: (context) => AlertDialog( - shape: const RoundedRectangleBorder( - borderRadius: BorderRadius.all(Radius.circular(10.0))), + shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(10.0))), title: Text(AppLocalizations.of(context)!.web_view_exit_popup_title), content: Text(AppLocalizations.of(context)!.web_view_exit_popup_content), actions: [ @@ -58,7 +57,7 @@ class HexColor extends Color { return int.parse(hexColor, radix: 16); } catch (e) { logError("Color from manifest is not valid use primary color"); - return primaryColor.value; + return HumhubTheme.primaryColor.value; } } diff --git a/lib/util/form_helper.dart b/lib/util/form_helper.dart index bfd0b2b..fd93f41 100644 --- a/lib/util/form_helper.dart +++ b/lib/util/form_helper.dart @@ -11,4 +11,4 @@ class FormHelper { bool validate() => key.currentState?.validate() ?? false; void save() => key.currentState?.save(); -} \ No newline at end of file +} diff --git a/lib/util/intent/intent_plugin.dart b/lib/util/intent/intent_plugin.dart index 245a852..7b98b92 100644 --- a/lib/util/intent/intent_plugin.dart +++ b/lib/util/intent/intent_plugin.dart @@ -7,7 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/pages/web_view.dart'; import 'package:humhub/util/loading_provider.dart'; import 'package:humhub/util/router.dart'; -import 'package:humhub/util/universal_opener_controller.dart'; +import 'package:humhub/util/openers/universal_opener_controller.dart'; import 'package:loggy/loggy.dart'; import 'package:receive_sharing_intent/receive_sharing_intent.dart'; import 'package:uni_links/uni_links.dart'; diff --git a/lib/util/log.dart b/lib/util/log.dart index 0bd8661..b8d6956 100644 --- a/lib/util/log.dart +++ b/lib/util/log.dart @@ -11,8 +11,7 @@ class GlobalLog extends LoggyPrinter { bool get _colorize => showColors ?? false; static final _levelColors = { - LogLevel.debug: - AnsiColor(foregroundColor: AnsiColor.grey(0.5), italic: true), + LogLevel.debug: AnsiColor(foregroundColor: AnsiColor.grey(0.5), italic: true), LogLevel.info: AnsiColor(foregroundColor: 35), LogLevel.warning: AnsiColor(foregroundColor: 214), LogLevel.error: AnsiColor(foregroundColor: 196), @@ -30,21 +29,14 @@ class GlobalLog extends LoggyPrinter { @override void onLog(LogRecord record) { final time = record.time.toIso8601String().split('T')[1]; - final callerFrame = - record.callerFrame == null ? '-' : '(${record.callerFrame?.location})'; - final logLevel = record.level - .toString() - .replaceAll('Level.', '') - .toUpperCase() - .padRight(8); + final callerFrame = record.callerFrame == null ? '-' : '(${record.callerFrame?.location})'; + final logLevel = record.level.toString().replaceAll('Level.', '').toUpperCase().padRight(8); - final color = - _colorize ? levelColor(record.level) ?? AnsiColor() : AnsiColor(); + final color = _colorize ? levelColor(record.level) ?? AnsiColor() : AnsiColor(); final prefix = levelPrefix(record.level) ?? _defaultPrefix; if (kDebugMode) { - print(color( - '$prefix$time $logLevel GLOBAL $callerFrame ${record.message}')); + print(color('$prefix$time $logLevel GLOBAL $callerFrame ${record.message}')); } if (record.stackTrace != null) { @@ -61,4 +53,4 @@ class GlobalLog extends LoggyPrinter { AnsiColor? levelColor(LogLevel level) { return _levelColors[level]; } -} \ No newline at end of file +} diff --git a/lib/util/notifications/channel.dart b/lib/util/notifications/channel.dart index b38843f..0e23786 100644 --- a/lib/util/notifications/channel.dart +++ b/lib/util/notifications/channel.dart @@ -1,6 +1,6 @@ import 'package:flutter/cupertino.dart'; import 'package:humhub/pages/web_view.dart'; -import 'package:humhub/util/universal_opener_controller.dart'; +import 'package:humhub/util/openers/universal_opener_controller.dart'; import 'package:humhub/util/router.dart'; import 'package:loggy/loggy.dart'; diff --git a/lib/util/opener_controller.dart b/lib/util/openers/opener_controller.dart similarity index 95% rename from lib/util/opener_controller.dart rename to lib/util/openers/opener_controller.dart index a5f9647..0ac8eca 100644 --- a/lib/util/opener_controller.dart +++ b/lib/util/openers/opener_controller.dart @@ -6,9 +6,9 @@ import 'package:humhub/models/manifest.dart'; import 'package:humhub/util/providers.dart'; import 'package:http/http.dart' as http; import 'package:loggy/loggy.dart'; -import 'api_provider.dart'; -import 'connectivity_plugin.dart'; -import 'form_helper.dart'; +import '../api_provider.dart'; +import '../connectivity_plugin.dart'; +import '../form_helper.dart'; class OpenerController { late AsyncValue? asyncData; @@ -93,7 +93,9 @@ class OpenerController { String currentUrl = urlTextController.text; String hash = HumHub.generateHash(32); if (lastUrl == currentUrl) hash = ref.read(humHubProvider).randomHash ?? hash; - await ref.read(humHubProvider).setInstance(HumHub(manifest: manifest, randomHash: hash, manifestUrl: manifestUrl)); + await ref + .read(humHubProvider) + .setInstance(HumHub(manifest: manifest, randomHash: hash, manifestUrl: manifestUrl)); } } diff --git a/lib/util/universal_opener_controller.dart b/lib/util/openers/universal_opener_controller.dart similarity index 97% rename from lib/util/universal_opener_controller.dart rename to lib/util/openers/universal_opener_controller.dart index 8a9f96d..1177193 100644 --- a/lib/util/universal_opener_controller.dart +++ b/lib/util/openers/universal_opener_controller.dart @@ -3,8 +3,8 @@ import 'package:http/http.dart'; import 'package:humhub/models/hum_hub.dart'; import 'package:humhub/models/manifest.dart'; import 'package:http/http.dart' as http; -import 'api_provider.dart'; -import 'connectivity_plugin.dart'; +import '../api_provider.dart'; +import '../connectivity_plugin.dart'; class UniversalOpenerController { late AsyncValue? asyncData; diff --git a/lib/util/override_locale.dart b/lib/util/override_locale.dart index 075035b..b73b0d0 100644 --- a/lib/util/override_locale.dart +++ b/lib/util/override_locale.dart @@ -15,9 +15,9 @@ class OverrideLocale extends StatefulWidget { static OverrideLocaleModel of(BuildContext context) { final result = context.dependOnInheritedWidgetOfExactType(); assert( - result != null, - 'No OverrideLocale found in context' - 'Place OverrideLocale widget as high in widget tree as possible.', + result != null, + 'No OverrideLocale found in context' + 'Place OverrideLocale widget as high in widget tree as possible.', ); return result!; } diff --git a/lib/util/providers.dart b/lib/util/providers.dart index 8704437..3bffdea 100644 --- a/lib/util/providers.dart +++ b/lib/util/providers.dart @@ -1,11 +1,9 @@ import 'dart:convert'; -import 'package:firebase_messaging/firebase_messaging.dart'; import 'package:flutter/cupertino.dart'; import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:flutter_secure_storage/flutter_secure_storage.dart'; import 'package:humhub/models/hum_hub.dart'; import 'package:humhub/models/manifest.dart'; -import 'package:humhub/util/extensions.dart'; import 'package:package_info_plus/package_info_plus.dart'; import 'const.dart'; @@ -42,7 +40,7 @@ class HumHubNotifier extends ChangeNotifier { _humHubInstance.isHideOpener = instance.isHideOpener; _humHubInstance.randomHash = instance.randomHash; _humHubInstance.appVersion = packageInfo.version; - _humHubInstance.manifestUrl = instance.manifestUrl; + _humHubInstance.manifestUrl = instance.manifestUrl; _updateSafeStorage(); notifyListeners(); } @@ -86,33 +84,3 @@ class HumHubNotifier extends ChangeNotifier { final humHubProvider = ChangeNotifierProvider((ref) { return HumHubNotifier(HumHub()); }); - -/// Remembers whether current FirebaseApp is initialized. -final firebaseInitialized = StateProvider>( - (ref) => const AsyncValue.loading(), -); - -final _pushTokenProvider = FutureProvider>( - (ref) async { - var initialized = ref.watch(firebaseInitialized.notifier).state; - if (initialized.isLoaded) { - return AsyncValue.guard(FirebaseMessaging.instance.getToken); - } - return const AsyncValue.loading(); - }, -); - -/// Provides current push token. Will wait until Firebase is initialized. -/// -/// See also: -/// * [_PushPluginState._init] -final pushTokenProvider = Provider>( - (ref) { - var provider = ref.watch(_pushTokenProvider); - return provider.when( - data: (value) => value, - error: (e, s) => AsyncValue.error(e, s), - loading: () => const AsyncValue.loading(), - ); - }, -); diff --git a/lib/util/push/provider.dart b/lib/util/push/provider.dart new file mode 100644 index 0000000..76ce8b4 --- /dev/null +++ b/lib/util/push/provider.dart @@ -0,0 +1,33 @@ +import 'package:firebase_messaging/firebase_messaging.dart'; +import 'package:flutter_riverpod/flutter_riverpod.dart'; +import 'package:humhub/util/extensions.dart'; + +/// Remembers whether current FirebaseApp is initialized. +final firebaseInitialized = StateProvider>( + (ref) => const AsyncValue.loading(), +); + +final _pushTokenProvider = FutureProvider>( + (ref) async { + var initialized = ref.watch(firebaseInitialized.notifier).state; + if (initialized.isLoaded) { + return AsyncValue.guard(FirebaseMessaging.instance.getToken); + } + return const AsyncValue.loading(); + }, +); + +/// Provides current push token. Will wait until Firebase is initialized. +/// +/// See also: +/// * [_PushPluginState._init] +final pushTokenProvider = Provider>( + (ref) { + var provider = ref.watch(_pushTokenProvider); + return provider.when( + data: (value) => value, + error: (e, s) => AsyncValue.error(e, s), + loading: () => const AsyncValue.loading(), + ); + }, +); diff --git a/lib/util/push/push_plugin.dart b/lib/util/push/push_plugin.dart index a01195a..43448db 100644 --- a/lib/util/push/push_plugin.dart +++ b/lib/util/push/push_plugin.dart @@ -7,8 +7,8 @@ import 'package:humhub/models/event.dart'; import 'package:humhub/util/notifications/channel.dart'; import 'package:humhub/util/notifications/plugin.dart'; import 'package:humhub/util/notifications/service.dart'; +import 'package:humhub/util/push/provider.dart'; import 'package:humhub/util/push/register_token_plugin.dart'; -import 'package:humhub/util/providers.dart'; import 'package:loggy/loggy.dart'; class PushPlugin extends ConsumerStatefulWidget { diff --git a/lib/util/push/register_token_plugin.dart b/lib/util/push/register_token_plugin.dart index 329c27f..f01d169 100644 --- a/lib/util/push/register_token_plugin.dart +++ b/lib/util/push/register_token_plugin.dart @@ -7,6 +7,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:humhub/util/api_provider.dart'; import 'package:humhub/util/extensions.dart'; import 'package:humhub/util/providers.dart'; +import 'package:humhub/util/push/provider.dart'; import 'package:loggy/loggy.dart'; class RegisterToken extends ConsumerWidget { @@ -42,7 +43,7 @@ class _RegisterToken extends ConsumerStatefulWidget { _RegisterTokenState createState() => _RegisterTokenState(); } -class _RegisterTokenState extends ConsumerState<_RegisterToken>{ +class _RegisterTokenState extends ConsumerState<_RegisterToken> { Future _maybeRegisterToken() async { final token = await FirebaseMessaging.instance.getToken(); if (token == null) { @@ -66,13 +67,13 @@ class _RegisterTokenState extends ConsumerState<_RegisterToken>{ } Future Function(Dio dio) _registerToken(String? token) => (dio) async { - await dio.post( - '/fcm-push/token/update', - data: { - 'token': token, - }, - ); - }; + await dio.post( + '/fcm-push/token/update', + data: { + 'token': token, + }, + ); + }; @override void didUpdateWidget(oldWidget) { diff --git a/test/opener_test.dart b/test/opener_test.dart index 6acfdf1..a8a7758 100644 --- a/test/opener_test.dart +++ b/test/opener_test.dart @@ -1,5 +1,5 @@ import 'package:flutter_test/flutter_test.dart'; -import 'package:humhub/util/opener_controller.dart'; +import 'package:humhub/util/openers/opener_controller.dart'; void main() { void testGroupOfURIs(Map uriMap) {