Skip to content

Commit

Permalink
feat: Repository And UseCase 파일 생성
Browse files Browse the repository at this point in the history
  • Loading branch information
MaraMincho committed Jan 2, 2024
1 parent ccaae90 commit e3b83a9
Show file tree
Hide file tree
Showing 8 changed files with 124 additions and 21 deletions.
66 changes: 66 additions & 0 deletions iOS/Projects/Features/Home/Sources/Data/FeedRepository.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
//
// FeedRepository.swift
// HomeFeature
//
// Created by MaraMincho on 12/7/23.
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Combine
import CommonNetworkingKeyManager
import Foundation
import Trinet

// MARK: - FeedRepository

public struct FeedRepository: FeedRepositoryRepresentable {
let decoder = JSONDecoder()
let provider: TNProvider<FeedEndPoint>
init(session: URLSessionProtocol = URLSession.shared) {
provider = .init(session: session)
}

public func fetchFeed(at page: Int) -> AnyPublisher<[FeedElement], Never> {
return Future<[FeedElement], Error> { promise in
Task { [provider] in
do {
let data = try await provider.request(.fetchPosts(page: page), interceptor: TNKeychainInterceptor.shared)
let feedElementList = try decoder.decode([FeedElement].self, from: data)
promise(.success(feedElementList))
} catch {
promise(.failure(error))
}
}
}
.catch { _ in return Empty() }
.eraseToAnyPublisher()
}
}

// MARK: - FeedEndPoint

public enum FeedEndPoint: TNEndPoint {
case fetchPosts(page: Int)
public var path: String {
return ""
}

public var method: TNMethod {
return .post
}

public var query: Encodable? {
return nil
}

public var body: Encodable? {
switch self {
case let .fetchPosts(page):
return page
}
}

public var headers: TNHeaders {
return .default
}
}
9 changes: 0 additions & 9 deletions iOS/Projects/Features/Home/Sources/Data/HomeRepository.swift

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

import Foundation

struct FeedElement: Hashable {
public struct FeedElement: Hashable, Decodable {
/// 개시물의 아이디 입니다.
let ID: Int

Expand Down
20 changes: 20 additions & 0 deletions iOS/Projects/Features/Home/Sources/Domain/HomeUseCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,24 @@
// Copyright © 2023 kr.codesquad.boostcamp8. All rights reserved.
//

import Combine
import Foundation

// MARK: - HomeUseCaseRepresentable

public protocol HomeUseCaseRepresentable {
func fetchFeed(at page: Int) -> AnyPublisher<[FeedElement], Never>
}

// MARK: - HomeUseCase

public struct HomeUseCase: HomeUseCaseRepresentable {
let feedRepositoryRepresentable: FeedRepositoryRepresentable
init(feedRepositoryRepresentable: FeedRepositoryRepresentable) {
self.feedRepositoryRepresentable = feedRepositoryRepresentable
}

public func fetchFeed(at page: Int) -> AnyPublisher<[FeedElement], Never> {
return feedRepositoryRepresentable.fetchFeed(at: page)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// FeedRepositoryRepresentable.swift
// HomeFeature
//
// Created by MaraMincho on 1/2/24.
// Copyright © 2024 kr.codesquad.boostcamp8. All rights reserved.
//

import Combine
import Foundation

// MARK: - FeedRepositoryRepresentable

public protocol FeedRepositoryRepresentable {
func fetchFeed(at page: Int) -> AnyPublisher<[FeedElement], Never>
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// HomeViewController+UICollectionViewDelegate.swift
// HomeFeature
//
// Created by MaraMincho on 1/2/24.
// Copyright © 2024 kr.codesquad.boostcamp8. All rights reserved.
//

import Log
import UIKit

extension HomeViewController: UICollectionViewDelegate {
func scrollViewDidEndDragging(_: UIScrollView, willDecelerate _: Bool) {
Log.make().debug("스크롤 끝남")
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ final class HomeViewController: UIViewController {

private extension HomeViewController {
func setup() {
setCollectionViewDelegate()
setDataSource()
setupStyles()
setupHierarchyAndConstraints()
Expand All @@ -84,6 +85,10 @@ private extension HomeViewController {
testCollectionViewDataSource()
}

func setCollectionViewDelegate() {
feedListCollectionView.delegate = self
}

func setDataSource() {
dataSource = .init(collectionView: feedListCollectionView) { collectionView, indexPath, item in
let dequeuedCell = collectionView.dequeueReusableCell(withReuseIdentifier: FeedItemCollectionViewCell.identifier, for: indexPath)
Expand Down

0 comments on commit e3b83a9

Please sign in to comment.