Skip to content

Commit

Permalink
[#30] - Remove Kingfisher
Browse files Browse the repository at this point in the history
  • Loading branch information
Brent Mifsud committed Sep 15, 2021
1 parent 213f6fb commit 6e6fc15
Show file tree
Hide file tree
Showing 6 changed files with 163 additions and 224 deletions.
12 changes: 4 additions & 8 deletions Shared/View/History/HistoryRowView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,10 @@ struct HistoryRowView: View {

@ViewBuilder private var memeRow: some View {
HStack {
AsyncImage(url: meme.url) {
Image(Asset.Images.memeNotFound.name)
.resizable()
.aspectRatio(contentMode: .fill)
}
.aspectRatio(contentMode: .fill)
.frame(maxWidth: 60, maxHeight: 60)
.clipShape(RoundedRectangle(cornerRadius: .smallRadius))
MemeImage(url: meme.url)
.aspectRatio(contentMode: .fill)
.frame(maxWidth: 60, maxHeight: 60)
.clipShape(RoundedRectangle(cornerRadius: .smallRadius))

VStack(alignment: .leading, spacing: .margin) {

Expand Down
68 changes: 32 additions & 36 deletions Shared/View/History/MemeDetails.swift
Original file line number Diff line number Diff line change
Expand Up @@ -42,43 +42,39 @@ struct MemeDetails: View {
.edgesIgnoringSafeArea(.all)

VStack(spacing: .margin) {
AsyncImage(url: meme.url) {
Image("meme-not-found")
.resizable()
.aspectRatio(contentMode: .fit)
}
.aspectRatio(contentMode: .fit)
.cornerRadius(.cornerRadius)
.shadow(radius: .smallRadius)
.contextMenu(
menuItems: {
#if os(iOS) || os(watchOS) || os(tvOS)
Button {
showShareSheet = true
} label: {
Label("Share", systemImage: "square.and.arrow.up")
}

Button {
let pasteboard = UIPasteboard.general
pasteboard.url = meme.url
} label: {
Image(systemName: "doc.on.doc")
Text("Copy")
}
#elseif os(macOS)
ShareMenu(sharedItems: [meme.url], showText: true)
#endif

Button {
openMemeInBrowser()
} label: {
Image(systemName: "safari")
Text("Open in Browser")
MemeImage(url: meme.url)
.scaledToFit()
.cornerRadius(.cornerRadius)
.shadow(radius: .smallRadius)
.contextMenu(
menuItems: {
#if os(iOS) || os(watchOS) || os(tvOS)
Button {
showShareSheet = true
} label: {
Label("Share", systemImage: "square.and.arrow.up")
}

Button {
let pasteboard = UIPasteboard.general
pasteboard.url = meme.url
} label: {
Image(systemName: "doc.on.doc")
Text("Copy")
}
#elseif os(macOS)
ShareMenu(sharedItems: [meme.url], showText: true)
#endif

Button {
openMemeInBrowser()
} label: {
Image(systemName: "safari")
Text("Open in Browser")
}
}
}
)
.contentShape(RoundedRectangle(cornerRadius: .smallRadius))
)
.contentShape(RoundedRectangle(cornerRadius: .smallRadius))

detailsView
.background(Color.systemBackground)
Expand Down
82 changes: 39 additions & 43 deletions Shared/View/Home/MemeCardView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,51 +28,47 @@ struct MemeCardView: View {
.frame(width: 0, height: 0)
.hidden()

AsyncImage(url: meme.url) {
Image(Asset.Images.memeNotFound.name)
.resizable()
.aspectRatio(contentMode: .fit)
}
.aspectRatio(contentMode: .fit)
.cornerRadius(.cornerRadius)
.padding(.margin)
.frame(
maxWidth: geometrySize.width,
maxHeight: geometrySize.height/2
)
.foregroundColor(.secondarySystemBackground)
.animation(.interactiveSpring())
.offset(x: self.translation.width, y: self.translation.height)
.rotationEffect(
.degrees(Double(self.translation.width / geometrySize.width) * 25),
anchor: .bottom
)
.gesture(
DragGesture()
.onChanged { value in
self.translation = value.translation
self.swipingAction = value.translation.swipeDirection.asMemeAction
}
.onEnded { value in
/// remove overlay
self.swipingAction = nil
MemeImage(url: meme.url)
.aspectRatio(contentMode: .fit)
.cornerRadius(.cornerRadius)
.padding(.margin)
.frame(
maxWidth: geometrySize.width,
maxHeight: geometrySize.height/2
)
.foregroundColor(.secondarySystemBackground)
.animation(.interactiveSpring(), value: translation)
.offset(x: self.translation.width, y: self.translation.height)
.rotationEffect(
.degrees(Double(self.translation.width / geometrySize.width) * 25),
anchor: .bottom
)
.gesture(
DragGesture()
.onChanged { value in
self.translation = value.translation
self.swipingAction = value.translation.swipeDirection.asMemeAction
}
.onEnded { value in
/// remove overlay
self.swipingAction = nil

/// complete swipe
switch value.translation.swipeDirection {
case .up:
self.swipe(.skip)
case .down:
self.translation = .zero
case .left:
self.swipe(.dislike)
case .right:
self.swipe(.like)
/// complete swipe
switch value.translation.swipeDirection {
case .up:
self.swipe(.skip)
case .down:
self.translation = .zero
case .left:
self.swipe(.dislike)
case .right:
self.swipe(.like)
}
}
}
)
.onTapGesture {
showMeme = true
}
)
.onTapGesture {
showMeme = true
}
}
}

Expand Down
91 changes: 0 additions & 91 deletions Shared/View/Image/AsyncImage.swift

This file was deleted.

73 changes: 73 additions & 0 deletions Shared/View/Image/MemeImage.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// MemeImage.swift
// MemeImage
//
// Created by Brent Mifsud on 2021-09-14.
//

import SwiftUI
import os

struct MemeImage: View {
private class MemeCache {
private lazy var cache = [URL: Image]()

func set(image: Image, for url: URL) {
cache[url] = image
}

func getImage(for url: URL) -> Image? {
cache[url]
}

func clearCache() {
cache = [:]
}
}

private static let cache = MemeCache()

var url: URL?

var body: some View {
if let url = url,
let image = Self.cache.getImage(for: url) {
image.resizable()
} else {
AsyncImage(
url: url,
scale: 1.0,
transaction: Transaction(animation: .easeOut(duration: 0.2))
) { phase in
switch phase {
case .empty:
Color.secondary
.overlay(ProgressView().progressViewStyle(.circular))
case let .success(image):
cacheAndReturnImage(image: image)
.resizable()
.transition(.opacity)
case .failure:
Image("meme-not-found")
.resizable()
.transition(.opacity)
@unknown default:
Image("meme-not-found")
.resizable()
}
}
}
}

private func cacheAndReturnImage(image: Image) -> Image {
guard let url = url else { return image }
Self.cache.set(image: image, for: url)
return image
}
}

struct MemeImage_Previews: PreviewProvider {
static var previews: some View {
MemeImage()
}
}
Loading

0 comments on commit 6e6fc15

Please sign in to comment.