|
| 1 | +import ScreenData |
| 2 | +import Combine |
| 3 | +import Foundation |
| 4 | + |
| 5 | +// MARK: ScreenProviding |
| 6 | + |
| 7 | +public protocol ScreenProviding { |
| 8 | + func screen(forID id: String) -> Future<SomeScreen, Error> |
| 9 | +} |
| 10 | + |
| 11 | +// MARK: ScreenProviding Basic Implementation |
| 12 | + |
| 13 | +public struct MockScreenProvider: ScreenProviding { |
| 14 | + public var mockScreen: SomeScreen |
| 15 | + |
| 16 | + public init(mockScreen: SomeScreen) { |
| 17 | + self.mockScreen = mockScreen |
| 18 | + } |
| 19 | + |
| 20 | + public func screen(forID id: String) -> Future<SomeScreen, Error> { |
| 21 | + Future { promise in |
| 22 | + var screen = mockScreen |
| 23 | + screen.id = id |
| 24 | + promise(.success(screen)) |
| 25 | + } |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +public struct URLScreenProvider: ScreenProviding { |
| 30 | + public enum URLScreenProviderError: Error { |
| 31 | + case noResponse |
| 32 | + case noData |
| 33 | + } |
| 34 | + |
| 35 | + public var baseURL: URL |
| 36 | + |
| 37 | + public init(baseURL: URL) { |
| 38 | + self.baseURL = baseURL |
| 39 | + } |
| 40 | + |
| 41 | + public func screen(forID id: String) -> Future<SomeScreen, Error> { |
| 42 | + Future { promise in |
| 43 | + URLSession.shared.dataTask(with: baseURL.appendingPathComponent(id)) { (data, response, error) in |
| 44 | + if let error = error { |
| 45 | + promise(.failure(error)) |
| 46 | + } |
| 47 | + |
| 48 | + guard let _ = response else { |
| 49 | + promise(.failure(URLScreenProviderError.noResponse)) |
| 50 | + return |
| 51 | + } |
| 52 | + |
| 53 | + guard let data = data else { |
| 54 | + promise(.failure(URLScreenProviderError.noData)) |
| 55 | + return |
| 56 | + } |
| 57 | + |
| 58 | + do { |
| 59 | + promise(.success(try JSONDecoder().decode(SomeScreen.self, from: data))) |
| 60 | + } catch { |
| 61 | + promise(.failure(error)) |
| 62 | + } |
| 63 | + } |
| 64 | + } |
| 65 | + } |
| 66 | +} |
| 67 | + |
| 68 | +public struct UserDefaultScreenProvider: ScreenProviding { |
| 69 | + public enum UserDefaultScreenProviderError: Error { |
| 70 | + case noData |
| 71 | + } |
| 72 | + |
| 73 | + public var baseKey: String |
| 74 | + |
| 75 | + public init(baseKey: String) { |
| 76 | + self.baseKey = baseKey |
| 77 | + } |
| 78 | + |
| 79 | + public func screen(forID id: String) -> Future<SomeScreen, Error> { |
| 80 | + Future { promise in |
| 81 | + guard let data = UserDefaults.standard.data(forKey: baseKey + id) else { |
| 82 | + promise(.failure(UserDefaultScreenProviderError.noData)) |
| 83 | + return |
| 84 | + } |
| 85 | + |
| 86 | + do { |
| 87 | + promise(.success(try JSONDecoder().decode(SomeScreen.self, from: data))) |
| 88 | + } catch { |
| 89 | + promise(.failure(error)) |
| 90 | + } |
| 91 | + } |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +// MARK: ScreenStoring |
| 96 | +public protocol ScreenStoring { |
| 97 | + func store(screens: [SomeScreen]) -> Future<Void, Error> |
| 98 | +} |
| 99 | + |
| 100 | +// MARK: ScreenStoring Basic Implementation |
| 101 | + |
| 102 | +public struct UserDefaultScreenStorer: ScreenStoring { |
| 103 | + public var baseKey: String |
| 104 | + |
| 105 | + public init(baseKey: String) { |
| 106 | + self.baseKey = baseKey |
| 107 | + } |
| 108 | + |
| 109 | + public func store(screens: [SomeScreen]) -> Future<Void, Error> { |
| 110 | + Future { promise in |
| 111 | + do { |
| 112 | + try screens.forEach { screen in |
| 113 | + let data = try JSONEncoder().encode(screen) |
| 114 | + let key = baseKey + (screen.id ?? "") |
| 115 | + UserDefaults.standard.set(data, |
| 116 | + forKey: key) |
| 117 | + } |
| 118 | + promise(.success(())) |
| 119 | + } catch { |
| 120 | + promise(.failure(error)) |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// MARK: ScreenLoading |
| 127 | +public protocol ScreenLoading { |
| 128 | + func load(withProvider provider: ScreenProviding) -> Future<[SomeScreen], Error> |
| 129 | +} |
| 130 | + |
| 131 | +// MARK: ScreenLoading Basic Implementation [WIP] |
| 132 | +// |
| 133 | +//extension SomeScreen: ScreenLoading { |
| 134 | +// |
| 135 | +// func load(withProvider provider: ScreenProviding) -> Future<[SomeScreen], Error> { |
| 136 | +// Future { promise in |
| 137 | +// let headerViewDestinations = headerView?.destinations ?? [] |
| 138 | +// let footerViewDestinations = footerView?.destinations ?? [] |
| 139 | +// let destinations = headerViewDestinations + |
| 140 | +// someView.destinations + |
| 141 | +// footerViewDestinations |
| 142 | +// |
| 143 | +// let task = Publishers.MergeMany( |
| 144 | +// destinations.filter { destination in |
| 145 | +// destination.type == .screen |
| 146 | +// } |
| 147 | +// .map { destination in |
| 148 | +// provider.screen(forID: destination.toID).eraseToAnyPublisher() |
| 149 | +// } |
| 150 | +// .publisher |
| 151 | +// .collect() |
| 152 | +// ) |
| 153 | +// |
| 154 | +// |
| 155 | +// |
| 156 | +// |
| 157 | +// } |
| 158 | +// } |
| 159 | +//} |
| 160 | + |
| 161 | +public extension SomeView { |
| 162 | + var destinations: [Destination] { |
| 163 | + guard let container = container else { |
| 164 | + if let someLabel = someLabel, |
| 165 | + let destination = someLabel.destination { |
| 166 | + return [destination] |
| 167 | + } else if let someImage = someImage, |
| 168 | + let destination = someImage.destination { |
| 169 | + return [destination] |
| 170 | + } else if let someLabeledImage = someLabeledImage { |
| 171 | + return [someLabeledImage.destination, |
| 172 | + someLabeledImage.someImage.destination] |
| 173 | + .compactMap { $0 } |
| 174 | + } else if let someCustomView = someCustomView { |
| 175 | + let destinations = [someCustomView.destination, |
| 176 | + someCustomView.someImage?.destination] |
| 177 | + .compactMap { $0 } |
| 178 | + let subViewDestinations = someCustomView.views |
| 179 | + .map(\.destinations) |
| 180 | + .reduce([], +) |
| 181 | + |
| 182 | + return destinations + subViewDestinations |
| 183 | + } |
| 184 | + |
| 185 | + |
| 186 | + return [] |
| 187 | + } |
| 188 | + |
| 189 | + return container.views |
| 190 | + .map(\.destinations) |
| 191 | + .reduce([], +) |
| 192 | + } |
| 193 | +} |
| 194 | + |
| 195 | +public extension SomeScreen { |
| 196 | + var destinations: [Destination] { |
| 197 | + let headerViewDestinations = headerView?.destinations ?? [] |
| 198 | + let footerViewDestinations = footerView?.destinations ?? [] |
| 199 | + |
| 200 | + return headerViewDestinations + |
| 201 | + someView.destinations + |
| 202 | + footerViewDestinations |
| 203 | + } |
| 204 | +} |
0 commit comments