Skip to content

Commit

Permalink
feat: Support selective disclosure nested selection #2553
Browse files Browse the repository at this point in the history
  • Loading branch information
bibash28 committed Apr 1, 2024
1 parent fd8e82a commit 084b4bf
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 67 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import 'package:altme/app/shared/shared.dart';
import 'package:altme/dashboard/home/home.dart';
import 'package:altme/selective_disclosure/selective_disclosure.dart';
import 'package:equatable/equatable.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:json_annotation/json_annotation.dart';
Expand All @@ -9,39 +11,47 @@ part 'selective_disclosure_pick_cubit.g.dart';
class SelectiveDisclosureCubit extends Cubit<SelectiveDisclosureState> {
SelectiveDisclosureCubit() : super(const SelectiveDisclosureState());

void toggle(int index) {
final bool isSelected = state.selected.contains(index);
void toggle(String claimKeyId) {
final List<String> selectedClaimsKeys = List.of(state.selectedClaimsKeyIds);

late List<int> selected;
late List<String> id;

if (isSelected) {
if (selectedClaimsKeys.contains(claimKeyId)) {
/// deSelecting the credential
selected = List<int>.from(state.selected)
..removeWhere((element) => element == index);
id = List<String>.from(state.selectedClaimsKeyIds)
..removeWhere((element) => element == claimKeyId);
} else {
/// selecting the credential
selected = [
...state.selected,
...[index],
id = [
...state.selectedClaimsKeyIds,
...[claimKeyId],
];
}
emit(state.copyWith(selected: selected));
emit(state.copyWith(selectedClaimsKeyIds: id));
}

void saveIndexOfSDJWT(int index) {
final bool isSelected = state.selectedSDIndexInJWT.contains(index);
void saveIndexOfSDJWT({
required String claimsKey,
required CredentialModel credentialModel,
}) {
final selectiveDisclosure = SelectiveDisclosure(credentialModel);
final sdIndexInJWT = selectiveDisclosure.extractedValuesFromJwt.entries
.toList()
.indexWhere((entry) => entry.key == claimsKey);

final bool isSelected = state.selectedSDIndexInJWT.contains(sdIndexInJWT);

late List<int> selected;

if (isSelected) {
/// deSelecting the credential
selected = List<int>.from(state.selectedSDIndexInJWT)
..removeWhere((element) => element == index);
..removeWhere((element) => element == sdIndexInJWT);
} else {
/// selecting the credential
selected = [
...state.selectedSDIndexInJWT,
...[index],
...[sdIndexInJWT],
];
}
emit(state.copyWith(selectedSDIndexInJWT: selected));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@ part of 'selective_disclosure_pick_cubit.dart';
class SelectiveDisclosureState extends Equatable {
const SelectiveDisclosureState({
this.message,
this.selected = const [],
this.selectedClaimsKeyIds = const [],
this.selectedSDIndexInJWT = const [],
});

factory SelectiveDisclosureState.fromJson(Map<String, dynamic> json) =>
_$SelectiveDisclosureStateFromJson(json);

final StateMessage? message;
final List<int> selected;
final List<String> selectedClaimsKeyIds;
final List<int> selectedSDIndexInJWT;

SelectiveDisclosureState copyWith({
List<int>? selected,
List<String>? selectedClaimsKeyIds,
List<int>? selectedSDIndexInJWT,
StateMessage? message,
}) {
return SelectiveDisclosureState(
selected: selected ?? this.selected,
selectedClaimsKeyIds: selectedClaimsKeyIds ?? this.selectedClaimsKeyIds,
selectedSDIndexInJWT: selectedSDIndexInJWT ?? this.selectedSDIndexInJWT,
message: message,
);
Expand All @@ -31,7 +31,7 @@ class SelectiveDisclosureState extends Equatable {

@override
List<Object?> get props => [
selected,
selectedClaimsKeyIds,
selectedSDIndexInJWT,
message,
];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,13 @@ class SelectiveDisclosurePickView extends StatelessWidget {
DisplaySelectiveDisclosure(
credentialModel: credentialToBePresented,
claims: null,
selectedIndex: state.selected,
onPressed: (claimIndex, sdIndexInJWT) {
context.read<SelectiveDisclosureCubit>().toggle(claimIndex);
context
.read<SelectiveDisclosureCubit>()
.saveIndexOfSDJWT(sdIndexInJWT);
selectedClaimsKeyIds: state.selectedClaimsKeyIds,
onPressed: (claimKey, claimKeyId) {
context.read<SelectiveDisclosureCubit>().toggle(claimKeyId);
context.read<SelectiveDisclosureCubit>().saveIndexOfSDJWT(
claimsKey: claimKey,
credentialModel: credentialToBePresented,
);
},
showVertically: true,
),
Expand Down
69 changes: 27 additions & 42 deletions lib/selective_disclosure/widget/display_selective_disclosure.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,23 @@ class DisplaySelectiveDisclosure extends StatelessWidget {
required this.showVertically,
this.claims,
this.onPressed,
this.selectedIndex,
this.selectedClaimsKeyIds,
this.parentKeyId,
});

final CredentialModel credentialModel;
final bool showVertically;
final Map<String, dynamic>? claims;
final void Function(int, int)? onPressed;
final List<int>? selectedIndex;
final void Function(String, String)? onPressed;
final List<String>? selectedClaimsKeyIds;
final String? parentKeyId;

@override
Widget build(BuildContext context) {
final selectiveDisclosure = SelectiveDisclosure(credentialModel);
final currentClaims = claims ?? selectiveDisclosure.claims;
final languageCode = context.read<LangCubit>().state.locale.languageCode;

return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: currentClaims.entries.map((MapEntry<String, dynamic> map) {
Expand All @@ -36,14 +39,6 @@ class DisplaySelectiveDisclosure extends StatelessWidget {
final key = map.key;
final value = map.value;

final claimIndex = currentClaims.entries
.toList()
.indexWhere((entry) => entry.key == key);

final sdIndexInJWT = selectiveDisclosure.extractedValuesFromJwt.entries
.toList()
.indexWhere((entry) => entry.key == key);

// "mandatory": True,
// "value_type": "string",
// "display": [
Expand Down Expand Up @@ -76,7 +71,7 @@ class DisplaySelectiveDisclosure extends StatelessWidget {
final bool hasNestedData =
value.values.any((element) => element is Map<String, dynamic>);

if (hasNestedData) {
if (hasNestedData && parentKeyId == null) {
return Column(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand All @@ -90,33 +85,17 @@ class DisplaySelectiveDisclosure extends StatelessWidget {
),
),
),
TransparentInkWell(
onTap: () => onPressed?.call(claimIndex, sdIndexInJWT),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Padding(
padding: const EdgeInsets.only(left: 10),
child: DisplaySelectiveDisclosure(
credentialModel: credentialModel,
claims: value,
showVertically: showVertically,
),
),
if (selectedIndex != null) ...[
const Spacer(),
Padding(
padding: const EdgeInsets.only(top: 15, right: 10),
child: Icon(
selectedIndex!.contains(claimIndex)
? Icons.check_box
: Icons.check_box_outline_blank,
size: 25,
color: Theme.of(context).colorScheme.onPrimary,
),
),
],
],
Padding(
padding: const EdgeInsets.only(left: 10),
child: DisplaySelectiveDisclosure(
credentialModel: credentialModel,
claims: value,
showVertically: showVertically,
selectedClaimsKeyIds: selectedClaimsKeyIds,
parentKeyId: key,
onPressed: (nestedKey, _) {
onPressed?.call(nestedKey, '$key-$nestedKey');
},
),
),
],
Expand All @@ -131,8 +110,14 @@ class DisplaySelectiveDisclosure extends StatelessWidget {

if (data == null) return Container();

var keyToCheck = key;

if (parentKeyId != null) {
keyToCheck = '$parentKeyId-$key';
}

return TransparentInkWell(
onTap: () => onPressed?.call(claimIndex, sdIndexInJWT),
onTap: () => onPressed?.call(key, key),
child: Row(
crossAxisAlignment: CrossAxisAlignment.start,
children: [
Expand All @@ -147,12 +132,12 @@ class DisplaySelectiveDisclosure extends StatelessWidget {
showVertically: showVertically,
),
),
if (selectedIndex != null && isfromDisclosureOfJWT) ...[
if (selectedClaimsKeyIds != null && isfromDisclosureOfJWT) ...[
const Spacer(),
Padding(
padding: const EdgeInsets.only(top: 15, right: 10),
child: Icon(
selectedIndex!.contains(claimIndex)
selectedClaimsKeyIds!.contains(keyToCheck)
? Icons.check_box
: Icons.check_box_outline_blank,
size: 25,
Expand Down

0 comments on commit 084b4bf

Please sign in to comment.