Move platform check to helper

Also, use TargetPlatform since it it better suited for tests (and seems to
be now the recommended way of checking this for UI elements)
This commit is contained in:
Roland Geider
2023-04-15 12:17:08 +02:00
parent fe957e4981
commit bf7e2e8956
7 changed files with 120 additions and 102 deletions

24
lib/helpers/platform.dart Normal file
View File

@@ -0,0 +1,24 @@
/*
* This file is part of wger Workout Manager <https://github.com/wger-project>.
* Copyright (C) 2020, 2021 wger Team
*
* wger Workout Manager is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* wger Workout Manager is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'package:flutter/foundation.dart';
// Note: we're not using Platform.isAndroid etc because during tests these would+
// always take the value of the current platform. TargetPlatform is always Android
// during tests, but can be changed to check for correct UI switches, etc.
final isDesktop = [TargetPlatform.linux, TargetPlatform.macOS].contains(defaultTargetPlatform);

View File

@@ -16,11 +16,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/platform.dart';
import 'package:wger/providers/gallery.dart';
import 'package:wger/widgets/core/app_bar.dart';
import 'package:wger/widgets/gallery/forms.dart';
@@ -37,7 +36,7 @@ class GalleryScreen extends StatelessWidget {
Widget build(BuildContext context) {
return Scaffold(
appBar: EmptyAppBar(AppLocalizations.of(context).gallery),
floatingActionButton: (Platform.isLinux || Platform.isMacOS)
floatingActionButton: isDesktop
? null
: FloatingActionButton(
child: const Icon(Icons.add),

View File

@@ -16,8 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_html/flutter_html.dart';
@@ -25,6 +23,7 @@ import 'package:flutter_svg/svg.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/i18n.dart';
import 'package:wger/helpers/platform.dart';
import 'package:wger/models/exercises/base.dart';
import 'package:wger/models/exercises/muscle.dart';
import 'package:wger/models/exercises/translation.dart';
@@ -219,7 +218,7 @@ class ExerciseDetail extends StatelessWidget {
List<Widget> getVideos() {
// TODO: add carousel for the other videos
final List<Widget> out = [];
if (_exerciseBase.videos.isNotEmpty && !(Platform.isLinux || Platform.isMacOS)) {
if (_exerciseBase.videos.isNotEmpty && !isDesktop) {
_exerciseBase.videos.map((v) => ExerciseVideoWidget(video: v)).forEach((element) {
out.add(element);
});

View File

@@ -16,13 +16,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_staggered_grid_view/flutter_staggered_grid_view.dart';
import 'package:intl/intl.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/platform.dart';
import 'package:wger/providers/gallery.dart';
import 'package:wger/screens/form_screen.dart';
@@ -40,76 +39,76 @@ class Gallery extends StatelessWidget {
child: RefreshIndicator(
onRefresh: () => provider.fetchAndSetGallery(),
child: MasonryGridView.count(
crossAxisCount: 2,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
itemCount: provider.images.length,
itemBuilder: (context, index) {
final currentImage = provider.images[index];
crossAxisCount: 2,
mainAxisSpacing: 5,
crossAxisSpacing: 5,
itemCount: provider.images.length,
itemBuilder: (context, index) {
final currentImage = provider.images[index];
return GestureDetector(
onTap: () {
showModalBottomSheet(
builder: (context) => Material(
child: Container(
key: Key('image-${currentImage.id}-detail'),
padding: const EdgeInsets.all(10),
color: Colors.white,
child: Column(
children: [
Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentImage.date),
style: Theme.of(context).textTheme.headline5,
),
Expanded(
child: Image.network(currentImage.url!),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(currentImage.description),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
Provider.of<GalleryProvider>(context, listen: false)
.deleteImage(currentImage);
Navigator.of(context).pop();
}),
if (!(Platform.isLinux || Platform.isMacOS))
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
ImageForm(currentImage),
hasListView: true,
),
);
}),
],
)
],
return GestureDetector(
onTap: () {
showModalBottomSheet(
builder: (context) => Container(
key: Key('image-${currentImage.id}-detail'),
padding: const EdgeInsets.all(10),
color: Colors.white,
child: Column(
children: [
Text(
DateFormat.yMd(Localizations.localeOf(context).languageCode)
.format(currentImage.date),
style: Theme.of(context).textTheme.headline5,
),
),
Expanded(
child: Image.network(currentImage.url!),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 8),
child: Text(currentImage.description),
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
IconButton(
icon: const Icon(Icons.delete),
onPressed: () {
Provider.of<GalleryProvider>(context, listen: false)
.deleteImage(currentImage);
Navigator.of(context).pop();
}),
if (!isDesktop)
IconButton(
icon: const Icon(Icons.edit),
onPressed: () {
Navigator.pushNamed(
context,
FormScreen.routeName,
arguments: FormScreenArguments(
AppLocalizations.of(context).edit,
ImageForm(currentImage),
hasListView: true,
),
);
},
),
],
)
],
),
context: context,
);
},
child: FadeInImage(
key: Key('image-${currentImage.id}'),
placeholder: const AssetImage('assets/images/placeholder.png'),
image: NetworkImage(currentImage.url!),
fit: BoxFit.cover,
),
);
}),
),
context: context,
);
},
child: FadeInImage(
key: Key('image-${currentImage.id}'),
placeholder: const AssetImage('assets/images/placeholder.png'),
image: NetworkImage(currentImage.url!),
fit: BoxFit.cover,
),
);
},
),
),
);
}

View File

@@ -16,8 +16,6 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_barcode_scanner/flutter_barcode_scanner.dart';
@@ -25,6 +23,7 @@ import 'package:flutter_gen/gen_l10n/app_localizations.dart';
import 'package:flutter_typeahead/flutter_typeahead.dart';
import 'package:provider/provider.dart';
import 'package:wger/helpers/consts.dart';
import 'package:wger/helpers/platform.dart';
import 'package:wger/helpers/ui.dart';
import 'package:wger/providers/nutrition.dart';
import 'package:wger/widgets/core/core.dart';
@@ -82,9 +81,7 @@ class _IngredientTypeaheadState extends State<IngredientTypeahead> {
decoration: InputDecoration(
prefixIcon: const Icon(Icons.search),
labelText: AppLocalizations.of(context).searchIngredient,
suffixIcon: (widget.showScanner || Platform.isLinux || Platform.isMacOS)
? scanButton()
: null,
suffixIcon: (widget.showScanner && !isDesktop) ? scanButton() : null,
),
),
suggestionsCallback: (pattern) async {

View File

@@ -555,18 +555,18 @@ packages:
dependency: "direct main"
description:
name: image_picker
sha256: cb25f04595a88450970dbe727243ba8cd21b6f7e0d7d1fc5b789fc6f52e95494
sha256: f202f5d730eb8219e35e80c4461fb3a779940ad30ce8fde1586df756e3af25e6
url: "https://pub.dev"
source: hosted
version: "0.8.7+1"
version: "0.8.7+3"
image_picker_android:
dependency: transitive
description:
name: image_picker_android
sha256: dfb5b0f28b8786fcc662b7ed42bfb4b82a6cbbd74da1958384b10d40bdf212a7
sha256: "1ea6870350f56af8dab716459bd9d5dc76947e29e07a2ba1d0c172eaaf4f269c"
url: "https://pub.dev"
source: hosted
version: "0.8.6+6"
version: "0.8.6+7"
image_picker_for_web:
dependency: transitive
description:
@@ -760,10 +760,10 @@ packages:
dependency: "direct main"
description:
name: package_info_plus
sha256: "8df5ab0a481d7dc20c0e63809e90a588e496d276ba53358afc4c4443d0a00697"
sha256: cbff87676c352d97116af6dbea05aa28c4d65eb0f6d5677a520c11a69ca9a24d
url: "https://pub.dev"
source: hosted
version: "3.0.3"
version: "3.1.0"
package_info_plus_platform_interface:
dependency: transitive
description:
@@ -904,10 +904,10 @@ packages:
dependency: "direct main"
description:
name: rive
sha256: "763b4915b5245428f1188d38c2ff8c26da83ca194d345daa319ca32d69ed670f"
sha256: f7f365ee0e6cf0af99fb239bc3424370ca6ee2b9ad6fc879b1a79ba5e3b40770
url: "https://pub.dev"
source: hosted
version: "0.10.3"
version: "0.10.4"
rive_common:
dependency: transitive
description:
@@ -928,18 +928,18 @@ packages:
dependency: transitive
description:
name: shared_preferences_android
sha256: "8304d8a1f7d21a429f91dee552792249362b68a331ac5c3c1caf370f658873f6"
sha256: "7fa90471a6875d26ad78c7e4a675874b2043874586891128dc5899662c97db46"
url: "https://pub.dev"
source: hosted
version: "2.1.0"
version: "2.1.2"
shared_preferences_foundation:
dependency: transitive
description:
name: shared_preferences_foundation
sha256: cf2a42fb20148502022861f71698db12d937c7459345a1bdaa88fc91a91b3603
sha256: "0c1c16c56c9708aa9c361541a6f0e5cc6fc12a3232d866a687a7b7db30032b07"
url: "https://pub.dev"
source: hosted
version: "2.2.0"
version: "2.2.1"
shared_preferences_linux:
dependency: transitive
description:
@@ -1125,10 +1125,10 @@ packages:
dependency: transitive
description:
name: url_launcher_android
sha256: dd729390aa936bf1bdf5cd1bc7468ff340263f80a2c4f569416507667de8e3c8
sha256: a52628068d282d01a07cd86e6ba99e497aa45ce8c91159015b2416907d78e411
url: "https://pub.dev"
source: hosted
version: "6.0.26"
version: "6.0.27"
url_launcher_ios:
dependency: transitive
description:
@@ -1149,10 +1149,10 @@ packages:
dependency: transitive
description:
name: url_launcher_macos
sha256: "0ef2b4f97942a16523e51256b799e9aa1843da6c60c55eefbfa9dbc2dcb8331a"
sha256: "91ee3e75ea9dadf38036200c5d3743518f4a5eb77a8d13fda1ee5764373f185e"
url: "https://pub.dev"
source: hosted
version: "3.0.4"
version: "3.0.5"
url_launcher_platform_interface:
dependency: transitive
description:
@@ -1301,10 +1301,10 @@ packages:
dependency: transitive
description:
name: web_socket_channel
sha256: ca49c0bc209c687b887f30527fb6a9d80040b072cc2990f34b9bec3e7663101b
sha256: d88238e5eac9a42bb43ca4e721edba3c08c6354d4a53063afaa568516217621b
url: "https://pub.dev"
source: hosted
version: "2.3.0"
version: "2.4.0"
webdriver:
dependency: transitive
description:

View File

@@ -23,28 +23,28 @@ List<gallery.Image> getTestImages() {
gallery.Image(
id: 1,
url:
'https://github.com/wger-project/flutter/raw/master/android/fastlane/metadata/android/en-US/images/phoneScreenshots/01%20-%20workout%20plan.png?raw=true',
'https://raw.githubusercontent.com/wger-project/flutter/master/fastlane/metadata/android/en-US/images/phoneScreenshots/02%20-%20workout%20detail.png',
description: 'A very cool image from the gym',
date: DateTime(2021, 5, 30),
),
gallery.Image(
id: 2,
url:
'https://github.com/wger-project/flutter/raw/master/android/fastlane/metadata/android/en-US/images/phoneScreenshots/02%20-%20workout%20log.png?raw=true',
'https://raw.githubusercontent.com/wger-project/flutter/master/fastlane/metadata/android/en-US/images/phoneScreenshots/01%20-%20dashboard.png',
description: 'Some description',
date: DateTime(2021, 4, 20),
),
gallery.Image(
id: 3,
url:
'https://github.com/wger-project/flutter/raw/master/android/fastlane/metadata/android/en-US/images/phoneScreenshots/04%20-%20nutritional%20plan.png?raw=true',
'https://raw.githubusercontent.com/wger-project/flutter/master/fastlane/metadata/android/en-US/images/phoneScreenshots/05%20-%20nutritional%20plan.png',
description: '1 22 333 4444',
date: DateTime(2021, 5, 30),
),
gallery.Image(
id: 4,
url:
'https://raw.githubusercontent.com/wger-project/flutter/master/android/fastlane/metadata/android/en-US/images/phoneScreenshots/05%20-%20weight.png',
'https://raw.githubusercontent.com/wger-project/flutter/master/fastlane/metadata/android/en-US/images/phoneScreenshots/06%20-%20weight.png',
description: '',
date: DateTime(2021, 2, 22),
)