Skip to content

Commit

Permalink
feat: Add a REST API Client package (#521)
Browse files Browse the repository at this point in the history
* feat: Add a REST API Client package

* test: add rest_api_client_test

* chore: update slang file

* refactor: rest_api_client to pub_dev_api_client

* chore: rename provider

* chore: update dependencies and improve pub.dev package providers

* refactor: rename package details provider and implement new provider for pub.dev package details
  • Loading branch information
k-nkmr authored Dec 17, 2024
1 parent b6362bd commit bcb00b1
Show file tree
Hide file tree
Showing 46 changed files with 3,267 additions and 176 deletions.
15 changes: 0 additions & 15 deletions packages/flutter_app/lib/features/post/view/post_page.dart

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:pub_dev_api_client/pub_dev_api_client.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../package_adaptor/pub_dev_api_client_provider.dart';

part 'pub_dev_package_details_provider.g.dart';

/// Provider for pub.dev package details by package name.
@riverpod
Future<GetPackageDetailsResponseBody> pubDevPackageDetails(
Ref ref, {
required String packageName,
}) async {
final client = ref.watch(pubDevApiClientProvider);
final response = await client.getPackageDetails(packageName: packageName);
return response;
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import 'package:pub_dev_api_client/pub_dev_api_client.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

import '../../../package_adaptor/pub_dev_api_client_provider.dart';

part 'pub_dev_packages_page_state_provider.g.dart';

/// Provider for the search word of pub.dev packages.
@Riverpod(keepAlive: true)
class PubDevPackageSearchWordState extends _$PubDevPackageSearchWordState {
@override
String build() => '';

/// Update the search word.
// ignore: use_setters_to_change_properties
void update(String word) {
state = word;
}

/// Clear the search word.
void clear() {
state = '';
}
}

@Riverpod(keepAlive: true)
class PubDevPackagesPageState extends _$PubDevPackagesPageState {
@override
Future<GetSearchedPackagesResponseBody> build() async {
final searchWord = ref.watch(pubDevPackageSearchWordStateProvider);
final client = ref.watch(pubDevApiClientProvider);
final response = await client.getSearchedPackages(searchWord: searchWord);
return response;
}

Future<void> loadNext(int nextPage) async {
state = const AsyncLoading<GetSearchedPackagesResponseBody>()
.copyWithPrevious(state);

final searchWord = ref.read(pubDevPackageSearchWordStateProvider);
final client = ref.read(pubDevApiClientProvider);
final response = await client.getSearchedPackages(
searchWord: searchWord,
page: nextPage,
);
final currentPackages = state.requireValue.packages;
final newPackages = [...currentPackages, ...response.packages];

state = AsyncData(
GetSearchedPackagesResponseBody(
packages: newPackages,
nextPageUrl: response.nextPageUrl,
),
);
}
}

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export 'pub_dev_package_details_provider.dart';
export 'pub_dev_packages_page_state_provider.dart';
Loading

0 comments on commit bcb00b1

Please sign in to comment.