Skip to content

Commit 689c6f2

Browse files
authored
Adding doc snippets for most features (#24)
* Adding doc snippets for most features * fix some warnings * Formatting
1 parent af43bc6 commit 689c6f2

23 files changed

+837
-33
lines changed

docs_code_snippets/04_01_feeds.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// ignore_for_file: unused_local_variable, avoid_redundant_argument_values
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
22

33
import 'package:stream_feeds/stream_feeds.dart';
44

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
8+
Future<void> feedVisibilityLevels() async {
9+
// More options
10+
const query = FeedQuery(
11+
fid: FeedId(group: 'user', id: 'jack'),
12+
data: FeedInputData(
13+
visibility: FeedVisibility.public,
14+
),
15+
);
16+
final feed = client.feedFromQuery(query);
17+
await feed.getOrCreate();
18+
}
19+
20+
Future<void> activityVisibilityLevels() async {
21+
// Premium users can see full activity, others a preview
22+
final privateActivity = await feed.addActivity(
23+
request: const FeedAddActivityRequest(
24+
text: 'Premium content',
25+
type: 'post',
26+
visibility: AddActivityRequestVisibility.tag,
27+
visibilityTag: 'premium',
28+
),
29+
);
30+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late StreamFeedsClient saraClient;
7+
late StreamFeedsClient adamClient;
8+
late Feed feed;
9+
late Feed timeline;
10+
11+
Future<void> followsAndUnfollows() async {
12+
// Follow a user
13+
final timeline = client.feed(group: 'timeline', id: 'john');
14+
await timeline.follow(targetFid: const FeedId(group: 'user', id: 'tom'));
15+
// Follow a stock
16+
await timeline.follow(targetFid: const FeedId(group: 'stock', id: 'apple'));
17+
// Follow with more fields
18+
await timeline.follow(
19+
targetFid: const FeedId(group: 'stock', id: 'apple'),
20+
custom: {'reason': 'investment'},
21+
);
22+
}
23+
24+
Future<void> queryFollows() async {
25+
// Do I follow a list of feeds
26+
// My feed is timeline:john
27+
const followQuery = FollowsQuery(
28+
filter: Filter.and([
29+
Filter.equal(FollowsFilterField.sourceFeed, 'timeline:john'),
30+
Filter.in_(FollowsFilterField.targetFeed, ['user:sara', 'user:adam']),
31+
]),
32+
);
33+
final followList = client.followList(followQuery);
34+
final page1 = await followList.get();
35+
final page2 = await followList.queryMoreFollows();
36+
final page1And2 = followList.state.follows;
37+
// Paginating through followers for a feed
38+
// My feed is timeline:john
39+
const followerQuery = FollowsQuery(
40+
filter: Filter.equal(FollowsFilterField.targetFeed, 'timeline:john'),
41+
);
42+
final followerList = client.followList(followerQuery);
43+
final followerPage1 = await followerList.get();
44+
}
45+
46+
Future<void> followRequests() async {
47+
// Sara needs to configure the feed with visibility = followers for enabling follow requests
48+
const saraFeedQuery = FeedQuery(
49+
fid: FeedId(group: 'user', id: 'sara'),
50+
data: FeedInputData(visibility: FeedVisibility.followers),
51+
);
52+
final saraFeed = saraClient.feedFromQuery(saraFeedQuery);
53+
await saraFeed.getOrCreate();
54+
55+
// Adam requesting to follow the feed
56+
final adamTimeline = adamClient.feed(group: 'timeline', id: 'adam');
57+
await adamTimeline.getOrCreate();
58+
final followRequest =
59+
await adamTimeline.follow(targetFid: saraFeed.fid); // user:sara
60+
print(followRequest.getOrNull()?.status); // .pending
61+
// Sara accepting
62+
await saraFeed.acceptFollow(
63+
sourceFid: adamTimeline.fid, // timeline:adam
64+
role: 'feed_member', // optional
65+
);
66+
// or rejecting the request
67+
await saraFeed.rejectFollow(sourceFid: adamTimeline.fid); // timeline:adam
68+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
late Feed notificationFeed;
8+
9+
Future<void> notificationFeedExample() async {
10+
final notificationFeed = client.feed(group: 'notification', id: 'john');
11+
await notificationFeed.getOrCreate();
12+
}
13+
14+
Future<void> markNotificationsAsRead() async {
15+
await notificationFeed.markActivity(
16+
request: const MarkActivityRequest(
17+
// Mark all notifications as read...
18+
markAllRead: true,
19+
// ...or only selected ones
20+
markRead: [
21+
// group names to mark as read
22+
],
23+
),
24+
);
25+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
8+
Future<void> whenToUseFeedViews() async {
9+
const query = FeedQuery(
10+
fid: FeedId(group: 'user', id: 'john'),
11+
view: '<id of a feed view>', // Override the default view id
12+
);
13+
final feed = client.feedFromQuery(query);
14+
await feed.getOrCreate();
15+
}
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
late Feed ericFeed;
8+
late Feed janeFeed;
9+
late Feed notificationFeed;
10+
late Activity janeActivity;
11+
late Activity saraComment;
12+
13+
Future<void> creatingNotificationActivities() async {
14+
// Eric follows Jane
15+
await ericFeed.follow(
16+
targetFid: janeFeed.fid,
17+
createNotificationActivity:
18+
true, // When true Jane's notification feed will be updated with follow activity
19+
);
20+
// Eric comments on Jane's activity
21+
await ericFeed.addComment(
22+
request: ActivityAddCommentRequest(
23+
comment: 'Agree!',
24+
activityId: janeActivity.activityId,
25+
createNotificationActivity:
26+
true, // When true Jane's notification feed will be updated with comment activity
27+
),
28+
);
29+
// Eric reacts to Jane's activity
30+
await ericFeed.addReaction(
31+
activityId: janeActivity.activityId,
32+
request: const AddReactionRequest(
33+
type: 'like',
34+
createNotificationActivity:
35+
true, // When true Jane's notification feed will be updated with reaction activity
36+
),
37+
);
38+
// Eric reacts to a comment posted to Jane's activity by Sara
39+
await ericFeed.addCommentReaction(
40+
commentId: saraComment.activityId,
41+
request: const AddCommentReactionRequest(
42+
type: 'like',
43+
createNotificationActivity:
44+
true, // When true Sara's notification feed will be updated with comment reaction activity
45+
),
46+
);
47+
}
48+
49+
Future<void> readingNotificationActivities() async {
50+
final notificationFeed = client.feed(group: 'notification', id: 'jane');
51+
final notifications = await notificationFeed.getOrCreate();
52+
}
53+
54+
Future<void> markNotificationsAsSeen() async {
55+
await notificationFeed.markActivity(
56+
request: const MarkActivityRequest(
57+
// Mark all notifications as seen...
58+
markAllSeen: true,
59+
// ...or only selected ones
60+
markSeen: [
61+
/* group names to mark as seen */
62+
],
63+
),
64+
);
65+
}
66+
67+
Future<void> markNotificationsAsRead() async {
68+
await notificationFeed.markActivity(
69+
request: const MarkActivityRequest(
70+
// Mark all notifications as seen...
71+
markAllRead: true,
72+
// ...or only selected ones
73+
markRead: [
74+
/* group names to mark as seen */
75+
],
76+
),
77+
);
78+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
late ActivityData activity;
8+
9+
Future<void> readingTopics() async {
10+
await feed.addActivity(
11+
request: const FeedAddActivityRequest(
12+
text: 'check out my 10 fitness tips for reducing lower back pain',
13+
type: 'post',
14+
),
15+
);
16+
17+
// Wait for feeds.activity.updated event
18+
print(activity.interestTags); // ["fitness", "health", "tips"]
19+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
8+
Future<void> overview() async {
9+
// Add a reaction to an activity
10+
final reaction = await feed.addReaction(
11+
activityId: 'activity_123',
12+
request: const AddReactionRequest(custom: {'emoji': '❤️'}, type: 'like'),
13+
);
14+
// Remove a reaction
15+
await feed.deleteReaction(activityId: 'activity_123', type: 'like');
16+
}
17+
18+
Future<void> overviewRead() async {
19+
final feedData = await feed.getOrCreate();
20+
// Last 15 reactions on the first activity
21+
print(feed.state.activities[0].latestReactions);
22+
// Count of reactions by type
23+
print(feed.state.activities[0].reactionGroups);
24+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
// ignore_for_file: file_names, unused_local_variable, avoid_redundant_argument_values
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
8+
Future<void> addingBookmarks() async {
9+
// Adding a bookmark to a new folder
10+
final bookmark = await feed.addBookmark(activityId: 'activity_123');
11+
// Adding to an existing folder
12+
final bookmarkWithFolder = await feed.addBookmark(
13+
activityId: 'activity_123',
14+
request: const AddBookmarkRequest(folderId: 'folder_456'),
15+
);
16+
// Update a bookmark (without a folder initially) - add custom data and move it to a new folder
17+
final updatedBookmark = await feed.updateBookmark(
18+
activityId: 'activity_123',
19+
request: const UpdateBookmarkRequest(
20+
custom: {'color': 'blue'},
21+
newFolder: AddFolderRequest(
22+
custom: {'icon': '📁'},
23+
name: 'New folder name',
24+
),
25+
),
26+
);
27+
// Update a bookmark - move it from one existing folder to another existing folder
28+
final movedBookmark = await feed.updateBookmark(
29+
activityId: 'activity_123',
30+
request: const UpdateBookmarkRequest(
31+
folderId: 'folder_456',
32+
newFolderId: 'folder_789',
33+
),
34+
);
35+
}
36+
37+
Future<void> removingBookmarks() async {
38+
// Removing a bookmark
39+
await feed.deleteBookmark(activityId: 'activity_123', folderId: 'folder_456');
40+
// When you read a feed we include the bookmark
41+
final feedData = await feed.getOrCreate();
42+
print(feed.state.activities[0].ownBookmarks);
43+
}
44+
45+
Future<void> queryingBookmarks() async {
46+
// Query bookmarks
47+
const query = BookmarksQuery(limit: 5);
48+
final bookmarkList = client.bookmarkList(query);
49+
final page1 = await bookmarkList.get();
50+
// Get next page
51+
final page2 = await bookmarkList.queryMoreBookmarks(limit: 3);
52+
// Query by activity ID
53+
final activityBookmarkList = client.bookmarkList(
54+
const BookmarksQuery(
55+
filter: Filter.equal(BookmarksFilterField.activityId, 'activity_123'),
56+
),
57+
);
58+
final activityBookmarks = await activityBookmarkList.get();
59+
// Query by folder ID
60+
final folderBookmarkList = client.bookmarkList(
61+
const BookmarksQuery(
62+
filter: Filter.equal(BookmarksFilterField.folderId, 'folder_456'),
63+
),
64+
);
65+
final folderBookmarks = await folderBookmarkList.get();
66+
}
67+
68+
Future<void> queryingBookmarkFolders() async {
69+
// Query bookmark folders
70+
const query = BookmarkFoldersQuery(limit: 5);
71+
final bookmarkFolderList = client.bookmarkFolderList(query);
72+
final page1 = await bookmarkFolderList.get();
73+
// Get next page
74+
final page2 = await bookmarkFolderList.queryMoreBookmarkFolders(limit: 3);
75+
// Query by folder name (partial matching)
76+
final projectFolderList = client.bookmarkFolderList(
77+
const BookmarkFoldersQuery(
78+
filter: Filter.contains(BookmarkFoldersFilterField.name, 'project'),
79+
),
80+
);
81+
final projectFolders = await projectFolderList.get();
82+
}

0 commit comments

Comments
 (0)