-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
ice-tychon
committed
Jul 29, 2024
1 parent
3f0b569
commit 7f8e124
Showing
3 changed files
with
56 additions
and
16 deletions.
There are no files selected for viewing
9 changes: 5 additions & 4 deletions
9
lib/app/features/feed/providers/post_reply/send_reply_request_notifier.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); | ||
} | ||
} |