Skip to content

Commit 84056b3

Browse files
authored
Merge pull request #17 from ServerDriven/develop
0.6.0
2 parents cc7b084 + b3b1128 commit 84056b3

File tree

13 files changed

+322
-196
lines changed

13 files changed

+322
-196
lines changed

Package.swift

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ let package = Package(
1717
dependencies: [
1818
// Dependencies declare other packages that this package depends on.
1919
// .package(url: /* package url */, from: "1.0.0"),
20-
.package(name: "ScreenData", url: "https://github.com/ServerDriven/ScreenData-swift", from: "0.4.0"),
21-
.package(name: "ScreenDataNavigation", url: "https://github.com/ServerDriven/ScreenDataNavigation-swift", from: "0.4.0")
20+
.package(name: "ScreenData", url: "https://github.com/ServerDriven/ScreenData-swift", from: "0.4.1"),
21+
.package(name: "ScreenDataNavigation", url: "https://github.com/ServerDriven/ScreenDataNavigation-swift", from: "0.5.0"),
22+
.package(url: "https://github.com/0xLeif/Task", from: "1.0.0"),
23+
.package(url: "https://github.com/0xLeif/Chronicle", from: "0.2.3")
2224
],
2325
targets: [
2426
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
@@ -27,7 +29,9 @@ let package = Package(
2729
name: "ScreenDataUI",
2830
dependencies: [
2931
"ScreenData",
30-
"ScreenDataNavigation"
32+
"ScreenDataNavigation",
33+
"Task",
34+
"Chronicle"
3135
]),
3236
.testTarget(
3337
name: "ScreenDataUITests",

Sources/.DS_Store

6 KB
Binary file not shown.

Sources/ScreenDataUI/Helpers/AsyncImage.swift

Lines changed: 0 additions & 129 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// Logger.swift
3+
//
4+
//
5+
// Created by Leif on 5/19/21.
6+
//
7+
8+
import Chronicle
9+
10+
public enum ScreenDataUILogger {
11+
public static var formatter: ChronicleFormatter = Chronicle.DefaultFormatters.DefaultFormatter()
12+
13+
public static var handler: ChronicleHandler = Chronicle.DefaultHandlers.PrintHandler()
14+
15+
public static let console = Chronicle(
16+
label: "screendata.ui.ios",
17+
formatter: ScreenDataUILogger.formatter,
18+
handler: ScreenDataUILogger.handler
19+
)
20+
}
21+
22+
func log(level: Chronicle.LogLevel) {
23+
ScreenDataUILogger.console.log(level: level)
24+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
//
2+
// SDImageProviding.swift
3+
//
4+
//
5+
// Created by Leif on 5/19/21.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import Combine
11+
import Task
12+
13+
public protocol SDImageProviding {
14+
func image(forURL url: URL) -> AnyPublisher<UIImage?, Error>
15+
}
16+
17+
public struct SDImageURLProvider: SDImageProviding {
18+
public init() { }
19+
20+
public func image(forURL url: URL) -> AnyPublisher<UIImage?, Error> {
21+
Task.fetch(url: url)
22+
.flatMap { (data, response) in
23+
Task.do {
24+
guard let data = data else {
25+
return nil
26+
}
27+
let image = UIImage(data: data)
28+
29+
return image
30+
}
31+
}
32+
.eraseToAnyPublisher()
33+
}
34+
}
35+
36+
public struct SDImageUserDefaultsProvider: SDImageProviding {
37+
public init() { }
38+
39+
public func image(forURL url: URL) -> AnyPublisher<UIImage?, Error> {
40+
Task.do {
41+
let imageData: Data? = UserDefaults.standard.data(forKey: url.absoluteString)
42+
43+
guard let data = imageData else {
44+
return nil
45+
}
46+
let image = UIImage(data: data)
47+
48+
return image
49+
50+
}
51+
.eraseToAnyPublisher()
52+
}
53+
}
54+
55+
public struct SDImageURLUserDefaultsProvider: SDImageProviding {
56+
public init() { }
57+
58+
public func image(forURL url: URL) -> AnyPublisher<UIImage?, Error> {
59+
SDImageUserDefaultsProvider()
60+
.image(forURL: url)
61+
.flatMap { image -> AnyPublisher<UIImage?, Error> in
62+
guard let image = image else {
63+
return SDImageURLProvider().image(forURL: url)
64+
}
65+
66+
return Just(image)
67+
.mapError { (Never) -> Error in }
68+
.eraseToAnyPublisher()
69+
}
70+
.eraseToAnyPublisher()
71+
72+
}
73+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//
2+
// SDImageStoring.swift
3+
//
4+
//
5+
// Created by Leif on 5/19/21.
6+
//
7+
8+
import Foundation
9+
import SwiftUI
10+
import Combine
11+
import Task
12+
13+
public protocol SDImageStoring {
14+
func store(image: UIImage?, forURL url: URL) -> AnyPublisher<Void, Error>
15+
}
16+
17+
public struct SDImageUserDefaultsStorer: SDImageStoring {
18+
public init() { }
19+
20+
public func store(image: UIImage?, forURL url: URL) -> AnyPublisher<Void, Error> {
21+
Task.do {
22+
if let imageData = image?.pngData() {
23+
UserDefaults.standard.set(imageData, forKey: url.absoluteString)
24+
} else {
25+
UserDefaults.standard.set(nil, forKey: url.absoluteString)
26+
}
27+
}
28+
.eraseToAnyPublisher()
29+
}
30+
}

Sources/ScreenDataUI/Navigation/SDScreenStore.swift

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import Foundation
99
import ScreenData
1010
import ScreenDataNavigation
1111
import Combine
12+
import Chronicle
1213

1314
public struct SDScreenStore: ScreenStoring {
1415
public static var `default`: ScreenStoring?
@@ -17,6 +18,7 @@ public struct SDScreenStore: ScreenStoring {
1718

1819
public func store(screens: [SomeScreen]) -> AnyPublisher<Void, Error> {
1920
guard let store = SDScreenStore.default else {
21+
log(level: .info("SAVED Screens: \(screens.map(\.id))"))
2022
return ScreenDataNavigation.UserDefaultScreenStorer(baseKey: "SDScreenStore")
2123
.store(screens: screens)
2224
}

Sources/ScreenDataUI/Theme/SDFont.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import ScreenData
1212
public struct SDFont {
1313
public static var largeTitleFont: Font?
1414
public static var titleFont: Font?
15-
public static var headlingFont: Font?
15+
public static var headlineFont: Font?
1616
public static var bodyFont: Font?
1717
public static var footnoteFont: Font?
1818
public static var captionFont: Font?
@@ -24,7 +24,7 @@ public struct SDFont {
2424
case .title:
2525
return titleFont ?? .title
2626
case .headline:
27-
return headlingFont ?? .headline
27+
return headlineFont ?? .headline
2828
case .body:
2929
return bodyFont ?? .body
3030
case .footnote:

Sources/ScreenDataUI/Views/SDButton.swift

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import SwiftUI
99
import ScreenData
1010
import ScreenDataNavigation
11+
import Chronicle
1112

1213
public typealias SDAction = () -> Void
1314

@@ -30,27 +31,44 @@ public struct SDButton: View {
3031
}
3132

3233
public var body: some View {
33-
Button(action: {
34-
print("[SDButton] ActionID: \(String(describing: button.actionID)) & Destination: \(String(describing: button.destination))")
35-
if let actionID = button.actionID,
36-
let action = SDButton.actions[actionID] {
37-
action()
38-
}
39-
40-
if let destination = button.destination {
41-
print("Navigate to destination: (\(destination))")
42-
modalScreen = store.destinationView
43-
}
44-
}) {
45-
Text(button.title)
46-
.font(SDFont.font(for: .body))
47-
.background(with: button.style)
34+
guard button.destination != nil else {
35+
return AnyView(
36+
Button(
37+
action: buttonAction,
38+
label: {
39+
Text(button.title)
40+
.font(SDFont.font(for: .body))
41+
.background(with: button.style)
42+
}
43+
)
44+
)
4845
}
49-
.sheet(item: $modalScreen) { (destination) in
50-
destination
46+
47+
return AnyView(
48+
SDDestinationLink(
49+
provider: SDScreenProvider(),
50+
destination: button.destination,
51+
action: buttonAction,
52+
content: {
53+
Text(button.title)
54+
.font(SDFont.font(for: .body))
55+
.background(with: button.style)
56+
}
57+
)
58+
)
59+
}
60+
61+
private func buttonAction() {
62+
log(level: .info("[SDButton] ActionID: \(String(describing: button.actionID)) & Destination: \(String(describing: button.destination))"))
63+
if let actionID = button.actionID,
64+
let action = SDButton.actions[actionID] {
65+
action()
5166
}
52-
.onAppear {
53-
store.load(destination: button.destination, provider: SDScreenProvider())
67+
68+
if let destination = button.destination {
69+
log(level: .info("Navigate to destination: (\(destination))"))
70+
modalScreen = store.destinationView
5471
}
72+
5573
}
5674
}

0 commit comments

Comments
 (0)