Skip to content

Commit

Permalink
Add button+menu for sharing
Browse files Browse the repository at this point in the history
The two PDF-related menu items don't do anything yet, the other two
sharing options are already operational.
TODO improve the icons and add tests
  • Loading branch information
holybiber committed Jul 12, 2024
1 parent 4df7e96 commit 819e9a7
Show file tree
Hide file tree
Showing 7 changed files with 129 additions and 3 deletions.
6 changes: 5 additions & 1 deletion lib/l10n/locales/app_de.arb
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,9 @@
"updatesExplanation": "Ab und zu werden manche Materialien aktualisiert: Wir veröffentlichen eine neue Version eines Arbeitsblattes oder fügen eine neue Übersetzung hinzu.\nUnser Ziel ist, dass du dir darüber keine Gedanken machen brauchst, sondern immer die aktuellsten Versionen einsatzbereit dabei hast. Deshalb kann die App im Hintergrund nach Aktualisierungen suchen und sie automatisch herunterladen, wenn du das möchtest.",
"letsGo": "Los geht's!",
"homeExplanation": "Gott baut sein Reich überall auf der Welt. Er möchte, dass wir dabei mitmachen und andere zu Jüngern machen!\nDiese App will dir diese Aufgabe erleichtern: Wir stellen dir gute Trainingsmaterialien zur Verfügung. Und das Beste ist: Du kannst dasselbe Arbeitsblatt in verschiedenen Sprachen anschauen, so dass du immer weißt, was es bedeutet, selbst wenn du eine Sprache nicht verstehst.\n\nAlle Inhalte sind nun offline verfügbar und jederzeit bereit auf deinem Handy:",
"foundBgActivity": "Im Hintergrund wurde nach Updates gesucht"
"foundBgActivity": "Im Hintergrund wurde nach Updates gesucht",
"sharePdf": "PDF teilen",
"openPdf": "PDF öffnen",
"openInBrowser": "Im Browser öffnen",
"shareLink": "Link teilen"
}
6 changes: 5 additions & 1 deletion lib/l10n/locales/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -334,5 +334,9 @@
"updatesExplanation": "From time to time the resources get updated: We release a new version of a worksheet or add a new translation.\nWe want you to not worry about that but be always ready to use the latest versions. Therefore the app can check for updates in the background and download them automatically if you want.",
"letsGo": "Let's go!",
"homeExplanation": "God is building His kingdom all around the world. He wants us to join in His work and make disciples!\nThis app wants to serve you and make your job easier: We equip you with good training worksheets. The best thing is: You can access the same worksheet in different languages so that you always know what it means, even if you don't understand the language.\n\nAll this content is now available offline, always ready on your phone:",
"foundBgActivity": "Searched for updates in the background"
"foundBgActivity": "Searched for updates in the background",
"sharePdf": "Share PDF",
"openPdf": "Open PDF",
"openInBrowser": "Open in browser",
"shareLink": "Share link"
}
3 changes: 2 additions & 1 deletion lib/routes/view_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:app4training/data/globals.dart';
import 'package:app4training/l10n/l10n.dart';
import 'package:app4training/widgets/error_message.dart';
import 'package:app4training/widgets/html_view.dart';
import 'package:app4training/widgets/share_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:app4training/data/languages.dart';
Expand Down Expand Up @@ -41,7 +42,7 @@ class ViewPage extends ConsumerWidget {
return Scaffold(
appBar: AppBar(
title: const Text(Globals.appTitle),
actions: const [LanguageSelectionButton()],
actions: const [ShareButton(), LanguageSelectionButton()],
),
drawer: MainDrawer(page, langCode),
body: FutureBuilder(
Expand Down
86 changes: 86 additions & 0 deletions lib/widgets/share_button.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'dart:async';

import 'package:app4training/l10n/l10n.dart';
import 'package:app4training/routes/view_page.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';

/// Share button in the top right corner of the main view.
/// Opens a dropdown with several sharing options.
/// Implemented with MenuAnchor+MenuItemButton
/// (seems to be preferred over PopupMenuButton since Material 3)
class ShareButton extends ConsumerWidget {
const ShareButton({super.key});

@override
Widget build(BuildContext context, WidgetRef ref) {
final menuController = MenuController();
final viewPage = context.findAncestorWidgetOfExactType<ViewPage>()!;
String currentPage = viewPage.page;
String currentLang = viewPage.langCode;

return MenuAnchor(
controller: menuController,
builder:
(BuildContext context, MenuController controller, Widget? child) {
return IconButton(
onPressed: () {
if (controller.isOpen) {
controller.close();
} else {
controller.open();
}
},
icon: const Icon(Icons.share),
tooltip: 'Share',
);
},
menuChildren: [
Padding(
padding: const EdgeInsets.fromLTRB(10, 0, 10, 0),
child: Column(
children: [
Column(children: [
ListTile(
dense: true,
title: Text(context.l10n.sharePdf),
leading: const Icon(Icons.picture_as_pdf),
onTap: () {
menuController.close();
},
),
ListTile(
dense: true,
title: Text(context.l10n.openPdf),
leading: const Icon(Icons.picture_as_pdf_outlined),
onTap: () {
menuController.close();
},
),
ListTile(
dense: true,
title: Text(context.l10n.openInBrowser),
leading: const Icon(Icons.open_in_browser),
onTap: () async {
menuController.close();
unawaited(launchUrl(Uri.parse(
'https://www.4training.net/$currentPage/$currentLang')));
}),
ListTile(
dense: true,
title: Text(context.l10n.shareLink),
leading: const Icon(Icons.content_copy),
onTap: () {
menuController.close();
Share.share(
'https://www.4training.net/$currentPage/$currentLang');
},
),
])
],
))
]);
}
}
24 changes: 24 additions & 0 deletions pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,14 @@ packages:
url: "https://pub.dev"
source: hosted
version: "1.8.0"
cross_file:
dependency: transitive
description:
name: cross_file
sha256: fedaadfa3a6996f75211d835aaeb8fede285dae94262485698afd832371b9a5e
url: "https://pub.dev"
source: hosted
version: "0.3.3+8"
crypto:
dependency: transitive
description:
Expand Down Expand Up @@ -700,6 +708,22 @@ packages:
url: "https://pub.dev"
source: hosted
version: "0.27.7"
share_plus:
dependency: "direct main"
description:
name: share_plus
sha256: "3ef39599b00059db0990ca2e30fca0a29d8b37aae924d60063f8e0184cf20900"
url: "https://pub.dev"
source: hosted
version: "7.2.2"
share_plus_platform_interface:
dependency: transitive
description:
name: share_plus_platform_interface
sha256: "251eb156a8b5fa9ce033747d73535bf53911071f8d3b6f4f0b578505ce0d4496"
url: "https://pub.dev"
source: hosted
version: "3.4.0"
shared_preferences:
dependency: "direct main"
description:
Expand Down
1 change: 1 addition & 0 deletions pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ dependencies:
flutter_linkify: ^6.0.0
package_info_plus: ^5.0.1
workmanager: ^0.5.2
share_plus: ^7.2.2


dev_dependencies:
Expand Down
6 changes: 6 additions & 0 deletions test/view_page_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import 'package:app4training/data/globals.dart';
import 'package:app4training/data/languages.dart';
import 'package:app4training/l10n/l10n.dart';
import 'package:app4training/routes/view_page.dart';
import 'package:app4training/widgets/language_selection_button.dart';
import 'package:app4training/widgets/share_button.dart';
import 'package:flutter/material.dart';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:flutter_test/flutter_test.dart';
Expand Down Expand Up @@ -49,6 +51,10 @@ void main() {
// recentPage and recentLang should be set in SharedPreferences
expect(prefs.getString('recentPage'), 'Healing');
expect(prefs.getString('recentLang'), 'de');

// Our two action buttons should be visible
expect(find.byType(LanguageSelectionButton), findsOneWidget);
expect(find.byType(ShareButton), findsOneWidget);
});

testWidgets('Test LanguageNotDownloadedException handling',
Expand Down

0 comments on commit 819e9a7

Please sign in to comment.