From 1286a5d848ba4345f0310398b8a4e0af39f1152c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mike=20Schw=C3=B6rer?= Date: Sun, 18 Feb 2024 16:23:10 +0100 Subject: [PATCH] send page qr code --- flutter/lib/api/api_client.dart | 14 ++ .../lib/bottom_fab/fab_bottom_app_bar.dart | 44 +++--- flutter/lib/bottom_fab/fab_with_icons.dart | 4 +- flutter/lib/main.dart | 2 + flutter/lib/nav_layout.dart | 16 +- flutter/lib/pages/account/login.dart | 4 +- flutter/lib/pages/send/root.dart | 143 ++++++++++++++++++ flutter/lib/state/user_account.dart | 19 +++ .../flutter/generated_plugin_registrant.cc | 4 + flutter/linux/flutter/generated_plugins.cmake | 1 + .../Flutter/GeneratedPluginRegistrant.swift | 2 + flutter/pubspec.lock | 80 ++++++++++ flutter/pubspec.yaml | 2 + .../flutter/generated_plugin_registrant.cc | 3 + .../windows/flutter/generated_plugins.cmake | 1 + 15 files changed, 300 insertions(+), 39 deletions(-) create mode 100644 flutter/lib/pages/send/root.dart diff --git a/flutter/lib/api/api_client.dart b/flutter/lib/api/api_client.dart index 5273de5..14dc54d 100644 --- a/flutter/lib/api/api_client.dart +++ b/flutter/lib/api/api_client.dart @@ -1,4 +1,7 @@ +import 'dart:convert'; + import 'package:http/http.dart' as http; +import 'package:simplecloudnotifier/models/user.dart'; class APIClient { static const String _base = 'https://simplecloudnotifier.de/api/v2'; @@ -9,4 +12,15 @@ class APIClient { return (response.statusCode == 200); } + + static Future getUser(String uid, String tok) async { + final uri = Uri.parse('$_base/users/$uid'); + final response = await http.get(uri, headers: {'Authorization': 'SCN $tok'}); + + if (response.statusCode != 200) { + throw Exception('API request failed'); + } + + return User.fromJson(jsonDecode(response.body)); + } } diff --git a/flutter/lib/bottom_fab/fab_bottom_app_bar.dart b/flutter/lib/bottom_fab/fab_bottom_app_bar.dart index 7975b7f..ad55c6a 100644 --- a/flutter/lib/bottom_fab/fab_bottom_app_bar.dart +++ b/flutter/lib/bottom_fab/fab_bottom_app_bar.dart @@ -6,7 +6,7 @@ class FABBottomAppBarItem { String text; } -class FABBottomAppBar extends StatefulWidget { +class FABBottomAppBar extends StatelessWidget { FABBottomAppBar({ super.key, required this.items, @@ -18,6 +18,7 @@ class FABBottomAppBar extends StatefulWidget { this.selectedColor, this.notchedShape, this.onTabSelected, + this.selectedIndex = 0, }) { assert(items.length == 2 || items.length == 4); } @@ -31,26 +32,13 @@ class FABBottomAppBar extends StatefulWidget { final Color? selectedColor; final NotchedShape? notchedShape; final ValueChanged? onTabSelected; - - @override - State createState() => FABBottomAppBarState(); -} - -class FABBottomAppBarState extends State { - int _selectedIndex = 0; - - _updateIndex(int index) { - if (widget.onTabSelected != null) widget.onTabSelected!(index); - setState(() { - _selectedIndex = index; - }); - } + final int selectedIndex; @override Widget build(BuildContext context) { - List items = List.generate(widget.items.length, (int index) { + List items = List.generate(this.items.length, (int index) { return _buildTabItem( - item: widget.items[index], + item: this.items[index], index: index, onPressed: _updateIndex, ); @@ -58,8 +46,8 @@ class FABBottomAppBarState extends State { items.insert(items.length >> 1, _buildMiddleTabItem()); return BottomAppBar( - shape: widget.notchedShape, - color: widget.backgroundColor, + shape: notchedShape, + color: backgroundColor, child: Row( mainAxisSize: MainAxisSize.max, mainAxisAlignment: MainAxisAlignment.spaceAround, @@ -71,15 +59,15 @@ class FABBottomAppBarState extends State { Widget _buildMiddleTabItem() { return Expanded( child: SizedBox( - height: widget.height, + height: height, child: Column( mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - SizedBox(height: widget.iconSize), + SizedBox(height: iconSize), Text( - widget.centerItemText ?? '', - style: TextStyle(color: widget.color), + centerItemText ?? '', + style: TextStyle(color: color), ), ], ), @@ -88,10 +76,10 @@ class FABBottomAppBarState extends State { } Widget _buildTabItem({required FABBottomAppBarItem item, required int index, required ValueChanged onPressed}) { - Color color = (_selectedIndex == index ? widget.selectedColor : widget.color) ?? Colors.black; + Color color = (selectedIndex == index ? selectedColor : this.color) ?? Colors.black; return Expanded( child: SizedBox( - height: widget.height, + height: height, child: Material( type: MaterialType.transparency, child: InkWell( @@ -100,7 +88,7 @@ class FABBottomAppBarState extends State { mainAxisSize: MainAxisSize.min, mainAxisAlignment: MainAxisAlignment.center, children: [ - Icon(item.iconData, color: color, size: widget.iconSize), + Icon(item.iconData, color: color, size: iconSize), Text( item.text, style: TextStyle(color: color), @@ -112,4 +100,8 @@ class FABBottomAppBarState extends State { ), ); } + + void _updateIndex(int index) { + if (onTabSelected != null) onTabSelected!(index); + } } diff --git a/flutter/lib/bottom_fab/fab_with_icons.dart b/flutter/lib/bottom_fab/fab_with_icons.dart index b62a0ec..a5f55a1 100644 --- a/flutter/lib/bottom_fab/fab_with_icons.dart +++ b/flutter/lib/bottom_fab/fab_with_icons.dart @@ -3,7 +3,7 @@ import 'package:flutter/material.dart'; // https://stackoverflow.com/questions/46480221/flutter-floating-action-button-with-speed-dail class FabWithIcons extends StatefulWidget { - FabWithIcons({required this.icons, required this.onIconTapped}); + FabWithIcons({super.key, required this.icons, required this.onIconTapped}); final List icons; ValueChanged onIconTapped; @override @@ -68,8 +68,8 @@ class FabWithIconsState extends State with TickerProviderStateMixi } }, tooltip: 'Increment', - child: Icon(Icons.add), elevation: 2.0, + child: const Icon(Icons.add), ); } diff --git a/flutter/lib/main.dart b/flutter/lib/main.dart index 63e41b1..ea5929f 100644 --- a/flutter/lib/main.dart +++ b/flutter/lib/main.dart @@ -22,6 +22,8 @@ class SCNApp extends StatelessWidget { @override Widget build(BuildContext context) { + Provider.of(context); // ensure UserAccount is loaded + return Consumer( builder: (context, appTheme, child) => MaterialApp( title: 'SimpleCloudNotifier', diff --git a/flutter/lib/nav_layout.dart b/flutter/lib/nav_layout.dart index 3c9a916..dc88777 100644 --- a/flutter/lib/nav_layout.dart +++ b/flutter/lib/nav_layout.dart @@ -1,6 +1,7 @@ import 'package:flutter/material.dart'; import 'package:font_awesome_flutter/font_awesome_flutter.dart'; import 'package:provider/provider.dart'; +import 'package:simplecloudnotifier/pages/send/root.dart'; import 'bottom_fab/fab_bottom_app_bar.dart'; import 'pages/account/root.dart'; @@ -15,13 +16,14 @@ class SCNNavLayout extends StatefulWidget { } class _SCNNavLayoutState extends State { - int _selectedIndex = 0; + int _selectedIndex = 0; // 4 == FAB static const List _subPages = [ MessageListPage(title: 'Messages'), MessageListPage(title: 'Page 2'), AccountRootPage(), MessageListPage(title: 'Page 4'), + SendRootPage(), ]; void _onItemTapped(int index) { @@ -30,9 +32,9 @@ class _SCNNavLayoutState extends State { }); } - void _onFABTapped(int index) { + void _onFABTapped() { setState(() { - //TODO + _selectedIndex = 4; }); } @@ -49,7 +51,7 @@ class _SCNNavLayoutState extends State { Widget _buildFAB(BuildContext context) { return FloatingActionButton( - onPressed: () {}, + onPressed: _onFABTapped, tooltip: 'Increment', shape: const RoundedRectangleBorder(borderRadius: BorderRadius.all(Radius.circular(17))), elevation: 2.0, @@ -59,6 +61,7 @@ class _SCNNavLayoutState extends State { Widget _buildNavBar(BuildContext context) { return FABBottomAppBar( + selectedIndex: _selectedIndex, onTabSelected: _onItemTapped, color: Theme.of(context).disabledColor, selectedColor: Theme.of(context).primaryColorDark, @@ -105,11 +108,6 @@ class _SCNNavLayoutState extends State { tooltip: 'Search', onPressed: () {}, ), - IconButton( - icon: const Icon(FontAwesomeIcons.solidQrcode), - tooltip: 'Show Account QR Code', - onPressed: () {}, - ), ], backgroundColor: Theme.of(context).secondaryHeaderColor, ); diff --git a/flutter/lib/pages/account/login.dart b/flutter/lib/pages/account/login.dart index 7f5ed27..36e0241 100644 --- a/flutter/lib/pages/account/login.dart +++ b/flutter/lib/pages/account/login.dart @@ -81,7 +81,7 @@ class _AccountLoginPageState extends State { if (verified) { msgr.showSnackBar( const SnackBar( - content: Text('Data ok'), //TODO toast + content: Text('Data ok'), ), ); prov.setToken(KeyTokenAuth(userId: uid, token: tok)); @@ -90,7 +90,7 @@ class _AccountLoginPageState extends State { } else { msgr.showSnackBar( const SnackBar( - content: Text('Failed to verify token'), //TODO toast + content: Text('Failed to verify token'), ), ); } diff --git a/flutter/lib/pages/send/root.dart b/flutter/lib/pages/send/root.dart new file mode 100644 index 0000000..7dc9587 --- /dev/null +++ b/flutter/lib/pages/send/root.dart @@ -0,0 +1,143 @@ +import 'package:flutter/material.dart'; +import 'package:provider/provider.dart'; +import 'package:qr_flutter/qr_flutter.dart'; +import 'package:url_launcher/url_launcher.dart'; + +import '../../state/user_account.dart'; + +class SendRootPage extends StatefulWidget { + const SendRootPage({super.key}); + + @override + State createState() => _SendRootPageState(); +} + +class _SendRootPageState extends State { + late TextEditingController _msgTitle; + late TextEditingController _msgContent; + + @override + void initState() { + super.initState(); + _msgTitle = TextEditingController(); + _msgContent = TextEditingController(); + } + + @override + void dispose() { + _msgTitle.dispose(); + _msgContent.dispose(); + super.dispose(); + } + + @override + Widget build(BuildContext context) { + return Consumer( + builder: (context, acc, child) { + return Padding( + padding: const EdgeInsets.all(16.0), + child: Column( + mainAxisAlignment: MainAxisAlignment.center, + children: [ + _buildQRCode(context, acc), + const SizedBox(height: 16), + FractionallySizedBox( + widthFactor: 1.0, + child: TextField( + controller: _msgTitle, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Title', + ), + ), + ), + const SizedBox(height: 16), + FractionallySizedBox( + widthFactor: 1.0, + child: TextField( + controller: _msgContent, + decoration: const InputDecoration( + border: OutlineInputBorder(), + labelText: 'Text', + ), + ), + ), + const SizedBox(height: 16), + ElevatedButton( + style: ElevatedButton.styleFrom(textStyle: const TextStyle(fontSize: 20)), + onPressed: _send, + child: const Text('Send'), + ), + ], + ), + ); + }, + ); + } + + void _send() { + //... + } + + _buildQRCode(BuildContext context, UserAccount acc) { + if (acc.auth == null) { + return const Placeholder(); + } + + if (acc.user == null) { + return FutureBuilder( + future: acc.loadUser(false), + builder: ((context, snapshot) { + if (snapshot.connectionState == ConnectionState.done) { + if (snapshot.hasError) { + return Text('Error: ${snapshot.error}'); + } + var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key + return GestureDetector( + onTap: () { + _openWeb(url); + }, + child: QrImageView( + data: url, + version: QrVersions.auto, + size: 400.0, + ), + ); + } + return const SizedBox( + width: 400.0, + height: 400.0, + child: Center(child: CircularProgressIndicator()), + ); + }), + ); + } + + var url = 'https://simplecloudnotifier.com?preset_user_id=${acc.user!.userID}&preset_user_key=TODO'; // TODO get send-only key + + return GestureDetector( + onTap: () { + _openWeb(url); + }, + child: QrImageView( + data: url, + version: QrVersions.auto, + size: 400.0, + ), + ); + } + + void _openWeb(String url) async { + try { + final Uri uri = Uri.parse(url); + + if (await canLaunchUrl(uri)) { + await launchUrl(uri); + } else { + // TODO ("Cannot open URL"); + } + } catch (e) { + // TODO ('Cannot open URL'); + } + } +} diff --git a/flutter/lib/state/user_account.dart b/flutter/lib/state/user_account.dart index deaf2f7..12de32a 100644 --- a/flutter/lib/state/user_account.dart +++ b/flutter/lib/state/user_account.dart @@ -1,5 +1,6 @@ import 'package:flutter/foundation.dart'; import 'package:shared_preferences/shared_preferences.dart'; +import 'package:simplecloudnotifier/api/api_client.dart'; import '../models/key_token_auth.dart'; import '../models/user.dart'; @@ -60,4 +61,22 @@ class UserAccount extends ChangeNotifier { await prefs.setString('auth.token', _auth!.token); } } + + loadUser(bool force) async { + if (!force && _user != null) { + return _user; + } + + if (_auth == null) { + throw Exception('Not authenticated'); + } + + final user = await APIClient.getUser(_auth!.userId, _auth!.token); + + setUser(user); + + await save(); + + return user; + } } diff --git a/flutter/linux/flutter/generated_plugin_registrant.cc b/flutter/linux/flutter/generated_plugin_registrant.cc index e71a16d..f6f23bf 100644 --- a/flutter/linux/flutter/generated_plugin_registrant.cc +++ b/flutter/linux/flutter/generated_plugin_registrant.cc @@ -6,6 +6,10 @@ #include "generated_plugin_registrant.h" +#include void fl_register_plugins(FlPluginRegistry* registry) { + g_autoptr(FlPluginRegistrar) url_launcher_linux_registrar = + fl_plugin_registry_get_registrar_for_plugin(registry, "UrlLauncherPlugin"); + url_launcher_plugin_register_with_registrar(url_launcher_linux_registrar); } diff --git a/flutter/linux/flutter/generated_plugins.cmake b/flutter/linux/flutter/generated_plugins.cmake index 2e1de87..f16b4c3 100644 --- a/flutter/linux/flutter/generated_plugins.cmake +++ b/flutter/linux/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + url_launcher_linux ) list(APPEND FLUTTER_FFI_PLUGIN_LIST diff --git a/flutter/macos/Flutter/GeneratedPluginRegistrant.swift b/flutter/macos/Flutter/GeneratedPluginRegistrant.swift index 724bb2a..997e35d 100644 --- a/flutter/macos/Flutter/GeneratedPluginRegistrant.swift +++ b/flutter/macos/Flutter/GeneratedPluginRegistrant.swift @@ -6,7 +6,9 @@ import FlutterMacOS import Foundation import shared_preferences_foundation +import url_launcher_macos func RegisterGeneratedPlugins(registry: FlutterPluginRegistry) { SharedPreferencesPlugin.register(with: registry.registrar(forPlugin: "SharedPreferencesPlugin")) + UrlLauncherPlugin.register(with: registry.registrar(forPlugin: "UrlLauncherPlugin")) } diff --git a/flutter/pubspec.lock b/flutter/pubspec.lock index ecf6a8f..d55e46a 100644 --- a/flutter/pubspec.lock +++ b/flutter/pubspec.lock @@ -215,6 +215,22 @@ packages: url: "https://pub.dev" source: hosted version: "6.1.1" + qr: + dependency: transitive + description: + name: qr + sha256: "64957a3930367bf97cc211a5af99551d630f2f4625e38af10edd6b19131b64b3" + url: "https://pub.dev" + source: hosted + version: "3.0.1" + qr_flutter: + dependency: "direct main" + description: + name: qr_flutter + sha256: "5095f0fc6e3f71d08adef8feccc8cea4f12eec18a2e31c2e8d82cb6019f4b097" + url: "https://pub.dev" + source: hosted + version: "4.1.0" shared_preferences: dependency: "direct main" description: @@ -332,6 +348,70 @@ packages: url: "https://pub.dev" source: hosted version: "1.3.2" + url_launcher: + dependency: "direct main" + description: + name: url_launcher + sha256: c512655380d241a337521703af62d2c122bf7b77a46ff7dd750092aa9433499c + url: "https://pub.dev" + source: hosted + version: "6.2.4" + url_launcher_android: + dependency: transitive + description: + name: url_launcher_android + sha256: d4ed0711849dd8e33eb2dd69c25db0d0d3fdc37e0a62e629fe32f57a22db2745 + url: "https://pub.dev" + source: hosted + version: "6.3.0" + url_launcher_ios: + dependency: transitive + description: + name: url_launcher_ios + sha256: "75bb6fe3f60070407704282a2d295630cab232991eb52542b18347a8a941df03" + url: "https://pub.dev" + source: hosted + version: "6.2.4" + url_launcher_linux: + dependency: transitive + description: + name: url_launcher_linux + sha256: ab360eb661f8879369acac07b6bb3ff09d9471155357da8443fd5d3cf7363811 + url: "https://pub.dev" + source: hosted + version: "3.1.1" + url_launcher_macos: + dependency: transitive + description: + name: url_launcher_macos + sha256: b7244901ea3cf489c5335bdacda07264a6e960b1c1b1a9f91e4bc371d9e68234 + url: "https://pub.dev" + source: hosted + version: "3.1.0" + url_launcher_platform_interface: + dependency: transitive + description: + name: url_launcher_platform_interface + sha256: "552f8a1e663569be95a8190206a38187b531910283c3e982193e4f2733f01029" + url: "https://pub.dev" + source: hosted + version: "2.3.2" + url_launcher_web: + dependency: transitive + description: + name: url_launcher_web + sha256: fff0932192afeedf63cdd50ecbb1bc825d31aed259f02bb8dba0f3b729a5e88b + url: "https://pub.dev" + source: hosted + version: "2.2.3" + url_launcher_windows: + dependency: transitive + description: + name: url_launcher_windows + sha256: ecf9725510600aa2bb6d7ddabe16357691b6d2805f66216a97d1b881e21beff7 + url: "https://pub.dev" + source: hosted + version: "3.1.1" vector_math: dependency: transitive description: diff --git a/flutter/pubspec.yaml b/flutter/pubspec.yaml index 16baa53..249b099 100644 --- a/flutter/pubspec.yaml +++ b/flutter/pubspec.yaml @@ -39,6 +39,8 @@ dependencies: http: ^1.2.0 provider: ^6.1.1 shared_preferences: ^2.2.2 + qr_flutter: ^4.1.0 + url_launcher: ^6.2.4 dependency_overrides: diff --git a/flutter/windows/flutter/generated_plugin_registrant.cc b/flutter/windows/flutter/generated_plugin_registrant.cc index 8b6d468..4f78848 100644 --- a/flutter/windows/flutter/generated_plugin_registrant.cc +++ b/flutter/windows/flutter/generated_plugin_registrant.cc @@ -6,6 +6,9 @@ #include "generated_plugin_registrant.h" +#include void RegisterPlugins(flutter::PluginRegistry* registry) { + UrlLauncherWindowsRegisterWithRegistrar( + registry->GetRegistrarForPlugin("UrlLauncherWindows")); } diff --git a/flutter/windows/flutter/generated_plugins.cmake b/flutter/windows/flutter/generated_plugins.cmake index b93c4c3..88b22e5 100644 --- a/flutter/windows/flutter/generated_plugins.cmake +++ b/flutter/windows/flutter/generated_plugins.cmake @@ -3,6 +3,7 @@ # list(APPEND FLUTTER_PLUGIN_LIST + url_launcher_windows ) list(APPEND FLUTTER_FFI_PLUGIN_LIST