-
Notifications
You must be signed in to change notification settings - Fork 72
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #42 from ginsudev/feature/font-discovery-page
Added font discovery page
- Loading branch information
Showing
8 changed files
with
308 additions
and
5 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 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
58 changes: 58 additions & 0 deletions
58
WDBFontOverwrite/MainInterface/FontDiscovery/FontDiscoveryCard.ViewModel.swift
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,58 @@ | ||
// | ||
// FontDiscoveryCell.ViewModel.swift | ||
// WDBFontOverwrite | ||
// | ||
// Created by Noah Little on 5/1/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
extension FontDiscoveryCard { | ||
struct ViewDescriptor { | ||
let avatarPath: String | ||
let name: String | ||
let twitterHandle: String | ||
let role: CustomFontType | ||
let links: [FontDiscoveryLinkDescriptor] | ||
} | ||
|
||
struct AvatarDownloader { | ||
func downloadAvatar(fromURL url: String) async -> UIImage? { | ||
guard let url = URL(string: url) else { | ||
return nil | ||
} | ||
do { | ||
let (data, response) = try await URLSession.shared.data(from: url) | ||
let image = handleResponse(data: data, response: response) | ||
return image | ||
} catch { | ||
print("Unable to load avatar for card") | ||
return nil | ||
} | ||
} | ||
|
||
private func handleResponse(data: Data?, response: URLResponse?) -> UIImage? { | ||
guard let data, | ||
let image = UIImage(data: data), | ||
let response = response as? HTTPURLResponse, | ||
(200..<300).contains(response.statusCode) else { | ||
print("No image") | ||
return nil | ||
} | ||
return image | ||
} | ||
} | ||
|
||
final class ViewModel: ObservableObject { | ||
let downloader = AvatarDownloader() | ||
@Published var image = Image(systemName: "person.crop.circle") | ||
|
||
func fetchImage(fromURL url: String) async { | ||
if let image = await self.downloader.downloadAvatar(fromURL: url) { | ||
await MainActor.run { [weak self] in | ||
self?.image = Image(uiImage: image) | ||
} | ||
} | ||
} | ||
} | ||
} |
81 changes: 81 additions & 0 deletions
81
WDBFontOverwrite/MainInterface/FontDiscovery/FontDiscoveryCard.swift
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,81 @@ | ||
// | ||
// FontDiscoveryCell.swift | ||
// WDBFontOverwrite | ||
// | ||
// Created by Noah Little on 5/1/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
// MARK: - Public | ||
|
||
struct FontDiscoveryCard: View { | ||
@Environment(\.openURL) private var openURL | ||
@StateObject private var viewModel = ViewModel() | ||
let descriptor: ViewDescriptor | ||
|
||
var body: some View { | ||
VStack(alignment: .leading) { | ||
header | ||
links | ||
} | ||
.frame(maxWidth: .infinity) | ||
.padding() | ||
.overlay( | ||
RoundedRectangle(cornerRadius: 5) | ||
.stroke(Color.secondary, lineWidth: 1) | ||
) | ||
.onAppear { | ||
Task { | ||
await viewModel.fetchImage(fromURL: descriptor.avatarPath) | ||
} | ||
} | ||
} | ||
} | ||
|
||
// MARK: - Private | ||
|
||
private extension FontDiscoveryCard { | ||
var header: some View { | ||
HStack { | ||
viewModel.image | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.frame(width: 60, height: 60) | ||
.clipShape(Circle()) | ||
.overlay( | ||
Circle() | ||
.stroke(Color.secondary, lineWidth: 1) | ||
.foregroundColor(.clear) | ||
) | ||
VStack(alignment: .leading) { | ||
Text(descriptor.name) | ||
.font(.title2) | ||
Text(descriptor.role.rawValue.uppercased()) | ||
.foregroundColor(.secondary) | ||
.font(.footnote) | ||
.bold() | ||
} | ||
Spacer() | ||
} | ||
} | ||
|
||
var links: some View { | ||
HStack { | ||
ForEach(descriptor.links, id: \.title) { link in | ||
Button { | ||
openURL(link.url) | ||
} label: { | ||
HStack(spacing: 5) { | ||
Image(systemName: link.imageName) | ||
Text(link.title) | ||
} | ||
.foregroundColor(.white) | ||
} | ||
.padding(EdgeInsets(top: 5, leading: 10, bottom: 5, trailing: 10)) | ||
.background(Color(UIColor(red: 0.44, green: 0.69, blue: 0.67, alpha: 1.00))) | ||
.clipShape(RoundedRectangle(cornerRadius: 8)) | ||
} | ||
} | ||
} | ||
} |
70 changes: 70 additions & 0 deletions
70
WDBFontOverwrite/MainInterface/FontDiscovery/FontDiscoveryView.ViewModel.swift
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,70 @@ | ||
// | ||
// FontDiscoveryView.ViewModel.swift | ||
// WDBFontOverwrite | ||
// | ||
// Created by Noah Little on 5/1/2023. | ||
// | ||
|
||
import Foundation | ||
|
||
struct FontDiscoveryLinkDescriptor { | ||
let url: URL | ||
let title: String | ||
let imageName: String | ||
} | ||
|
||
extension FontDiscoveryView { | ||
final class ViewModel: ObservableObject { | ||
let descriptors = [ | ||
FontDiscoveryCard.ViewDescriptor( | ||
avatarPath: "https://unavatar.io/github/evynw", | ||
name: "Evelyn", | ||
twitterHandle: "ev_ynw", | ||
role: .font, | ||
links: [ | ||
.init( | ||
url: URL(string: "https://twitter.com/ev_ynw")!, | ||
title: "Twitter", | ||
imageName: "person.crop.circle.badge.plus" | ||
), | ||
.init( | ||
url: URL(string: "https://bit.ly/3e3nJdm")!, | ||
title: "Font library", | ||
imageName: "books.vertical" | ||
) | ||
] | ||
), | ||
FontDiscoveryCard.ViewDescriptor( | ||
avatarPath: "https://unavatar.io/github/PoomSmart", | ||
name: "PoomSmart", | ||
twitterHandle: "PoomSmart", | ||
role: .emoji, | ||
links: [ | ||
.init( | ||
url: URL(string: "https://twitter.com/PoomSmart")!, | ||
title: "Twitter", | ||
imageName: "person.crop.circle.badge.plus" | ||
), | ||
.init( | ||
url: URL(string: "https://github.com/PoomSmart/EmojiFonts/releases")!, | ||
title: "Emoji library", | ||
imageName: "face.smiling" | ||
) | ||
] | ||
), | ||
FontDiscoveryCard.ViewDescriptor( | ||
avatarPath: "https://unavatar.io/github/AlexMan1979", | ||
name: "AlexMan1979", | ||
twitterHandle: "AlexMan1979", | ||
role: .font, | ||
links: [ | ||
.init( | ||
url: URL(string: "https://twitter.com/AlexMan1979")!, | ||
title: "Twitter", | ||
imageName: "person.crop.circle.badge.plus" | ||
) | ||
] | ||
), | ||
] | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
WDBFontOverwrite/MainInterface/FontDiscovery/FontDiscoveryView.swift
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,56 @@ | ||
// | ||
// FontDiscoveryView.swift | ||
// WDBFontOverwrite | ||
// | ||
// Created by Noah Little on 5/1/2023. | ||
// | ||
|
||
import SwiftUI | ||
|
||
struct FontDiscoveryView: View { | ||
@StateObject private var viewModel = ViewModel() | ||
|
||
var body: some View { | ||
NavigationView { | ||
VStack(spacing: 10) { | ||
message | ||
ForEach(viewModel.descriptors, id: \.name) { descriptor in | ||
Section { | ||
FontDiscoveryCard(descriptor: descriptor) | ||
.frame(maxWidth: .infinity) | ||
.padding(.horizontal) | ||
} | ||
} | ||
.navigationTitle("Discovery") | ||
Spacer() | ||
} | ||
} | ||
} | ||
|
||
private var message: some View { | ||
ZStack { | ||
RoundedRectangle(cornerRadius: 5) | ||
.fill(Color(UIColor(red: 0.44, green: 0.69, blue: 0.67, alpha: 1.00))) | ||
VStack(alignment: .center) { | ||
Image(systemName: "star.fill") | ||
.resizable() | ||
.aspectRatio(contentMode: .fit) | ||
.frame(width: 50, height: 50) | ||
.foregroundColor(.white) | ||
Text("Find fonts and emojis from these talented developers and themers.") | ||
.foregroundColor(.white) | ||
.multilineTextAlignment(.center) | ||
} | ||
.padding() | ||
} | ||
.frame(maxWidth: .infinity) | ||
.fixedSize(horizontal: false, vertical: true) | ||
.padding(.horizontal) | ||
} | ||
} | ||
|
||
struct FontDiscoveryView_Previews: PreviewProvider { | ||
static var previews: some View { | ||
FontDiscoveryView() | ||
} | ||
} |
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