Skip to content

Commit

Permalink
fix: firebase service and notifications (#1273)
Browse files Browse the repository at this point in the history
* fix: handle network error in remote config

* fix: restructure initialisation of services

* fix: remove hashnode articles from search
  • Loading branch information
Nirajn2311 authored Jun 2, 2024
1 parent 93f5bf5 commit 66ad3e2
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 30 deletions.
10 changes: 5 additions & 5 deletions mobile-app/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ import 'package:upgrader/upgrader.dart';
Future<void> main({bool testing = false}) async {
WidgetsFlutterBinding.ensureInitialized();
setupLocator();
locator<LocaleService>().init();

await DioService().init();
await AppAudioService().init();
await AuthenticationService().init();
var fbApp = await Firebase.initializeApp(
options: DefaultFirebaseOptions.currentPlatform);
options: DefaultFirebaseOptions.currentPlatform,
);
if (!testing) {
if (kReleaseMode) {
FlutterError.onError =
Expand All @@ -44,11 +48,7 @@ Future<void> main({bool testing = false}) async {
}
}
await RemoteConfigService().init();
await AuthenticationService().init();
await NotificationService().init();
await AppAudioService().init();

locator<LocaleService>().init();

runApp(const FreeCodeCampMobileApp());

Expand Down
62 changes: 39 additions & 23 deletions mobile-app/lib/service/firebase/remote_config_service.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
import 'dart:developer';

import 'package:firebase_crashlytics/firebase_crashlytics.dart';
import 'package:firebase_remote_config/firebase_remote_config.dart';
import 'package:flutter/services.dart';
import 'package:freecodecamp/utils/upgrade_controller.dart';
import 'package:upgrader/upgrader.dart';

Expand All @@ -12,30 +16,42 @@ class RemoteConfigService {
}

Future<void> init() async {
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(minutes: 10),
minimumFetchInterval: const Duration(seconds: 10),
),
);
await remoteConfig.setDefaults({
'min_app_version': '4.2.1',
});
await remoteConfig.fetchAndActivate();

remoteConfig.onConfigUpdated.listen((event) async {
await remoteConfig.activate();

// Update the min app version
UpgraderState currUpgradeState = upgraderController.state;
upgraderController.updateState(currUpgradeState.copyWith(
minAppVersion: Upgrader.parseVersion(
remoteConfig.getString('min_app_version'),
'minAppVersion',
false,
try {
await remoteConfig.setConfigSettings(
RemoteConfigSettings(
fetchTimeout: const Duration(minutes: 1),
minimumFetchInterval: const Duration(hours: 1),
),
));
});
);
await remoteConfig.setDefaults({
'min_app_version': '4.2.1',
});

await remoteConfig.fetchAndActivate();

remoteConfig.onConfigUpdated.listen((event) async {
await remoteConfig.activate();

// Update the min app version
UpgraderState currUpgradeState = upgraderController.state;
upgraderController.updateState(currUpgradeState.copyWith(
minAppVersion: Upgrader.parseVersion(
remoteConfig.getString('min_app_version'),
'minAppVersion',
false,
),
));
});
} on PlatformException catch (exception, stack) {
log('Platform exception - $exception');
await FirebaseCrashlytics.instance.recordError(
exception,
stack,
reason: 'Remote Config Platform Exception',
);
} catch (exception) {
log('Unable to fetch remote config. Cached or default values will be used $exception');
}
}

RemoteConfigService._internal();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,20 @@
import 'dart:io';

import 'package:algolia_helper_flutter/algolia_helper_flutter.dart';
import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter_dotenv/flutter_dotenv.dart';
import 'package:freecodecamp/app/app.locator.dart';
import 'package:freecodecamp/app/app.router.dart';
import 'package:freecodecamp/constants/radio_articles.dart';
import 'package:freecodecamp/service/dio_service.dart';
import 'package:stacked/stacked.dart';
import 'package:stacked_services/stacked_services.dart';

class NewsSearchModel extends BaseViewModel {
String _searchTerm = '';
String get getSearchTerm => _searchTerm;
final _dio = DioService.dio;

bool _hasData = false;
bool get hasData => _hasData;
Expand Down Expand Up @@ -40,14 +43,37 @@ class NewsSearchModel extends BaseViewModel {
notifyListeners();
}

Future<bool> isHashnodeArticle(Hit hit) async {
final res = await _dio.get(
'https://www.freecodecamp.org/news/ghost/api/v3/content/posts/${hit["objectID"]}/?key=${dotenv.env['NEWSKEY']}',
options: Options(
validateStatus: (status) {
return status == 404 || status == 200;
},
),
);
return res.statusCode == 404;
}

void init() {
algolia.query('JavaScript');
algolia.responses.listen((res) {
algolia.responses.listen((res) async {
currentResult = [];

// Remove radio articles from search on iOS
if (Platform.isIOS) {
res.hits.removeWhere(
(element) => radioArticles.contains(element['objectID']));
}
currentResult = res.hits;

// Remove Hashnode articles from search
for (final hit in res.hits) {
if (!await isHashnodeArticle(hit)) {
currentResult.add(hit);
}
}
res.hits.removeWhere((element) => !currentResult.contains(element));

_hasData = res.hits.isNotEmpty;
_isLoading = false;
notifyListeners();
Expand Down

0 comments on commit 66ad3e2

Please sign in to comment.