Skip to content

Commit

Permalink
Merge pull request #50 from mash-up-kr/feature/#48-mypage-pagination
Browse files Browse the repository at this point in the history
feat: 나의 파밈 pagination 구현 및 설정 화면 연결
  • Loading branch information
hryeong66 authored Jul 22, 2024
2 parents 7206bd3 + 5e42d03 commit 0073c36
Show file tree
Hide file tree
Showing 27 changed files with 513 additions and 38 deletions.
2 changes: 2 additions & 0 deletions Projects/App/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ let project = Project.configure(
.Feature.Recommend,
.Feature.Search,
.Feature.MyPage,
.Feature.Setting,
.ResourceKit,
.Core.DesignSystem,
.Core.PPACNetwork,
Expand Down Expand Up @@ -58,6 +59,7 @@ let project = Project.configure(
.Feature.Recommend,
.Feature.Search,
.Feature.MyPage,
.Feature.Setting,
.ResourceKit,
.Core.DesignSystem,
.Core.PPACNetwork,
Expand Down
25 changes: 23 additions & 2 deletions Projects/Core/DesignSystem/Sources/View/Meme/MemeListView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,22 @@ public struct MemeListView: View {
alignment: .center),count: 2)
private let memeClickHandler: ((MemeDetail) -> ())?
private let memeCopyHandler: ((MemeDetail) -> ())?
private let onAppearLastMemeHandler: (() -> ())?

private var lastMemeId: String {
return memeDetailList.last?.id ?? ""
}

public init(
memeDetailList: [MemeDetail],
memeClickHandler: ((MemeDetail) -> ())? = nil,
memeCopyHandler: ((MemeDetail) -> ())? = nil
memeCopyHandler: ((MemeDetail) -> ())? = nil,
onAppearLastMemeHandler: (() -> ())? = nil
) {
self.memeDetailList = memeDetailList
self.memeClickHandler = memeClickHandler
self.memeCopyHandler = memeCopyHandler
self.onAppearLastMemeHandler = onAppearLastMemeHandler
}

var oddIndexedItems: [MemeDetail] {
Expand All @@ -41,14 +48,19 @@ public struct MemeListView: View {

public var body: some View {
ScrollView {
HStack {
HStack(alignment: .top) {
LazyVStack {
ForEach(oddIndexedItems) { memeDetail in
MemeItemView(
memeDetail: memeDetail,
memeClickHandler: memeClickHandler,
memeCopyHandler: memeCopyHandler
)
.onAppear {
if memeDetail.id == lastMemeId {
onAppearLastMemeHandler?()
}
}
}
}

Expand All @@ -59,9 +71,18 @@ public struct MemeListView: View {
memeClickHandler: memeClickHandler,
memeCopyHandler: memeCopyHandler
)
.onAppear {
if memeDetail.id == lastMemeId {
onAppearLastMemeHandler?()
}
}
}
}
}
// FIXME: pull to refresh 했을 때 onAppear가 호출되지 않아서 onChange로 임시 호출, 수정필요
// .onChange(of: memeDetailList) {
// onAppearLastMemeHandler?()
// }
.frame(maxWidth: .infinity)
}
.scrollTargetBehavior(.viewAligned)
Expand Down
19 changes: 19 additions & 0 deletions Projects/Core/PPACData/Sources/DTO/MemeResponseDTO.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,25 @@ struct MemeWithPaginationResponseDTO: Decodable {
}
}

extension MemeWithPaginationResponseDTO {
func toModel() -> MemeListWithPagination {
let pagination = MemeListWithPagination
.Pagination(
totalPages: self.pagination.totalPages,
totalMemes: self.pagination.total,
perPageOfMemes: self.pagination.perPage,
currentPage: self.pagination.currentPage
)
let memeList = memeList.map { $0.toModel() }

return MemeListWithPagination(
pagination: pagination,
memeList: memeList
)
}
}


struct MemeResponseDTO: Decodable {
let _id: String
let title: String
Expand Down
8 changes: 7 additions & 1 deletion Projects/Core/PPACData/Sources/Endpoint/UserEndpoint.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public enum UserEndpoint: Requestable {

case create(deviceId: String)
case userDetail
case savedMeme
case savedMeme(page: Int, size: Int)
case lastSeenMeme

public var url: String {
Expand Down Expand Up @@ -55,6 +55,12 @@ public enum UserEndpoint: Requestable {
case .create(let deviceId):
let createDeviceRequest = CreateUserRequestDTO(deviceId: deviceId)
return .body(createDeviceRequest)
case .savedMeme(let page, let size):
let parameters: [String: String] = [
"page" : "\(page)",
"size" : "\(size)"
]
return .query(parameters)
default:
return nil
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,17 +54,17 @@ public final class UserRepositoryImpl: UserRepository {
}
}

public func getSavedMeme() async throws -> [MemeDetail] {
public func getSavedMeme(page: Int, size: Int) async throws -> MemeListWithPagination {
let result = await networkservice
.request(
UserEndpoint.savedMeme,
UserEndpoint.savedMeme(page: page, size: size),
dataType: BaseDTO<MemeWithPaginationResponseDTO>.self
)

switch result {
case .success(let data):
guard let memeResponseDTOList = data.data?.memeList else { throw NetworkError.dataDecodingError }
return memeResponseDTOList.map { $0.toModel() }
guard let memeWithPaginationResponseDTO = data.data else { throw NetworkError.dataDecodingError }
return memeWithPaginationResponseDTO.toModel()
case .failure(let error):
throw error
}
Expand Down
1 change: 1 addition & 0 deletions Projects/Core/PPACDomain/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ let project = Project(
resources: "Resources/**",
dependencies: [
.Core.PPACModels,
.Core.PPACNetwork
]
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ import PPACModels
public protocol UserRepository {
func create(deviceId: String) async throws -> UserDetail
func getUserDetail() async throws -> UserDetail
func getSavedMeme() async throws -> [MemeDetail]
func getSavedMeme(page: Int, size: Int) async throws -> MemeListWithPagination
func getLastSeenMeme() async throws -> [MemeDetail]
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public class MockCheckUserInfoUseCase: CheckUserInfoUseCase {
class MockUserRepository: UserRepository {
func create(deviceId: String) async throws -> UserDetail { return UserDetail.mock }
func getUserDetail() async throws -> UserDetail { return UserDetail.mock }
func getSavedMeme() async throws -> [MemeDetail] { return [] }
func getSavedMeme(page: Int, size: Int) async throws -> MemeListWithPagination { return MemeListWithPagination.mock }
func getLastSeenMeme() async throws -> [MemeDetail] { return [] }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import Foundation
import PPACModels

public protocol GetSavedMemeUseCase {
func execute() async throws -> [MemeDetail]
func execute(page: Int, size: Int) async throws -> MemeListWithPagination
}

public class GetSavedMemeUseCaseImpl: GetSavedMemeUseCase {
Expand All @@ -19,7 +19,7 @@ public class GetSavedMemeUseCaseImpl: GetSavedMemeUseCase {
self.userRepository = userRepository
}

public func execute() async throws -> [MemeDetail] {
return try await self.userRepository.getSavedMeme()
public func execute(page: Int, size: Int) async throws -> MemeListWithPagination {
return try await self.userRepository.getSavedMeme(page: page, size: size)
}
}
57 changes: 57 additions & 0 deletions Projects/Core/PPACModels/Sources/Meme/MemeListWithPagination.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
//
// MemeListWithPagination.swift
// PPACModels
//
// Created by 장혜령 on 2024/07/21.
//

import Foundation

public struct MemeListWithPagination {
public let pagination: Pagination
public let memeList: [MemeDetail]

public init(
pagination: Pagination,
memeList: [MemeDetail]
) {
self.pagination = pagination
self.memeList = memeList
}

public struct Pagination {
/// 전체 페이지 개수
public let totalPages: Int
/// 전체 밈 개수
public let totalMemes: Int
/// page 당 밈 개수
public let perPageOfMemes: Int
/// 현재 page
public let currentPage: Int

public init(
totalPages: Int,
totalMemes: Int,
perPageOfMemes: Int,
currentPage: Int
) {
self.totalPages = totalPages
self.totalMemes = totalMemes
self.perPageOfMemes = perPageOfMemes
self.currentPage = currentPage
}

static public let none = Pagination(totalPages: 0, totalMemes: 0, perPageOfMemes: 0, currentPage: 0)
}
}

public extension MemeListWithPagination {
static let mock = MemeListWithPagination(
pagination: Pagination(
totalPages: 1,
totalMemes: 5,
perPageOfMemes: 5,
currentPage: 1
),
memeList: [])
}
1 change: 1 addition & 0 deletions Projects/Features/MyPage/Project.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ let project = Project(
.Core.DesignSystem,
.Core.PPACModels,
.Feature.MemeDetail,
.Feature.Setting
]
)
]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,13 @@ struct LevelProgressView: View {
.stroke(Color.Border.secondary, lineWidth: 1, fill: Color.Background.assistive)
}

private func getCurrentLevelWidth(_ wiewWidth: CGFloat) -> CGFloat {
let width = wiewWidth / 20.0 * CGFloat(conditionCount)
private func getCurrentLevelWidth(_ viewWidth: CGFloat) -> CGFloat {
let width = viewWidth / 20.0 * CGFloat(conditionCount)
return width < minimumWidth ? minimumWidth : width
}
}


#Preview {
LevelProgressView(level: .level1, conditionCount: 20)
LevelProgressView(level: .level1, conditionCount: 1)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
//
// MyPagePregressView.swift
// MyPage
//
// Created by 장혜령 on 2024/07/21.
//

import SwiftUI

struct MyPagePregressView: View {
@Binding var isRefreshCompleted: Bool
var body: some View {
if !isRefreshCompleted {
VStack {
ProgressView()
.padding(.top, 80)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct SavedMemeListView: View {
@Binding var memeDetailList: [MemeDetail]
var memeClickHandler: ((MemeDetail) -> ())?
var memeCopyHandler: ((MemeDetail) -> ())?
var onAppearLastMemeHandler: (() -> ())?

var body: some View {
VStack {
Expand All @@ -33,9 +34,10 @@ struct SavedMemeListView: View {
MemeListView(
memeDetailList: memeDetailList,
memeClickHandler: memeClickHandler,
memeCopyHandler: memeCopyHandler
memeCopyHandler: memeCopyHandler,
onAppearLastMemeHandler: onAppearLastMemeHandler
)
.padding(.horizontal, 20)
.padding(.horizontal, 20)
}
}

Expand Down
7 changes: 6 additions & 1 deletion Projects/Features/MyPage/Sources/MyPageRouter.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import PPACData

import MemeDetail
import DesignSystem
import Setting

public final class MyPageRouter: Router, MyPageRouting {

Expand Down Expand Up @@ -61,7 +62,11 @@ public final class MyPageRouter: Router, MyPageRouting {
router.start()
}

public func showSettingView() { }
public func showSettingView() {
let router = SettingRouter(navigationController: self.navigationController)
self.childRouters.append(router)
router.start()
}

}

16 changes: 13 additions & 3 deletions Projects/Features/MyPage/Sources/MyPageView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ public struct MyPageView: View {

public var body: some View {
ScrollView {
MyPagePregressView(isRefreshCompleted: $viewModel.state.isRefreshCompleted)
levelView
divider
RecentlyMemeListView(
Expand All @@ -28,13 +29,17 @@ public struct MyPageView: View {
SavedMemeListView(
memeDetailList: $viewModel.state.savedMemeList,
memeClickHandler: viewModel.handler.memeClickHandler,
memeCopyHandler: viewModel.handler.memeCopyHandler
memeCopyHandler: viewModel.handler.memeCopyHandler,
onAppearLastMemeHandler: viewModel.handler.onAppearLastMemeHandler
)
Spacer(minLength: 70)
Spacer(minLength: 80)
}
.onAppear {
viewModel.dispatch(type: .onAppearMyPageView)
}
.refreshable {
viewModel.dispatch(type: .pullToRefresh)
}
.background {
gradientBackgroundView
}
Expand Down Expand Up @@ -69,8 +74,11 @@ public struct MyPageView: View {
.frame(width: 20, height: 20, alignment: .center)
.padding(.vertical, 15)
.padding(.trailing, 20)
.onTapGesture {
viewModel.dispatch(type: .settingButtonTapped)
}
}
.padding(.top, 30)
.padding(.top, 40)
}

var levelTitleTextView: some View {
Expand All @@ -88,6 +96,8 @@ public struct MyPageView: View {
}




//#Preview {
// let mockImageList = ["https://plus.unsplash.com/premium_photo-1661892088256-0a17130b3d0d?q=80&w=3560&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
// "https://plus.unsplash.com/premium_photo-1676955432796-226f504a560b?q=80&w=3333&auto=format&fit=crop&ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8fA%3D%3D",
Expand Down
Loading

0 comments on commit 0073c36

Please sign in to comment.