Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add rated category snaps to the Games tab #1740

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
12 changes: 10 additions & 2 deletions packages/app_center/lib/games/games_page.dart
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,16 @@ class GamesPage extends ConsumerWidget {
const SizedBox(height: kPagePadding),
],
),
const CategorySnapList(
category: SnapCategoryEnum.games,
const RatedCategorySnapList(
categories: [
SnapCategoryEnum.games,
SnapCategoryEnum.kdeGames,
SnapCategoryEnum.gnomeGames,
SnapCategoryEnum.gameLaunchers,
SnapCategoryEnum.gameEmulators,
SnapCategoryEnum.gameContentCreation,
SnapCategoryEnum.gameDev,
],
),
SliverList.list(
children: [
Expand Down
2 changes: 1 addition & 1 deletion packages/app_center/lib/games/games_page_featured.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class FeaturedCarousel extends ConsumerStatefulWidget {

class _FeaturedCarouselState extends ConsumerState<FeaturedCarousel> {
late YaruCarouselController controller;
late Iterable<Snap> snaps;
Iterable<Snap> snaps = [];

@override
Widget build(BuildContext context) {
Expand Down
35 changes: 35 additions & 0 deletions packages/app_center/lib/ratings/rated_category_model.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import 'package:app_center/ratings/ratings_service.dart';
import 'package:app_center/snapd/snapd.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';
import 'package:snapd/snapd.dart';
import 'package:ubuntu_service/ubuntu_service.dart';

part 'rated_category_model.g.dart';

@riverpod
class RatedCategoryModel extends _$RatedCategoryModel {
late final _ratings = getService<RatingsService>();
late final _snapd = getService<SnapdService>();

@override
Future<List<Snap>> build(
List<SnapCategoryEnum> categories,
int numberOfSnaps,
) async {
final snaps = <Snap>[];

for (final category in categories) {
final chart = await _ratings.getChart(category);
var i = 0;
while (snaps.length < numberOfSnaps && i < chart.length) {
final snap = await _snapd.findById(chart[i].rating.snapId);
if (snap != null && snap.screenshotUrls.isNotEmpty) {
snaps.add(snap);
}
i++;
}
}

return snaps;
}
}
199 changes: 199 additions & 0 deletions packages/app_center/lib/ratings/rated_category_model.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions packages/app_center/lib/ratings/ratings_service.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import 'dart:convert';
import 'dart:io';

import 'package:app_center/snapd/snap_category_enum.dart';
import 'package:app_center_ratings_client/app_center_ratings_client.dart';
import 'package:crypto/crypto.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:glib/glib.dart';
import 'package:jwt_decode/jwt_decode.dart';
Expand Down Expand Up @@ -32,6 +34,11 @@ class RatingsService {
return client.getRating(snapId, _jwt!);
}

Future<List<ChartData>> getChart(SnapCategoryEnum category) async {
await _ensureValidToken();
return client.getChart(Timeframe.unspecified, _jwt!, category.index);
}

Future<void> vote(Vote vote) async {
await _ensureValidToken();
await client.vote(vote.snapId, vote.snapRevision, vote.voteUp, _jwt!);
Expand Down
15 changes: 15 additions & 0 deletions packages/app_center/lib/snapd/snapd_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,19 @@ import 'package:snapd/snapd.dart';
class SnapdService extends SnapdClient with SnapdCache, SnapdWatcher {
Future<void> waitChange(String changeId) =>
watchChange(changeId).firstWhere((change) => change.ready);

Future<Snap?> findById(String snapId) async {
final queryParams = {
'series': '16',
'remote': 'true',
'snap-id': snapId,
};
final result =
await getAssertions(assertion: 'snap-declaration', params: queryParams);
final declaration = SnapDeclaration.fromJson(result);
final findResult = await find(name: declaration.snapName);
return findResult
.where((element) => element.id == declaration.snapId)
.firstOrNull;
}
}
2 changes: 1 addition & 1 deletion packages/app_center/lib/src/l10n/app_en.arb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
"developmentPageLabel": "Development",
"gamesPageLabel": "Games",
"gamesPageTitle": "What's Hot",
"gamesPageTop": "Top Games",
"gamesPageTop": "Top Rated",
"gamesPageFeatured": "Featured Titles",
"gamesPageBundles": "App Bundles",
"unknownPublisher": "Unknown publisher",
Expand Down
Loading
Loading