Skip to content

Commit

Permalink
[#113] 북마크 상태 변경 api연결시도
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiddich-Dev committed Feb 18, 2024
1 parent 303e249 commit 8facb45
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 41 deletions.
16 changes: 12 additions & 4 deletions Spon-us.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,9 @@
3B81BCBA2B622F280067E9CB /* MyNoticeDetailView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3B81BCB92B622F280067E9CB /* MyNoticeDetailView.swift */; };
3B81BCBE2B62312C0067E9CB /* Product.storekit in Resources */ = {isa = PBXBuildFile; fileRef = 3B81BCBD2B62312C0067E9CB /* Product.storekit */; };
3B81BCC02B6231680067E9CB /* ProductList.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3B81BCBF2B6231680067E9CB /* ProductList.plist */; };
3BA7C0852B81D6C300654B62 /* ChangeBookMarkModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BA7C0842B81D6C300654B62 /* ChangeBookMarkModel.swift */; };
3BA7C0872B81D73F00654B62 /* ChangeBookMarkViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BA7C0862B81D73F00654B62 /* ChangeBookMarkViewModel.swift */; };
3BA7C0892B81DA7400654B62 /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = 3BA7C0882B81DA7400654B62 /* GoogleService-Info.plist */; };
3BD26D062B62342C0035FE80 /* ChargerInfoView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 10C8AC642B5EA69B00B40547 /* ChargerInfoView.swift */; };
3BFC8D152B54AA55000D6006 /* EditPostView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BFC8D142B54AA55000D6006 /* EditPostView.swift */; };
3BFC8D1B2B54D46D000D6006 /* MultiImagePicker.swift in Sources */ = {isa = PBXBuildFile; fileRef = 3BFC8D1A2B54D46D000D6006 /* MultiImagePicker.swift */; };
Expand Down Expand Up @@ -124,7 +127,6 @@
DF5B7E922B7FA430001C009C /* PortfolioOfferViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF5B7E912B7FA430001C009C /* PortfolioOfferViewModel.swift */; };
DF5B7E942B808DF1001C009C /* EditAnnouncementViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF5B7E932B808DF0001C009C /* EditAnnouncementViewModel.swift */; };
DF5B7E962B809257001C009C /* EditAnnouncementModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF5B7E952B809257001C009C /* EditAnnouncementModel.swift */; };
DF5B7EAA2B81036A001C009C /* GoogleService-Info.plist in Resources */ = {isa = PBXBuildFile; fileRef = DF5B7EA92B810369001C009C /* GoogleService-Info.plist */; };
DF692B182B7E3DA700BF9B75 /* RefreshTokenModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF692B172B7E3DA700BF9B75 /* RefreshTokenModel.swift */; };
DF692B1A2B7E970F00BF9B75 /* EditAnnouncementView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF692B192B7E970F00BF9B75 /* EditAnnouncementView.swift */; };
DF692B1C2B7EFC6F00BF9B75 /* ChangeAnnouncementStatusModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DF692B1B2B7EFC6F00BF9B75 /* ChangeAnnouncementStatusModel.swift */; };
Expand Down Expand Up @@ -196,6 +198,9 @@
3B81BCB92B622F280067E9CB /* MyNoticeDetailView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MyNoticeDetailView.swift; sourceTree = "<group>"; };
3B81BCBD2B62312C0067E9CB /* Product.storekit */ = {isa = PBXFileReference; lastKnownFileType = text; path = Product.storekit; sourceTree = "<group>"; };
3B81BCBF2B6231680067E9CB /* ProductList.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = ProductList.plist; sourceTree = "<group>"; };
3BA7C0842B81D6C300654B62 /* ChangeBookMarkModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChangeBookMarkModel.swift; sourceTree = "<group>"; };
3BA7C0862B81D73F00654B62 /* ChangeBookMarkViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChangeBookMarkViewModel.swift; sourceTree = "<group>"; };
3BA7C0882B81DA7400654B62 /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; name = "GoogleService-Info.plist"; path = "../../../../Downloads/GoogleService-Info.plist"; sourceTree = "<group>"; };
3BFC8D142B54AA55000D6006 /* EditPostView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditPostView.swift; sourceTree = "<group>"; };
3BFC8D1A2B54D46D000D6006 /* MultiImagePicker.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MultiImagePicker.swift; sourceTree = "<group>"; };
3BFC8D1E2B5597C9000D6006 /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -226,7 +231,6 @@
DF5B7E912B7FA430001C009C /* PortfolioOfferViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PortfolioOfferViewModel.swift; sourceTree = "<group>"; };
DF5B7E932B808DF0001C009C /* EditAnnouncementViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAnnouncementViewModel.swift; sourceTree = "<group>"; };
DF5B7E952B809257001C009C /* EditAnnouncementModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAnnouncementModel.swift; sourceTree = "<group>"; };
DF5B7EA92B810369001C009C /* GoogleService-Info.plist */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text.plist.xml; path = "GoogleService-Info.plist"; sourceTree = "<group>"; };
DF692B172B7E3DA700BF9B75 /* RefreshTokenModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RefreshTokenModel.swift; sourceTree = "<group>"; };
DF692B192B7E970F00BF9B75 /* EditAnnouncementView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EditAnnouncementView.swift; sourceTree = "<group>"; };
DF692B1B2B7EFC6F00BF9B75 /* ChangeAnnouncementStatusModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChangeAnnouncementStatusModel.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -364,7 +368,7 @@
100D387F2B44826500498977 /* Spon-us */ = {
isa = PBXGroup;
children = (
DF5B7EA92B810369001C009C /* GoogleService-Info.plist */,
3BA7C0882B81DA7400654B62 /* GoogleService-Info.plist */,
80B41FFF2B77F0DF0021DD90 /* Spon-us.entitlements */,
100D389F2B44833F00498977 /* Info.plist */,
3B36F09F2B6FEBCB0000ACFB /* PortOne */,
Expand Down Expand Up @@ -580,6 +584,8 @@
children = (
806AF8692B79DBD0008F7F62 /* CategoryModel.swift */,
806AF86B2B79EB3F008F7F62 /* CategoryModelData.swift */,
3BA7C0842B81D6C300654B62 /* ChangeBookMarkModel.swift */,
3BA7C0862B81D73F00654B62 /* ChangeBookMarkViewModel.swift */,
);
path = Category;
sourceTree = "<group>";
Expand Down Expand Up @@ -740,7 +746,7 @@
files = (
3B81BCC02B6231680067E9CB /* ProductList.plist in Resources */,
100D389C2B44833500498977 /* SUIT-Medium.otf in Resources */,
DF5B7EAA2B81036A001C009C /* GoogleService-Info.plist in Resources */,
3BA7C0892B81DA7400654B62 /* GoogleService-Info.plist in Resources */,
3B81BCBE2B62312C0067E9CB /* Product.storekit in Resources */,
100D38A32B44834700498977 /* Preview Assets.xcassets in Resources */,
100D389E2B44833500498977 /* Pretendard-SemiBold.otf in Resources */,
Expand All @@ -765,6 +771,7 @@
10EBD8132B6FF2350082CD8E /* ImagePicker.swift in Sources */,
DF91F4042B4864330021291A /* Portfolio.swift in Sources */,
DF3AD3E32B621FFA00DBFCCC /* SearchPostView.swift in Sources */,
3BA7C0872B81D73F00654B62 /* ChangeBookMarkViewModel.swift in Sources */,
10025B3D2B7BA14900DCCC5A /* ProposalDetailViewModel.swift in Sources */,
3BFC8D152B54AA55000D6006 /* EditPostView.swift in Sources */,
10025B3B2B7BA13D00DCCC5A /* ProposalDetailModel.swift in Sources */,
Expand Down Expand Up @@ -823,6 +830,7 @@
3B36F09A2B6FEB800000ACFB /* PaymentSettingView.swift in Sources */,
DF498F2C2B7CDDB200ADE078 /* MyAnnouncementsViewModel.swift in Sources */,
8042C3932B80BDFC003DE096 /* SearchAnnouncementViewModel.swift in Sources */,
3BA7C0852B81D6C300654B62 /* ChangeBookMarkModel.swift in Sources */,
10025B412B7C024200DCCC5A /* OrganizationViewModel.swift in Sources */,
DFCD24822B5A67FC00B00515 /* DocumentPicker.swift in Sources */,
DFB22ED72B766FF3007903DF /* JoinModel.swift in Sources */,
Expand Down
30 changes: 0 additions & 30 deletions Spon-us/GoogleService-Info.plist

This file was deleted.

21 changes: 21 additions & 0 deletions Spon-us/Model/Category/ChangeBookMarkModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
//
// ChangeBookMarkModel.swift
// Spon-us
//
// Created by 황인성 on 2/18/24.
//

import Foundation

// MARK: - ChangeBookMarkModel
struct ChangeBookMarkResponse: Codable {
let statusCode, message: String
let content: ChangeBookMarkResponseDetail
}

// MARK: - Content
struct ChangeBookMarkResponseDetail: Codable {
let id, organizationID, announcementID: Int
let bookmarked: Bool

}
45 changes: 45 additions & 0 deletions Spon-us/Model/Category/ChangeBookMarkViewModel.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//
// ChangeBookMarkViewModel.swift
// Spon-us
//
// Created by 황인성 on 2/18/24.
//

import Foundation
import Moya
import SwiftUI
import Alamofire

class ChangeBookMarkViewModel: ObservableObject {
@Published var changeBookmark: ChangeBookMarkResponseDetail?

private let provider = MoyaProvider<SponusAPI>(session: Session(interceptor: AuthInterceptor.shared))


func fetchBookMark(announcementID: Int) {
// JSON 데이터 생성
let json: [String: Any] = [
"announcementId": announcementID
]

// HTTP 요청에 필요한 헤더 생성
let headers: HTTPHeaders = [
"Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"
]

// 요청 보낼 URL
let url = "http://3.38.233.66:8080/api/v1/me/announcements/bookmarked"

AF.request(url, method: .post, parameters: json, encoding: JSONEncoding.default, headers: headers)
.responseJSON { response in
switch response.result {
case .success(let value):
print("Request succeeded with response: \(value)")
// Process response data
case .failure(let error):
print("Request failed with error: \(error)")
// Error handling
}
}
}
}
11 changes: 11 additions & 0 deletions Spon-us/SponusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ enum SponusAPI {
//MARK: 검색
case searchOrganization(keyword: String)
case searchAnnouncement(keyword: String)
case postChangeBookMark(announcementId: Int)
}

extension SponusAPI: TargetType {
Expand Down Expand Up @@ -85,6 +86,8 @@ extension SponusAPI: TargetType {
return "/api/v1/organizations"
case .searchAnnouncement(keyword: let keyword):
return "/api/v1/announcements"
case .postChangeBookMark(announcementId: let announcementId):
return "/api/v1/me/announcements/bookmarked"
}
}

Expand Down Expand Up @@ -133,6 +136,8 @@ extension SponusAPI: TargetType {
return .get
case .searchAnnouncement(keyword: let keyword):
return .get
case .postChangeBookMark(announcementId: let announcementId):
return .post
}
}

Expand Down Expand Up @@ -211,6 +216,8 @@ extension SponusAPI: TargetType {
return Data()
case .searchAnnouncement(keyword: let keyword):
return Data()
case .postChangeBookMark(announcementId: let announcementId):
return Data()
}
}

Expand Down Expand Up @@ -328,6 +335,8 @@ extension SponusAPI: TargetType {
return .requestParameters(parameters: ["search": keyword], encoding: URLEncoding.queryString)
case .searchAnnouncement(keyword: let keyword):
return .requestParameters(parameters: ["search": keyword], encoding: URLEncoding.queryString)
case .postChangeBookMark(announcementId: let announcementId):
return .requestParameters(parameters: ["announcementId": announcementId], encoding: URLEncoding.queryString)
}
}

Expand Down Expand Up @@ -380,6 +389,8 @@ extension SponusAPI: TargetType {
return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"]
case .searchAnnouncement(keyword: let keyword):
return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"]
case .postChangeBookMark(announcementId: let announcementId):
return ["Authorization": "Bearer \(loadAccessToken(userID: UserDefaults.standard.string(forKey: "loginAccount") ?? "loadAccessToken Error"))"]
}
}
}
Expand Down
25 changes: 18 additions & 7 deletions Spon-us/View/Category/CategoryView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ struct CategoryView: View {
@State var selectedCategoryList = "전체"
@State var selectedCategoryDetailList = "전체"
@StateObject private var categoryModelData = CategoryModelData()
@StateObject var changeBookMarkViewModel = ChangeBookMarkViewModel()

private func updateCategoryModelData() {
let category = changeToEnglish(category: selectedCategoryList)
Expand Down Expand Up @@ -78,7 +79,9 @@ struct CategoryView: View {
VStack{
Spacer().frame(height: 30)
ForEach(categoryModelData.categoryModelDatas, id: \.id) { categoryContent in
CategoryCell(categoryContent: categoryContent)
CategoryCell(categoryContent: categoryContent, categoryModelData: categoryModelData, changeBookMarkViewModel: changeBookMarkViewModel,
announcementId: categoryContent.id
)
}
}
.padding(.horizontal, 20)
Expand Down Expand Up @@ -164,6 +167,9 @@ struct CategoryCell: View {

var categoryContent: CategoryContent
@State private var isBookmarked = false
@ObservedObject var categoryModelData: CategoryModelData
@ObservedObject var changeBookMarkViewModel: ChangeBookMarkViewModel
var announcementId: Int

var body: some View {
NavigationLink{SearchPostView(announcementId: categoryContent.id, selectedSaveButton: $isBookmarked)} label: {
Expand Down Expand Up @@ -203,9 +209,12 @@ struct CategoryCell: View {
Spacer()
}
}.frame(width: 160, height: 95).padding(.trailing, 36)
Button(action: {toggleBookmark()
print(categoryContent.id)}){
Image(isBookmarked ? "ic_saved_check" : "ic_saved")
Button(action: {
toggleBookmark()
print(categoryContent.id)
print(changeBookMarkViewModel.changeBookmark?.bookmarked ?? "qwe")
}){
Image(Bool(changeBookMarkViewModel.changeBookmark?.bookmarked ?? false) ? "ic_saved_check" : "ic_saved")
.frame(width: 28, height: 28)
}
}.padding(.bottom, 16)
Expand All @@ -217,14 +226,16 @@ struct CategoryCell: View {
isBookmarked.toggle()
if isBookmarked {
print("추가") //api
changeBookMarkViewModel.fetchBookMark(announcementID: categoryContent.id)
} else {
print("해제") //api
changeBookMarkViewModel.fetchBookMark(announcementID: categoryContent.id)
}
}
}


#Preview {
CategoryView()
}
//#Preview {
// CategoryView()
//}

0 comments on commit 8facb45

Please sign in to comment.