From 9f7050b6ec4bf5035f0721842c482a5461bc5fd5 Mon Sep 17 00:00:00 2001 From: Salah Nahed Date: Sun, 31 Mar 2024 01:30:22 +0200 Subject: [PATCH] wip --- ...otification_center_remote_data_source.dart | 12 ++-- lib/core/theme/app_light_theme_data.dart | 1 + .../bond_pop_menu/bond_pop_menu_button.dart | 2 +- lib/features/post/data/api.dart | 11 ++++ lib/features/post/data/post_faker.dart | 63 +++++++++++++++++++ .../post/presentations/posts_page.dart | 4 +- .../providers/post_provider.dart | 2 +- .../providers/posts_provider.dart | 29 +++++---- .../post/presentations/views/post_item.dart | 2 +- pubspec.lock | 2 +- pubspec.yaml | 1 + 11 files changed, 103 insertions(+), 26 deletions(-) create mode 100644 lib/features/post/data/post_faker.dart diff --git a/lib/core/notifications/notification_center_remote_data_source.dart b/lib/core/notifications/notification_center_remote_data_source.dart index 3a13184d..da373a43 100644 --- a/lib/core/notifications/notification_center_remote_data_source.dart +++ b/lib/core/notifications/notification_center_remote_data_source.dart @@ -1,6 +1,5 @@ import 'package:bond_network/bond_network.dart'; import 'package:bond_notifications/bond_notifications.dart'; -import 'package:dio/dio.dart'; class NotificationCenterRemoteDataSource extends NotificationCenterDataSource { final ApiClient _client; @@ -10,11 +9,12 @@ class NotificationCenterRemoteDataSource extends NotificationCenterDataSource { @override Future> loadNotifications( {String? nextUrl}) async { - final Response response = await _client.get( - nextUrl ?? NotificationsApis.notifications, - headers: Api.headers(), - ); - return mapListResponse(response); + // final Response response = await _client.get( + // nextUrl ?? NotificationsApis.notifications, + // headers: Api.headers(), + // ); + // return mapListResponse(response); + return ListResponse.fromJson(const {"data": []}); } @override diff --git a/lib/core/theme/app_light_theme_data.dart b/lib/core/theme/app_light_theme_data.dart index f35cc4dd..c5589289 100644 --- a/lib/core/theme/app_light_theme_data.dart +++ b/lib/core/theme/app_light_theme_data.dart @@ -68,6 +68,7 @@ ThemeData appLightThemeData() { textStyle: appTextTheme.titleSmall?.copyWith( color: Colors.black, ), + color: Colors.white, ), /// Text theme diff --git a/lib/core/widgets/bond_pop_menu/bond_pop_menu_button.dart b/lib/core/widgets/bond_pop_menu/bond_pop_menu_button.dart index 795c30a0..0efb4faf 100644 --- a/lib/core/widgets/bond_pop_menu/bond_pop_menu_button.dart +++ b/lib/core/widgets/bond_pop_menu/bond_pop_menu_button.dart @@ -74,7 +74,7 @@ class BondPopMenuButton extends ConsumerWidget { case Menu.logout: break; case Menu.notifications: - ref.context.go('/notifications'); + ref.context.push('/notifications'); break; } } diff --git a/lib/features/post/data/api.dart b/lib/features/post/data/api.dart index 4f0630e5..83aab22d 100644 --- a/lib/features/post/data/api.dart +++ b/lib/features/post/data/api.dart @@ -1,3 +1,4 @@ +import 'package:bond/features/post/data/post_faker.dart'; import 'package:bond_network/bond_network.dart'; import 'models/post.dart'; @@ -7,6 +8,16 @@ class PostsApi { PostsApi(this._bondFire); + Future> fakePosts() => Future.delayed( + const Duration(seconds: 1), + () => ListResponse.fromJson(PostFaker.posts()), + ); + + Future> fakePost() => Future.delayed( + const Duration(seconds: 1), + () => SingleResponse.fromJson({'data': PostFaker.post()}), + ); + Future> posts() => _bondFire .get>('/posts') .cache() diff --git a/lib/features/post/data/post_faker.dart b/lib/features/post/data/post_faker.dart new file mode 100644 index 00000000..96e78026 --- /dev/null +++ b/lib/features/post/data/post_faker.dart @@ -0,0 +1,63 @@ +import 'dart:math'; + +import 'package:faker/faker.dart' show Faker, Internet; + +class PostFaker { + static Map posts() { + return { + 'data': List.generate(10, (index) => post()), + }; + } + + static Map post() { + final faker = Faker(); + + // Constructing thumbnail URL with random parameters + const imageUrl = 'https://fakeimg.pl/600x400'; + + return { + 'uuid': faker.guid.guid(), + 'created_at': faker.date.dateTime().toIso8601String(), + 'updated_at': faker.date.dateTime().toIso8601String(), + 'color': faker.internet.color(), + 'description': faker.lorem.sentence(), + 'urls': { + 'thumb': imageUrl, + 'full': imageUrl, + 'raw': imageUrl, + 'small': imageUrl, + 'regular': imageUrl, + 'small_s3': imageUrl, + }, + 'likes': faker.randomGenerator.integer(1000), + 'author': { + 'uuid': faker.guid.guid(), + 'name': faker.person.name(), + 'username': faker.person.name(), + 'first_name': faker.person.firstName(), + 'last_name': faker.person.lastName(), + 'avatar': faker.internet.httpsUrl(), + 'updated_at': faker.date.dateTime().toIso8601String(), + 'twitter_username': faker.person.name(), + 'bio': faker.lorem.sentence(), + 'total_likes': faker.randomGenerator.integer(1000), + 'total_photos': faker.randomGenerator.integer(1000), + 'instagram_username': faker.person.name(), + }, + 'views': faker.randomGenerator.integer(1000), + 'downloads': faker.randomGenerator.integer(1000), + }; + } +} + +extension XInternet on Internet { + String color() { + final random = Random(); + const hexDigits = '0123456789ABCDEF'; + String color = ''; + for (int i = 0; i < 6; i++) { + color += hexDigits[random.nextInt(16)]; + } + return color; + } +} diff --git a/lib/features/post/presentations/posts_page.dart b/lib/features/post/presentations/posts_page.dart index da10c578..8506ba61 100644 --- a/lib/features/post/presentations/posts_page.dart +++ b/lib/features/post/presentations/posts_page.dart @@ -36,9 +36,7 @@ class PostsPage extends ConsumerWidget { ], ), ), - error: (error, _) => Center( - child: Text(error.toString()), - ), + error: (error, stackTrace) => Center(child: Text(error.toString())), ), ); } diff --git a/lib/features/post/presentations/providers/post_provider.dart b/lib/features/post/presentations/providers/post_provider.dart index 8b8b7b72..5790bd17 100644 --- a/lib/features/post/presentations/providers/post_provider.dart +++ b/lib/features/post/presentations/providers/post_provider.dart @@ -6,6 +6,6 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; final postFeatureProvider = FutureProvider.autoDispose.family((ref, id) async { final api = sl(); - final response = await api.post(id); + final response = await api.fakePost(); return response.data; }); diff --git a/lib/features/post/presentations/providers/posts_provider.dart b/lib/features/post/presentations/providers/posts_provider.dart index 0c3cefb6..5245ca31 100644 --- a/lib/features/post/presentations/providers/posts_provider.dart +++ b/lib/features/post/presentations/providers/posts_provider.dart @@ -16,29 +16,32 @@ final postsProvider = ); class PostController extends AutoDisposeAsyncNotifier> { - PostController(this._api, this.scrollController) : super() { - scrollController.addListener(_scrollControllerListener); - ref.onDispose( - () { - scrollController.dispose(); - scrollController.removeListener(_scrollControllerListener); - }, - ); - } + PostController(this._api, this.scrollController) : super(); final PostsApi _api; final ScrollController scrollController; @override Future> build() async { - final response = await _api.posts(); + _initialize(); + final response = await _api.fakePosts(); return ListState.data(response); } - void loadMore() async { + void _initialize() { + scrollController.addListener(_scrollControllerListener); + ref.onDispose( + () { + scrollController.dispose(); + scrollController.removeListener(_scrollControllerListener); + }, + ); + } + + void _loadMore() async { state = AsyncData(state.requireValue.loading()); state = await AsyncValue.guard(() async { - final response = await _api.posts(); + final response = await _api.fakePosts(); return state.requireValue.success(response); }); } @@ -49,7 +52,7 @@ class PostController extends AutoDisposeAsyncNotifier> { const delta = 200.0; if (maxScroll - currentScroll <= delta && state.value?.status != ListStatus.loading) { - loadMore(); + _loadMore(); } } } diff --git a/lib/features/post/presentations/views/post_item.dart b/lib/features/post/presentations/views/post_item.dart index edfacfe7..d400b8e6 100644 --- a/lib/features/post/presentations/views/post_item.dart +++ b/lib/features/post/presentations/views/post_item.dart @@ -11,7 +11,7 @@ class PostItem extends StatelessWidget { Widget build(BuildContext context) { return GridTile( child: GestureDetector( - onTap: () => context.go('/post/${post.uuid}'), + onTap: () => context.push('/post/${post.uuid}'), child: Hero( tag: post.uuid, child: ClipRRect( diff --git a/pubspec.lock b/pubspec.lock index 64620557..ec2c322f 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -322,7 +322,7 @@ packages: source: hosted version: "1.3.1" faker: - dependency: transitive + dependency: "direct main" description: name: faker sha256: "746e59f91d8b06a389e74cf76e909a05ed69c12691768e2f93557fdf29200fd0" diff --git a/pubspec.yaml b/pubspec.yaml index f03acfcd..205547b5 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -52,6 +52,7 @@ dependencies: timeago: ^3.3.0 args: ^2.3.1 device_info_plus: ^10.0.1 + faker: ^2.1.0 # platforms packages universal_platform: ^1.0.0+1