This repository has been archived by the owner on Sep 9, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 31
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
1 parent
51381dc
commit 3dec8e5
Showing
6 changed files
with
351 additions
and
76 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import "dart:async"; | ||
|
||
import "package:kaiteki/di.dart"; | ||
import "package:kaiteki/model/auth/account_key.dart"; | ||
import "package:kaiteki/model/pagination_state.dart"; | ||
import "package:kaiteki_core/kaiteki_core.dart"; | ||
import "package:riverpod_annotation/riverpod_annotation.dart"; | ||
|
||
part "bookmarks.g.dart"; | ||
|
||
@Riverpod(keepAlive: true, dependencies: [adapter, account]) | ||
class BookmarksService extends _$BookmarksService { | ||
late BookmarkSupport _adapter; | ||
|
||
Future<void> loadMore() async { | ||
final previousState = state.valueOrNull; | ||
|
||
if (previousState == null) return; | ||
|
||
state = const AsyncLoading(); | ||
state = await AsyncValue.guard( | ||
() async { | ||
if (previousState.items.isEmpty) { | ||
return PaginationState( | ||
previousState.items, | ||
canPaginateFurther: false, | ||
); | ||
} | ||
|
||
final query = TimelineQuery(untilId: previousState.items.last.id); | ||
final page = await _fetch(query); | ||
return PaginationState( | ||
[...previousState.items, ...page], | ||
canPaginateFurther: page.isNotEmpty, | ||
); | ||
}, | ||
); | ||
} | ||
|
||
Future<Iterable<Post>> _fetch([TimelineQuery<String>? query]) { | ||
return _adapter.getBookmarks( | ||
minId: query?.untilId, | ||
maxId: query?.sinceId, | ||
); | ||
} | ||
|
||
@override | ||
FutureOr<PaginationState<Post>> build(AccountKey key) async { | ||
_adapter = ref.watch(accountProvider(key))!.adapter as BookmarkSupport; | ||
final posts = await _fetch(); | ||
return PaginationState( | ||
posts.toList(), | ||
canPaginateFurther: posts.isNotEmpty, | ||
); | ||
} | ||
|
||
Future<void> remove(String postId) async { | ||
await _adapter.unbookmarkPost(postId); | ||
|
||
final previousState = state.valueOrNull; | ||
if (previousState == null) return; | ||
|
||
state = AsyncValue.data( | ||
PaginationState( | ||
previousState.items.toList() | ||
..removeWhere((element) => element.id == postId), | ||
canPaginateFurther: previousState.canPaginateFurther, | ||
), | ||
); | ||
} | ||
|
||
Future<void> add(String postId) async { | ||
await _adapter.bookmarkPost(postId); | ||
|
||
final previousState = state.valueOrNull; | ||
if (previousState == null) return; | ||
|
||
ref.invalidateSelf(); | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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
Oops, something went wrong.