Skip to content
This repository has been archived by the owner on Sep 9, 2024. It is now read-only.

Commit

Permalink
refactor: bookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
Craftplacer committed Oct 29, 2023
1 parent 51381dc commit 3dec8e5
Show file tree
Hide file tree
Showing 6 changed files with 351 additions and 76 deletions.
80 changes: 80 additions & 0 deletions src/kaiteki/lib/fediverse/services/bookmarks.dart
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();
}
}
182 changes: 182 additions & 0 deletions src/kaiteki/lib/fediverse/services/bookmarks.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/kaiteki/lib/fediverse/services/timeline.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion src/kaiteki/lib/ui/main/main_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import "package:flutter/services.dart";
import "package:go_router/go_router.dart";
import "package:kaiteki/constants.dart";
import "package:kaiteki/di.dart";
import "package:kaiteki/fediverse/services/bookmarks.dart";
import "package:kaiteki/fediverse/services/notifications.dart";
import "package:kaiteki/fediverse/services/timeline.dart";
import "package:kaiteki/preferences/app_experiment.dart";
Expand Down Expand Up @@ -294,9 +295,11 @@ class MainScreenState extends ConsumerState<MainScreen> {

ref.invalidate(timelineServiceProvider(accountKey, timeline));
break;
case TabKind.bookmarks:
ref.invalidate(bookmarksServiceProvider(accountKey));
break;

case TabKind.chats:
case TabKind.bookmarks:
case TabKind.explore:
case TabKind.directMessages:
break;
Expand Down
Loading

0 comments on commit 3dec8e5

Please sign in to comment.