generated from Xixn2/Tuist_Modular_Template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #32 from GPle-Team/25-fetch-location-post-function
🔀 :: [#25] 위치별 게시글 조회 기능 추가
- Loading branch information
Showing
7 changed files
with
522 additions
and
161 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
207 changes: 207 additions & 0 deletions
207
Projects/App/Sources/Feature/MainFeature/Sources/LocationPostView.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,207 @@ | ||
import SwiftUI | ||
|
||
struct LocationPostView: View { | ||
@StateObject var viewModel: LocationPostViewModel | ||
@State private var topNavigationState: Bool = false | ||
@Environment(\.dismiss) private var dismiss | ||
|
||
var locationType: String | ||
|
||
var body: some View { | ||
ZStack { | ||
GPleAsset.Color.back.swiftUIColor | ||
.ignoresSafeArea() | ||
|
||
VStack(alignment: .leading, spacing: 0) { | ||
ZStack { | ||
HStack { | ||
Button { | ||
dismiss() | ||
} label: { | ||
GPleAsset.Assets.chevronRight.swiftUIImage | ||
.padding(.leading, 20) | ||
} | ||
Spacer() | ||
} | ||
Text(locationTypeText()) | ||
.foregroundStyle(.white) | ||
.font(GPleFontFamily.Pretendard.semiBold.swiftUIFont(size: 18)) | ||
} | ||
.padding(.bottom, 16) | ||
|
||
ScrollView { | ||
switch locationType { | ||
case "GYM": | ||
ForEach(viewModel.gymPostList) { post in | ||
DetailView( | ||
viewModel: DetailViewModel(), | ||
postViewModel: PostViewModel(), | ||
postId: post.id, | ||
title: post.title, | ||
name: post.author.name, | ||
grade: post.author.grade, | ||
imageUrl: post.imageUrl, | ||
tagList: post.tagList.map{ ($0.name, $0.id) }, | ||
emojiList: [ | ||
post.emojiList.chinaCount, | ||
post.emojiList.congCount, | ||
post.emojiList.heartCount, | ||
post.emojiList.poopCount, | ||
post.emojiList.thinkCount, | ||
post.emojiList.thumbsCount | ||
], | ||
checkEmojiList: post.checkEmoji, | ||
createTime: post.createdTime | ||
) | ||
|
||
Rectangle() | ||
.foregroundStyle(GPleAsset.Color.gray900.swiftUIColor) | ||
.frame(height: 3) | ||
.padding(.vertical, 10) | ||
} | ||
case "HOME": | ||
ForEach(viewModel.homePostList) { post in | ||
DetailView( | ||
viewModel: DetailViewModel(), | ||
postViewModel: PostViewModel(), | ||
postId: post.id, | ||
title: post.title, | ||
name: post.author.name, | ||
grade: post.author.grade, | ||
imageUrl: post.imageUrl, | ||
tagList: post.tagList.map{ ($0.name, $0.id) }, | ||
emojiList: [ | ||
post.emojiList.chinaCount, | ||
post.emojiList.congCount, | ||
post.emojiList.heartCount, | ||
post.emojiList.poopCount, | ||
post.emojiList.thinkCount, | ||
post.emojiList.thumbsCount | ||
], | ||
checkEmojiList: post.checkEmoji, | ||
createTime: post.createdTime | ||
) | ||
|
||
Rectangle() | ||
.foregroundStyle(GPleAsset.Color.gray900.swiftUIColor) | ||
.frame(height: 3) | ||
.padding(.vertical, 10) | ||
} | ||
case "PLAYGROUND": | ||
ForEach(viewModel.playgroundPostList) { post in | ||
DetailView( | ||
viewModel: DetailViewModel(), | ||
postViewModel: PostViewModel(), | ||
postId: post.id, | ||
title: post.title, | ||
name: post.author.name, | ||
grade: post.author.grade, | ||
imageUrl: post.imageUrl, | ||
tagList: post.tagList.map{ ($0.name, $0.id) }, | ||
emojiList: [ | ||
post.emojiList.chinaCount, | ||
post.emojiList.congCount, | ||
post.emojiList.heartCount, | ||
post.emojiList.poopCount, | ||
post.emojiList.thinkCount, | ||
post.emojiList.thumbsCount | ||
], | ||
checkEmojiList: post.checkEmoji, | ||
createTime: post.createdTime | ||
) | ||
|
||
Rectangle() | ||
.foregroundStyle(GPleAsset.Color.gray900.swiftUIColor) | ||
.frame(height: 3) | ||
.padding(.vertical, 10) | ||
} | ||
case "DOMITORY": | ||
ForEach(viewModel.domitoryPostList) { post in | ||
DetailView( | ||
viewModel: DetailViewModel(), | ||
postViewModel: PostViewModel(), | ||
postId: post.id, | ||
title: post.title, | ||
name: post.author.name, | ||
grade: post.author.grade, | ||
imageUrl: post.imageUrl, | ||
tagList: post.tagList.map{ ($0.name, $0.id) }, | ||
emojiList: [ | ||
post.emojiList.chinaCount, | ||
post.emojiList.congCount, | ||
post.emojiList.heartCount, | ||
post.emojiList.poopCount, | ||
post.emojiList.thinkCount, | ||
post.emojiList.thumbsCount | ||
], | ||
checkEmojiList: post.checkEmoji, | ||
createTime: post.createdTime | ||
) | ||
|
||
Rectangle() | ||
.foregroundStyle(GPleAsset.Color.gray900.swiftUIColor) | ||
.frame(height: 3) | ||
.padding(.vertical, 10) | ||
} | ||
case "WALKING_TRAIL": | ||
ForEach(viewModel.walkingTrailPostList) { post in | ||
DetailView( | ||
viewModel: DetailViewModel(), | ||
postViewModel: PostViewModel(), | ||
postId: post.id, | ||
title: post.title, | ||
name: post.author.name, | ||
grade: post.author.grade, | ||
imageUrl: post.imageUrl, | ||
tagList: post.tagList.map{ ($0.name, $0.id) }, | ||
emojiList: [ | ||
post.emojiList.chinaCount, | ||
post.emojiList.congCount, | ||
post.emojiList.heartCount, | ||
post.emojiList.poopCount, | ||
post.emojiList.thinkCount, | ||
post.emojiList.thumbsCount | ||
], | ||
checkEmojiList: post.checkEmoji, | ||
createTime: post.createdTime | ||
) | ||
|
||
Rectangle() | ||
.foregroundStyle(GPleAsset.Color.gray900.swiftUIColor) | ||
.frame(height: 3) | ||
.padding(.vertical, 10) | ||
} | ||
default: | ||
EmptyView() | ||
} | ||
} | ||
.padding(.top, 8) | ||
} | ||
.navigationBarBackButtonHidden() | ||
.onAppear { | ||
viewModel.fetchGymList() | ||
viewModel.fetchDomitoryList() | ||
viewModel.fetchHomeList() | ||
viewModel.fetchPlayGroundList() | ||
viewModel.fetchWalkingTrailList() | ||
} | ||
} | ||
} | ||
|
||
func locationTypeText() -> String { | ||
switch locationType { | ||
case "GYM": | ||
return "금봉관" | ||
case "DOMITORY": | ||
return "동행관" | ||
case "HOME": | ||
return "본관" | ||
case "PLAYGROUND": | ||
return "운동장" | ||
case "WALKING_TRAIL": | ||
return "산책로" | ||
default: | ||
return "" | ||
} | ||
} | ||
} |
129 changes: 129 additions & 0 deletions
129
Projects/App/Sources/Feature/MainFeature/Sources/LocationPostViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
import Moya | ||
import Domain | ||
import Foundation | ||
|
||
final class LocationPostViewModel: ObservableObject { | ||
private let authProvider = MoyaProvider<MainAPI>() | ||
private var accessToken: String = "Bearer eyJhbGciOiJIUzM4NCJ9.eyJzdWIiOiIyIiwiaWF0IjoxNzM0NjYyNTg4LCJleHAiOjE3NDQ2NjI1ODh9.FG4FVQ4oikC4HNy5h7gq0QyCIjVZtceIOKwAMnkULAt4y0lX5gGIF1s2Mdj9qr1H" | ||
|
||
@Published public var gymPostList: [Post] = [] | ||
@Published public var homePostList: [Post] = [] | ||
@Published public var playgroundPostList: [Post] = [] | ||
@Published public var domitoryPostList: [Post] = [] | ||
@Published public var walkingTrailPostList: [Post] = [] | ||
|
||
@MainActor | ||
public func fetchGymList() { | ||
authProvider.request(.fetchGymPostList(authorization: accessToken)) { result in | ||
switch result { | ||
case let .success(res): | ||
do { | ||
if let responseString = String(data: res.data, encoding: .utf8) { | ||
print("서버 응답 데이터: \(responseString)") | ||
} | ||
|
||
let responseModel = try JSONDecoder().decode([Post].self, from: res.data) | ||
self.gymPostList = responseModel | ||
|
||
print("게시물 불러오기 성공") | ||
} catch { | ||
print("JSON 디코딩 에러: \(error)") | ||
} | ||
case let .failure(err): | ||
print("Network request failed: \(err)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
public func fetchHomeList() { | ||
authProvider.request(.fetchHomePostList(authorization: accessToken)) { result in | ||
switch result { | ||
case let .success(res): | ||
do { | ||
if let responseString = String(data: res.data, encoding: .utf8) { | ||
print("서버 응답 데이터: \(responseString)") | ||
} | ||
|
||
let responseModel = try JSONDecoder().decode([Post].self, from: res.data) | ||
self.homePostList = responseModel | ||
|
||
print("게시물 불러오기 성공") | ||
} catch { | ||
print("JSON 디코딩 에러: \(error)") | ||
} | ||
case let .failure(err): | ||
print("Network request failed: \(err)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
public func fetchPlayGroundList() { | ||
authProvider.request(.fetchPlaygroundPostList(authorization: accessToken)) { result in | ||
switch result { | ||
case let .success(res): | ||
do { | ||
if let responseString = String(data: res.data, encoding: .utf8) { | ||
print("서버 응답 데이터: \(responseString)") | ||
} | ||
|
||
let responseModel = try JSONDecoder().decode([Post].self, from: res.data) | ||
self.playgroundPostList = responseModel | ||
|
||
print("게시물 불러오기 성공") | ||
} catch { | ||
print("JSON 디코딩 에러: \(error)") | ||
} | ||
case let .failure(err): | ||
print("Network request failed: \(err)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
public func fetchDomitoryList() { | ||
authProvider.request(.fetchGymPostList(authorization: accessToken)) { result in | ||
switch result { | ||
case let .success(res): | ||
do { | ||
if let responseString = String(data: res.data, encoding: .utf8) { | ||
print("서버 응답 데이터: \(responseString)") | ||
} | ||
|
||
let responseModel = try JSONDecoder().decode([Post].self, from: res.data) | ||
self.domitoryPostList = responseModel | ||
|
||
print("게시물 불러오기 성공") | ||
} catch { | ||
print("JSON 디코딩 에러: \(error)") | ||
} | ||
case let .failure(err): | ||
print("Network request failed: \(err)") | ||
} | ||
} | ||
} | ||
|
||
@MainActor | ||
public func fetchWalkingTrailList() { | ||
authProvider.request(.fetchWalkingTrailPostList(authorization: accessToken)) { result in | ||
switch result { | ||
case let .success(res): | ||
do { | ||
if let responseString = String(data: res.data, encoding: .utf8) { | ||
print("서버 응답 데이터: \(responseString)") | ||
} | ||
|
||
let responseModel = try JSONDecoder().decode([Post].self, from: res.data) | ||
self.walkingTrailPostList = responseModel | ||
|
||
print("게시물 불러오기 성공") | ||
} catch { | ||
print("JSON 디코딩 에러: \(error)") | ||
} | ||
case let .failure(err): | ||
print("Network request failed: \(err)") | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.