-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
84c6d44
commit 2f44e26
Showing
12 changed files
with
322 additions
and
57 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
This file was deleted.
Oops, something went wrong.
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,48 @@ | ||
// | ||
// AsyncImageView.swift | ||
// Spon-us | ||
// | ||
// Created by 김수민 on 2/17/24. | ||
// | ||
|
||
import SwiftUI | ||
import Combine | ||
|
||
class ImageLoader: ObservableObject { | ||
@Published var image: UIImage? | ||
private var cancellable: AnyCancellable? | ||
|
||
func load(fromURL url: URL) { | ||
cancellable = URLSession.shared.dataTaskPublisher(for: url) | ||
.map { UIImage(data: $0.data) } | ||
.replaceError(with: nil) | ||
.receive(on: DispatchQueue.main) | ||
.sink { [weak self] in self?.image = $0 } | ||
} | ||
|
||
deinit { | ||
cancellable?.cancel() | ||
} | ||
} | ||
|
||
struct AsyncImageView: View { | ||
@StateObject private var loader = ImageLoader() | ||
let url: URL? | ||
|
||
var body: some View { | ||
Group { | ||
if let image = loader.image { | ||
Image(uiImage: image) | ||
.resizable() | ||
.scaledToFit() | ||
} else { | ||
ProgressView() | ||
} | ||
} | ||
.onAppear { | ||
if let url = url { | ||
loader.load(fromURL: url) | ||
} | ||
} | ||
} | ||
} |
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,51 @@ | ||
// | ||
// SearchAnnouncementModel.swift | ||
// Spon-us | ||
// | ||
// Created by 김수민 on 2/17/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SearchAnnouncementModel: Codable { | ||
let statusCode: String | ||
let message: String | ||
let content: [SearchAnnouncementContent] | ||
} | ||
|
||
struct SearchAnnouncementContent: Codable, Hashable, Equatable,Identifiable { | ||
static func == (lhs: SearchAnnouncementContent, rhs: SearchAnnouncementContent) -> Bool { | ||
return lhs.id == rhs.id | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
|
||
let id: Int | ||
let writerId: Int | ||
let writerName: String | ||
let title: String | ||
let type: String | ||
let category: String | ||
let mainImage: SearchAnnouncementMainImage | ||
let status: String | ||
let viewCount: Int | ||
let createdAt: String | ||
let updatedAt: String | ||
let saveCount: Int | ||
} | ||
|
||
struct SearchAnnouncementMainImage: Codable, Hashable, Equatable, Identifiable { | ||
static func == (lhs: SearchAnnouncementMainImage, rhs: SearchAnnouncementMainImage) -> Bool { | ||
return lhs.id == rhs.id | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
let id: Int | ||
let name: String | ||
let url: String | ||
} | ||
|
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,36 @@ | ||
// | ||
// SearchAnnouncementViewModel.swift | ||
// Spon-us | ||
// | ||
// Created by 김수민 on 2/17/24. | ||
// | ||
|
||
import Foundation | ||
import Moya | ||
|
||
class SearchAnnouncementViewModel: ObservableObject { | ||
@Published var searchAnnouncementResult: SearchAnnouncementModel? | ||
@Published var searchAnnouncementContents: [SearchAnnouncementContent] = [] | ||
@Published var isLoading = false | ||
let provider = MoyaProvider<SponusAPI>(plugins: [NetworkLoggerPlugin()]) | ||
|
||
func getSearchAnnouncement(keyword: String) { | ||
self.isLoading = true | ||
provider.request(.searchAnnouncement(keyword: keyword ?? "")){ result in | ||
switch result { | ||
case .success(let response): | ||
do { | ||
// 성공적으로 데이터를 받아온 경우, JSON 디코딩을 시도합니다. | ||
let searchResults = try JSONDecoder().decode(SearchAnnouncementModel.self, from: response.data) | ||
self.searchAnnouncementResult = searchResults | ||
self.searchAnnouncementContents = searchResults.content | ||
} catch let error { | ||
// 디코딩 과정에서 오류가 발생한 경우, 오류를 처리합니다. | ||
print("Error decoding data: \(error)") | ||
} | ||
case .failure(let error): | ||
print(error) | ||
} | ||
} | ||
} | ||
} |
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,41 @@ | ||
// | ||
// SearchOrganizationModel.swift | ||
// Spon-us | ||
// | ||
// Created by 김수민 on 2/17/24. | ||
// | ||
|
||
import Foundation | ||
|
||
struct SearchOrganizationModel: Codable { | ||
let statusCode: String | ||
let message: String | ||
let content: [SearchOrganizationContent] | ||
} | ||
|
||
struct SearchOrganizationContent: Codable, Hashable, Equatable,Identifiable { | ||
static func == (lhs: SearchOrganizationContent, rhs: SearchOrganizationContent) -> Bool { | ||
return lhs.id == rhs.id | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
|
||
let id: Int | ||
let name: String | ||
let image: String? | ||
let tags: [SearchOrganizationTags] | ||
} | ||
|
||
struct SearchOrganizationTags: Codable, Hashable, Equatable, Identifiable { | ||
static func == (lhs: SearchOrganizationTags, rhs: SearchOrganizationTags) -> Bool { | ||
return lhs.id == rhs.id | ||
} | ||
|
||
func hash(into hasher: inout Hasher) { | ||
hasher.combine(id) | ||
} | ||
let id: Int | ||
let name: String | ||
} |
Oops, something went wrong.