Skip to content

Commit

Permalink
feat: add AsyncResponse
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-tychon committed Jul 29, 2024
1 parent 3f0b569 commit 7f8e124
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
import 'package:ice/app/features/feed/providers/post_reply/reply_data_notifier.dart';
import 'package:ice/app/services/async_response/async_response.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'send_reply_request_notifier.g.dart';

@riverpod
class SendReplyRequestNotifier extends _$SendReplyRequestNotifier {
@override
Future<bool?> build() async {
return null;
AsyncResponse<void> build() {
return AsyncResponse.initial();
}

Future<void> sendReply() async {
state = AsyncValue.loading();
state = AsyncResponse.loading();

// send reply
await Future<void>.delayed(Duration(seconds: 1));
ref.read(replyDataNotifierProvider.notifier).clear();

state = AsyncValue.data(true);
state = AsyncResponse.success(null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:ice/app/features/feed/views/pages/post_details_page/components/p
import 'package:ice/app/features/feed/views/pages/post_details_page/components/reply_input_field/reply_input_field.dart';
import 'package:ice/app/features/feed/views/pages/post_details_page/components/reply_sent_notification/reply_sent_notification.dart';
import 'package:ice/app/router/components/navigation_app_bar/navigation_app_bar.dart';
import 'package:ice/app/services/async_response/async_response.dart';
import 'package:ice/generated/assets.gen.dart';

class PostDetailsPage extends HookConsumerWidget {
Expand All @@ -31,7 +32,8 @@ class PostDetailsPage extends HookConsumerWidget {
return PostNotFound();
}

final showReplySentNotification = _listenReplySentNotification(ref);
final showReplySentNotification = useState(false);
_listenReplySentNotification(ref, showReplySentNotification);

return Scaffold(
appBar: NavigationAppBar.screen(
Expand Down Expand Up @@ -87,22 +89,17 @@ class PostDetailsPage extends HookConsumerWidget {
);
}

ValueNotifier<bool> _listenReplySentNotification(WidgetRef ref) {
final showReplySentNotification = useState(false);

ref.listen(
void _listenReplySentNotification(
WidgetRef ref,
ValueNotifier<bool> showReplySentNotification,
) {
ref.listenAsyncResponse(
sendReplyRequestNotifierProvider,
(previous, next) async {
if (previous == null || !previous.isLoading || next.hasError || next.valueOrNull != true) {
return;
}

onSuccess: (response) async {
showReplySentNotification.value = true;
await Future<void>.delayed(Duration(seconds: 2));
showReplySentNotification.value = false;
},
);

return showReplySentNotification;
}
}
42 changes: 42 additions & 0 deletions lib/app/services/async_response/async_response.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import 'package:freezed_annotation/freezed_annotation.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';

part 'async_response.freezed.dart';

@freezed
class AsyncResponse<TSuccess> with _$AsyncResponse<TSuccess> {
const AsyncResponse._();

const factory AsyncResponse.initial() = _AsyncResponseInitial;

const factory AsyncResponse.loading() = _AsyncResponseLoading;

const factory AsyncResponse.success(TSuccess? payload) = _AsyncResponseSuccess;

const factory AsyncResponse.failure(Exception e) = _AsyncResponseFailure;

bool get isLoading => this is _AsyncResponseLoading;

bool get isFailure => this is _AsyncResponseFailure;

bool get isSuccess => this is _AsyncResponseSuccess;
}

extension AsyncResponseListener on WidgetRef {
void listenAsyncResponse<TSuccess>(
ProviderListenable<AsyncResponse<TSuccess>> provider, {
void Function()? onInitial,
void Function()? onLoading,
void Function(TSuccess? response)? onSuccess,
void Function(Exception e)? onFailure,
}) {
listen(provider, (previous, next) {
next.whenOrNull(
initial: onInitial,
loading: onLoading,
success: onSuccess,
failure: onFailure,
);
});
}
}

0 comments on commit 7f8e124

Please sign in to comment.