-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove post cubit and flutter bloc from bond
- Loading branch information
1 parent
dd6d5a8
commit 87225d7
Showing
8 changed files
with
82 additions
and
102 deletions.
There are no files selected for viewing
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
This file was deleted.
Oops, something went wrong.
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
55 changes: 55 additions & 0 deletions
55
lib/features/post/presentations/providers/posts_provider.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 |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import 'package:bond/features/post/data/api.dart'; | ||
import 'package:bond/features/post/data/models/post.dart'; | ||
import 'package:bond_core/bond_core.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
import 'list_state.dart'; | ||
|
||
final postsProvider = | ||
AsyncNotifierProvider.autoDispose<PostController, ListState<Post>>( | ||
() { | ||
final scrollController = ScrollController(); | ||
|
||
return PostController(sl(), scrollController); | ||
}, | ||
); | ||
|
||
class PostController extends AutoDisposeAsyncNotifier<ListState<Post>> { | ||
PostController(this._api, this.scrollController) : super() { | ||
scrollController.addListener(_scrollControllerListener); | ||
ref.onDispose( | ||
() { | ||
scrollController.dispose(); | ||
scrollController.removeListener(_scrollControllerListener); | ||
}, | ||
); | ||
} | ||
|
||
final PostsApi _api; | ||
final ScrollController scrollController; | ||
|
||
@override | ||
Future<ListState<Post>> build() async { | ||
final response = await _api.posts(); | ||
return ListState.data(response); | ||
} | ||
|
||
void loadMore() async { | ||
state = AsyncData(state.requireValue.loading()); | ||
state = await AsyncValue.guard(() async { | ||
final response = await _api.posts(); | ||
return state.requireValue.success(response); | ||
}); | ||
} | ||
|
||
void _scrollControllerListener() { | ||
final maxScroll = scrollController.position.maxScrollExtent; | ||
final currentScroll = scrollController.position.pixels; | ||
const delta = 200.0; | ||
if (maxScroll - currentScroll <= delta && | ||
state.value?.status != ListStatus.loading) { | ||
loadMore(); | ||
} | ||
} | ||
} |
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