Overview
The PostsManager class handles all post-related operations in Threadly, including creating image and video posts, retrieving user posts, managing feeds, and deleting posts. It supports file uploads with progress tracking and pagination for efficient content loading.
Constructor
Initializes the PostsManager and sets up SharedPreferences for authentication.
Methods
uploadImagePost
Uploads an image post with a caption. Provides progress tracking during upload.
public void uploadImagePost(
File imagefile,
String caption,
NetworkCallbackInterfaceWithProgressTracking callback
)
Caption text for the post
callback
NetworkCallbackInterfaceWithProgressTracking
required
Callback interface to handle upload progress and response
Contains the created post details including post ID and URLs
Example
PostsManager postsManager = new PostsManager();
File imageFile = new File("/path/to/image.jpg");
postsManager.uploadImagePost(
imageFile,
"Check out this amazing view!",
new NetworkCallbackInterfaceWithProgressTracking() {
@Override
public void progress(long bytesUploaded, long totalBytes) {
int percentage = (int) ((bytesUploaded * 100) / totalBytes);
// Update progress UI
}
@Override
public void onSuccess(JSONObject response) {
// Post uploaded successfully
int postId = response.optInt("postId");
}
@Override
public void onError(String error) {
// Handle error
}
});
uploadVideoPost
Uploads a video post with a caption. Provides progress tracking during upload.
public void uploadVideoPost(
File videofile,
String caption,
NetworkCallbackInterfaceWithProgressTracking callback
)
Caption text for the post
callback
NetworkCallbackInterfaceWithProgressTracking
required
Callback interface to handle upload progress and response
Contains the created post details including post ID and URLs
Example
PostsManager postsManager = new PostsManager();
File videoFile = new File("/path/to/video.mp4");
postsManager.uploadVideoPost(
videoFile,
"My latest adventure!",
new NetworkCallbackInterfaceWithProgressTracking() {
@Override
public void progress(long bytesUploaded, long totalBytes) {
// Update progress UI
}
@Override
public void onSuccess(JSONObject response) {
// Video uploaded successfully
}
@Override
public void onError(String error) {
// Handle error
}
});
getPostWithId
Retrieves a specific post by its ID.
public void getPostWithId(
int postId,
NetworkCallbackInterfaceWithJsonObjectDelivery callbackInterface
)
Unique identifier of the post
callbackInterface
NetworkCallbackInterfaceWithJsonObjectDelivery
required
Callback interface to handle the response
Post object containing all post details (see Posts_Model.java:23)
Example
PostsManager postsManager = new PostsManager();
postsManager.getPostWithId(12345,
new NetworkCallbackInterfaceWithJsonObjectDelivery() {
@Override
public void onSuccess(JSONObject response) {
String caption = response.optString("caption");
int likeCount = response.optInt("likeCount");
}
@Override
public void onError(String error) {
// Handle error
}
});
getUserPosts
Retrieves posts for a specific user with pagination support.
public void getUserPosts(
int page,
String userId,
NetworkCallbackInterfaceJsonObject callbackInterface
)
Page number for pagination (starts at 0)
User ID whose posts to retrieve
callbackInterface
NetworkCallbackInterfaceJsonObject
required
Callback interface to handle the response
Array of post objects for the specified user
Example
PostsManager postsManager = new PostsManager();
postsManager.getUserPosts(0, "user123",
new NetworkCallbackInterfaceJsonObject() {
@Override
public void onSuccess(JSONObject response) {
JSONArray posts = response.optJSONArray("posts");
// Display user's posts
}
@Override
public void onError(int errorCode) {
// Handle error
}
});
getLoggedInUserPost
Retrieves posts for the currently logged-in user with pagination support.
public void getLoggedInUserPost(
int page,
NetworkCallbackInterfaceWithJsonObjectDelivery callbackInterface
)
Page number for pagination (starts at 0)
callbackInterface
NetworkCallbackInterfaceWithJsonObjectDelivery
required
Callback interface to handle the response
Array of post objects for the logged-in user
getImageFeed
Retrieves the image feed for the user’s home timeline.
public void getImageFeed(
NetworkCallbackInterfaceWithJsonObjectDelivery callback
)
callback
NetworkCallbackInterfaceWithJsonObjectDelivery
required
Callback interface to handle the response
Array of image posts from users the current user follows
Example
PostsManager postsManager = new PostsManager();
postsManager.getImageFeed(
new NetworkCallbackInterfaceWithJsonObjectDelivery() {
@Override
public void onSuccess(JSONObject response) {
JSONArray feed = response.optJSONArray("feed");
// Display image feed
}
@Override
public void onError(String error) {
// Handle error
}
});
getVideoFeed
Retrieves the video feed for the user’s home timeline.
public void getVideoFeed(
NetworkCallbackInterfaceWithJsonObjectDelivery callback
)
callback
NetworkCallbackInterfaceWithJsonObjectDelivery
required
Callback interface to handle the response
Array of video posts from users the current user follows
Example
PostsManager postsManager = new PostsManager();
postsManager.getVideoFeed(
new NetworkCallbackInterfaceWithJsonObjectDelivery() {
@Override
public void onSuccess(JSONObject response) {
JSONArray videoFeed = response.optJSONArray("feed");
// Display video feed
}
@Override
public void onError(String error) {
// Handle error
}
});
RemovePost
Deletes a post by its ID.
public void RemovePost(
int postId,
NetworkCallbackInterface callbackInterface
)
callbackInterface
NetworkCallbackInterface
required
Callback interface to handle the response
Example
PostsManager postsManager = new PostsManager();
postsManager.RemovePost(12345,
new NetworkCallbackInterface() {
@Override
public void onSuccess() {
// Post deleted successfully
}
@Override
public void onError(String error) {
// Handle error
}
});
Post Model Structure
Posts returned by these methods follow the structure defined in Posts_Model.java:23:
postId (int) - Unique post identifier
userId (String) - ID of the user who created the post
username (String) - Username of the post creator
userDpUrl (String) - Profile picture URL
postUrl (String) - URL of the post media
caption (String) - Post caption text
createdAt (String) - Post creation timestamp
likeCount (int) - Number of likes
commentCount (int) - Number of comments
shareCount (int) - Number of shares
isliked (Boolean) - Whether current user liked the post
isVideo (boolean) - Whether the post is a video
isFollowed (boolean) - Whether current user follows the post creator
API Endpoints Used
POST /api/posts/image - Upload image post
POST /api/posts/video - Upload video post
GET /api/posts/{id} - Get post by ID
GET /api/posts/user/{userId} - Get user posts
GET /api/feed/images - Get image feed
GET /api/feed/videos - Get video feed
DELETE /api/posts/{id} - Delete post