Skip to content

Commit 6f4bcb2

Browse files
authored
Feat/docs (#17)
* first doc pages * rename state into notifier and create state * decrease min sdk version
1 parent 0d9d6c0 commit 6f4bcb2

32 files changed

+939
-87
lines changed
Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
// ignore_for_file: prefer_const_constructors, unused_local_variable, file_names, depend_on_referenced_packages
2+
3+
import 'package:flutter/widgets.dart';
4+
import 'package:flutter_state_notifier/flutter_state_notifier.dart';
5+
import 'package:stream_feeds/stream_feeds.dart';
6+
7+
late StreamFeedsClient client;
8+
late Feed feed;
9+
10+
Future<void> gettingStarted() async {
11+
// Import the package
12+
// import 'package:stream_feeds/stream_feeds.dart';
13+
14+
// Initialize the client
15+
final client = StreamFeedsClient(
16+
apiKey: '<your_api_key>',
17+
user: User(id: 'john'),
18+
tokenProvider: TokenProvider.static(UserToken('<user_token>')),
19+
);
20+
await client.connect();
21+
22+
// Create a feed (or get its data if exists)
23+
final feed = client.feed(group: 'user', id: 'john');
24+
final result = await feed.getOrCreate();
25+
26+
// Add activity
27+
final activity = await feed.addActivity(
28+
request: FeedAddActivityRequest(type: 'post', text: 'Hello, Stream Feeds!'),
29+
);
30+
}
31+
32+
Future<void> socialMediaFeed() async {
33+
// Create a timeline feed
34+
final timeline = client.feed(group: 'timeline', id: 'john');
35+
await timeline.getOrCreate();
36+
37+
// Add a reaction to activity
38+
await timeline.addReaction(
39+
activityId: 'activity_123',
40+
request: AddReactionRequest(type: 'like'),
41+
);
42+
43+
// Add a comment to activity
44+
await timeline.addComment(
45+
request: AddCommentRequest(
46+
comment: 'Great post!',
47+
objectId: 'activity_123',
48+
objectType: 'activity',
49+
),
50+
);
51+
52+
// Add a reaction to comment
53+
final activity = client.activity(
54+
activityId: 'activity_123',
55+
fid: FeedId(group: 'timeline', id: 'john'),
56+
);
57+
await activity.addCommentReaction(
58+
commentId: 'commentId',
59+
request: AddCommentReactionRequest(type: 'like'),
60+
);
61+
}
62+
63+
Future<void> notificationFeed() async {
64+
// Create a notification feed
65+
final notifications = client.feed(group: 'notification', id: 'john');
66+
await notifications.getOrCreate();
67+
68+
// Mark notifications as read
69+
await notifications.markActivity(
70+
request: MarkActivityRequest(markAllRead: true),
71+
);
72+
}
73+
74+
Future<void> polls() async {
75+
// Create a poll
76+
final feedId = FeedId(group: 'timeline', id: 'john');
77+
final feed = client.feedFromId(feedId);
78+
final result = await feed.createPoll(
79+
request: CreatePollRequest(
80+
name: "What's your favorite color?",
81+
options: const [
82+
PollOptionInput(text: 'Red'),
83+
PollOptionInput(text: 'Blue'),
84+
PollOptionInput(text: 'Green'),
85+
],
86+
),
87+
activityType: 'poll',
88+
);
89+
90+
// Vote on a poll
91+
final activityData = result.getOrThrow();
92+
final activity = client.activity(activityId: activityData.id, fid: feedId);
93+
await activity.castPollVote(
94+
CastPollVoteRequest(vote: VoteData(optionId: 'option_456')),
95+
);
96+
}
97+
98+
Future<void> customActivityTypes() async {
99+
final workoutActivity = await feed.addActivity(
100+
request: FeedAddActivityRequest(
101+
text: 'Just finished my run',
102+
custom: {'distance': 5.2, 'duration': 1800, 'calories': 450},
103+
type: 'workout',
104+
),
105+
);
106+
}
107+
108+
// Real-time updates with StateLayer
109+
110+
// imports
111+
// import 'package:flutter/widgets.dart';
112+
// import 'package:flutter_state_notifier/flutter_state_notifier.dart';
113+
// import 'package:stream_feeds/stream_feeds.dart';
114+
class FeedView extends StatefulWidget {
115+
const FeedView({super.key, required this.feed});
116+
final Feed feed;
117+
118+
@override
119+
State<FeedView> createState() => _FeedViewState();
120+
}
121+
122+
class _FeedViewState extends State<FeedView> {
123+
@override
124+
void initState() {
125+
super.initState();
126+
widget.feed.getOrCreate();
127+
}
128+
129+
@override
130+
void dispose() {
131+
widget.feed.dispose();
132+
super.dispose();
133+
}
134+
135+
@override
136+
Widget build(BuildContext context) {
137+
return StateNotifierBuilder(
138+
stateNotifier: widget.feed.notifier,
139+
builder: (BuildContext context, FeedState state, Widget? child) {
140+
return Text(state.feed?.name ?? '');
141+
},
142+
);
143+
}
144+
}
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
// ignore_for_file: unused_local_variable, file_names, 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> creatingActivities() async {
9+
// Add an activity to 1 feed
10+
final activity = await feed.addActivity(
11+
request: const FeedAddActivityRequest(text: 'Hello world', type: 'post'),
12+
);
13+
14+
// Add an activity to multiple feeds
15+
final multiFeedActivity = await feed.addActivity(
16+
request: const FeedAddActivityRequest(
17+
feeds: ['user:1', 'stock:apple'],
18+
text: 'apple stock will go up',
19+
type: 'post',
20+
),
21+
);
22+
}
23+
24+
Future<void> imageAndVideo() async {
25+
final imageActivity = await feed.addActivity(
26+
request: const FeedAddActivityRequest(
27+
attachments: [
28+
Attachment(imageUrl: 'https://example.com/image.jpg', type: 'image'),
29+
],
30+
text: 'look at NYC',
31+
type: 'post',
32+
),
33+
);
34+
}
35+
36+
Future<void> stories() async {
37+
final tomorrow = DateTime.now().add(const Duration(days: 1));
38+
final storyActivity = await feed.addActivity(
39+
request: FeedAddActivityRequest(
40+
attachments: [
41+
const Attachment(
42+
imageUrl: 'https://example.com/image1.jpg',
43+
type: 'image',
44+
),
45+
const Attachment(
46+
assetUrl: 'https://example.com/video1.mp4',
47+
type: 'video',
48+
),
49+
],
50+
expiresAt: tomorrow.toIso8601String(),
51+
text: 'My story',
52+
type: 'story',
53+
),
54+
);
55+
}
56+
57+
Future<void> addManyActivities() async {
58+
final activities = [
59+
const ActivityRequest(
60+
feeds: ['user:123'],
61+
id: '1',
62+
text: 'hi',
63+
type: 'post',
64+
),
65+
const ActivityRequest(
66+
feeds: ['user:456'],
67+
id: '2',
68+
text: 'hi',
69+
type: 'post',
70+
),
71+
];
72+
final upsertedActivities = await client.upsertActivities(
73+
activities: activities,
74+
);
75+
}
76+
77+
Future<void> updatingAndDeletingActivities() async {
78+
// Update an activity
79+
final updatedActivity = await feed.updateActivity(
80+
id: '123',
81+
request: const UpdateActivityRequest(
82+
text: 'Updated text',
83+
custom: {'custom': 'custom'},
84+
),
85+
);
86+
// Delete an activity
87+
88+
const hardDelete =
89+
false; // Soft delete sets deleted at but retains the data, hard delete fully removes it
90+
await feed.deleteActivity(id: '123', hardDelete: hardDelete);
91+
// Batch delete activities
92+
await client.deleteActivities(
93+
request: const DeleteActivitiesRequest(
94+
ids: ['123', '456'],
95+
hardDelete: false,
96+
),
97+
);
98+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// ignore_for_file: unused_local_variable, file_names
2+
3+
import 'package:stream_feeds/stream_feeds.dart';
4+
5+
late StreamFeedsClient client;
6+
late Feed feed;
7+
8+
Future<void> activitySearchAndQueries() async {
9+
final query = ActivitiesQuery(
10+
filter: Filter.equal(ActivitiesFilterField.type, 'post'),
11+
sort: [ActivitiesSort.desc(ActivitiesSortField.createdAt)],
12+
limit: 10,
13+
);
14+
15+
final activityList = client.activityList(query);
16+
final activities = await activityList.get();
17+
}
18+
19+
Future<void> queryActivitiesByText() async {
20+
// search for activities where the text includes the word 'popularity'.
21+
final query = ActivitiesQuery(
22+
filter: Filter.equal(ActivitiesFilterField.text, 'popularity'),
23+
);
24+
25+
final activityList = client.activityList(query);
26+
final activities = await activityList.get();
27+
}
28+
29+
Future<void> queryActivitiesBySearchData() async {
30+
// search for activities associated with the campaign ID 'spring-sale-2025'
31+
final searchValue = {'campaign': {'id': 'spring-sale-2025'}};
32+
final query = ActivitiesQuery(
33+
filter: Filter.contains(ActivitiesFilterField.searchData, searchValue),
34+
);
35+
36+
final activityList = client.activityList(query);
37+
final activities = await activityList.get();
38+
39+
// search for activities where the campaign took place in a mall
40+
final query2 = ActivitiesQuery(
41+
filter: Filter.pathExists(ActivitiesFilterField.searchData, 'campaign.location.mall'),
42+
);
43+
44+
final activityList2 = client.activityList(query2);
45+
final activities2 = await activityList2.get();
46+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
// ignore_for_file: file_names
2+
3+
// TODO

0 commit comments

Comments
 (0)