// Create a feed id
let feedId = FeedId(group: "user", id: "john")
// Create the feed instance from your client
let feed = client.feed(for: feedId)
// Get the latest data
try await feed.getOrCreate()
// Access the state
let feedState = feed.state
State Layer
The State layer provides observable state management for feeds and activities in Stream Feeds. It uses @Published
properties to automatically update your UI when data changes.
FeedState
FeedState
manages the state of a feed, including activities, followers, members, and pagination information. It’s an ObservableObject
that automatically updates when the feed data changes.
Getting FeedState
To access the state of a feed, you need to create a Feed
instance and access its state
property:
// Create a feed id
val fid = FeedId(group = "user", id = "john")
// Create the feed instance from your client
val feed = client.feed(fid = fid)
// Get the latest data
feed.getOrCreate()
// Access the state
val feedState = feed.state
Available Properties
The FeedState
provides the following published properties:
// Core feed data
@Published var feed: FeedData? // The feed information
@Published var activities: [ActivityData] // Activities in the feed
@Published var ownCapabilities: [FeedOwnCapability] // Your permissions
// Follow relationships
@Published var followers: [FollowData] // Users following this feed
@Published var following: [FollowData] // Feeds this user follows
@Published var followRequests: [FollowData] // Pending follow requests
// Feed members
@Published var members: [FeedMemberData] // Members of the feed
// Pagination
var canLoadMoreActivities: Bool // Whether more activities can be loaded
// Core feed data
val feed: StateFlow<FeedData?> // The feed information
val activities: StateFlow<List<ActivityData>> // Activities in the feed
val ownCapabilities: StateFlow<List<FeedOwnCapability>> // Your permissions
// Follow relationships
val followers: StateFlow<List<FollowData>> // Users following this feed
val following: StateFlow<List<FollowData>> // Feeds this user follows
val followRequests: StateFlow<List<FollowData>> // Pending follow requests
// Feed members
val members: StateFlow<List<FeedMemberData>> // Members of the feed
// Pagination
val canLoadMoreActivities: Boolean // Whether more activities can be loaded
ActivityState
ActivityState
manages the state of a single activity, including its comments, reactions, and associated poll data. It’s also an ObservableObject
that updates automatically.
Getting ActivityState
To access the state of an activity, you need to create an Activity
instance:
// Create an activity instance
let activity = client.activity(
for: "activity-123",
in: FeedId(group: "user", id: "john")
)
// Get the latest data
try await activity.get()
// Access the state
let activityState = activity.state
// Create an activity instance
val activity = client.activity(
activityId = "activity-123",
fid = FeedId(group = "user", id = "john")
)
// Get the latest data
activity.get()
// Access the state
val activityState = activity.state
Available Properties
The ActivityState
provides the following properties:
@Published var activity: ActivityData? // The activity data
@Published var comments: [CommentData] // Comments on the activity
@Published var poll: PollData? // Associated poll (if any)
val activity: StateFlow<ActivityData?> // The activity data
val comments: StateFlow<List<ThreadedCommentData>> // Comments on the activity
val poll: StateFlow<PollData?> // Associated poll (if any)