diff --git a/app/lib/app/presentation/cubit/remote_config_cubit.dart b/app/lib/app/presentation/cubit/remote_config_cubit.dart index cdaa90cc..3e33d5c3 100644 --- a/app/lib/app/presentation/cubit/remote_config_cubit.dart +++ b/app/lib/app/presentation/cubit/remote_config_cubit.dart @@ -1,17 +1,38 @@ import 'package:equatable/equatable.dart'; import 'package:flutter/material.dart'; import 'package:flutter_bloc/flutter_bloc.dart'; +import 'package:meta/meta.dart'; +import 'package:my_quran/core/core.dart'; +import 'package:package_info_plus/package_info_plus.dart'; part 'remote_config_state.dart'; class RemoteConfigCubit extends Cubit { - RemoteConfigCubit() : super(const RemoteConfigState(appVersionStatus: NoNewVersion())); + RemoteConfigCubit(this.packageInfo, this.remoteConfig) + : super(const RemoteConfigState(appVersionStatus: NoNewVersion(), isHatimEnable: true)); + + final PackageInfo packageInfo; + final MqRemoteConfig remoteConfig; + + Future init() async { + await setAppVersionStatus( + requiredBuildNumber: remoteConfig.requiredBuildNumber, + recommendedBuildNumber: remoteConfig.recommendedBuildNumber, + ); + + emit(state.copyWith(isHatimEnable: remoteConfig.hatimIsEnable)); + + remoteConfig.remoteConfig.onConfigUpdated.listen((event) async { + await remoteConfig.remoteConfig.activate(); + }); + } Future setAppVersionStatus({ required int requiredBuildNumber, required int recommendedBuildNumber, - required int currentBuildNumber, }) async { + final currentBuildNumber = int.parse(packageInfo.buildNumber); + if (currentBuildNumber < requiredBuildNumber) { emit(state.copyWith(appVersionStatus: YesRequiredVersion(requiredBuildNumber))); } else if (currentBuildNumber < recommendedBuildNumber) { diff --git a/app/lib/app/presentation/cubit/remote_config_state.dart b/app/lib/app/presentation/cubit/remote_config_state.dart index aa7c830f..a435cd49 100644 --- a/app/lib/app/presentation/cubit/remote_config_state.dart +++ b/app/lib/app/presentation/cubit/remote_config_state.dart @@ -3,22 +3,21 @@ part of 'remote_config_cubit.dart'; class RemoteConfigState extends Equatable { const RemoteConfigState({ required this.appVersionStatus, + required this.isHatimEnable, this.deviceId, }); final AppVersionStatus appVersionStatus; final String? deviceId; + final bool isHatimEnable; @override - List get props => [appVersionStatus, deviceId]; + List get props => [appVersionStatus, deviceId, isHatimEnable]; - RemoteConfigState copyWith({ - AppVersionStatus? appVersionStatus, - String? deviceId, - bool? disableHatim, - }) { + RemoteConfigState copyWith({AppVersionStatus? appVersionStatus, String? deviceId, bool? isHatimEnable}) { return RemoteConfigState( appVersionStatus: appVersionStatus ?? this.appVersionStatus, + isHatimEnable: isHatimEnable ?? this.isHatimEnable, deviceId: deviceId ?? this.deviceId, ); } diff --git a/app/lib/app/presentation/view/app_view.dart b/app/lib/app/presentation/view/app_view.dart index 252fa872..6abc6cf2 100644 --- a/app/lib/app/presentation/view/app_view.dart +++ b/app/lib/app/presentation/view/app_view.dart @@ -80,7 +80,10 @@ class MyApp extends StatelessWidget { )..init(), ), BlocProvider( - create: (context) => RemoteConfigCubit(), + create: (context) => RemoteConfigCubit( + context.read(), + context.read(), + ), ), ], child: const QuranApp(), @@ -100,7 +103,7 @@ class _QuranAppState extends State { void initState() { super.initState(); SchedulerBinding.instance.addPostFrameCallback((_) { - _listenRemoteConfig(); + context.read().init(); }); } @@ -121,19 +124,4 @@ class _QuranAppState extends State { ), ); } - - void _listenRemoteConfig() { - final remoteConfig = context.read(); - - context.read().setAppVersionStatus( - requiredBuildNumber: remoteConfig.requiredBuildNumber, - recommendedBuildNumber: remoteConfig.recommendedBuildNumber, - currentBuildNumber: remoteConfig.currentBuildNumber, - ); - - remoteConfig.remoteConfig.onConfigUpdated.listen((event) async { - await remoteConfig.remoteConfig.activate(); - setState(() {}); - }); - } } diff --git a/app/lib/core/remote_config/mq_remote_config.dart b/app/lib/core/remote_config/mq_remote_config.dart index 36327f5c..21377116 100644 --- a/app/lib/core/remote_config/mq_remote_config.dart +++ b/app/lib/core/remote_config/mq_remote_config.dart @@ -37,7 +37,7 @@ class MqRemoteConfig { return (versionData['requiredBuildNumber'], versionData['recommendedBuildNumber']); } - static const _disableHatim = 'isHatimDisabled'; + static const _hatimIsEnable = 'hatimIsEnable'; static const _appVersion = 'appVersion'; static Map _defaultAppVersionValue(int currentBuildNumber) { @@ -55,10 +55,10 @@ class MqRemoteConfig { static Map _defaultParams(int currentBuildNumber) { return { - _disableHatim: true, + _hatimIsEnable: true, _appVersion: jsonEncode(_defaultAppVersionValue(currentBuildNumber)), }; } - bool get isHatimDisabled => remoteConfig.getBool(_disableHatim); + bool get hatimIsEnable => remoteConfig.getBool(_hatimIsEnable); } diff --git a/app/lib/modules/home/presentation/view/home_view.dart b/app/lib/modules/home/presentation/view/home_view.dart index 8e9be61a..fb2f308f 100644 --- a/app/lib/modules/home/presentation/view/home_view.dart +++ b/app/lib/modules/home/presentation/view/home_view.dart @@ -99,18 +99,24 @@ class HomeBody extends StatelessWidget { ), Padding( padding: const EdgeInsets.symmetric(horizontal: 15), - child: CustomButton( - key: const Key(MqKeys.participantToHatim), - text: l10n.homeGoHatim, - onPressed: () { - if (context.read().isHatimDisabled) { - ScaffoldMessenger.of(context).showSnackBar( - SnackBar(content: Text(context.l10n.hatimNotAvailable)), - ); - } else { - MqAnalytic.track(AnalyticKey.goHatim); - context.goNamed(AppRouter.hatim); - } + child: BlocBuilder( + builder: (context, state) { + return CustomButton( + key: const Key(MqKeys.participantToHatim), + text: l10n.homeGoHatim, + onPressed: () { + if (state.isHatimEnable) { + MqAnalytic.track(AnalyticKey.goHatim); + context.goNamed(AppRouter.hatim); + } else { + ScaffoldMessenger.of(context).showSnackBar( + SnackBar( + content: Text(context.l10n.hatimNotAvailable), + ), + ); + } + }, + ); }, ), ), diff --git a/app/test/helpers/pump_app.dart b/app/test/helpers/pump_app.dart index 8bf3ffb8..e2e1bd07 100644 --- a/app/test/helpers/pump_app.dart +++ b/app/test/helpers/pump_app.dart @@ -5,6 +5,7 @@ import 'package:my_quran/app/app.dart'; import 'package:my_quran/config/app_config.dart'; import 'package:my_quran/core/core.dart'; import 'package:my_quran/modules/modules.dart'; +import 'package:package_info_plus/package_info_plus.dart'; extension PumpApp on WidgetTester { Future pumpApp( @@ -21,10 +22,14 @@ extension PumpApp on WidgetTester { PatchLocaleCodeUseCase patchLocaleCodeUseCase, LogoutUseCase logoutUseCase, MqRemoteConfig remoteConfig, + PackageInfo packageIngo, ) { return pumpWidget( - RepositoryProvider( - create: (context) => const AppConfig(isIntegrationTest: true), + MultiRepositoryProvider( + providers: [ + RepositoryProvider(create: (context) => const AppConfig(isIntegrationTest: true)), + RepositoryProvider(create: (context) => remoteConfig), + ], child: MultiBlocProvider( providers: [ BlocProvider( @@ -50,9 +55,8 @@ extension PumpApp on WidgetTester { create: (context) => HomeCubit(GetHomeDataUseCase(homeRepo)), ), BlocProvider( - create: (context) => RemoteConfigCubit(), + create: (context) => RemoteConfigCubit(packageIngo, remoteConfig), ), - RepositoryProvider(create: (context) => remoteConfig), ], child: const QuranApp(), ), diff --git a/app/test/widget_test.dart b/app/test/widget_test.dart index 0adea541..481d0c9e 100644 --- a/app/test/widget_test.dart +++ b/app/test/widget_test.dart @@ -20,6 +20,9 @@ final class MockSccialAuth extends Mock implements SoccialAuth {} final class MockPackageInfo extends Mock implements PackageInfo { @override String get version => '1.3.0'; + + @override + String get buildNumber => '10'; } final class MockHomeRepositoryImpl implements HomeRepository { @@ -47,7 +50,7 @@ class MockMqRemoteConfig implements MqRemoteConfig { Future initialise() async {} @override - bool get isHatimDisabled => false; + bool get hatimIsEnable => false; @override int get recommendedBuildNumber => 10; @@ -118,6 +121,7 @@ void main() { patchLocaleCodeUseCase, logoutUseCase, remoteConfig, + packageInfo, ); await tester.pumpAndSettle(); expect(find.byType(MaterialApp), findsOneWidget);