-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Leif/p 12 allow customized content (#18)
* P-12: Create file for #9 * P-12: Create file for #8 * P-12: Add PictureViewModel and ability to customize * P-12: Fix indentation
- Loading branch information
Showing
6 changed files
with
320 additions
and
12 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
import SwiftUI | ||
|
||
// TODO: [P-9](https://github.com/0xOpenBytes/Picture/issues/9) | ||
public struct MultipleImageView: View { | ||
let images: [Image] | ||
|
||
public var body: some View { | ||
switch images.count { | ||
case 0: placeholderView | ||
case 1: SingleImageView(image: images[0]) | ||
case 2: doubleImageView | ||
case 3: tripleImageView | ||
default: fallbackImageView | ||
|
||
} | ||
} | ||
|
||
private var placeholderView: some View { | ||
Image(systemName: "photo.artframe") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
|
||
private var doubleImageView: some View { | ||
VStack { | ||
images[0] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
images[1] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
} | ||
|
||
private var tripleImageView: some View { | ||
HStack { | ||
images[0] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
VStack { | ||
images[1] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
|
||
images[2] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
} | ||
} | ||
|
||
private var fallbackImageView: some View { | ||
Grid { | ||
GridRow { | ||
images[0] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
images[1] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
GridRow { | ||
images[2] | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
Image(systemName: "ellipsis") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct MultipleImageView_Preview: PreviewProvider { | ||
static var previews: some View { | ||
MultipleImageView( | ||
images: [ | ||
Image(systemName: "photo.artframe") | ||
] | ||
) | ||
|
||
MultipleImageView( | ||
images: [ | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe") | ||
] | ||
) | ||
|
||
MultipleImageView( | ||
images: [ | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe") | ||
] | ||
) | ||
|
||
MultipleImageView( | ||
images: [ | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe"), | ||
Image(systemName: "photo.artframe") | ||
] | ||
) | ||
} | ||
} |
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,40 @@ | ||
import OSLog | ||
import SwiftUI | ||
|
||
class PictureViewModel: ObservableObject { | ||
private let logger = Logger(subsystem: "PictureViewModel", category: "VM") | ||
private var task: Task<Void, Never>? | ||
|
||
let sources: [PictureSource] | ||
|
||
@Published var images: [Image] | ||
|
||
init(sources: [PictureSource]) { | ||
self.sources = sources | ||
self.task = nil | ||
self.images = [] | ||
} | ||
|
||
deinit { | ||
task?.cancel() | ||
} | ||
|
||
@MainActor | ||
func load() { | ||
task?.cancel() | ||
|
||
task = Task { | ||
for source in sources.prefix(5) { | ||
do { | ||
if let loadedImage = try await source.load() { | ||
DispatchQueue.main.async { [weak self] in | ||
self?.images.append(loadedImage) | ||
} | ||
} | ||
} catch { | ||
logger.error("\(error.localizedDescription)") | ||
} | ||
} | ||
} | ||
} | ||
} |
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,16 @@ | ||
import SwiftUI | ||
|
||
// TODO: [P-8](https://github.com/0xOpenBytes/Picture/issues/8) | ||
public struct SingleImageView: View { | ||
let image: Image | ||
|
||
public var body: some View { | ||
image | ||
} | ||
} | ||
|
||
struct SingleImageView_Preview: PreviewProvider { | ||
static var previews: some View { | ||
SingleImageView(image: Image(systemName: "photo.artframe")) | ||
} | ||
} |
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 |
---|---|---|
@@ -1,19 +1,103 @@ | ||
import SwiftUI | ||
|
||
extension Picture { | ||
// MARK: - Default Implementation | ||
|
||
extension Picture | ||
where SingleImageContent == SingleImageView, | ||
MultipleImageContent == MultipleImageView | ||
{ | ||
|
||
public init( | ||
sources: [PictureSource] | ||
) { | ||
self.init( | ||
sources: sources, | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
|
||
public init(image: Image) { | ||
self.init(sources: [.local(image)]) | ||
self.init( | ||
sources: [.local(image)], | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
|
||
public init(url: URL) { | ||
self.init(sources: [.remote(url)]) | ||
self.init( | ||
sources: [.remote(url)], | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
|
||
public init(images: [Image]) { | ||
self.init(sources: images.map(PictureSource.local)) | ||
self.init( | ||
sources: images.map(PictureSource.local), | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
|
||
public init(urls: [URL]) { | ||
self.init(sources: urls.map(PictureSource.remote)) | ||
self.init( | ||
sources: urls.map(PictureSource.remote), | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
} | ||
|
||
// MARK: - Single Image Initialization | ||
|
||
extension Picture where MultipleImageContent == MultipleImageView { | ||
public init( | ||
image: Image, | ||
@ViewBuilder singleImageContent: @escaping (Image) -> SingleImageContent | ||
) { | ||
self.init( | ||
sources: [.local(image)], | ||
singleImageContent: singleImageContent, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
|
||
public init( | ||
url: URL, | ||
@ViewBuilder singleImageContent: @escaping (Image) -> SingleImageContent | ||
) { | ||
self.init( | ||
sources: [.remote(url)], | ||
singleImageContent: singleImageContent, | ||
multipleImageContent: MultipleImageContent.init | ||
) | ||
} | ||
} | ||
|
||
// MARK: - Multiple Image Initialization | ||
|
||
extension Picture where SingleImageContent == SingleImageView { | ||
public init( | ||
images: [Image], | ||
@ViewBuilder multipleImageContent: @escaping ([Image]) -> MultipleImageContent | ||
) { | ||
self.init( | ||
sources: images.map(PictureSource.local), | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: multipleImageContent | ||
) | ||
} | ||
|
||
public init( | ||
urls: [URL], | ||
@ViewBuilder multipleImageContent: @escaping ([Image]) -> MultipleImageContent | ||
) { | ||
self.init( | ||
sources: urls.map(PictureSource.remote), | ||
singleImageContent: SingleImageView.init, | ||
multipleImageContent: multipleImageContent | ||
) | ||
} | ||
} |
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 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,32 @@ | ||
import SwiftUI | ||
|
||
public struct PictureSourceImage: View { | ||
@ObservedObject private var viewModel: InteractableImageViewModel | ||
|
||
public init( | ||
source: PictureSource | ||
) { | ||
self._viewModel = ObservedObject( | ||
initialValue: InteractableImageViewModel(source: source) | ||
) | ||
} | ||
|
||
public var body: some View { | ||
if let image = viewModel.image { | ||
image | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
} else { | ||
ProgressView() | ||
.onAppear { | ||
viewModel.load() | ||
} | ||
} | ||
} | ||
} | ||
|
||
struct PictureSourceImage_Preview: PreviewProvider { | ||
static var previews: some View { | ||
PictureSourceImage(source: .local(Image(systemName: "photo.artframe"))) | ||
} | ||
} |