Move flag checking for minimum required version to manifest

This is still not the best solution, ideally we would save this to some kind
of configuration file that we can use for dev/prod etc. Also, this solution
doesn't work with iOS or web
This commit is contained in:
Roland Geider
2021-11-08 09:42:24 +01:00
parent 03f7e1e371
commit cf882c194f
4 changed files with 19 additions and 19 deletions

View File

@@ -26,6 +26,7 @@
android:icon="@mipmap/ic_launcher"> android:icon="@mipmap/ic_launcher">
<meta-data android:name="wger.api_key" android:value="${WGER_API_KEY}" /> <meta-data android:name="wger.api_key" android:value="${WGER_API_KEY}" />
<meta-data android:name="wger.check_min_app_version" android:value="true" />
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"

View File

@@ -26,6 +26,10 @@ const double ICON_SIZE_SMALL = 20;
/// Default wger server during login /// Default wger server during login
const DEFAULT_SERVER = 'https://wger.de'; const DEFAULT_SERVER = 'https://wger.de';
/// Keys used in the android manifest
const MANIFEST_KEY_API = 'wger.api_key';
const MANIFEST_KEY_CHECK_UPDATE = 'wger.check_min_app_version';
/// Default weight unit is "kg" /// Default weight unit is "kg"
const DEFAULT_WEIGHT_UNIT = 1; const DEFAULT_WEIGHT_UNIT = 1;
@@ -70,6 +74,3 @@ const ENERGY_CARBOHYDRATES = 4;
/// kcal per gram of fat (approx) /// kcal per gram of fat (approx)
const ENERGY_FAT = 9; const ENERGY_FAT = 9;
/// Flag to check for updates to the new version.
const CHECK_APP_MIN_VERSION = false;

View File

@@ -39,11 +39,18 @@ class AuthProvider with ChangeNotifier {
String? serverUrl; String? serverUrl;
String? serverVersion; String? serverVersion;
PackageInfo? applicationVersion; PackageInfo? applicationVersion;
Map<String, String>? metadata = {};
late http.Client client; late http.Client client;
AuthProvider([http.Client? client]) { AuthProvider([http.Client? client]) {
this.client = client ?? http.Client(); this.client = client ?? http.Client();
try {
AndroidMetadata.metaDataAsMap.then((value) => metadata = value);
} on PlatformException {
throw Exception('An error occurred reading the metadata from AndroidManifest');
}
} }
/// flag to indicate that the application has successfully loaded all initial data /// flag to indicate that the application has successfully loaded all initial data
@@ -85,7 +92,8 @@ class AuthProvider with ChangeNotifier {
/// Checking if there is a new version of the application. /// Checking if there is a new version of the application.
Future<bool> applicationUpdateRequired([String? version]) async { Future<bool> applicationUpdateRequired([String? version]) async {
if (!CHECK_APP_MIN_VERSION) { if (metadata!.containsKey('wger.check_min_app_version') ||
metadata!['wger.check_min_app_version'] == 'false') {
return false; return false;
} }
@@ -105,14 +113,6 @@ class AuthProvider with ChangeNotifier {
required String email, required String email,
required String serverUrl}) async { required String serverUrl}) async {
final uri = Uri.parse('$serverUrl/api/v2/register/'); final uri = Uri.parse('$serverUrl/api/v2/register/');
Map<String, String>? metadata = {};
// Read the api key from the manifest file
try {
metadata = await AndroidMetadata.metaDataAsMap;
} on PlatformException {
throw Exception('An error occurred reading the API key');
}
// Register // Register
try { try {
@@ -124,7 +124,7 @@ class AuthProvider with ChangeNotifier {
uri, uri,
headers: { headers: {
HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8', HttpHeaders.contentTypeHeader: 'application/json; charset=UTF-8',
HttpHeaders.authorizationHeader: "Token ${metadata!['wger.api_key']}" HttpHeaders.authorizationHeader: 'Token ${metadata![MANIFEST_KEY_API]}'
}, },
body: json.encode(data), body: json.encode(data),
); );

View File

@@ -16,7 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
import 'package:android_metadata/android_metadata.dart';
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'package:flutter/services.dart'; import 'package:flutter/services.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart'; import 'package:flutter_gen/gen_l10n/app_localizations.dart';
@@ -119,11 +118,10 @@ class _AuthCardState extends State<AuthCard> {
// //
// If not, the user will not be able to register via the app // If not, the user will not be able to register via the app
try { try {
AndroidMetadata.metaDataAsMap.then((data) { final metadata = Provider.of<AuthProvider>(context, listen: false).metadata;
if (!data!.containsKey('wger.api_key') || data['wger.api_key'] == '') { if (metadata!.containsKey(MANIFEST_KEY_API) || metadata[MANIFEST_KEY_API] == '') {
_canRegister = false; _canRegister = false;
} }
});
} on PlatformException { } on PlatformException {
_canRegister = false; _canRegister = false;
} }