Skip to content

Commit

Permalink
Merge branch 'develop' into #53
Browse files Browse the repository at this point in the history
  • Loading branch information
Fiddich-Dev committed Jul 9, 2024
2 parents a331215 + 5cd261b commit 9336671
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 36 deletions.
25 changes: 13 additions & 12 deletions Spon-Us/Bookmark/BookmarkView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -163,20 +163,21 @@ struct BookmarkListCell: View {

Spacer()

Button(action: {
bookmarkViewModel.postBookmark(target: bookmarkListCellViewModel.target) { success in
if success {
print("\(bookmarkListCellViewModel.companyName) 북마크 상태 업데이트 성공")
fetchBookmarksForSelectedBookmark()
} else {
print("북마크 상태 업데이트 실패")
Image(.icBookmark)
.renderingMode(.template)
.foregroundStyle(
bookmarkListCellViewModel.isBookmarked ? Color.textDisabled : Color.textBrand
)
.onTapGesture {
bookmarkViewModel.toggleBookmark(target: bookmarkListCellViewModel.target) { completed in
if completed {
print("북마크 \(bookmarkListCellViewModel.companyName) \(bookmarkListCellViewModel.isBookmarked) 업데이트 성공")
withAnimation {
bookmarkListCellViewModel.isBookmarked.toggle()
}
}
}
}
}, label: {
Image(.icBookmark)
.renderingMode(.template)
.foregroundStyle(Color.textBrand)
})
}
.padding(.all, 20)
}
Expand Down
22 changes: 14 additions & 8 deletions Spon-Us/Bookmark/BookmarkViewModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ final class BookmarkListCellViewModel: Identifiable {
let target: Int
var companyName: String
var imageURL: String?

var isBookmarked: Bool = false

init(bookmarkModel: BookmarkModel) {
self.target = bookmarkModel.target
self.companyName = bookmarkModel.name
Expand Down Expand Up @@ -45,19 +46,24 @@ final class BookmarkListViewModel {
}
}

func postBookmark(target: Int, completion: @escaping (Bool) -> Void) {
provider.request(.postBookmark(target: target)) { response in
switch response {
func toggleBookmark(target: Int, completion: @escaping (Bool) -> Void) {
provider.request(.postBookmark(target: target)) { [weak self] result in
switch result {
case .success(let response):
do {
_ = try JSONDecoder().decode(postBookmarkResponseModel.self, from: response.data)
completion(true)
let body = try JSONDecoder().decode(BookmarkPostResponseModel.self, from: response.data)
if let index = self?.bookmarkList.firstIndex(where: { $0.target == target }) {
self?.bookmarkList[index].isBookmarked = body.content.bookmarked
completion(true)
} else {
completion(false)
}
} catch {
print("post bookmark decode error", error.localizedDescription)
print("postBookmark parse error", error.localizedDescription)
completion(false)
}
case .failure(let error):
print("postBookmark API error: \(error.localizedDescription)")
print("postBookmark API error", error.localizedDescription)
completion(false)
}
}
Expand Down
15 changes: 0 additions & 15 deletions Spon-Us/Network/Models/Bookmark.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,18 +43,3 @@ enum BookmarkTargetType: String, Codable {
case company = "COMPANY"
case club = "CLUB"
}

struct postBookmarkResponseModel: Codable {
let statusCode: String
let message: String
let content: postBookmarkModel
}

struct postBookmarkModel: Codable {
let id: Int
let organizationId: Int
let target: Int
let targetType: String
let createdAt: String
let bookmarked: Bool
}
2 changes: 1 addition & 1 deletion Spon-Us/Network/SponusAPI.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ extension SponusAPI: TargetType {
return "/api/v2/companies/\(companyId)"
case let .getClub(clubId):
return "/api/v2/clubs/\(clubId)"
case .getBookmark, .postBookmark:
case .getBookmark:
return "/api/v2/organizations/bookmarked"
case .postBookmark:
return "/api/v2/organizations/bookmarked"
Expand Down

0 comments on commit 9336671

Please sign in to comment.