Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
  • Loading branch information
aidaiym committed Jul 8, 2024
1 parent c17bae0 commit 236d99c
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 45 deletions.
25 changes: 23 additions & 2 deletions app/lib/app/presentation/cubit/remote_config_cubit.dart
Original file line number Diff line number Diff line change
@@ -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<RemoteConfigState> {
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<void> 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<void> 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) {
Expand Down
11 changes: 5 additions & 6 deletions app/lib/app/presentation/cubit/remote_config_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object?> get props => [appVersionStatus, deviceId];
List<Object?> 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,
);
}
Expand Down
22 changes: 5 additions & 17 deletions app/lib/app/presentation/view/app_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,10 @@ class MyApp extends StatelessWidget {
)..init(),
),
BlocProvider(
create: (context) => RemoteConfigCubit(),
create: (context) => RemoteConfigCubit(
context.read<PackageInfo>(),
context.read<MqRemoteConfig>(),
),
),
],
child: const QuranApp(),
Expand All @@ -100,7 +103,7 @@ class _QuranAppState extends State<QuranApp> {
void initState() {
super.initState();
SchedulerBinding.instance.addPostFrameCallback((_) {
_listenRemoteConfig();
context.read<RemoteConfigCubit>().init();
});
}

Expand All @@ -121,19 +124,4 @@ class _QuranAppState extends State<QuranApp> {
),
);
}

void _listenRemoteConfig() {
final remoteConfig = context.read<MqRemoteConfig>();

context.read<RemoteConfigCubit>().setAppVersionStatus(
requiredBuildNumber: remoteConfig.requiredBuildNumber,
recommendedBuildNumber: remoteConfig.recommendedBuildNumber,
currentBuildNumber: remoteConfig.currentBuildNumber,
);

remoteConfig.remoteConfig.onConfigUpdated.listen((event) async {
await remoteConfig.remoteConfig.activate();
setState(() {});
});
}
}
6 changes: 3 additions & 3 deletions app/lib/core/remote_config/mq_remote_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, dynamic> _defaultAppVersionValue(int currentBuildNumber) {
Expand All @@ -55,10 +55,10 @@ class MqRemoteConfig {

static Map<String, dynamic> _defaultParams(int currentBuildNumber) {
return {
_disableHatim: true,
_hatimIsEnable: true,
_appVersion: jsonEncode(_defaultAppVersionValue(currentBuildNumber)),
};
}

bool get isHatimDisabled => remoteConfig.getBool(_disableHatim);
bool get hatimIsEnable => remoteConfig.getBool(_hatimIsEnable);
}
30 changes: 18 additions & 12 deletions app/lib/modules/home/presentation/view/home_view.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<MqRemoteConfig>().isHatimDisabled) {
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text(context.l10n.hatimNotAvailable)),
);
} else {
MqAnalytic.track(AnalyticKey.goHatim);
context.goNamed(AppRouter.hatim);
}
child: BlocBuilder<RemoteConfigCubit, RemoteConfigState>(
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),
),
);
}
},
);
},
),
),
Expand Down
12 changes: 8 additions & 4 deletions app/test/helpers/pump_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> pumpApp(
Expand All @@ -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<MqRemoteConfig>(create: (context) => remoteConfig),
],
child: MultiBlocProvider(
providers: [
BlocProvider(
Expand All @@ -50,9 +55,8 @@ extension PumpApp on WidgetTester {
create: (context) => HomeCubit(GetHomeDataUseCase(homeRepo)),
),
BlocProvider(
create: (context) => RemoteConfigCubit(),
create: (context) => RemoteConfigCubit(packageIngo, remoteConfig),
),
RepositoryProvider<MqRemoteConfig>(create: (context) => remoteConfig),
],
child: const QuranApp(),
),
Expand Down
6 changes: 5 additions & 1 deletion app/test/widget_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -47,7 +50,7 @@ class MockMqRemoteConfig implements MqRemoteConfig {
Future<void> initialise() async {}

@override
bool get isHatimDisabled => false;
bool get hatimIsEnable => false;

@override
int get recommendedBuildNumber => 10;
Expand Down Expand Up @@ -118,6 +121,7 @@ void main() {
patchLocaleCodeUseCase,
logoutUseCase,
remoteConfig,
packageInfo,
);
await tester.pumpAndSettle();
expect(find.byType(MaterialApp), findsOneWidget);
Expand Down

0 comments on commit 236d99c

Please sign in to comment.