Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: The JSON of the tagline is now hosted on Github #5733

Merged
merged 1 commit into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import 'dart:isolate';
import 'package:collection/collection.dart';
import 'package:flutter/widgets.dart';
import 'package:http/http.dart' as http;
import 'package:openfoodfacts/openfoodfacts.dart';
import 'package:path/path.dart';
import 'package:path_provider/path_provider.dart';
import 'package:smooth_app/data_models/news_feed/newsfeed_model.dart';
Expand All @@ -28,8 +27,6 @@ class AppNewsProvider extends ChangeNotifier {
_preferences = preferences,
_uriOverride = preferences.getDevModeString(
UserPreferencesDevMode.userPreferencesCustomNewsJSONURI),
_domain = preferences.getDevModeString(
UserPreferencesDevMode.userPreferencesTestEnvDomain),
_prodEnv = preferences
.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) {
_preferences.addListener(_onPreferencesChanged);
Expand Down Expand Up @@ -102,27 +99,20 @@ class AppNewsProvider extends ChangeNotifier {
}
}

/// API URL: [https://world.openfoodfacts.[org/net]/resources/files/tagline-off-ios-v3.json]
/// or [https://world.openfoodfacts.[org/net]/resources/files/tagline-off-android-v3.json]
/// The content is stored on GitHub with two static files: one for Android,
/// and the other for iOS.
/// [https://github.com/openfoodfacts/smooth-app_assets/tree/main/prod/tagline]
Future<String?> _fetchJSON() async {
try {
final UriProductHelper uriProductHelper =
ProductQuery.getUriProductHelper();
final Map<String, String> headers = <String, String>{};
final Uri uri;

if (_uriOverride?.isNotEmpty == true) {
uri = Uri.parse(_uriOverride!);
} else {
uri = uriProductHelper.getUri(path: _newsUrl);
uri = Uri.parse(_newsUrl);
}

if (uriProductHelper.userInfoForPatch != null) {
headers['Authorization'] =
'Basic ${base64Encode(utf8.encode(uriProductHelper.userInfoForPatch!))}';
}

final http.Response response = await http.get(uri, headers: headers);
final http.Response response = await http.get(uri);

if (response.statusCode == 404) {
Logs.e("Remote file $uri doesn't exist!");
Expand All @@ -143,10 +133,12 @@ class AppNewsProvider extends ChangeNotifier {

/// Based on the platform, the URL may differ
String get _newsUrl {
final String env = _prodEnv != false ? 'prod' : 'dev';

if (Platform.isIOS || Platform.isMacOS) {
return '/resources/files/tagline-off-ios-v3.json';
return 'https://raw.githubusercontent.com/openfoodfacts/smooth-app_assets/refs/heads/main/$env/tagline/ios/main.json';
} else {
return '/resources/files/tagline-off-android-v3.json';
return 'https://raw.githubusercontent.com/openfoodfacts/smooth-app_assets/refs/heads/main/$env/tagline/android/main.json';
}
}

Expand All @@ -166,7 +158,6 @@ class AppNewsProvider extends ChangeNotifier {
.isAfter(DateTime.now().add(const Duration(days: -1)));

bool? _prodEnv;
String? _domain;
String? _uriOverride;

/// [ProductQuery.uriProductHelper] is not synced yet,
Expand All @@ -175,15 +166,11 @@ class AppNewsProvider extends ChangeNotifier {
final String jsonURI = _preferences.getDevModeString(
UserPreferencesDevMode.userPreferencesCustomNewsJSONURI) ??
'';
final String domain = _preferences.getDevModeString(
UserPreferencesDevMode.userPreferencesTestEnvDomain) ??
'';
final bool prodEnv =
_preferences.getFlag(UserPreferencesDevMode.userPreferencesFlagProd) ??
true;

if (domain != _domain || prodEnv != _prodEnv || jsonURI != _uriOverride) {
_domain = domain;
if (prodEnv != _prodEnv || jsonURI != _uriOverride) {
_prodEnv = prodEnv;
_uriOverride = jsonURI;
loadLatestNews(forceUpdate: true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ import rive_common
import sentry_flutter
import share_plus
import shared_preferences_foundation
import sqflite
import sqflite_darwin
import url_launcher_macos
import webview_flutter_wkwebview

func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
AudioplayersDarwinPlugin.register(with: registry.registrar(forPlugin: "AudioplayersDarwinPlugin"))
Expand All @@ -39,4 +40,5 @@ func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) {
SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin"))
SqflitePlugin.register(with: registry.registrar(forPlugin: "SqflitePlugin"))
UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin"))
FLTWebViewFlutterPlugin.register(with: registry.registrar(forPlugin: "FLTWebViewFlutterPlugin"))
}
Loading