Skip to content

Commit

Permalink
Merge pull request #32 from GPle-Team/25-fetch-location-post-function
Browse files Browse the repository at this point in the history
🔀 :: [#25] 위치별 게시글 조회 기능 추가
  • Loading branch information
shwaaaa authored Dec 27, 2024
2 parents 3ace344 + 837c556 commit a1d10be
Show file tree
Hide file tree
Showing 7 changed files with 522 additions and 161 deletions.
3 changes: 1 addition & 2 deletions Projects/App/Sources/Application/GPleApp.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import SwiftUI
struct GPleApp: App {
var body: some Scene {
WindowGroup {
//RankView(postViewModel: PostViewModel())
MyPageView(viewModel: MyPageViewModel(),postViewModel: PostViewModel())
MainView(postViewModel: PostViewModel())
}
}
}
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 ""
}
}
}
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)")
}
}
}
}
Loading

0 comments on commit a1d10be

Please sign in to comment.