Skip to content

Commit

Permalink
add feed network
Browse files Browse the repository at this point in the history
  • Loading branch information
KateKashko committed Nov 2, 2024
1 parent 012018a commit ca6a30a
Show file tree
Hide file tree
Showing 15 changed files with 189 additions and 64 deletions.
1 change: 1 addition & 0 deletions SwiftBuddiesIOS/Sources/SwiftBuddiesIOSApp.swift
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import SwiftUI
import GoogleSignIn
import Core
import Feed

@main
struct SwiftBuddiesIOSApp: App {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ struct CommentCell: View {

// MARK: - User photo

Image(user.profileImageUrl)
Image(user.profileImageUrl ?? "")
.resizable()
.scaledToFill()
.frame(width: 35, height: 35)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ struct CommentView: View {
VStack{
ScrollView{
LazyVStack(spacing: 12) {
ForEach(PostModel.MockPosts) { post in
CommentCell(post: post)
}
// ForEach(PostModel.MockPosts) { post in
// CommentCell(post: post)
// }
}
.padding(.top, 20)
.padding(.horizontal, 12)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// FeedResponse.swift
// SwiftBuddiesIOS
//
// Created by Kate Kashko on 1.11.2024.
//

import Foundation

struct FeedResponse: Codable {
let feed: [PostResponse]?
}

struct PostResponse: Codable {
let user: UserResponse?
let post: PostDataResponse?
}

struct UserResponse: Codable {
let name: String?
let picture: String?
}

struct PostDataResponse: Codable {
let uid: String
let sharedDate: String
let content: String
let images: [String]?
let likeCount: Int
let commentCount: Int
}

81 changes: 49 additions & 32 deletions SwiftBuddiesIOS/Targets/FeedModule/Sources/Model/PostModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,37 +16,54 @@ struct PostModel: Identifiable, Hashable, Codable {
let content: String
let imageUrl: String?
var user: UserModel?

public init(id: String,
authorId: String,
likeCount: Int,
commentsCount: Int,
content: String,
imageUrl: String?,
user: UserModel?
) {
self.id = id
self.authorId = authorId
self.likeCount = likeCount
self.commentsCount = commentsCount
self.content = content
self.imageUrl = imageUrl
self.user = user
}
}

extension PostModel {
static var MockPosts: [PostModel] = [
.init(
id: NSUUID().uuidString,
authorId: NSUUID().uuidString,
likeCount: 98,
commentsCount: 10,
content: "Lets do it...",
imageUrl: "example",
user: UserModel.MockUsers[0]
),
.init(
id: NSUUID().uuidString,
authorId: NSUUID().uuidString,
likeCount: 123,
commentsCount: 15,
content: "Hello everyone",
imageUrl: "",
user: UserModel.MockUsers[0]
),
.init(
id: NSUUID().uuidString,
authorId: NSUUID().uuidString,
likeCount: 120,
commentsCount: 20,
content: "Our first steps in project...",
imageUrl: "logo",
user: UserModel.MockUsers[0]
)

]
}
//extension PostModel {
// static var MockPosts: [PostModel] = [
// .init(
// id: NSUUID().uuidString,
// authorId: NSUUID().uuidString,
// likeCount: 98,
// commentsCount: 10,
// content: "Lets do it...",
// imageUrl: "example",
// user: UserModel.MockUsers[0]
// ),
// .init(
// id: NSUUID().uuidString,
// authorId: NSUUID().uuidString,
// likeCount: 123,
// commentsCount: 15,
// content: "Hello everyone",
// imageUrl: "",
// user: UserModel.MockUsers[0]
// ),
// .init(
// id: NSUUID().uuidString,
// authorId: NSUUID().uuidString,
// likeCount: 120,
// commentsCount: 20,
// content: "Our first steps in project...",
// imageUrl: "logo",
// user: UserModel.MockUsers[0]
// )
//
// ]
//}
16 changes: 8 additions & 8 deletions SwiftBuddiesIOS/Targets/FeedModule/Sources/Model/UserModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@ import Foundation
struct UserModel: Identifiable, Hashable, Codable {
let id: String
let name: String
let profileImageUrl: String
let profileImageUrl: String?
}

extension UserModel {
static var MockUsers: [UserModel] = [
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
.init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook")
]
}
//extension UserModel {
// static var MockUsers: [UserModel] = [
// .init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
// .init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook"),
// .init(id: NSUUID().uuidString, name: "Tim Cook", profileImageUrl: "timcook")
// ]
//}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ struct FeedCell: View {
.clipShape(RoundedRectangle(cornerRadius: 15))
}
}

#Preview {
FeedCell(post: PostModel.MockPosts[0])
}
//
//#Preview {
// FeedCell(post: PostModel.MockPosts[0])
//}
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ struct FeedCellContentView: View {
}
}
}

#Preview {
FeedCellContentView(post: PostModel.MockPosts[0])
}
//
//#Preview {
// FeedCellContentView(post: PostModel.MockPosts[0])
//}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ struct FeedCellCountersView: View {
}
}

#Preview {
FeedCellCountersView(post: PostModel.MockPosts[0])
}
//#Preview {
// FeedCellCountersView(post: PostModel.MockPosts[0])
//}
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ struct FeedCellHeaderView: View {
HStack {
if let user = post.user {
// MARK: - User photo
Image(user.profileImageUrl, bundle: DesignResources.bundle)
Image(user.profileImageUrl ?? "", bundle: DesignResources.bundle)
.resizable()
.scaledToFill()
.frame(width: 43, height: 43)
Expand Down Expand Up @@ -46,6 +46,6 @@ struct FeedCellHeaderView: View {
}
}

#Preview {
FeedCellHeaderView(post: PostModel.MockPosts[0])
}
//#Preview {
// FeedCellHeaderView(post: PostModel.MockPosts[0])
//}
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,18 @@ import SwiftUI
import Design

public struct FeedView: View {

@StateObject private var viewModel = FeedViewModel()
@State private var addPost = false

public init() {}
public var body: some View {
NavigationStack {
ScrollView {
LazyVStack(spacing: 10) {
ForEach(PostModel.MockPosts) { post in
FeedCell(post: post)
if let posts = viewModel.posts {
ForEach(posts) { post in
FeedCell(post: post)
}
}
}
.padding(.top, 20)
Expand Down Expand Up @@ -45,6 +47,9 @@ public struct FeedView: View {
.sheet(isPresented: $addPost) {
AddPostView()
}
.task {
await viewModel.fetchFeed()
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
//
// FeedViewModel.swift
// SwiftBuddiesIOS
//
// Created by Kate Kashko on 1.11.2024.
//

import Foundation
import BuddiesNetwork
import Network

@MainActor
class FeedViewModel: ObservableObject {
@Published var posts: [PostModel]?
private let apiClient: BuddiesClient

init() {
self.apiClient = .shared
}

func fetchFeed() async {
var request = FeedRequest(range: "0-2")

do {
let response = try await apiClient.perform(request)
self.posts = response.feed?.map { postResponse in
PostModel(
id: postResponse.post?.uid ?? "",
authorId: "", // Add authorId if available
likeCount: postResponse.post?.likeCount ?? 0,
commentsCount: postResponse.post?.commentCount ?? 0,
content: postResponse.post?.content ?? "",
imageUrl: postResponse.post?.images?.first ?? "",
user: UserModel(
id: UUID().uuidString, // Use a unique ID
name: postResponse.user?.name ?? "",
profileImageUrl: postResponse.user?.picture
)
)
}
} catch {
print("Failed to fetch feed: \(error)")
}
}
}

// MARK: - FeedRequest
struct FeedRequest: Requestable {
var range: String

typealias Data = FeedResponse

func toUrlRequest() throws -> URLRequest {
// Construct the base URL with the endpoint
try URLProvider.returnUrlRequest(
method: .get,
url: APIs.Feed.getFeed.url(),
data: self
)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,12 @@ import BuddiesNetwork
@MainActor
final public class AuthenticationViewModel: ObservableObject {
private let apiClient: BuddiesClient
private let authManager: AuthWithSSOProtocol
private var authManager: AuthWithSSOProtocol
// @Dependency(\.authManager) var authManager

public init() {
self.authManager = AuthenticationManager(authService: .shared)
self.apiClient = .shared
self.authManager = AuthenticationManager(authService: .shared)
}

func signIn(provider: AuthProviderOption) throws {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,16 @@ public enum APIs {
}
}
}

public enum Feed: Endpoint {
case getFeed

public var value: String {
switch self {
case .getFeed: return "getFeed"
}
}
}
}

extension Endpoint {
Expand Down

0 comments on commit ca6a30a

Please sign in to comment.