Skip to content

Commit

Permalink
v3.0.0
Browse files Browse the repository at this point in the history
added `save as pdf to disk` button on pdf preview page (thanks to Charles)
minor fixes and improvements
  • Loading branch information
DonnC committed May 31, 2021
1 parent 8f0da45 commit 88df70d
Show file tree
Hide file tree
Showing 7 changed files with 187 additions and 27 deletions.
6 changes: 5 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
## 1.0.0 - May 2021
- Initial version

## 2.0.0 - June 2021
## 2.0.0 - May 2021
- fixed high CPU usage
- fixed logbook pdf layout
- minor bug fixes

## 3.0.0 - May 2021
- added `save as pdf to disk` button on pdf preview page (thanks to Charles)
- minor fixes and improvements
15 changes: 11 additions & 4 deletions lib/components/pdf-printer-view/pdf-printer-view.model.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import 'dart:typed_data';

import 'package:momentum/momentum.dart';

import 'index.dart';
Expand All @@ -7,19 +9,24 @@ class PdfPrinterViewModel extends MomentumModel<PdfPrinterViewController> {
PdfPrinterViewController controller, {
this.logBookEntries,
this.loading,
this.logBookData,
}) : super(controller);

final List<List> logBookEntries;
final Uint8List logBookData;
final bool loading;

@override
void update({
List<List> logBookEntries,
bool loading,
Uint8List logBookData,
}) {
PdfPrinterViewModel(controller,
loading: loading ?? this.loading,
logBookEntries: logBookEntries ?? this.logBookEntries)
.updateMomentum();
PdfPrinterViewModel(
controller,
loading: loading ?? this.loading,
logBookEntries: logBookEntries ?? this.logBookEntries,
logBookData: logBookData ?? this.logBookData,
).updateMomentum();
}
}
48 changes: 47 additions & 1 deletion lib/services/app_service.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,14 @@
import 'dart:io';
import 'dart:typed_data';

import 'package:intl/intl.dart';
import 'package:momentum/momentum.dart';
import 'package:path/path.dart';
import 'package:path_provider_windows/path_provider_windows.dart';

import 'package:log_book/constants.dart';
import 'package:log_book/data/index.dart';
import 'package:log_book/models/index.dart';
import 'package:momentum/momentum.dart';

// service class to perform CRUD operations against sembast db
// @DonnC
Expand All @@ -11,6 +18,45 @@ import 'package:momentum/momentum.dart';
class AppService extends MomentumService {
static final Dao _dao = Dao();

Future<AppResponse> savePdfToDisk(Uint8List data) async {
final PathProviderWindows _provider = PathProviderWindows();
final format = DateFormat('dd-M-yyyy_HH.mm.ss', 'en_US');

String _time = format.format(DateTime.now());

try {
final String fileName = 'logbook $_time.pdf';

final appDocumentDir = await _provider.getApplicationDocumentsPath();

final String _subDir = 'LogBook';
final String _logBkDir = join(appDocumentDir, _subDir);

File log = File(join(_logBkDir, fileName));

log.createSync(recursive: true);

print(log.path);
final _output = await log.writeAsBytes(data);

// write contents to pdf file
return AppResponse(
data: _output.path,
action: ResponseAction.Success,
message: 'log book saved to ${_output.path}',
);
}

// catch error, if any
catch (e) {
print(e.toString());
return AppResponse(
action: ResponseAction.Error,
message: e.toString(),
);
}
}

Future<AppResponse> getTodos() async {
try {
final todos = await _dao.getAllTodos();
Expand Down
51 changes: 51 additions & 0 deletions lib/services/dialog_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,57 @@ class DialogService extends MomentumService {
);
}

Future<void> showFlashInfoDialog(
BuildContext context, String info, String title) async {
await showFlash(
context: context,
//persistent: false,
builder: (context, controller) {
return Flash.bar(
controller: controller,
backgroundGradient: LinearGradient(
colors: [bgColor, secondaryColor],
),
borderRadius: const BorderRadius.all(Radius.circular(8)),
margin: const EdgeInsets.all(8),
forwardAnimationCurve: Curves.easeOutBack,
reverseAnimationCurve: Curves.slowMiddle,
position: FlashPosition.bottom,
enableDrag: true,
child: FlashBar(
icon: Icon(
Icons.delete,
color: textColor,
),
shouldIconPulse: false,
showProgressIndicator: true,
message: Text(
info,
style: TextStyle(
fontSize: 14,
),
),
title: Text(
title,
style: TextStyle(
fontSize: 16,
fontWeight: FontWeight.bold,
),
),
actions: [
ElevatedButton(
onPressed: () {
controller.dismiss();
},
child: Text('CANCEL'),
),
],
),
);
},
);
}

Future<bool> showFlashDialogConfirm(
BuildContext context, String info, String title) async {
var res = showFlash<bool>(
Expand Down
76 changes: 64 additions & 12 deletions lib/views/pdf_gen_view.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
import 'dart:typed_data';

import 'package:flutter/material.dart';
import 'package:flutter_icons/flutter_icons.dart';
import 'package:momentum/momentum.dart';
import 'package:pdf/pdf.dart';
import 'package:pdf/widgets.dart' as pw;
import 'package:printing/printing.dart';
import 'package:relative_scale/relative_scale.dart';

import 'package:log_book/components/index.dart';
import 'package:log_book/constants.dart';
import 'package:log_book/services/index.dart';
import 'package:log_book/utils/index.dart';
import 'package:log_book/widgets/index.dart';

class PdfGenView extends StatelessWidget {
Expand All @@ -19,17 +23,11 @@ class PdfGenView extends StatelessWidget {
];

return pw.Table.fromTextArray(
// border: null,
cellAlignment: pw.Alignment.centerLeft,
//headerDecoration: pw.BoxDecoration(
// color: baseColor,
//),
//headerHeight: 25,
//cellHeight: 40,
columnWidths: {
0: pw.FixedColumnWidth(70.0), // fixed to 100 width
0: pw.FixedColumnWidth(70.0),
1: pw.FlexColumnWidth(),
2: pw.FixedColumnWidth(80.0), //fixed to 100 width
2: pw.FixedColumnWidth(80.0),
},
cellAlignments: {
0: pw.Alignment.center,
Expand All @@ -42,12 +40,10 @@ class PdfGenView extends StatelessWidget {
2: pw.Alignment.center,
},
headerStyle: pw.TextStyle(
//color: _baseTextColor,
fontSize: 10,
fontWeight: pw.FontWeight.bold,
),
cellStyle: const pw.TextStyle(
// color: _darkColor,
fontSize: 10,
),
rowDecoration: pw.BoxDecoration(
Expand Down Expand Up @@ -88,6 +84,57 @@ class PdfGenView extends StatelessWidget {
),
onPressed: () => MomentumRouter.pop(context),
),
actions: [
Padding(
padding: const EdgeInsets.all(10),
child: IconButton(
tooltip: 'save log book as .pdf to disk',
splashRadius: 5,
hoverColor: secondaryColor,
icon: Icon(FontAwesome.file_pdf_o),
onPressed: () {
final dialogService =
Momentum.service<DialogService>(context);
final bytes =
Momentum.controller<PdfPrinterViewController>(context)
.model
?.logBookData;

if (bytes != null) {
Momentum.service<AppService>(context)
.savePdfToDisk(bytes)
.then((value) {
switch (value.action) {
case ResponseAction.Success:
dialogService.showFlashInfoDialog(
context,
value.message,
'LogBook',
);

break;
default:
dialogService.showFlashInfoDialog(
context,
value.message,
'LogBook',
);
}
});
}

// show prompt
else {
dialogService.showFlashBar(
context,
'no generated log book data found for saving',
'LogBook',
);
}
},
),
),
],
),
body: RelativeBuilder(builder: (context, height, width, sy, sx) {
return MomentumBuilder(
Expand All @@ -105,8 +152,13 @@ class PdfGenView extends StatelessWidget {
)
: PdfPreview(
allowSharing: false,
build: (format) =>
_generatePdf(format, model.logBookEntries),
build: (format) async {
final _data =
_generatePdf(format, model.logBookEntries);
var logData = await _data;
model.update(logBookData: logData);
return _data;
},
);
});
}),
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 2.0.0+1
version: 3.0.0+1

environment:
#sdk: ">=2.7.0 <3.0.0"
Expand Down
16 changes: 8 additions & 8 deletions windows/runner/Runner.rc
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,13 @@ IDI_APP_ICON ICON "resources\\app_icon.ico"
#ifdef FLUTTER_BUILD_NUMBER
#define VERSION_AS_NUMBER FLUTTER_BUILD_NUMBER
#else
#define VERSION_AS_NUMBER 1,0,0
#define VERSION_AS_NUMBER 3,0,0
#endif

#ifdef FLUTTER_BUILD_NAME
#define VERSION_AS_STRING #FLUTTER_BUILD_NAME
#else
#define VERSION_AS_STRING "1.0.0"
#define VERSION_AS_STRING "3.0.0"
#endif

VS_VERSION_INFO VERSIONINFO
Expand All @@ -89,13 +89,13 @@ BEGIN
BEGIN
BLOCK "040904e4"
BEGIN
VALUE "CompanyName", "com.example" "\0"
VALUE "FileDescription", "A new Flutter project." "\0"
VALUE "CompanyName", "com.donnclab" "\0"
VALUE "FileDescription", "An Intern LogBook App." "\0"
VALUE "FileVersion", VERSION_AS_STRING "\0"
VALUE "InternalName", "log_book" "\0"
VALUE "LegalCopyright", "Copyright (C) 2021 com.example. All rights reserved." "\0"
VALUE "OriginalFilename", "log_book.exe" "\0"
VALUE "ProductName", "log_book" "\0"
VALUE "InternalName", "LogBook" "\0"
VALUE "LegalCopyright", "Copyright (C) 2021 com.donnclab. All rights reserved." "\0"
VALUE "OriginalFilename", "LogBook.exe" "\0"
VALUE "ProductName", "LogBook" "\0"
VALUE "ProductVersion", VERSION_AS_STRING "\0"
END
END
Expand Down

0 comments on commit 88df70d

Please # to comment.