mirror of
https://github.com/Instagram/IGListKit
synced 2026-05-24 09:48:21 +00:00
Summary: ## Changes in this pull request This PR adds a new feed view controller demo to the IGListKit example app, showcasing how to implement a social media feed with IGListKit. The demo features infinite scrolling with pagination, post deletion, and interactive elements. Also, I've improved the UI testing infrastructure by adding helper methods for more reliable tests across different device types. The new helpers make the tests more resilient when dealing with iPad split views and ensure elements are properly visible before interacting with them. ### Checklist - [X] All tests pass. Demo project builds and runs. - [X] I added tests, an experiment, or detailed why my change isn't tested. - [X] I added an entry to the `CHANGELOG.md` for any breaking changes, enhancements, or bug fixes. - [x] I have reviewed the [contributing guide](https://github.com/Instagram/IGListKit/blob/main/.github/CONTRIBUTING.md) Pull Request resolved: https://github.com/Instagram/IGListKit/pull/1629 Reviewed By: fabiomassimo Differential Revision: D77482204 Pulled By: TimOliver fbshipit-source-id: 44ac86ed253e8452388aad50a1ed05bc6f3a31ab
72 lines
2.4 KiB
Swift
72 lines
2.4 KiB
Swift
/*
|
|
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
*
|
|
* This source code is licensed under the MIT license found in the
|
|
* LICENSE file in the root directory of this source tree.
|
|
*/
|
|
|
|
import Foundation
|
|
|
|
// MARK: - Data Provider
|
|
|
|
// In IGListKit architecture, the data provider is separated from the UI components
|
|
// This enables clean separation of concerns and makes testing easier
|
|
class APIService {
|
|
static let shared = APIService()
|
|
|
|
// Pagination state
|
|
private var currentPage = 1
|
|
private var isLoading = false
|
|
private var hasMoreData = true
|
|
|
|
// Fetch posts with pagination support
|
|
// This is called by the view controller to load data
|
|
// IGListKit will handle the diffing and UI updates based on the results
|
|
func fetchPosts(completion: @escaping ([Post]) -> Void) {
|
|
guard !isLoading, hasMoreData else { return }
|
|
|
|
self.isLoading = true
|
|
|
|
// Simulate network delay
|
|
DispatchQueue.global().asyncAfter(deadline: .now() + 2.0) {
|
|
let posts = self.generateMockPosts(page: self.currentPage)
|
|
|
|
if self.currentPage >= 5 {
|
|
self.hasMoreData = false
|
|
}
|
|
|
|
self.currentPage += 1
|
|
self.isLoading = false
|
|
|
|
DispatchQueue.main.async {
|
|
completion(posts)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Reset pagination state for refreshing
|
|
func resetPagination() {
|
|
self.currentPage = 1
|
|
self.hasMoreData = true
|
|
}
|
|
|
|
// Generate mock data for the demo app
|
|
// In a real app, this would be replaced with API calls
|
|
func generateMockPosts(page: Int) -> [Post] {
|
|
let baseCount = (page - 1) * 5
|
|
|
|
return (1...5).map { index in
|
|
let id = "\(baseCount + index)"
|
|
return Post(
|
|
id: id,
|
|
username: "user\(Int.random(in: 100...999))",
|
|
userAvatarURL: URL(string: "https://randomuser.me/api/portraits/men/\(Int.random(in: 1...99)).jpg"),
|
|
imageURL: URL(string: "https://picsum.photos/id/\(baseCount + index + 10)/500/500"),
|
|
title: "Post #\(id)",
|
|
description: "This is a beautiful photo I took while traveling. What do you think? #travel #photography #nature",
|
|
likes: Int.random(in: 10...1000),
|
|
timeStamp: Date().addingTimeInterval(-Double(Int.random(in: 1...86400) * (baseCount + index)))
|
|
)
|
|
}
|
|
}
|
|
}
|