-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #13 from ayekaunic/QC-13-Add-Share-PDF-Module
QC-13: Add 'Share PDF Module'
- Loading branch information
Showing
16 changed files
with
401 additions
and
45 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains 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
This file contains 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
151 changes: 134 additions & 17 deletions
151
app/lib/global/widgets/ayat_tile/ayat_tile_view_model.dart
This file contains 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 |
---|---|---|
@@ -1,48 +1,165 @@ | ||
import 'dart:io'; | ||
import 'package:flutter/services.dart'; | ||
import 'package:path_provider/path_provider.dart' as path_provider; | ||
import 'package:quran_companion/global/setup/bottom_sheet_setup.dart'; | ||
import 'package:quran_companion/global/setup/snackbar_setup.dart'; | ||
import 'package:quran_companion/services_locator.dart'; | ||
import 'package:share_plus/share_plus.dart'; | ||
import 'package:stacked/stacked.dart'; | ||
import 'package:stacked_services/stacked_services.dart'; | ||
import 'package:pdf/widgets.dart' as pw; | ||
|
||
class AyatTileViewModel extends BaseViewModel { | ||
final BottomSheetService _bottomSheetService = locator<BottomSheetService>(); | ||
final SnackbarService _snackbarService = locator<SnackbarService>(); | ||
|
||
bool _isMakingNote = false; | ||
|
||
bool get isMakingNote => _isMakingNote; | ||
|
||
final int ayatNumber; | ||
AyatTileViewModel({required this.ayatNumber}); | ||
|
||
Future<void> onMakeNoteTap() async { | ||
_setIsMakingNote(true); | ||
await _showNoteDialog(); | ||
_setIsMakingNote(false); | ||
} | ||
|
||
void _setIsMakingNote(bool val) { | ||
_isMakingNote = val; | ||
rebuildUi(); | ||
} | ||
|
||
bool _isSharingPDF = false; | ||
bool get isSavingPDF => _isSharingPDF; | ||
void _setIsSavingPDF(bool val) { | ||
_isSharingPDF = val; | ||
rebuildUi(); | ||
} | ||
|
||
final int ayatNumber; | ||
final String arabic; | ||
final String urdu; | ||
AyatTileViewModel({ | ||
required this.ayatNumber, | ||
required this.arabic, | ||
required this.urdu, | ||
}); | ||
|
||
Future<void> _showNoteDialog() async { | ||
SheetResponse? response = await _bottomSheetService.showCustomSheet( | ||
variant: BottomSheetType.note, | ||
barrierDismissible: true, | ||
enableDrag: true, | ||
data: ayatNumber, | ||
); | ||
if (response != null) _showSnackbar(response.confirmed); | ||
if (response != null) { | ||
_snackbarService.showCustomSnackBar( | ||
title: response.confirmed ? 'Success!' : 'Error!', | ||
message: response.confirmed ? 'Note saved.' : 'Unable to save note.', | ||
variant: response.confirmed ? SnackbarType.success : SnackbarType.error, | ||
); | ||
} | ||
} | ||
|
||
Future<void> _showSnackbar(bool isNoteSaved) async { | ||
Future<pw.Document> _generateAyatPDF() async { | ||
final pdf = pw.Document(); | ||
|
||
final fontData = await rootBundle.load('assets/fonts/amiri_bold.ttf'); | ||
final ttf = pw.Font.ttf(fontData); | ||
|
||
pdf.addPage( | ||
pw.Page( | ||
build: (pw.Context context) => pw.Padding( | ||
padding: const pw.EdgeInsets.all(16), | ||
child: pw.Column( | ||
children: [ | ||
pw.Text( | ||
'Quran Companion (Ayat #$ayatNumber)', | ||
style: pw.TextStyle( | ||
fontSize: 24, | ||
fontWeight: pw.FontWeight.bold, | ||
), | ||
), | ||
pw.SizedBox(height: 16), | ||
pw.Align( | ||
alignment: pw.Alignment.centerRight, | ||
child: pw.Text( | ||
arabic, | ||
style: pw.TextStyle( | ||
fontSize: 20, | ||
font: ttf, | ||
), | ||
textAlign: pw.TextAlign.right, | ||
), | ||
), | ||
pw.SizedBox(height: 16), | ||
pw.Align( | ||
alignment: pw.Alignment.centerRight, | ||
child: pw.Text( | ||
urdu, | ||
style: pw.TextStyle( | ||
fontSize: 18, | ||
font: ttf, | ||
), | ||
textAlign: pw.TextAlign.right, | ||
), | ||
), | ||
], | ||
), | ||
), | ||
), | ||
); | ||
|
||
return pdf; | ||
} | ||
|
||
Future<File> _savePDFToFile(pw.Document pdf) async { | ||
final directory = Platform.isIOS | ||
? await path_provider.getApplicationDocumentsDirectory() | ||
: await path_provider.getExternalStorageDirectory(); | ||
|
||
final file = | ||
File('${directory!.path}/Quran-Companion-Ayat#$ayatNumber.pdf'); | ||
await file.writeAsBytes(await pdf.save()); | ||
|
||
return file; | ||
} | ||
|
||
Future<void> _sharePDFFile(File file) async { | ||
final result = await Share.shareXFiles( | ||
[XFile(file.path)], | ||
subject: 'Quran Companion - Ayat#$ayatNumber', | ||
text: 'Quran Companion - Ayat#$ayatNumber', | ||
); | ||
|
||
bool sharedSuccessfully = result.status == ShareResultStatus.success; | ||
|
||
_snackbarService.showCustomSnackBar( | ||
title: isNoteSaved ? 'Success!' : 'Error!', | ||
message: isNoteSaved ? 'Note saved' : 'Unable to save note', | ||
variant: isNoteSaved ? SnackbarType.success : SnackbarType.error, | ||
title: sharedSuccessfully ? 'Success!' : 'Error!', | ||
message: sharedSuccessfully | ||
? 'Ayat#$ayatNumber was shared.' | ||
: 'Unable to share Ayat#$ayatNumber', | ||
variant: sharedSuccessfully ? SnackbarType.success : SnackbarType.error, | ||
); | ||
} | ||
|
||
void onSavePdfTap() {} | ||
Future<void> _deletePDFFile(File file) async { | ||
if (await file.exists()) { | ||
await file.delete(); | ||
} | ||
} | ||
|
||
Future<void> onMakeNoteTap() async { | ||
_setIsMakingNote(true); | ||
await _showNoteDialog(); | ||
_setIsMakingNote(false); | ||
} | ||
|
||
Future<void> onSharePDFTap() async { | ||
_setIsSavingPDF(true); | ||
try { | ||
final pdf = await _generateAyatPDF(); | ||
final file = await _savePDFToFile(pdf); | ||
await _sharePDFFile(file); | ||
await _deletePDFFile(file); | ||
} catch (e) { | ||
_snackbarService.showCustomSnackBar( | ||
title: 'Error!', | ||
message: e.toString(), | ||
variant: SnackbarType.error, | ||
); | ||
} | ||
_setIsSavingPDF(false); | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import 'package:flutter/material.dart'; | ||
import 'package:quran_companion/global/services/size_helper_service.dart'; | ||
|
||
class HorizontalGap extends StatelessWidget { | ||
final double width; | ||
const HorizontalGap(this.width, {super.key}); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return SizedBox(width: width); | ||
return SizedBox(width: width.w); | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# | ||
|
||
list(APPEND FLUTTER_PLUGIN_LIST | ||
url_launcher_linux | ||
) | ||
|
||
list(APPEND FLUTTER_FFI_PLUGIN_LIST | ||
|
This file contains 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
Oops, something went wrong.