Skip to content

Commit

Permalink
added random string generator to create challenge to stamp ticket and…
Browse files Browse the repository at this point in the history
… its implementation in repository.dart remote_data_store.dart

owner_view_view_model.dart
mobile_scanner.dart
  • Loading branch information
Raviramnani1 committed Aug 28, 2024
1 parent 955f961 commit ff2566b
Show file tree
Hide file tree
Showing 6 changed files with 94 additions and 15 deletions.
13 changes: 10 additions & 3 deletions wallet/lib/pages/detailed_asset_view/owner_view_view_model.dart
Original file line number Diff line number Diff line change
Expand Up @@ -518,11 +518,17 @@ class OwnerViewViewModel extends ChangeNotifier {
}
}

Future<void> stampTicket({required bool enabled}) async {
Future<void> stampTicket({
required bool enabled,
required String cookbookId,
required String recipeId,
required String challenge,
}) async {
final response = await repository.stampTicket(
cookBookId: CookbookId(events.cookbookID),
recipeId: RecipeId(events.recipeID),
cookBookId: CookbookId(cookbookId),
recipeId: RecipeId(recipeId),
creatorAddress: Address(events.ownerAddress),
challenge: challenge,
);

if (response.isLeft()) {
Expand All @@ -533,6 +539,7 @@ class OwnerViewViewModel extends ChangeNotifier {
notifyListeners();
}


void logEvent() {
repository.logUserJourney(screenName: AnalyticsScreenEvents.ownerView);
}
Expand Down
10 changes: 6 additions & 4 deletions wallet/lib/pages/events/event_qr_code_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import 'package:pylons_wallet/model/event.dart';
import 'package:pylons_wallet/pages/detailed_asset_view/widgets/nft_image_asset.dart';
import 'package:pylons_wallet/utils/constants.dart';
import 'package:qr_flutter/qr_flutter.dart';
import 'package:pylons_wallet/utils/extension.dart';

import '../../providers/account_provider.dart';

Expand All @@ -29,7 +28,7 @@ class EventQrCodeScreen extends StatefulWidget {
}

class _EventQrCodeScreenState extends State<EventQrCodeScreen> {
String link = "";
String qrdata = "";
GlobalKey renderObjectKey = GlobalKey();


Expand All @@ -48,7 +47,10 @@ class _EventQrCodeScreenState extends State<EventQrCodeScreen> {
if (wallet == null) {
return;
}
link = widget.events.recipeID;
qrdata = jsonEncode({
'cookbookId': widget.events.cookbookID,
'recipeId': widget.events.recipeID,
});
}

@override
Expand Down Expand Up @@ -83,7 +85,7 @@ class _EventQrCodeScreenState extends State<EventQrCodeScreen> {
key: renderObjectKey,
child: QrImageView(
padding: EdgeInsets.zero,
data: link,
data: qrdata,
size: 200,
dataModuleStyle: const QrDataModuleStyle(
color: AppColors.kWhite),
Expand Down
31 changes: 25 additions & 6 deletions wallet/lib/pages/events/mobile_scanner.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'package:flutter/material.dart';
import 'package:get_it/get_it.dart';
import 'package:mobile_scanner/mobile_scanner.dart';

import '../../utils/string_utils.dart';
import '../detailed_asset_view/owner_view_view_model.dart';

class MobileQrScanner extends StatefulWidget {
Expand Down Expand Up @@ -39,22 +40,39 @@ class _MobileQrScannerState extends State<MobileQrScanner> {
_barcode = barcodes.barcodes.firstOrNull;

if (_barcode != null) {
final qrData = _barcode!.displayValue ?? '';
final dataParts = qrData.split(',');

final String eventId = _barcode!.displayValue ?? '';
if (dataParts.length >= 2) {
final cookbookId = dataParts[0];
final recipeId = dataParts[1];

// Use the QR code data to stamp the ticket
_stampTicket(eventId);

final challenge = StringUtils.generateRandomString(16);


stampTicket(cookbookId, recipeId, challenge);
} else {
ScaffoldMessenger.of(context).showSnackBar(
const SnackBar(content: Text('Invalid QR code data.')),
);
}
}
});
}
}

Future<void> stampTicket(String eventId) async {
Future<void> stampTicket(String cookbookId, String recipeId, String challenge) async {
try {
await ownerViewViewModel.stampTicket(
enabled: true,
cookbookId: cookbookId,
recipeId: recipeId,
challenge: challenge,
);

await ownerViewViewModel.stampTicket(enabled: true);
ScaffoldMessenger.of(context).showSnackBar(
SnackBar(content: Text('Ticket $eventId stamped successfully!')),
SnackBar(content: Text('Ticket with challenge $challenge stamped successfully!')),
);
} catch (e) {
ScaffoldMessenger.of(context).showSnackBar(
Expand All @@ -63,6 +81,7 @@ class _MobileQrScannerState extends State<MobileQrScanner> {
}
}


@override
Widget build(BuildContext context) {
return Scaffold(
Expand Down
17 changes: 15 additions & 2 deletions wallet/lib/services/data_stores/remote_data_store.dart
Original file line number Diff line number Diff line change
Expand Up @@ -361,6 +361,13 @@ abstract class RemoteDataStore {
/// Input : [address] the address & [Event] against which the invite link to be generated
/// Output: [String] return the generated dynamic link else will throw error
Future<String> createDynamicLinkForRecipeEventShare({required String address, required Events events});

Future<String> stampTicket({
required CookbookId cookBookId,
required RecipeId recipeId,
required Address creatorAddress,
required String challenge,
});
}

class RemoteDataStoreImp implements RemoteDataStore {
Expand Down Expand Up @@ -1305,25 +1312,31 @@ class RemoteDataStoreImp implements RemoteDataStore {
return _signAndBroadcast(msgUpdateRecipe);
}

@override
Future<String> stampTicket({
required CookbookId cookBookId,
required RecipeId recipeId,
required Address creatorAddress,
required String challenge,
}) async {
final recipe = await getRecipe(cookBookId: cookBookId, recipeId: recipeId);

final recipeProto3Json = recipe.toProto3Json()! as Map;
recipeProto3Json.remove(kCreatedAtCamelCase);
recipeProto3Json.remove(kUpdatedAtCamelCase);
recipeProto3Json.putIfAbsent("isStamped", true as Function());
recipeProto3Json.putIfAbsent("isStamped", () => true);

final msgUpdateRecipe = pylons.MsgUpdateRecipe.create()..mergeFromProto3Json(recipeProto3Json);
final msgUpdateRecipe = pylons.MsgUpdateRecipe.create()
..mergeFromProto3Json(recipeProto3Json);

msgUpdateRecipe.extraInfo = challenge;
msgUpdateRecipe.version = msgUpdateRecipe.version.incrementRecipeVersion();
msgUpdateRecipe.creator = creatorAddress.toString();

return _signAndBroadcast(msgUpdateRecipe);
}


@override
Future<String> createDynamicLinkForItemNftShare({
required String address,
Expand Down
23 changes: 23 additions & 0 deletions wallet/lib/services/repository/repository.dart
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,7 @@ abstract class Repository {
required CookbookId cookBookId,
required RecipeId recipeId,
required Address creatorAddress,
required String challenge,
});

Future<Either<Failure, void>> createTrade({required pylons.MsgCreateTrade msgCreateTrade});
Expand Down Expand Up @@ -2428,6 +2429,28 @@ class RepositoryImp implements Repository {
}
}

@override
Future<Either<Failure, void>> stampTicket({
required CookbookId cookBookId,
required RecipeId recipeId,
required Address creatorAddress,
required String challenge,
}) async {
try {
await remoteDataStore.stampTicket(
cookBookId: cookBookId,
recipeId: recipeId,
creatorAddress: creatorAddress,
challenge: challenge,
);
return const Right(null);
} on Exception catch (e) {
recordErrorInCrashlytics(e);
return Left(ServerFailure(e.toString()));
}
}


@override
Future<Either<Failure, void>> createTrade({required pylons.MsgCreateTrade msgCreateTrade}) async {
try {
Expand Down
15 changes: 15 additions & 0 deletions wallet/lib/utils/string_utils.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import 'dart:math';

class StringUtils {
static String generateRandomString(int length) {
const String _chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
Random _rnd = Random();

return String.fromCharCodes(
Iterable.generate(
length,
(_) => _chars.codeUnitAt(_rnd.nextInt(_chars.length)),
),
);
}
}

0 comments on commit ff2566b

Please sign in to comment.