Skip to content

Commit

Permalink
Completed project.
Browse files Browse the repository at this point in the history
  • Loading branch information
scottandrew committed Dec 20, 2022
1 parent 5ca82fd commit ede7bec
Show file tree
Hide file tree
Showing 13 changed files with 411 additions and 20 deletions.
41 changes: 41 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Xcode
#
# gitignore contributors: remember to update Global/Xcode.gitignore, Objective-C.gitignore & Swift.gitignore

## Build generated
build/
DerivedData/

## Various settings
*.pbxuser
!default.pbxuser
*.mode1v3
!default.mode1v3
*.mode2v3
!default.mode2v3
*.perspectivev3
!default.perspectivev3
xcuserdata/

## Other
*.moved-aside
*.xccheckout
*.xcscmblueprint

## Obj-C/Swift specific
*.hmap
*.ipa
*.dSYM.zip
*.dSYM

## Playgrounds
timeline.xctimeline
playground.xcworkspace

# Swift Package Manager
#
# Add this line if you want to avoid checking in source code from Swift Package Manager dependencies.
# Packages/
# Package.pins
# Package.resolved
.build/
40 changes: 39 additions & 1 deletion GoogleFontPreview.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,13 @@
406D27FA29481A00002E3540 /* GoogleFontPreviewTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D27F929481A00002E3540 /* GoogleFontPreviewTests.swift */; };
406D280429481A00002E3540 /* GoogleFontPreviewUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D280329481A00002E3540 /* GoogleFontPreviewUITests.swift */; };
406D280629481A00002E3540 /* GoogleFontPreviewUITestsLaunchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D280529481A00002E3540 /* GoogleFontPreviewUITestsLaunchTests.swift */; };
406D2813294822CB002E3540 /* GoogleFont.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D2812294822CB002E3540 /* GoogleFont.swift */; };
406D2815294823C7002E3540 /* GoogleFontService.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D2814294823C7002E3540 /* GoogleFontService.swift */; };
406D28172948FA0D002E3540 /* DetailsView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D28162948FA0D002E3540 /* DetailsView.swift */; };
406D2819294911F2002E3540 /* Font+Register.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D2818294911F2002E3540 /* Font+Register.swift */; };
406D281B294913BD002E3540 /* StyleCell.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D281A294913BD002E3540 /* StyleCell.swift */; };
406D281D29492DF9002E3540 /* Style.swift in Sources */ = {isa = PBXBuildFile; fileRef = 406D281C29492DF9002E3540 /* Style.swift */; };
40FF8417294E95D70034CE00 /* FontList.swift in Sources */ = {isa = PBXBuildFile; fileRef = 40FF8416294E95D70034CE00 /* FontList.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand Down Expand Up @@ -45,6 +52,13 @@
406D27FF29481A00002E3540 /* GoogleFontPreviewUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = GoogleFontPreviewUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
406D280329481A00002E3540 /* GoogleFontPreviewUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleFontPreviewUITests.swift; sourceTree = "<group>"; };
406D280529481A00002E3540 /* GoogleFontPreviewUITestsLaunchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = GoogleFontPreviewUITestsLaunchTests.swift; sourceTree = "<group>"; };
406D2812294822CB002E3540 /* GoogleFont.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GoogleFont.swift; sourceTree = "<group>"; };
406D2814294823C7002E3540 /* GoogleFontService.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = GoogleFontService.swift; sourceTree = "<group>"; };
406D28162948FA0D002E3540 /* DetailsView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DetailsView.swift; sourceTree = "<group>"; };
406D2818294911F2002E3540 /* Font+Register.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Font+Register.swift"; sourceTree = "<group>"; };
406D281A294913BD002E3540 /* StyleCell.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = StyleCell.swift; sourceTree = "<group>"; };
406D281C29492DF9002E3540 /* Style.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Style.swift; sourceTree = "<group>"; };
40FF8416294E95D70034CE00 /* FontList.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FontList.swift; sourceTree = "<group>"; };
/* End PBXFileReference section */

/* Begin PBXFrameworksBuildPhase section */
Expand Down Expand Up @@ -95,11 +109,18 @@
406D27E6294819FF002E3540 /* GoogleFontPreview */ = {
isa = PBXGroup;
children = (
406D2814294823C7002E3540 /* GoogleFontService.swift */,
406D2812294822CB002E3540 /* GoogleFont.swift */,
406D27E7294819FF002E3540 /* GoogleFontPreviewApp.swift */,
406D27E9294819FF002E3540 /* ContentView.swift */,
406D27EB29481A00002E3540 /* Assets.xcassets */,
406D27F029481A00002E3540 /* GoogleFontPreview.entitlements */,
406D27ED29481A00002E3540 /* Preview Content */,
406D28162948FA0D002E3540 /* DetailsView.swift */,
406D2818294911F2002E3540 /* Font+Register.swift */,
406D281A294913BD002E3540 /* StyleCell.swift */,
406D281C29492DF9002E3540 /* Style.swift */,
40FF8416294E95D70034CE00 /* FontList.swift */,
);
path = GoogleFontPreview;
sourceTree = "<group>";
Expand Down Expand Up @@ -193,7 +214,7 @@
attributes = {
BuildIndependentTargetsInParallel = 1;
LastSwiftUpdateCheck = 1410;
LastUpgradeCheck = 1410;
LastUpgradeCheck = 1420;
TargetAttributes = {
406D27E3294819FF002E3540 = {
CreatedOnToolsVersion = 14.1;
Expand Down Expand Up @@ -259,8 +280,15 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
406D2819294911F2002E3540 /* Font+Register.swift in Sources */,
406D2813294822CB002E3540 /* GoogleFont.swift in Sources */,
406D28172948FA0D002E3540 /* DetailsView.swift in Sources */,
406D27EA294819FF002E3540 /* ContentView.swift in Sources */,
406D27E8294819FF002E3540 /* GoogleFontPreviewApp.swift in Sources */,
406D281B294913BD002E3540 /* StyleCell.swift in Sources */,
406D281D29492DF9002E3540 /* Style.swift in Sources */,
406D2815294823C7002E3540 /* GoogleFontService.swift in Sources */,
40FF8417294E95D70034CE00 /* FontList.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
};
Expand Down Expand Up @@ -330,6 +358,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = dwarf;
ENABLE_STRICT_OBJC_MSGSEND = YES;
ENABLE_TESTABILITY = YES;
Expand Down Expand Up @@ -390,6 +419,7 @@
CLANG_WARN_UNREACHABLE_CODE = YES;
CLANG_WARN__DUPLICATE_METHOD_MATCH = YES;
COPY_PHASE_STRIP = NO;
DEAD_CODE_STRIPPING = YES;
DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym";
ENABLE_NS_ASSERTIONS = NO;
ENABLE_STRICT_OBJC_MSGSEND = YES;
Expand All @@ -416,9 +446,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = GoogleFontPreview/GoogleFontPreview.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"GoogleFontPreview/Preview Content\"";
DEVELOPMENT_TEAM = L4N82R5CV4;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -443,9 +475,11 @@
ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon;
ASSETCATALOG_COMPILER_GLOBAL_ACCENT_COLOR_NAME = AccentColor;
CODE_SIGN_ENTITLEMENTS = GoogleFontPreview/GoogleFontPreview.entitlements;
CODE_SIGN_IDENTITY = "-";
CODE_SIGN_STYLE = Automatic;
COMBINE_HIDPI_IMAGES = YES;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_ASSET_PATHS = "\"GoogleFontPreview/Preview Content\"";
DEVELOPMENT_TEAM = L4N82R5CV4;
ENABLE_HARDENED_RUNTIME = YES;
Expand All @@ -471,6 +505,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = L4N82R5CV4;
GENERATE_INFOPLIST_FILE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.0;
Expand All @@ -490,6 +525,7 @@
BUNDLE_LOADER = "$(TEST_HOST)";
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = L4N82R5CV4;
GENERATE_INFOPLIST_FILE = YES;
MACOSX_DEPLOYMENT_TARGET = 13.0;
Expand All @@ -508,6 +544,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = L4N82R5CV4;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
Expand All @@ -525,6 +562,7 @@
ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES;
CODE_SIGN_STYLE = Automatic;
CURRENT_PROJECT_VERSION = 1;
DEAD_CODE_STRIPPING = YES;
DEVELOPMENT_TEAM = L4N82R5CV4;
GENERATE_INFOPLIST_FILE = YES;
MARKETING_VERSION = 1.0;
Expand Down
38 changes: 27 additions & 11 deletions GoogleFontPreview/ContentView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,19 +8,35 @@
import SwiftUI

struct ContentView: View {
var body: some View {
VStack {
Image(systemName: "globe")
.imageScale(.large)
.foregroundColor(.accentColor)
Text("Hello, world!")
@State var fonts = [GoogleFont]()
@State var font: GoogleFont?

var body: some View {
NavigationSplitView {
FontList(selectedFont: $font, fonts: fonts)
} detail: {
if let font = font {
DetailsView(font: font)
} else {
EmptyView()
}
}
.task {
do {
fonts = try await GoogleFontService().syncFonts()

await MainActor.run {
font = fonts[0]
}
.padding()
} catch {
print (error)
}
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
struct ContainerView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
29 changes: 29 additions & 0 deletions GoogleFontPreview/DetailsView.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// FontDetails.swift
// GoogleFontPreview
//
// Created by Scott Andrew on 12/13/22.
//

import SwiftUI

struct DetailsView: View {
var font: GoogleFont

var body: some View {
VStack {
Text(font.family)
.font(.system(size: 25, weight: .bold))
Spacer()
ScrollView {
LazyVStack(alignment: .leading) {
ForEach(font.files, id: \.id) { value in
StyleCell(style: value)
}
}
}
.id(font.family)
}
.padding()
}
}
32 changes: 32 additions & 0 deletions GoogleFontPreview/Font+Register.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//
// Font+Utils.swift
// GoogleFontPreview
//
// Created by Scott Andrew on 12/13/22.
//

import SwiftUI

extension Font {
static func register(url: URL, size: CGFloat = 18) async throws -> Font? {
do {
let request = URLRequest(url: url)
let (data, _) = try await URLSession.shared.data(for: request)

guard let provider = CGDataProvider(data: data as CFData),
let cgFont = CGFont(provider)
else {
print("Unsucessfully registered font")
return nil
}

let ctFont = CTFontCreateWithGraphicsFont(cgFont, size, nil, nil)

return Font(ctFont)
} catch {
print(error)
}

return nil
}
}
27 changes: 27 additions & 0 deletions GoogleFontPreview/FontList.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
//
// FontList.swift
// GoogleFontPreview
//
// Created by Scott Andrew on 12/17/22.
//

import SwiftUI

struct FontList: View {
@Binding var selectedFont: GoogleFont?
var fonts: [GoogleFont]

var body: some View {
List(fonts, selection: $selectedFont) { font in
NavigationLink(value: font) {
VStack(alignment: .leading) {
Text(font.family)
.font(.system(size: 14, weight: .bold))
Text("\(font.files.count) styles")
.font(.system(size: 14))
}
.padding(8)
}
}
}
}
73 changes: 73 additions & 0 deletions GoogleFontPreview/GoogleFont.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
//
// GoogleFont.swift
// DuffleBag
//
// Created by Scott Andrew on 10/26/22.
//

import Foundation

struct GoogleResponse: Decodable {
let kind: String
let items: [GoogleFont]
}

struct FontFile: Hashable, Identifiable {
let style: Style
let id = UUID()
let url: URL
}

struct GoogleFont: Hashable, Decodable, Identifiable {
var id: String { family }

let family: String
let files: [FontFile]
let version: String
let category: String

enum CodingKeys: CodingKey {
case family
case files
case version
case category
}

init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.family = try container.decode(String.self, forKey: .family)
let files = try container.decode([String: String].self, forKey: .files)
self.version = try container.decode(String.self, forKey: .version)
self.category = try container.decode(String.self, forKey: .category)

var keys = Array(files.keys)

// lets do a bit of work to get our files that we want.
keys.sort { item1, item2 in
func itemFixer(_ value: String) -> String {
if value == "regular" {
return "400"
} else if value == "italic" {
return "400Italic"
}

return value
}

let fixedItem1 = itemFixer(item1)
let fixedItem2 = itemFixer(item2)

return fixedItem1 < fixedItem2
}

self.files = keys.compactMap { key in
guard let style = Style(rawValue: key),
let location = files[key]?.replacingOccurrences(of: "http", with: "https")
else {
return nil
}

return FontFile(style: style, url: URL(string: location)!)
}
}
}
Loading

0 comments on commit ede7bec

Please sign in to comment.