implemented RemoteConfigService

This commit is contained in:
Vadim 2023-10-28 22:40:56 +02:00
parent f3b08868be
commit e83e6cff3a
6 changed files with 86 additions and 12 deletions

View file

@ -3,11 +3,13 @@ import 'package:lightmeter/data/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/data/remote_config_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/data/volume_events_service.dart';
import 'package:lightmeter/environment.dart';
import 'package:lightmeter/providers/equipment_profile_provider.dart';
import 'package:lightmeter/providers/films_provider.dart';
import 'package:lightmeter/providers/remote_config_provider.dart';
import 'package:lightmeter/providers/services_provider.dart';
import 'package:lightmeter/providers/user_preferences_provider.dart';
import 'package:m3_lightmeter_iap/m3_lightmeter_iap.dart';
@ -23,9 +25,10 @@ class ApplicationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: Future.wait([
future: Future.wait<dynamic>([
SharedPreferences.getInstance(),
const LightSensorService(LocalPlatform()).hasSensor(),
const RemoteConfigService().activeAndFetchFeatures(),
]),
builder: (_, snapshot) {
if (snapshot.data != null) {
@ -38,8 +41,10 @@ class ApplicationWrapper extends StatelessWidget {
hapticsService: const HapticsService(),
lightSensorService: const LightSensorService(LocalPlatform()),
permissionsService: const PermissionsService(),
remoteConfigService: const RemoteConfigService(),
userPreferencesService: userPreferencesService,
volumeEventsService: const VolumeEventsService(LocalPlatform()),
child: RemoteConfig(
child: EquipmentProfileProvider(
storageService: iapService,
child: FilmsProvider(
@ -51,6 +56,7 @@ class ApplicationWrapper extends StatelessWidget {
),
),
),
),
);
} else if (snapshot.error != null) {
return Center(child: Text(snapshot.error!.toString()));

View file

@ -0,0 +1,5 @@
enum Feature { unlockProFeaturesText }
const featuresDefaultValues = {
Feature.unlockProFeaturesText: false,
};

View file

@ -0,0 +1,42 @@
import 'dart:async';
import 'dart:developer';
import 'package:firebase_core/firebase_core.dart';
import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/foundation.dart';
import 'package:lightmeter/data/models/feature.dart';
class RemoteConfigService {
const RemoteConfigService();
Future<void> activeAndFetchFeatures() async {
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.instance;
const cacheStaleDuration = kDebugMode ? Duration(minutes: 1) : Duration(hours: 12);
try {
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(seconds: 15),
minimumFetchInterval: cacheStaleDuration,
),
);
await remoteConfig.setDefaults(featuresDefaultValues.map((key, value) => MapEntry(key.name, value)));
await remoteConfig.activate();
await remoteConfig.ensureInitialized();
unawaited(remoteConfig.fetch());
log('Firebase remote config initialized successfully');
} on FirebaseException catch (e) {
_logError('Firebase exception during Firebase Remote Config initialization: $e');
} on Exception catch (e) {
_logError('Error during Firebase Remote Config initialization: $e');
}
}
bool isEnabled(Feature feature) => FirebaseRemoteConfig.instance.getBool(feature.name);
void _logError(dynamic throwable, {StackTrace? stackTrace}) {
FirebaseCrashlytics.instance.recordError(throwable, stackTrace);
}
}

View file

@ -0,0 +1,17 @@
import 'package:flutter/material.dart';
import 'package:lightmeter/data/models/feature.dart';
import 'package:lightmeter/providers/services_provider.dart';
class RemoteConfig extends InheritedWidget {
const RemoteConfig({
super.key,
required super.child,
});
static bool isEnabled(BuildContext context, Feature feature) {
return ServicesProvider.of(context).remoteConfigService.isEnabled(feature);
}
@override
bool updateShouldNotify(RemoteConfig oldWidget) => true;
}

View file

@ -3,6 +3,7 @@ import 'package:lightmeter/data/caffeine_service.dart';
import 'package:lightmeter/data/haptics_service.dart';
import 'package:lightmeter/data/light_sensor_service.dart';
import 'package:lightmeter/data/permissions_service.dart';
import 'package:lightmeter/data/remote_config_service.dart';
import 'package:lightmeter/data/shared_prefs_service.dart';
import 'package:lightmeter/data/volume_events_service.dart';
import 'package:lightmeter/environment.dart';
@ -14,6 +15,7 @@ class ServicesProvider extends InheritedWidget {
final HapticsService hapticsService;
final LightSensorService lightSensorService;
final PermissionsService permissionsService;
final RemoteConfigService remoteConfigService;
final UserPreferencesService userPreferencesService;
final VolumeEventsService volumeEventsService;
@ -23,6 +25,7 @@ class ServicesProvider extends InheritedWidget {
required this.hapticsService,
required this.lightSensorService,
required this.permissionsService,
required this.remoteConfigService,
required this.userPreferencesService,
required this.volumeEventsService,
required super.child,

View file

@ -13,8 +13,9 @@ dependencies:
clipboard: 0.1.3
dynamic_color: 1.6.6
exif: 3.1.4
firebase_core: 2.14.0
firebase_crashlytics: 3.3.3
firebase_core: 2.20.0
firebase_crashlytics: 3.4.2
firebase_remote_config: 4.3.2
flutter:
sdk: flutter
flutter_bloc: 8.1.3
@ -26,7 +27,7 @@ dependencies:
m3_lightmeter_iap:
git:
url: "https://github.com/vodemn/m3_lightmeter_iap"
ref: v0.5.0
ref: v0.6.2
m3_lightmeter_resources:
git:
url: "https://github.com/vodemn/m3_lightmeter_resources"