From 477eb6d7e1b00750def408710b28c63672420802 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonas=20Uek=C3=B6tter?= Date: Wed, 25 Sep 2024 20:15:23 +0200 Subject: [PATCH] example --- passkit_server/README.md | 91 +++++++++++++++++++ .../example/passkit_server_example.dart | 78 ++++++++++++++++ passkit_server/lib/src/utils.dart | 2 + 3 files changed, 171 insertions(+) diff --git a/passkit_server/README.md b/passkit_server/README.md index 23280ac..0d3db86 100644 --- a/passkit_server/README.md +++ b/passkit_server/README.md @@ -11,4 +11,95 @@ ------- +PassKit allows you to work with Apple's PkPass and Order files. This is a Dart library, which allows you to integrate the PassKit enpoint in your shelf (and potentially dart_frog) application. + +In order to show PassKit and Order files in Flutter, use the [`passkit_ui`](https://pub.dev/packages/passkit_ui) package, which includes ready made widgets. + +Want to work with Apple's native PassKit APIs in Flutter? Consider using [`apple_passkit`](https://pub.dev/packages/apple_passkit). + Please read through the [Apple Documentation](https://developer.apple.com/documentation/walletpasses/adding-a-web-service-to-update-passes) for the server implementation first. + +A brief example looks roughly like this. Unfortunately, it's not possible to move more logic into +the library, since it depends too much on your application logic. + +```dart +import 'dart:async'; + +import 'package:passkit/src/pkpass/pkpass.dart'; +import 'package:passkit_server/passkit_server.dart'; +import 'package:shelf/shelf.dart'; +import 'package:shelf/shelf_io.dart' as io; +import 'package:shelf_router/shelf_router.dart'; + +Future main() async { + var app = Router(); + + app.addPassKitServer(CustomPassKitBackend()); + + app.get('/hello', (Request request) { + return Response.ok('hello-world'); + }); + + var server = await io.serve(app.call, 'localhost', 8888); +} + +class CustomPassKitBackend extends PassKitBackend { + @override + Future getLatestPassFor(String identifier, String serial) async { + print('getLatestPassFor($identifier, $serial)'); + return null; + } + + @override + FutureOr isKnownDeviceId(String deviceId) { + print('isKnownDeviceId($deviceId)'); + return true; + } + + @override + FutureOr isValidAuthToken(String serial, String authToken) { + print('isValidAuthToken($serial, $authToken)'); + return true; + } + + @override + Future logMessage(Map message) async { + print('logMessage($message)'); + } + + @override + Future returnUpdatablePasses( + String deviceId, + String typeId, + String? lastTag, + ) async { + print('returnUpdatablePasses($deviceId, $typeId, $lastTag)'); + return null; + } + + @override + Future startSendingPushNotificationsFor( + String deviceId, + String passTypeId, + String serialNumber, + String pushToken, + ) async { + print( + 'startSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber, $pushToken)', + ); + return NotificationRegistrationReponse.created; + } + + @override + Future stopSendingPushNotificationsFor( + String deviceId, + String passTypeId, + String serialNumber, + ) async { + print( + 'stopSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber)', + ); + return true; + } +} +``` diff --git a/passkit_server/example/passkit_server_example.dart b/passkit_server/example/passkit_server_example.dart index 8b13789..ca8a6e6 100644 --- a/passkit_server/example/passkit_server_example.dart +++ b/passkit_server/example/passkit_server_example.dart @@ -1 +1,79 @@ +import 'dart:async'; +import 'package:passkit/src/pkpass/pkpass.dart'; +import 'package:passkit_server/passkit_server.dart'; +import 'package:shelf/shelf.dart'; +import 'package:shelf/shelf_io.dart' as io; +import 'package:shelf_router/shelf_router.dart'; + +Future main() async { + var app = Router(); + + app.addPassKitServer(CustomPassKitBackend()); + + app.get('/hello', (Request request) { + return Response.ok('hello-world'); + }); + + var server = await io.serve(app.call, 'localhost', 8888); +} + +class CustomPassKitBackend extends PassKitBackend { + @override + Future getLatestPassFor(String identifier, String serial) async { + print('getLatestPassFor($identifier, $serial)'); + return null; + } + + @override + FutureOr isKnownDeviceId(String deviceId) { + print('isKnownDeviceId($deviceId)'); + return true; + } + + @override + FutureOr isValidAuthToken(String serial, String authToken) { + print('isValidAuthToken($serial, $authToken)'); + return true; + } + + @override + Future logMessage(Map message) async { + print('logMessage($message)'); + } + + @override + Future returnUpdatablePasses( + String deviceId, + String typeId, + String? lastTag, + ) async { + print('returnUpdatablePasses($deviceId, $typeId, $lastTag)'); + return null; + } + + @override + Future startSendingPushNotificationsFor( + String deviceId, + String passTypeId, + String serialNumber, + String pushToken, + ) async { + print( + 'startSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber, $pushToken)', + ); + return NotificationRegistrationReponse.created; + } + + @override + Future stopSendingPushNotificationsFor( + String deviceId, + String passTypeId, + String serialNumber, + ) async { + print( + 'stopSendingPushNotificationsFor($deviceId, $passTypeId, $serialNumber)', + ); + return true; + } +} diff --git a/passkit_server/lib/src/utils.dart b/passkit_server/lib/src/utils.dart index ad5bfbc..40f7681 100644 --- a/passkit_server/lib/src/utils.dart +++ b/passkit_server/lib/src/utils.dart @@ -14,3 +14,5 @@ final _utf8JsonEncoder = JsonUtf8Encoder(); Uint8List utf8JsonEncode(Object data) => Uint8List.fromList(_utf8JsonEncoder.convert(data)); + +const kdebugMode = bool.fromEnvironment('dart.vm.product');