Skip to content

Commit

Permalink
URL handling for comparisons works
Browse files Browse the repository at this point in the history
  • Loading branch information
Rexios80 committed Nov 17, 2023
1 parent 818761c commit 9f9022e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 14 deletions.
27 changes: 23 additions & 4 deletions lib/controller/data_controller.dart
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,13 @@ class DataController {
globalStats: globalStats,
packageCounts: packageCounts,
);
await instance.fetchStats(pathPackage);
await instance.fetchStats(pathPackage, writeUrl: false);

final pathData = _url.getData();
final comparisons = pathData['compare']?.split(',') ?? [];
for (final package in comparisons) {
await instance.addToComparison(package, writeUrl: false);
}

if (_url.isDeveloperPackages()) {
await instance.fetchDeveloperPackageStats();
Expand Down Expand Up @@ -123,7 +129,7 @@ class DataController {
return stats;
}

Future<void> fetchStats(String package) async {
Future<void> fetchStats(String package, {bool writeUrl = true}) async {
if (package.isEmpty || loadedStats.value.package == package) {
// Don't load the same package twice
_logger.d('Already loaded $package');
Expand All @@ -137,10 +143,16 @@ class DataController {

developerPackageStats.clear();
loadedStats.value = stats;
_url.setPackage(package);

if (!writeUrl) return;

_url.setPackage(
package,
comparisons: comparisonStats.map((e) => e.package).toList(),
);
}

Future<void> addToComparison(String package) async {
Future<void> addToComparison(String package, {bool writeUrl = true}) async {
if (comparisonStats.any((e) => e.package == package)) {
// Don't load the same package twice
_logger.d('Already loaded $package');
Expand All @@ -151,6 +163,13 @@ class DataController {
if (stats == null) return;

comparisonStats.add(stats);

if (!writeUrl) return;

_url.setPackage(
loadedStats.value.package,
comparisons: comparisonStats.map((e) => e.package).toList(),
);
}

void removeFromComparison(String package) {
Expand Down
16 changes: 12 additions & 4 deletions lib/repo/url_repo.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,33 @@ import 'package:collection/collection.dart';
class UrlRepo {
final _url = UrlService.forPlatform();

void setPackage(String package) {
_url.setPath(package, 'packages/$package');
void setPackage(String package, {List<String> comparisons = const []}) {
var path = 'packages/$package';
if (comparisons.isNotEmpty) {
path += '?compare=${comparisons.join(',')}';
}
_url.setPath(package, path);
}

String getPackage() {
final split = _url.getPath().split('/');
final split = _url.getUri().path.split('/').skip(1);
if (split.firstOrNull == 'packages') {
return split.lastOrNull ?? '';
} else {
return '';
}
}

Map<String, String> getData() {
return _url.getUri().queryParameters;
}

void setDeveloperPackages() {
_url.setPath('Rexios\'s Packages', 'developer');
}

bool isDeveloperPackages() {
return _url.getPath() == 'developer';
return _url.getUri().path == '/developer';
}

void reset() {
Expand Down
2 changes: 1 addition & 1 deletion lib/service/url_service/url_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'package:pub_stats/service/url_service/url_service_stub.dart'

abstract class UrlService {
void setPath(String title, String path);
String getPath();
Uri getUri();

const UrlService();

Expand Down
4 changes: 2 additions & 2 deletions lib/service/url_service/url_service_stub.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class UrlServiceImpl extends UrlService {
void setPath(String title, String path) {}

@override
String getPath() {
return '';
Uri getUri() {
return Uri();
}
}
7 changes: 4 additions & 3 deletions lib/service/url_service/url_service_web.dart
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class UrlServiceImpl extends UrlService {
}

@override
String getPath() {
final path = html.window.location.hash;
return path.replaceFirst('#/', '');
Uri getUri() {
final uri = Uri.parse(html.window.location.toString());
// Convert fragmented url to a normal one so parsing is easier
return Uri.parse('${uri.scheme}://${uri.host}${uri.fragment}');
}
}

0 comments on commit 9f9022e

Please sign in to comment.