Created
August 31, 2022 10:27
-
-
Save erdemildiz/be933303752e47a77d7ca7ed1e9f8b57 to your computer and use it in GitHub Desktop.
PhotoListView
This file contains hidden or 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
struct PhotoListView: View { | |
// 1 | |
@StateObject var viewModel = PhotoListViewModel() | |
var body: some View { | |
NavigationView { | |
List(viewModel.photoItems) { photoItem in | |
// 2 | |
ListItem(photoItem: photoItem) | |
} | |
.navigationTitle("Photo List") | |
.task { | |
// 3 | |
await viewModel.fetchPhotoList() | |
} | |
} | |
} | |
} | |
// MARK: - List Item | |
extension PhotoListView { | |
struct ListItem: View { | |
let photoItem: PhotoItemModel | |
var body: some View { | |
HStack { | |
RoundedRectangle(cornerRadius: 12) | |
.stroke(Color.black.opacity(0.3)) | |
.frame(width: 50, height: 50) | |
.overlay( | |
ImagePresenter(imageURL: URL(string: photoItem.thumbnailUrl)) | |
.cornerRadius(12)) | |
Text(photoItem.title) | |
Spacer() | |
} | |
} | |
} | |
} | |
// MARK: - Image Presenter | |
extension PhotoListView { | |
struct ImagePresenter: View { | |
let imageURL: URL? | |
var body: some View { | |
AsyncImage(url: imageURL) { content in | |
content | |
.resizable() | |
.scaledToFill() | |
} placeholder: { | |
ProgressView() | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment