Skip to content

Commit

Permalink
Merge pull request #1 from MarcBiosca/load_animation
Browse files Browse the repository at this point in the history
Animation fix
  • Loading branch information
MarcBiosca authored Oct 6, 2022
2 parents 8a81534 + a9f6e71 commit e1c9be9
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 23 deletions.
66 changes: 50 additions & 16 deletions AsyncImageDemo/App/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import SwiftUI
import AsyncImage

struct ContentView: View {
@State private var showSlide = false

private let imageCache: ImageCache
private let publisherCache: PublisherCache
private let images: [ImageItem] = (1...100).map { _ in ImageItem() }
Expand All @@ -20,32 +22,64 @@ struct ContentView: View {

var body: some View {
NavigationView {
List {
ForEach(self.images) { item in
NavigationLink(
destination: DetailView(
imageCache: self.imageCache,
publisherCache: self.publisherCache,
item: item
)) {
HStack(alignment: .center) {
Spacer(minLength: 0)

ZStack {
List {
Button("Show slide") { self.showSlide.toggle() }

ForEach(self.images) { item in
NavigationLink(
destination: DetailView(
imageCache: self.imageCache,
publisherCache: self.publisherCache,
item: item
)) {
HStack(alignment: .center) {
Spacer(minLength: 0)

AsyncImage(
request: item.image.urlRequest,
imageCache: self.imageCache,
publisherCache: self.publisherCache
)
.frame(width: 200)
.cornerRadius(10)

Spacer(minLength: 0)
}
.frame(height: 300)
}
}
}

Group {
if self.showSlide {
VStack {
Spacer(minLength: 90)

HStack {
AsyncImage(
request: item.image.urlRequest,
request: self.images[self.images.count - 1].image.urlRequest,
imageCache: self.imageCache,
publisherCache: self.publisherCache
)
.frame(width: 200)
.cornerRadius(10)
.clipShape(Circle())

Spacer(minLength: 0)
AsyncImage(
request: self.images[self.images.count - 2].image.urlRequest,
imageCache: self.imageCache,
publisherCache: self.publisherCache
)
.frame(width: 200)
.clipShape(Circle())
}
.frame(height: 300)
}
}
}
.animation(.linear, value: self.showSlide)
.transition(.move(edge: .bottom))
.zIndex(99)
}
.padding(.horizontal)
.navigationBarTitle("AsyncImage", displayMode: .large)
}
}
Expand Down
19 changes: 12 additions & 7 deletions Sources/AsyncImage/Feature/AsyncImage/AsyncImage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,37 @@ public struct AsyncImage: View {
}

public var body: some View {
self.image
self.content
.onAppear(perform: self.viewModel.load)
.onDisappear(perform: self.viewModel.disappear)
}

private var image: some View {
VStack {
private var content: some View {
ZStack {
if self.viewModel.imageState.loading {
Rectangle()
.fill(Color(.tertiarySystemGroupedBackground))
.overlay(
Spinner(animating: true, style: .medium, color: UIColor.label)
)
}
else if let image = self.viewModel.imageState.image {
self.configuration(Image(uiImage: image))
}
else {
else if case .failure(_) = self.viewModel.imageState {
Rectangle()
.fill(Color(.tertiarySystemGroupedBackground))
.overlay(self.reloadView)
.onTapGesture(perform: self.viewModel.reload)
}

self.configuration(self.image)
}
}

private var image: Image {
guard let uiImage = self.viewModel.imageState.image else { return Image(uiImage: UIImage()) }

return Image(uiImage: uiImage)
}

private var reloadView: some View {
ZStack {
Image(systemName: "exclamationmark.circle.fill")
Expand Down

0 comments on commit e1c9be9

Please sign in to comment.