-
Notifications
You must be signed in to change notification settings - Fork 48
[Mobile]:Add share card entry points to mobile forecast details #3668
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
185 changes: 185 additions & 0 deletions
185
src/mobile/lib/src/app/dashboard/widgets/air_quality_share_sheet.dart
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,185 @@ | ||
| import 'dart:typed_data'; | ||
| import 'dart:ui' as ui; | ||
|
|
||
| import 'package:airqo/src/app/dashboard/models/airquality_response.dart'; | ||
| import 'package:airqo/src/app/dashboard/widgets/air_quality_share_card.dart'; | ||
| import 'package:airqo/src/app/shared/services/air_quality_share_service.dart'; | ||
| import 'package:airqo/src/meta/utils/colors.dart'; | ||
| import 'package:flutter/material.dart'; | ||
| import 'package:flutter/rendering.dart'; | ||
|
|
||
| Future<void> showAirQualityShareSheet( | ||
| BuildContext context, { | ||
| required Measurement measurement, | ||
| String? fallbackLocationName, | ||
| Rect? sharePositionOrigin, | ||
| }) { | ||
| return showModalBottomSheet<void>( | ||
| context: context, | ||
| useRootNavigator: true, | ||
| isScrollControlled: true, | ||
| showDragHandle: true, | ||
| backgroundColor: Colors.transparent, | ||
| builder: (_) => AirQualityShareSheet( | ||
| measurement: measurement, | ||
| fallbackLocationName: fallbackLocationName, | ||
| sharePositionOrigin: sharePositionOrigin, | ||
| ), | ||
| ); | ||
| } | ||
|
|
||
| class AirQualityShareSheet extends StatefulWidget { | ||
| final Measurement measurement; | ||
| final String? fallbackLocationName; | ||
| final Rect? sharePositionOrigin; | ||
|
|
||
| const AirQualityShareSheet({ | ||
| super.key, | ||
| required this.measurement, | ||
| this.fallbackLocationName, | ||
| this.sharePositionOrigin, | ||
| }); | ||
|
|
||
| @override | ||
| State<AirQualityShareSheet> createState() => _AirQualityShareSheetState(); | ||
| } | ||
|
|
||
| class _AirQualityShareSheetState extends State<AirQualityShareSheet> { | ||
| final GlobalKey _cardKey = GlobalKey(); | ||
| bool _isSharingCard = false; | ||
|
|
||
| Future<void> _shareCard() async { | ||
| if (_isSharingCard) return; | ||
|
|
||
| setState(() { | ||
| _isSharingCard = true; | ||
| }); | ||
|
|
||
| try { | ||
| final imageBytes = await _captureCard(); | ||
| if (imageBytes == null) { | ||
| if (!mounted) return; | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| const SnackBar( | ||
| content: | ||
| Text('Could not prepare the share card. Please try again.'), | ||
| ), | ||
| ); | ||
| return; | ||
| } | ||
|
|
||
| await AirQualityShareService.shareMeasurementCard( | ||
| imageBytes, | ||
| widget.measurement, | ||
| fallbackLocationName: widget.fallbackLocationName, | ||
| sharePositionOrigin: widget.sharePositionOrigin, | ||
| ); | ||
| } catch (_) { | ||
| if (!mounted) return; | ||
| ScaffoldMessenger.of(context).showSnackBar( | ||
| const SnackBar( | ||
| content: Text('Could not share the card. Please try again.'), | ||
| ), | ||
| ); | ||
| } finally { | ||
|
coderabbitai[bot] marked this conversation as resolved.
|
||
| if (mounted) { | ||
| setState(() { | ||
| _isSharingCard = false; | ||
| }); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| Future<Uint8List?> _captureCard() async { | ||
| final pixelRatio = | ||
| MediaQuery.of(context).devicePixelRatio.clamp(2.0, 3.0).toDouble(); | ||
|
|
||
| await Future<void>.delayed(const Duration(milliseconds: 16)); | ||
|
|
||
| final boundary = | ||
| _cardKey.currentContext?.findRenderObject() as RenderRepaintBoundary?; | ||
| if (boundary == null) return null; | ||
|
|
||
| final image = await boundary.toImage(pixelRatio: pixelRatio); | ||
| final byteData = await image.toByteData(format: ui.ImageByteFormat.png); | ||
|
|
||
| return byteData?.buffer.asUint8List(); | ||
| } | ||
|
|
||
| @override | ||
| Widget build(BuildContext context) { | ||
| final textColor = Theme.of(context).textTheme.headlineSmall?.color; | ||
| final subtitleColor = | ||
| Theme.of(context).textTheme.bodyMedium?.color?.withValues(alpha: 0.72); | ||
|
|
||
| return SafeArea( | ||
| child: Padding( | ||
| padding: const EdgeInsets.fromLTRB(16, 0, 16, 16), | ||
| child: DecoratedBox( | ||
| decoration: BoxDecoration( | ||
| color: Theme.of(context).cardColor, | ||
| borderRadius: BorderRadius.circular(28), | ||
| ), | ||
| child: Padding( | ||
| padding: const EdgeInsets.fromLTRB(16, 12, 16, 16), | ||
| child: Column( | ||
| mainAxisSize: MainAxisSize.min, | ||
| crossAxisAlignment: CrossAxisAlignment.start, | ||
| children: [ | ||
| Text( | ||
| 'Share card', | ||
| style: TextStyle( | ||
| fontSize: 20, | ||
| fontWeight: FontWeight.w700, | ||
| color: textColor, | ||
| ), | ||
| ), | ||
| const SizedBox(height: 6), | ||
| Text( | ||
| 'Preview the card before sending it.', | ||
| style: TextStyle( | ||
| fontSize: 14, | ||
| color: subtitleColor, | ||
| ), | ||
| ), | ||
| const SizedBox(height: 16), | ||
| RepaintBoundary( | ||
| key: _cardKey, | ||
| child: AirQualityShareCard( | ||
| measurement: widget.measurement, | ||
| fallbackLocationName: widget.fallbackLocationName, | ||
| ), | ||
| ), | ||
| const SizedBox(height: 16), | ||
| SizedBox( | ||
| width: double.infinity, | ||
| child: ElevatedButton.icon( | ||
| onPressed: _isSharingCard ? null : _shareCard, | ||
| icon: _isSharingCard | ||
| ? const SizedBox( | ||
| width: 16, | ||
| height: 16, | ||
| child: CircularProgressIndicator(strokeWidth: 2), | ||
| ) | ||
| : const Icon(Icons.ios_share_outlined), | ||
| label: Text( | ||
| _isSharingCard ? 'Preparing card...' : 'Share card', | ||
| ), | ||
| style: ElevatedButton.styleFrom( | ||
| backgroundColor: AppColors.primaryColor, | ||
| foregroundColor: Colors.white, | ||
| minimumSize: const Size.fromHeight(52), | ||
| shape: RoundedRectangleBorder( | ||
| borderRadius: BorderRadius.circular(18), | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ], | ||
| ), | ||
| ), | ||
| ), | ||
| ), | ||
| ); | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.