From 73ff25297718dc001e69b82060fc79d85428a605 Mon Sep 17 00:00:00 2001 From: Rexios Date: Fri, 8 Dec 2023 10:42:54 +0530 Subject: [PATCH] Compact dependents history item --- pub_stats/lib/extension/diff_extension.dart | 60 +++++++++++++++++++ .../lib/view/widget/stats/diff_list.dart | 3 +- pub_stats_core/lib/src/model/diff.dart | 8 +-- pub_stats_core/lib/src/model/diff.g.dart | 9 ++- 4 files changed, 73 insertions(+), 7 deletions(-) create mode 100644 pub_stats/lib/extension/diff_extension.dart diff --git a/pub_stats/lib/extension/diff_extension.dart b/pub_stats/lib/extension/diff_extension.dart new file mode 100644 index 0000000..0c138a0 --- /dev/null +++ b/pub_stats/lib/extension/diff_extension.dart @@ -0,0 +1,60 @@ +import 'package:flutter/material.dart'; +import 'package:pub_stats/constant/app_theme.dart'; +import 'package:pub_stats/constant/constants.dart'; +import 'package:pub_stats_core/pub_stats_core.dart'; +import 'package:url_launcher/url_launcher_string.dart'; + +extension DiffExtension on Diff { + Widget get widget => switch (this) { + (final StringDiff diff) => Text(diff.text), + (final SetDiff diff) => Row( + children: [ + PackageListButton( + packages: diff.added.cast(), + qualifier: 'Added', + ), + const SizedBox(width: 8), + PackageListButton( + packages: diff.removed.cast(), + qualifier: 'Removed', + ), + ], + ), + }; +} + +class PackageListButton extends StatelessWidget { + final Set packages; + final String qualifier; + + const PackageListButton({ + super.key, + required this.packages, + required this.qualifier, + }); + + @override + Widget build(BuildContext context) { + return Material( + color: Colors.transparent, + shape: RoundedRectangleBorder(borderRadius: AppTheme.pillRadius), + clipBehavior: Clip.antiAlias, + child: PopupMenuButton( + tooltip: 'Show packages', + itemBuilder: (context) => packages + .map( + (package) => PopupMenuItem( + child: Text(package), + onTap: () => + launchUrlString(Constants.pubPackageBaseUrl + package), + ), + ) + .toList(), + child: Padding( + padding: const EdgeInsets.all(8), + child: Text('${packages.length} $qualifier'), + ), + ), + ); + } +} diff --git a/pub_stats/lib/view/widget/stats/diff_list.dart b/pub_stats/lib/view/widget/stats/diff_list.dart index d5e60a1..6fc53d1 100644 --- a/pub_stats/lib/view/widget/stats/diff_list.dart +++ b/pub_stats/lib/view/widget/stats/diff_list.dart @@ -3,6 +3,7 @@ import 'package:firebase_ui_database/firebase_ui_database.dart'; import 'package:flutter/material.dart'; import 'package:get_it/get_it.dart'; import 'package:pub_stats/controller/data_controller.dart'; +import 'package:pub_stats/extension/diff_extension.dart'; import 'package:pub_stats/format/formatting.dart'; import 'package:pub_stats_core/pub_stats_core.dart'; import 'package:recase/recase.dart'; @@ -51,7 +52,7 @@ class DiffList extends StatelessWidget { width: 100, child: Text(e.key.name.titleCase), ), - title: Text(e.value.text), + title: e.value.widget, trailing: Text(Formatting.shortDate(date)), ), ) diff --git a/pub_stats_core/lib/src/model/diff.dart b/pub_stats_core/lib/src/model/diff.dart index bff25ce..ae04fd6 100644 --- a/pub_stats_core/lib/src/model/diff.dart +++ b/pub_stats_core/lib/src/model/diff.dart @@ -2,7 +2,7 @@ import 'package:freezed_annotation/freezed_annotation.dart'; part 'diff.g.dart'; -abstract class Diff { +sealed class Diff { bool get different; String get text; @@ -31,15 +31,15 @@ class StringDiff extends Diff { @JsonSerializable(constructor: '_') class SetDiff extends Diff { - final Set added; - final Set removed; + final Set added; + final Set removed; SetDiff._({ this.added = const {}, this.removed = const {}, }); - SetDiff(Set before, Set after) + SetDiff(Set before, Set after) : added = after.difference(before), removed = before.difference(after); diff --git a/pub_stats_core/lib/src/model/diff.g.dart b/pub_stats_core/lib/src/model/diff.g.dart index 52e75d6..1716074 100644 --- a/pub_stats_core/lib/src/model/diff.g.dart +++ b/pub_stats_core/lib/src/model/diff.g.dart @@ -20,8 +20,13 @@ Map _$StringDiffToJson(StringDiff instance) => }; SetDiff _$SetDiffFromJson(Map json) => SetDiff._( - added: (json['added'] as List?)?.toSet() ?? const {}, - removed: (json['removed'] as List?)?.toSet() ?? const {}, + added: + (json['added'] as List?)?.map((e) => e as Object).toSet() ?? + const {}, + removed: (json['removed'] as List?) + ?.map((e) => e as Object) + .toSet() ?? + const {}, ); Map _$SetDiffToJson(SetDiff instance) => {