├── .gitignore ├── CloudKitchenSink20.xcodeproj └── project.pbxproj ├── CloudKitchenSink20 ├── Bootstrap │ └── AppDelegate.swift ├── Preview Content │ └── Preview Assets.xcassets │ │ └── Contents.json ├── Resources │ ├── 1.jpg │ ├── 2.jpg │ ├── Assets.xcassets │ │ ├── AppIcon.appiconset │ │ │ └── Contents.json │ │ └── Contents.json │ ├── Base.lproj │ │ └── Main.storyboard │ ├── CloudKitchenSink20.entitlements │ └── Info.plist ├── ViewModels │ └── AddRecipeViewModel.swift └── Views │ ├── Aux │ ├── DropZoneView.swift │ └── MacEditorTextView.swift │ ├── RecipeFormView.swift │ └── RecipeListView.swift ├── KitchenCore ├── Info.plist ├── KitchenCore.h └── Source │ ├── Definitions │ └── SyncConstants.swift │ ├── Extensions │ └── Error+CloudKit.swift │ ├── Models │ ├── Recipe+CloudKit.swift │ ├── Recipe+Preview.swift │ └── Recipe.swift │ └── Storage │ ├── RecipeStore.swift │ ├── SyncEngine+Notifications.swift │ └── SyncEngine.swift ├── LICENSE ├── README.md └── screenshots └── main.png /.gitignore: -------------------------------------------------------------------------------- 1 | .DS_Store 2 | __MACOSX 3 | *.pbxuser 4 | !default.pbxuser 5 | *.mode1v3 6 | !default.mode1v3 7 | *.mode2v3 8 | !default.mode2v3 9 | *.perspectivev3 10 | !default.perspectivev3 11 | *.xcworkspace 12 | !default.xcworkspace 13 | xcuserdata 14 | profile 15 | *.moved-aside 16 | DerivedData 17 | .idea/ 18 | Crashlytics.sh 19 | generatechangelog.sh 20 | Pods/ 21 | Carthage 22 | Provisioning 23 | Crashlytics.sh -------------------------------------------------------------------------------- /CloudKitchenSink20.xcodeproj/project.pbxproj: -------------------------------------------------------------------------------- 1 | // !$*UTF8*$! 2 | { 3 | archiveVersion = 1; 4 | classes = { 5 | }; 6 | objectVersion = 50; 7 | objects = { 8 | 9 | /* Begin PBXBuildFile section */ 10 | DD609264240440BE0016ECB3 /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD609263240440BE0016ECB3 /* AppDelegate.swift */; }; 11 | DD609266240440BE0016ECB3 /* RecipeListView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD609265240440BE0016ECB3 /* RecipeListView.swift */; }; 12 | DD609268240440BF0016ECB3 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DD609267240440BF0016ECB3 /* Assets.xcassets */; }; 13 | DD60926B240440BF0016ECB3 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = DD60926A240440BF0016ECB3 /* Preview Assets.xcassets */; }; 14 | DD60926E240440BF0016ECB3 /* Main.storyboard in Resources */ = {isa = PBXBuildFile; fileRef = DD60926C240440BF0016ECB3 /* Main.storyboard */; }; 15 | DD609278240440DC0016ECB3 /* CloudKit.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD609277240440DC0016ECB3 /* CloudKit.framework */; }; 16 | DD9D3BCE240447CB003C7C91 /* KitchenCore.h in Headers */ = {isa = PBXBuildFile; fileRef = DD9D3BCC240447CB003C7C91 /* KitchenCore.h */; settings = {ATTRIBUTES = (Public, ); }; }; 17 | DD9D3BD1240447CB003C7C91 /* KitchenCore.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = DD9D3BCA240447CB003C7C91 /* KitchenCore.framework */; }; 18 | DD9D3BD2240447CB003C7C91 /* KitchenCore.framework in Embed Frameworks */ = {isa = PBXBuildFile; fileRef = DD9D3BCA240447CB003C7C91 /* KitchenCore.framework */; settings = {ATTRIBUTES = (CodeSignOnCopy, RemoveHeadersOnCopy, ); }; }; 19 | DD9D3BDA240447DE003C7C91 /* Recipe.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BD9240447DE003C7C91 /* Recipe.swift */; }; 20 | DD9D3BDC24044824003C7C91 /* Recipe+CloudKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BDB24044824003C7C91 /* Recipe+CloudKit.swift */; }; 21 | DD9D3BDF2404488F003C7C91 /* SyncConstants.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BDE2404488F003C7C91 /* SyncConstants.swift */; }; 22 | DD9D3BE224044A1D003C7C91 /* RecipeStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BE124044A1D003C7C91 /* RecipeStore.swift */; }; 23 | DD9D3BE424044E94003C7C91 /* SyncEngine.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BE324044E94003C7C91 /* SyncEngine.swift */; }; 24 | DD9D3BE624045006003C7C91 /* Recipe+Preview.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BE524045006003C7C91 /* Recipe+Preview.swift */; }; 25 | DD9D3BEB2404639E003C7C91 /* 1.jpg in Resources */ = {isa = PBXBuildFile; fileRef = DD9D3BEA2404639E003C7C91 /* 1.jpg */; }; 26 | DD9D3BED24046696003C7C91 /* 2.jpg in Resources */ = {isa = PBXBuildFile; fileRef = DD9D3BEC24046696003C7C91 /* 2.jpg */; }; 27 | DD9D3BEF2404673B003C7C91 /* AddRecipeViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BEE2404673B003C7C91 /* AddRecipeViewModel.swift */; }; 28 | DD9D3BF12404680F003C7C91 /* RecipeFormView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BF02404680F003C7C91 /* RecipeFormView.swift */; }; 29 | DD9D3BF32404694F003C7C91 /* MacEditorTextView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BF22404694F003C7C91 /* MacEditorTextView.swift */; }; 30 | DD9D3BF524046A51003C7C91 /* DropZoneView.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BF424046A51003C7C91 /* DropZoneView.swift */; }; 31 | DD9D3BF8240476F4003C7C91 /* Error+CloudKit.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BF7240476F4003C7C91 /* Error+CloudKit.swift */; }; 32 | DD9D3BFA24047D22003C7C91 /* SyncEngine+Notifications.swift in Sources */ = {isa = PBXBuildFile; fileRef = DD9D3BF924047D22003C7C91 /* SyncEngine+Notifications.swift */; }; 33 | /* End PBXBuildFile section */ 34 | 35 | /* Begin PBXContainerItemProxy section */ 36 | DD9D3BCF240447CB003C7C91 /* PBXContainerItemProxy */ = { 37 | isa = PBXContainerItemProxy; 38 | containerPortal = DD609258240440BE0016ECB3 /* Project object */; 39 | proxyType = 1; 40 | remoteGlobalIDString = DD9D3BC9240447CB003C7C91; 41 | remoteInfo = KitchenCore; 42 | }; 43 | /* End PBXContainerItemProxy section */ 44 | 45 | /* Begin PBXCopyFilesBuildPhase section */ 46 | DD9D3BD3240447CB003C7C91 /* Embed Frameworks */ = { 47 | isa = PBXCopyFilesBuildPhase; 48 | buildActionMask = 2147483647; 49 | dstPath = ""; 50 | dstSubfolderSpec = 10; 51 | files = ( 52 | DD9D3BD2240447CB003C7C91 /* KitchenCore.framework in Embed Frameworks */, 53 | ); 54 | name = "Embed Frameworks"; 55 | runOnlyForDeploymentPostprocessing = 0; 56 | }; 57 | /* End PBXCopyFilesBuildPhase section */ 58 | 59 | /* Begin PBXFileReference section */ 60 | DD609260240440BE0016ECB3 /* CloudKitchenSink20.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = CloudKitchenSink20.app; sourceTree = BUILT_PRODUCTS_DIR; }; 61 | DD609263240440BE0016ECB3 /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = ""; }; 62 | DD609265240440BE0016ECB3 /* RecipeListView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeListView.swift; sourceTree = ""; }; 63 | DD609267240440BF0016ECB3 /* Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = Assets.xcassets; sourceTree = ""; }; 64 | DD60926A240440BF0016ECB3 /* Preview Assets.xcassets */ = {isa = PBXFileReference; lastKnownFileType = folder.assetcatalog; path = "Preview Assets.xcassets"; sourceTree = ""; }; 65 | DD60926D240440BF0016ECB3 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.storyboard; name = Base; path = Base.lproj/Main.storyboard; sourceTree = ""; }; 66 | DD60926F240440BF0016ECB3 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 67 | DD609270240440BF0016ECB3 /* CloudKitchenSink20.entitlements */ = {isa = PBXFileReference; lastKnownFileType = text.plist.entitlements; path = CloudKitchenSink20.entitlements; sourceTree = ""; }; 68 | DD609277240440DC0016ECB3 /* CloudKit.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = CloudKit.framework; path = System/Library/Frameworks/CloudKit.framework; sourceTree = SDKROOT; }; 69 | DD9D3BCA240447CB003C7C91 /* KitchenCore.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = KitchenCore.framework; sourceTree = BUILT_PRODUCTS_DIR; }; 70 | DD9D3BCC240447CB003C7C91 /* KitchenCore.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = KitchenCore.h; sourceTree = ""; }; 71 | DD9D3BCD240447CB003C7C91 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = ""; }; 72 | DD9D3BD9240447DE003C7C91 /* Recipe.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Recipe.swift; sourceTree = ""; }; 73 | DD9D3BDB24044824003C7C91 /* Recipe+CloudKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Recipe+CloudKit.swift"; sourceTree = ""; }; 74 | DD9D3BDE2404488F003C7C91 /* SyncConstants.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncConstants.swift; sourceTree = ""; }; 75 | DD9D3BE124044A1D003C7C91 /* RecipeStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeStore.swift; sourceTree = ""; }; 76 | DD9D3BE324044E94003C7C91 /* SyncEngine.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SyncEngine.swift; sourceTree = ""; }; 77 | DD9D3BE524045006003C7C91 /* Recipe+Preview.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Recipe+Preview.swift"; sourceTree = ""; }; 78 | DD9D3BEA2404639E003C7C91 /* 1.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 1.jpg; sourceTree = ""; }; 79 | DD9D3BEC24046696003C7C91 /* 2.jpg */ = {isa = PBXFileReference; lastKnownFileType = image.jpeg; path = 2.jpg; sourceTree = ""; }; 80 | DD9D3BEE2404673B003C7C91 /* AddRecipeViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AddRecipeViewModel.swift; sourceTree = ""; }; 81 | DD9D3BF02404680F003C7C91 /* RecipeFormView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = RecipeFormView.swift; sourceTree = ""; }; 82 | DD9D3BF22404694F003C7C91 /* MacEditorTextView.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = MacEditorTextView.swift; sourceTree = ""; }; 83 | DD9D3BF424046A51003C7C91 /* DropZoneView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DropZoneView.swift; sourceTree = ""; }; 84 | DD9D3BF7240476F4003C7C91 /* Error+CloudKit.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Error+CloudKit.swift"; sourceTree = ""; }; 85 | DD9D3BF924047D22003C7C91 /* SyncEngine+Notifications.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "SyncEngine+Notifications.swift"; sourceTree = ""; }; 86 | /* End PBXFileReference section */ 87 | 88 | /* Begin PBXFrameworksBuildPhase section */ 89 | DD60925D240440BE0016ECB3 /* Frameworks */ = { 90 | isa = PBXFrameworksBuildPhase; 91 | buildActionMask = 2147483647; 92 | files = ( 93 | DD9D3BD1240447CB003C7C91 /* KitchenCore.framework in Frameworks */, 94 | DD609278240440DC0016ECB3 /* CloudKit.framework in Frameworks */, 95 | ); 96 | runOnlyForDeploymentPostprocessing = 0; 97 | }; 98 | DD9D3BC7240447CB003C7C91 /* Frameworks */ = { 99 | isa = PBXFrameworksBuildPhase; 100 | buildActionMask = 2147483647; 101 | files = ( 102 | ); 103 | runOnlyForDeploymentPostprocessing = 0; 104 | }; 105 | /* End PBXFrameworksBuildPhase section */ 106 | 107 | /* Begin PBXGroup section */ 108 | DD609257240440BE0016ECB3 = { 109 | isa = PBXGroup; 110 | children = ( 111 | DD609262240440BE0016ECB3 /* CloudKitchenSink20 */, 112 | DD9D3BCB240447CB003C7C91 /* KitchenCore */, 113 | DD609261240440BE0016ECB3 /* Products */, 114 | DD609276240440DC0016ECB3 /* Frameworks */, 115 | ); 116 | sourceTree = ""; 117 | }; 118 | DD609261240440BE0016ECB3 /* Products */ = { 119 | isa = PBXGroup; 120 | children = ( 121 | DD609260240440BE0016ECB3 /* CloudKitchenSink20.app */, 122 | DD9D3BCA240447CB003C7C91 /* KitchenCore.framework */, 123 | ); 124 | name = Products; 125 | sourceTree = ""; 126 | }; 127 | DD609262240440BE0016ECB3 /* CloudKitchenSink20 */ = { 128 | isa = PBXGroup; 129 | children = ( 130 | DD9D3BFE24048062003C7C91 /* Bootstrap */, 131 | DD9D3BFD2404805A003C7C91 /* ViewModels */, 132 | DD9D3BFB24048047003C7C91 /* Views */, 133 | DD9D3BE724046342003C7C91 /* Resources */, 134 | DD609269240440BF0016ECB3 /* Preview Content */, 135 | ); 136 | path = CloudKitchenSink20; 137 | sourceTree = ""; 138 | }; 139 | DD609269240440BF0016ECB3 /* Preview Content */ = { 140 | isa = PBXGroup; 141 | children = ( 142 | DD60926A240440BF0016ECB3 /* Preview Assets.xcassets */, 143 | ); 144 | path = "Preview Content"; 145 | sourceTree = ""; 146 | }; 147 | DD609276240440DC0016ECB3 /* Frameworks */ = { 148 | isa = PBXGroup; 149 | children = ( 150 | DD609277240440DC0016ECB3 /* CloudKit.framework */, 151 | ); 152 | name = Frameworks; 153 | sourceTree = ""; 154 | }; 155 | DD9D3BCB240447CB003C7C91 /* KitchenCore */ = { 156 | isa = PBXGroup; 157 | children = ( 158 | DD9D3BD7240447CF003C7C91 /* Source */, 159 | DD9D3BCC240447CB003C7C91 /* KitchenCore.h */, 160 | DD9D3BCD240447CB003C7C91 /* Info.plist */, 161 | ); 162 | path = KitchenCore; 163 | sourceTree = ""; 164 | }; 165 | DD9D3BD7240447CF003C7C91 /* Source */ = { 166 | isa = PBXGroup; 167 | children = ( 168 | DD9D3BF6240476EC003C7C91 /* Extensions */, 169 | DD9D3BE024044A10003C7C91 /* Storage */, 170 | DD9D3BDD24044886003C7C91 /* Definitions */, 171 | DD9D3BD8240447D3003C7C91 /* Models */, 172 | ); 173 | path = Source; 174 | sourceTree = ""; 175 | }; 176 | DD9D3BD8240447D3003C7C91 /* Models */ = { 177 | isa = PBXGroup; 178 | children = ( 179 | DD9D3BD9240447DE003C7C91 /* Recipe.swift */, 180 | DD9D3BDB24044824003C7C91 /* Recipe+CloudKit.swift */, 181 | DD9D3BE524045006003C7C91 /* Recipe+Preview.swift */, 182 | ); 183 | path = Models; 184 | sourceTree = ""; 185 | }; 186 | DD9D3BDD24044886003C7C91 /* Definitions */ = { 187 | isa = PBXGroup; 188 | children = ( 189 | DD9D3BDE2404488F003C7C91 /* SyncConstants.swift */, 190 | ); 191 | path = Definitions; 192 | sourceTree = ""; 193 | }; 194 | DD9D3BE024044A10003C7C91 /* Storage */ = { 195 | isa = PBXGroup; 196 | children = ( 197 | DD9D3BE124044A1D003C7C91 /* RecipeStore.swift */, 198 | DD9D3BE324044E94003C7C91 /* SyncEngine.swift */, 199 | DD9D3BF924047D22003C7C91 /* SyncEngine+Notifications.swift */, 200 | ); 201 | path = Storage; 202 | sourceTree = ""; 203 | }; 204 | DD9D3BE724046342003C7C91 /* Resources */ = { 205 | isa = PBXGroup; 206 | children = ( 207 | DD609267240440BF0016ECB3 /* Assets.xcassets */, 208 | DD60926C240440BF0016ECB3 /* Main.storyboard */, 209 | DD60926F240440BF0016ECB3 /* Info.plist */, 210 | DD609270240440BF0016ECB3 /* CloudKitchenSink20.entitlements */, 211 | DD9D3BEA2404639E003C7C91 /* 1.jpg */, 212 | DD9D3BEC24046696003C7C91 /* 2.jpg */, 213 | ); 214 | path = Resources; 215 | sourceTree = ""; 216 | }; 217 | DD9D3BF6240476EC003C7C91 /* Extensions */ = { 218 | isa = PBXGroup; 219 | children = ( 220 | DD9D3BF7240476F4003C7C91 /* Error+CloudKit.swift */, 221 | ); 222 | path = Extensions; 223 | sourceTree = ""; 224 | }; 225 | DD9D3BFB24048047003C7C91 /* Views */ = { 226 | isa = PBXGroup; 227 | children = ( 228 | DD9D3BFC2404804C003C7C91 /* Aux */, 229 | DD609265240440BE0016ECB3 /* RecipeListView.swift */, 230 | DD9D3BF02404680F003C7C91 /* RecipeFormView.swift */, 231 | ); 232 | path = Views; 233 | sourceTree = ""; 234 | }; 235 | DD9D3BFC2404804C003C7C91 /* Aux */ = { 236 | isa = PBXGroup; 237 | children = ( 238 | DD9D3BF22404694F003C7C91 /* MacEditorTextView.swift */, 239 | DD9D3BF424046A51003C7C91 /* DropZoneView.swift */, 240 | ); 241 | path = Aux; 242 | sourceTree = ""; 243 | }; 244 | DD9D3BFD2404805A003C7C91 /* ViewModels */ = { 245 | isa = PBXGroup; 246 | children = ( 247 | DD9D3BEE2404673B003C7C91 /* AddRecipeViewModel.swift */, 248 | ); 249 | path = ViewModels; 250 | sourceTree = ""; 251 | }; 252 | DD9D3BFE24048062003C7C91 /* Bootstrap */ = { 253 | isa = PBXGroup; 254 | children = ( 255 | DD609263240440BE0016ECB3 /* AppDelegate.swift */, 256 | ); 257 | path = Bootstrap; 258 | sourceTree = ""; 259 | }; 260 | /* End PBXGroup section */ 261 | 262 | /* Begin PBXHeadersBuildPhase section */ 263 | DD9D3BC5240447CB003C7C91 /* Headers */ = { 264 | isa = PBXHeadersBuildPhase; 265 | buildActionMask = 2147483647; 266 | files = ( 267 | DD9D3BCE240447CB003C7C91 /* KitchenCore.h in Headers */, 268 | ); 269 | runOnlyForDeploymentPostprocessing = 0; 270 | }; 271 | /* End PBXHeadersBuildPhase section */ 272 | 273 | /* Begin PBXNativeTarget section */ 274 | DD60925F240440BE0016ECB3 /* CloudKitchenSink20 */ = { 275 | isa = PBXNativeTarget; 276 | buildConfigurationList = DD609273240440BF0016ECB3 /* Build configuration list for PBXNativeTarget "CloudKitchenSink20" */; 277 | buildPhases = ( 278 | DD60925C240440BE0016ECB3 /* Sources */, 279 | DD60925D240440BE0016ECB3 /* Frameworks */, 280 | DD60925E240440BE0016ECB3 /* Resources */, 281 | DD9D3BD3240447CB003C7C91 /* Embed Frameworks */, 282 | ); 283 | buildRules = ( 284 | ); 285 | dependencies = ( 286 | DD9D3BD0240447CB003C7C91 /* PBXTargetDependency */, 287 | ); 288 | name = CloudKitchenSink20; 289 | productName = CloudKitchenSink20; 290 | productReference = DD609260240440BE0016ECB3 /* CloudKitchenSink20.app */; 291 | productType = "com.apple.product-type.application"; 292 | }; 293 | DD9D3BC9240447CB003C7C91 /* KitchenCore */ = { 294 | isa = PBXNativeTarget; 295 | buildConfigurationList = DD9D3BD6240447CB003C7C91 /* Build configuration list for PBXNativeTarget "KitchenCore" */; 296 | buildPhases = ( 297 | DD9D3BC5240447CB003C7C91 /* Headers */, 298 | DD9D3BC6240447CB003C7C91 /* Sources */, 299 | DD9D3BC7240447CB003C7C91 /* Frameworks */, 300 | DD9D3BC8240447CB003C7C91 /* Resources */, 301 | ); 302 | buildRules = ( 303 | ); 304 | dependencies = ( 305 | ); 306 | name = KitchenCore; 307 | productName = KitchenCore; 308 | productReference = DD9D3BCA240447CB003C7C91 /* KitchenCore.framework */; 309 | productType = "com.apple.product-type.framework"; 310 | }; 311 | /* End PBXNativeTarget section */ 312 | 313 | /* Begin PBXProject section */ 314 | DD609258240440BE0016ECB3 /* Project object */ = { 315 | isa = PBXProject; 316 | attributes = { 317 | LastSwiftUpdateCheck = 1130; 318 | LastUpgradeCheck = 1130; 319 | ORGANIZATIONNAME = "Guilherme Rambo"; 320 | TargetAttributes = { 321 | DD60925F240440BE0016ECB3 = { 322 | CreatedOnToolsVersion = 11.3.1; 323 | }; 324 | DD9D3BC9240447CB003C7C91 = { 325 | CreatedOnToolsVersion = 11.3.1; 326 | LastSwiftMigration = 1130; 327 | }; 328 | }; 329 | }; 330 | buildConfigurationList = DD60925B240440BE0016ECB3 /* Build configuration list for PBXProject "CloudKitchenSink20" */; 331 | compatibilityVersion = "Xcode 9.3"; 332 | developmentRegion = en; 333 | hasScannedForEncodings = 0; 334 | knownRegions = ( 335 | en, 336 | Base, 337 | ); 338 | mainGroup = DD609257240440BE0016ECB3; 339 | productRefGroup = DD609261240440BE0016ECB3 /* Products */; 340 | projectDirPath = ""; 341 | projectRoot = ""; 342 | targets = ( 343 | DD60925F240440BE0016ECB3 /* CloudKitchenSink20 */, 344 | DD9D3BC9240447CB003C7C91 /* KitchenCore */, 345 | ); 346 | }; 347 | /* End PBXProject section */ 348 | 349 | /* Begin PBXResourcesBuildPhase section */ 350 | DD60925E240440BE0016ECB3 /* Resources */ = { 351 | isa = PBXResourcesBuildPhase; 352 | buildActionMask = 2147483647; 353 | files = ( 354 | DD60926E240440BF0016ECB3 /* Main.storyboard in Resources */, 355 | DD9D3BEB2404639E003C7C91 /* 1.jpg in Resources */, 356 | DD9D3BED24046696003C7C91 /* 2.jpg in Resources */, 357 | DD60926B240440BF0016ECB3 /* Preview Assets.xcassets in Resources */, 358 | DD609268240440BF0016ECB3 /* Assets.xcassets in Resources */, 359 | ); 360 | runOnlyForDeploymentPostprocessing = 0; 361 | }; 362 | DD9D3BC8240447CB003C7C91 /* Resources */ = { 363 | isa = PBXResourcesBuildPhase; 364 | buildActionMask = 2147483647; 365 | files = ( 366 | ); 367 | runOnlyForDeploymentPostprocessing = 0; 368 | }; 369 | /* End PBXResourcesBuildPhase section */ 370 | 371 | /* Begin PBXSourcesBuildPhase section */ 372 | DD60925C240440BE0016ECB3 /* Sources */ = { 373 | isa = PBXSourcesBuildPhase; 374 | buildActionMask = 2147483647; 375 | files = ( 376 | DD9D3BF32404694F003C7C91 /* MacEditorTextView.swift in Sources */, 377 | DD9D3BF524046A51003C7C91 /* DropZoneView.swift in Sources */, 378 | DD609266240440BE0016ECB3 /* RecipeListView.swift in Sources */, 379 | DD609264240440BE0016ECB3 /* AppDelegate.swift in Sources */, 380 | DD9D3BF12404680F003C7C91 /* RecipeFormView.swift in Sources */, 381 | DD9D3BEF2404673B003C7C91 /* AddRecipeViewModel.swift in Sources */, 382 | ); 383 | runOnlyForDeploymentPostprocessing = 0; 384 | }; 385 | DD9D3BC6240447CB003C7C91 /* Sources */ = { 386 | isa = PBXSourcesBuildPhase; 387 | buildActionMask = 2147483647; 388 | files = ( 389 | DD9D3BF8240476F4003C7C91 /* Error+CloudKit.swift in Sources */, 390 | DD9D3BFA24047D22003C7C91 /* SyncEngine+Notifications.swift in Sources */, 391 | DD9D3BDC24044824003C7C91 /* Recipe+CloudKit.swift in Sources */, 392 | DD9D3BE224044A1D003C7C91 /* RecipeStore.swift in Sources */, 393 | DD9D3BE624045006003C7C91 /* Recipe+Preview.swift in Sources */, 394 | DD9D3BE424044E94003C7C91 /* SyncEngine.swift in Sources */, 395 | DD9D3BDF2404488F003C7C91 /* SyncConstants.swift in Sources */, 396 | DD9D3BDA240447DE003C7C91 /* Recipe.swift in Sources */, 397 | ); 398 | runOnlyForDeploymentPostprocessing = 0; 399 | }; 400 | /* End PBXSourcesBuildPhase section */ 401 | 402 | /* Begin PBXTargetDependency section */ 403 | DD9D3BD0240447CB003C7C91 /* PBXTargetDependency */ = { 404 | isa = PBXTargetDependency; 405 | target = DD9D3BC9240447CB003C7C91 /* KitchenCore */; 406 | targetProxy = DD9D3BCF240447CB003C7C91 /* PBXContainerItemProxy */; 407 | }; 408 | /* End PBXTargetDependency section */ 409 | 410 | /* Begin PBXVariantGroup section */ 411 | DD60926C240440BF0016ECB3 /* Main.storyboard */ = { 412 | isa = PBXVariantGroup; 413 | children = ( 414 | DD60926D240440BF0016ECB3 /* Base */, 415 | ); 416 | name = Main.storyboard; 417 | sourceTree = ""; 418 | }; 419 | /* End PBXVariantGroup section */ 420 | 421 | /* Begin XCBuildConfiguration section */ 422 | DD609271240440BF0016ECB3 /* Debug */ = { 423 | isa = XCBuildConfiguration; 424 | buildSettings = { 425 | ALWAYS_SEARCH_USER_PATHS = NO; 426 | CLANG_ANALYZER_NONNULL = YES; 427 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 428 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 429 | CLANG_CXX_LIBRARY = "libc++"; 430 | CLANG_ENABLE_MODULES = YES; 431 | CLANG_ENABLE_OBJC_ARC = YES; 432 | CLANG_ENABLE_OBJC_WEAK = YES; 433 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 434 | CLANG_WARN_BOOL_CONVERSION = YES; 435 | CLANG_WARN_COMMA = YES; 436 | CLANG_WARN_CONSTANT_CONVERSION = YES; 437 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 438 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 439 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 440 | CLANG_WARN_EMPTY_BODY = YES; 441 | CLANG_WARN_ENUM_CONVERSION = YES; 442 | CLANG_WARN_INFINITE_RECURSION = YES; 443 | CLANG_WARN_INT_CONVERSION = YES; 444 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 445 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 446 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 447 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 448 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 449 | CLANG_WARN_STRICT_PROTOTYPES = YES; 450 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 451 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 452 | CLANG_WARN_UNREACHABLE_CODE = YES; 453 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 454 | COPY_PHASE_STRIP = NO; 455 | DEBUG_INFORMATION_FORMAT = dwarf; 456 | ENABLE_STRICT_OBJC_MSGSEND = YES; 457 | ENABLE_TESTABILITY = YES; 458 | GCC_C_LANGUAGE_STANDARD = gnu11; 459 | GCC_DYNAMIC_NO_PIC = NO; 460 | GCC_NO_COMMON_BLOCKS = YES; 461 | GCC_OPTIMIZATION_LEVEL = 0; 462 | GCC_PREPROCESSOR_DEFINITIONS = ( 463 | "DEBUG=1", 464 | "$(inherited)", 465 | ); 466 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 467 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 468 | GCC_WARN_UNDECLARED_SELECTOR = YES; 469 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 470 | GCC_WARN_UNUSED_FUNCTION = YES; 471 | GCC_WARN_UNUSED_VARIABLE = YES; 472 | MACOSX_DEPLOYMENT_TARGET = 10.15; 473 | MTL_ENABLE_DEBUG_INFO = INCLUDE_SOURCE; 474 | MTL_FAST_MATH = YES; 475 | ONLY_ACTIVE_ARCH = YES; 476 | SDKROOT = macosx; 477 | SWIFT_ACTIVE_COMPILATION_CONDITIONS = DEBUG; 478 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 479 | }; 480 | name = Debug; 481 | }; 482 | DD609272240440BF0016ECB3 /* Release */ = { 483 | isa = XCBuildConfiguration; 484 | buildSettings = { 485 | ALWAYS_SEARCH_USER_PATHS = NO; 486 | CLANG_ANALYZER_NONNULL = YES; 487 | CLANG_ANALYZER_NUMBER_OBJECT_CONVERSION = YES_AGGRESSIVE; 488 | CLANG_CXX_LANGUAGE_STANDARD = "gnu++14"; 489 | CLANG_CXX_LIBRARY = "libc++"; 490 | CLANG_ENABLE_MODULES = YES; 491 | CLANG_ENABLE_OBJC_ARC = YES; 492 | CLANG_ENABLE_OBJC_WEAK = YES; 493 | CLANG_WARN_BLOCK_CAPTURE_AUTORELEASING = YES; 494 | CLANG_WARN_BOOL_CONVERSION = YES; 495 | CLANG_WARN_COMMA = YES; 496 | CLANG_WARN_CONSTANT_CONVERSION = YES; 497 | CLANG_WARN_DEPRECATED_OBJC_IMPLEMENTATIONS = YES; 498 | CLANG_WARN_DIRECT_OBJC_ISA_USAGE = YES_ERROR; 499 | CLANG_WARN_DOCUMENTATION_COMMENTS = YES; 500 | CLANG_WARN_EMPTY_BODY = YES; 501 | CLANG_WARN_ENUM_CONVERSION = YES; 502 | CLANG_WARN_INFINITE_RECURSION = YES; 503 | CLANG_WARN_INT_CONVERSION = YES; 504 | CLANG_WARN_NON_LITERAL_NULL_CONVERSION = YES; 505 | CLANG_WARN_OBJC_IMPLICIT_RETAIN_SELF = YES; 506 | CLANG_WARN_OBJC_LITERAL_CONVERSION = YES; 507 | CLANG_WARN_OBJC_ROOT_CLASS = YES_ERROR; 508 | CLANG_WARN_RANGE_LOOP_ANALYSIS = YES; 509 | CLANG_WARN_STRICT_PROTOTYPES = YES; 510 | CLANG_WARN_SUSPICIOUS_MOVE = YES; 511 | CLANG_WARN_UNGUARDED_AVAILABILITY = YES_AGGRESSIVE; 512 | CLANG_WARN_UNREACHABLE_CODE = YES; 513 | CLANG_WARN__DUPLICATE_METHOD_MATCH = YES; 514 | COPY_PHASE_STRIP = NO; 515 | DEBUG_INFORMATION_FORMAT = "dwarf-with-dsym"; 516 | ENABLE_NS_ASSERTIONS = NO; 517 | ENABLE_STRICT_OBJC_MSGSEND = YES; 518 | GCC_C_LANGUAGE_STANDARD = gnu11; 519 | GCC_NO_COMMON_BLOCKS = YES; 520 | GCC_WARN_64_TO_32_BIT_CONVERSION = YES; 521 | GCC_WARN_ABOUT_RETURN_TYPE = YES_ERROR; 522 | GCC_WARN_UNDECLARED_SELECTOR = YES; 523 | GCC_WARN_UNINITIALIZED_AUTOS = YES_AGGRESSIVE; 524 | GCC_WARN_UNUSED_FUNCTION = YES; 525 | GCC_WARN_UNUSED_VARIABLE = YES; 526 | MACOSX_DEPLOYMENT_TARGET = 10.15; 527 | MTL_ENABLE_DEBUG_INFO = NO; 528 | MTL_FAST_MATH = YES; 529 | SDKROOT = macosx; 530 | SWIFT_COMPILATION_MODE = wholemodule; 531 | SWIFT_OPTIMIZATION_LEVEL = "-O"; 532 | }; 533 | name = Release; 534 | }; 535 | DD609274240440BF0016ECB3 /* Debug */ = { 536 | isa = XCBuildConfiguration; 537 | buildSettings = { 538 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 539 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 540 | CODE_SIGN_ENTITLEMENTS = CloudKitchenSink20/Resources/CloudKitchenSink20.entitlements; 541 | CODE_SIGN_STYLE = Automatic; 542 | COMBINE_HIDPI_IMAGES = YES; 543 | DEVELOPMENT_ASSET_PATHS = "\"CloudKitchenSink20/Preview Content\""; 544 | DEVELOPMENT_TEAM = 8C7439RJLG; 545 | ENABLE_HARDENED_RUNTIME = YES; 546 | ENABLE_PREVIEWS = YES; 547 | INFOPLIST_FILE = CloudKitchenSink20/Resources/Info.plist; 548 | LD_RUNPATH_SEARCH_PATHS = ( 549 | "$(inherited)", 550 | "@executable_path/../Frameworks", 551 | ); 552 | MACOSX_DEPLOYMENT_TARGET = 10.15; 553 | PRODUCT_BUNDLE_IDENTIFIER = codes.rambo.CloudKitchenSink20; 554 | PRODUCT_NAME = "$(TARGET_NAME)"; 555 | SWIFT_VERSION = 5.0; 556 | }; 557 | name = Debug; 558 | }; 559 | DD609275240440BF0016ECB3 /* Release */ = { 560 | isa = XCBuildConfiguration; 561 | buildSettings = { 562 | ALWAYS_EMBED_SWIFT_STANDARD_LIBRARIES = YES; 563 | ASSETCATALOG_COMPILER_APPICON_NAME = AppIcon; 564 | CODE_SIGN_ENTITLEMENTS = CloudKitchenSink20/Resources/CloudKitchenSink20.entitlements; 565 | CODE_SIGN_STYLE = Automatic; 566 | COMBINE_HIDPI_IMAGES = YES; 567 | DEVELOPMENT_ASSET_PATHS = "\"CloudKitchenSink20/Preview Content\""; 568 | DEVELOPMENT_TEAM = 8C7439RJLG; 569 | ENABLE_HARDENED_RUNTIME = YES; 570 | ENABLE_PREVIEWS = YES; 571 | INFOPLIST_FILE = CloudKitchenSink20/Resources/Info.plist; 572 | LD_RUNPATH_SEARCH_PATHS = ( 573 | "$(inherited)", 574 | "@executable_path/../Frameworks", 575 | ); 576 | MACOSX_DEPLOYMENT_TARGET = 10.15; 577 | PRODUCT_BUNDLE_IDENTIFIER = codes.rambo.CloudKitchenSink20; 578 | PRODUCT_NAME = "$(TARGET_NAME)"; 579 | SWIFT_VERSION = 5.0; 580 | }; 581 | name = Release; 582 | }; 583 | DD9D3BD4240447CB003C7C91 /* Debug */ = { 584 | isa = XCBuildConfiguration; 585 | buildSettings = { 586 | CLANG_ENABLE_MODULES = YES; 587 | CODE_SIGN_STYLE = Automatic; 588 | COMBINE_HIDPI_IMAGES = YES; 589 | CURRENT_PROJECT_VERSION = 1; 590 | DEFINES_MODULE = YES; 591 | DEVELOPMENT_TEAM = 8C7439RJLG; 592 | DYLIB_COMPATIBILITY_VERSION = 1; 593 | DYLIB_CURRENT_VERSION = 1; 594 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 595 | INFOPLIST_FILE = KitchenCore/Info.plist; 596 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 597 | LD_RUNPATH_SEARCH_PATHS = ( 598 | "$(inherited)", 599 | "@executable_path/../Frameworks", 600 | "@loader_path/Frameworks", 601 | ); 602 | PRODUCT_BUNDLE_IDENTIFIER = codes.rambo.KitchenCore; 603 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 604 | SKIP_INSTALL = YES; 605 | SWIFT_OPTIMIZATION_LEVEL = "-Onone"; 606 | SWIFT_VERSION = 5.0; 607 | VERSIONING_SYSTEM = "apple-generic"; 608 | VERSION_INFO_PREFIX = ""; 609 | }; 610 | name = Debug; 611 | }; 612 | DD9D3BD5240447CB003C7C91 /* Release */ = { 613 | isa = XCBuildConfiguration; 614 | buildSettings = { 615 | CLANG_ENABLE_MODULES = YES; 616 | CODE_SIGN_STYLE = Automatic; 617 | COMBINE_HIDPI_IMAGES = YES; 618 | CURRENT_PROJECT_VERSION = 1; 619 | DEFINES_MODULE = YES; 620 | DEVELOPMENT_TEAM = 8C7439RJLG; 621 | DYLIB_COMPATIBILITY_VERSION = 1; 622 | DYLIB_CURRENT_VERSION = 1; 623 | DYLIB_INSTALL_NAME_BASE = "@rpath"; 624 | INFOPLIST_FILE = KitchenCore/Info.plist; 625 | INSTALL_PATH = "$(LOCAL_LIBRARY_DIR)/Frameworks"; 626 | LD_RUNPATH_SEARCH_PATHS = ( 627 | "$(inherited)", 628 | "@executable_path/../Frameworks", 629 | "@loader_path/Frameworks", 630 | ); 631 | PRODUCT_BUNDLE_IDENTIFIER = codes.rambo.KitchenCore; 632 | PRODUCT_NAME = "$(TARGET_NAME:c99extidentifier)"; 633 | SKIP_INSTALL = YES; 634 | SWIFT_VERSION = 5.0; 635 | VERSIONING_SYSTEM = "apple-generic"; 636 | VERSION_INFO_PREFIX = ""; 637 | }; 638 | name = Release; 639 | }; 640 | /* End XCBuildConfiguration section */ 641 | 642 | /* Begin XCConfigurationList section */ 643 | DD60925B240440BE0016ECB3 /* Build configuration list for PBXProject "CloudKitchenSink20" */ = { 644 | isa = XCConfigurationList; 645 | buildConfigurations = ( 646 | DD609271240440BF0016ECB3 /* Debug */, 647 | DD609272240440BF0016ECB3 /* Release */, 648 | ); 649 | defaultConfigurationIsVisible = 0; 650 | defaultConfigurationName = Release; 651 | }; 652 | DD609273240440BF0016ECB3 /* Build configuration list for PBXNativeTarget "CloudKitchenSink20" */ = { 653 | isa = XCConfigurationList; 654 | buildConfigurations = ( 655 | DD609274240440BF0016ECB3 /* Debug */, 656 | DD609275240440BF0016ECB3 /* Release */, 657 | ); 658 | defaultConfigurationIsVisible = 0; 659 | defaultConfigurationName = Release; 660 | }; 661 | DD9D3BD6240447CB003C7C91 /* Build configuration list for PBXNativeTarget "KitchenCore" */ = { 662 | isa = XCConfigurationList; 663 | buildConfigurations = ( 664 | DD9D3BD4240447CB003C7C91 /* Debug */, 665 | DD9D3BD5240447CB003C7C91 /* Release */, 666 | ); 667 | defaultConfigurationIsVisible = 0; 668 | defaultConfigurationName = Release; 669 | }; 670 | /* End XCConfigurationList section */ 671 | }; 672 | rootObject = DD609258240440BE0016ECB3 /* Project object */; 673 | } 674 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Bootstrap/AppDelegate.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AppDelegate.swift 3 | // CloudKitchenSink20 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Cocoa 10 | import SwiftUI 11 | import KitchenCore 12 | 13 | @NSApplicationMain 14 | class AppDelegate: NSObject, NSApplicationDelegate { 15 | 16 | var window: NSWindow! 17 | 18 | private let store = RecipeStore() 19 | 20 | func applicationDidFinishLaunching(_ aNotification: Notification) { 21 | let contentView = RecipeListView().environmentObject(store) 22 | 23 | window = NSWindow( 24 | contentRect: NSRect(x: 0, y: 0, width: 480, height: 300), 25 | styleMask: [.titled, .closable, .miniaturizable, .resizable, .fullSizeContentView], 26 | backing: .buffered, defer: false) 27 | window.center() 28 | window.setFrameAutosaveName("Main Window") 29 | window.contentView = NSHostingView(rootView: contentView) 30 | window.makeKeyAndOrderFront(nil) 31 | window.title = "Recipes" 32 | 33 | NSApp.registerForRemoteNotifications() 34 | } 35 | 36 | func application(_ application: NSApplication, didReceiveRemoteNotification userInfo: [String : Any]) { 37 | store.processSubscriptionNotification(with: userInfo) 38 | } 39 | 40 | 41 | } 42 | 43 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Preview Content/Preview Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/1.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insidegui/CloudKitchenSink20/9f0d253220ddb81d8c0f18211c187ac97e21cf96/CloudKitchenSink20/Resources/1.jpg -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/2.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insidegui/CloudKitchenSink20/9f0d253220ddb81d8c0f18211c187ac97e21cf96/CloudKitchenSink20/Resources/2.jpg -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/Assets.xcassets/AppIcon.appiconset/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "images" : [ 3 | { 4 | "idiom" : "mac", 5 | "size" : "16x16", 6 | "scale" : "1x" 7 | }, 8 | { 9 | "idiom" : "mac", 10 | "size" : "16x16", 11 | "scale" : "2x" 12 | }, 13 | { 14 | "idiom" : "mac", 15 | "size" : "32x32", 16 | "scale" : "1x" 17 | }, 18 | { 19 | "idiom" : "mac", 20 | "size" : "32x32", 21 | "scale" : "2x" 22 | }, 23 | { 24 | "idiom" : "mac", 25 | "size" : "128x128", 26 | "scale" : "1x" 27 | }, 28 | { 29 | "idiom" : "mac", 30 | "size" : "128x128", 31 | "scale" : "2x" 32 | }, 33 | { 34 | "idiom" : "mac", 35 | "size" : "256x256", 36 | "scale" : "1x" 37 | }, 38 | { 39 | "idiom" : "mac", 40 | "size" : "256x256", 41 | "scale" : "2x" 42 | }, 43 | { 44 | "idiom" : "mac", 45 | "size" : "512x512", 46 | "scale" : "1x" 47 | }, 48 | { 49 | "idiom" : "mac", 50 | "size" : "512x512", 51 | "scale" : "2x" 52 | } 53 | ], 54 | "info" : { 55 | "version" : 1, 56 | "author" : "xcode" 57 | } 58 | } -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/Assets.xcassets/Contents.json: -------------------------------------------------------------------------------- 1 | { 2 | "info" : { 3 | "version" : 1, 4 | "author" : "xcode" 5 | } 6 | } -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/Base.lproj/Main.storyboard: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | 38 | 39 | 40 | 41 | 42 | 43 | 44 | 45 | 46 | 47 | 48 | 49 | 50 | 51 | 52 | 53 | 54 | 55 | 56 | 57 | 58 | 59 | 60 | 61 | 62 | 63 | 64 | 65 | 66 | 67 | 68 | 69 | 70 | 71 | 72 | 73 | 74 | 75 | 76 | 77 | 78 | 79 | 80 | 81 | 82 | 83 | 84 | 85 | 86 | 87 | 88 | 89 | 90 | 91 | 92 | 93 | 94 | 95 | 96 | 97 | 98 | 99 | 100 | 101 | 102 | 103 | 104 | 105 | 106 | 107 | 108 | 109 | 110 | 111 | 112 | 113 | 114 | 115 | 116 | 117 | 118 | 119 | 120 | 121 | 122 | 123 | 124 | 125 | 126 | 127 | 128 | 129 | 130 | 131 | 132 | 133 | 134 | 135 | 136 | 137 | 138 | 139 | 140 | 141 | 142 | 143 | 144 | 145 | 146 | 147 | 148 | 149 | 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | 161 | 162 | 163 | 164 | 165 | 166 | 167 | 168 | 169 | 170 | 171 | 172 | 173 | 174 | 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | 184 | 185 | 186 | 187 | 188 | 189 | 190 | 191 | 192 | 193 | 194 | 195 | 196 | 197 | 198 | 199 | 200 | 201 | 202 | 203 | 204 | 205 | 206 | 207 | 208 | 209 | 210 | 211 | 212 | 213 | 214 | 215 | 216 | 217 | 218 | 219 | 220 | 221 | 222 | 223 | 224 | 225 | 226 | 227 | 228 | 229 | 230 | 231 | 232 | 233 | 234 | 235 | 236 | 237 | 238 | 239 | 240 | 241 | 242 | 243 | 244 | 245 | 246 | 247 | 248 | 249 | 250 | 251 | 252 | 253 | 254 | 255 | 256 | 257 | 258 | 259 | 260 | 261 | 262 | 263 | 264 | 265 | 266 | 267 | 268 | 269 | 270 | 271 | 272 | 273 | 274 | 275 | 276 | 277 | 278 | 279 | 280 | 281 | 282 | 283 | 284 | 285 | 286 | 287 | 288 | 289 | 290 | 291 | 292 | 293 | 294 | 295 | 296 | 297 | 298 | 299 | 300 | 301 | 302 | 303 | 304 | 305 | 306 | 307 | 308 | 309 | 310 | 311 | 312 | 313 | 314 | 315 | 316 | 317 | 318 | 319 | 320 | 321 | 322 | 323 | 324 | 325 | 326 | 327 | 328 | 329 | 330 | 331 | 332 | 333 | 334 | 335 | 336 | 337 | 338 | 339 | 340 | 341 | 342 | 343 | 344 | 345 | 346 | 347 | 348 | 349 | 350 | 351 | 352 | 353 | 354 | 355 | 356 | 357 | 358 | 359 | 360 | 361 | 362 | 363 | 364 | 365 | 366 | 367 | 368 | 369 | 370 | 371 | 372 | 373 | 374 | 375 | 376 | 377 | 378 | 379 | 380 | 381 | 382 | 383 | 384 | 385 | 386 | 387 | 388 | 389 | 390 | 391 | 392 | 393 | 394 | 395 | 396 | 397 | 398 | 399 | 400 | 401 | 402 | 403 | 404 | 405 | 406 | 407 | 408 | 409 | 410 | 411 | 412 | 413 | 414 | 415 | 416 | 417 | 418 | 419 | 420 | 421 | 422 | 423 | 424 | 425 | 426 | 427 | 428 | 429 | 430 | 431 | 432 | 433 | 434 | 435 | 436 | 437 | 438 | 439 | 440 | 441 | 442 | 443 | 444 | 445 | 446 | 447 | 448 | 449 | 450 | 451 | 452 | 453 | 454 | 455 | 456 | 457 | 458 | 459 | 460 | 461 | 462 | 463 | 464 | 465 | 466 | 467 | 468 | 469 | 470 | 471 | 472 | 473 | 474 | 475 | 476 | 477 | 478 | 479 | 480 | 481 | 482 | 483 | 484 | 485 | 486 | 487 | 488 | 489 | 490 | 491 | 492 | 493 | 494 | 495 | 496 | 497 | 498 | 499 | 500 | 501 | 502 | 503 | 504 | 505 | 506 | 507 | 508 | 509 | 510 | 511 | 512 | 513 | 514 | 515 | 516 | 517 | 518 | 519 | 520 | 521 | 522 | 523 | 524 | 525 | 526 | 527 | 528 | Default 529 | 530 | 531 | 532 | 533 | 534 | 535 | Left to Right 536 | 537 | 538 | 539 | 540 | 541 | 542 | Right to Left 543 | 544 | 545 | 546 | 547 | 548 | 549 | 550 | 551 | 552 | 553 | Default 554 | 555 | 556 | 557 | 558 | 559 | 560 | Left to Right 561 | 562 | 563 | 564 | 565 | 566 | 567 | Right to Left 568 | 569 | 570 | 571 | 572 | 573 | 574 | 575 | 576 | 577 | 578 | 579 | 580 | 581 | 582 | 583 | 584 | 585 | 586 | 587 | 588 | 589 | 590 | 591 | 592 | 593 | 594 | 595 | 596 | 597 | 598 | 599 | 600 | 601 | 602 | 603 | 604 | 605 | 606 | 607 | 608 | 609 | 610 | 611 | 612 | 613 | 614 | 615 | 616 | 617 | 618 | 619 | 620 | 621 | 622 | 623 | 624 | 625 | 626 | 627 | 628 | 629 | 630 | 631 | 632 | 633 | 634 | 635 | 636 | 637 | 638 | 639 | 640 | 641 | 642 | 643 | 644 | 645 | 646 | 647 | 648 | 649 | 650 | 651 | 652 | 653 | 654 | 655 | 656 | 657 | 658 | 659 | 660 | 661 | 662 | 663 | 664 | 665 | 666 | 667 | 668 | 669 | 670 | 671 | 672 | 673 | 674 | 675 | 676 | 677 | 678 | 679 | 680 | 681 | 682 | 683 | 684 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/CloudKitchenSink20.entitlements: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | com.apple.developer.aps-environment 6 | development 7 | com.apple.developer.icloud-container-identifiers 8 | 9 | iCloud.codes.rambo.CloudKitchenSink20 10 | 11 | com.apple.developer.icloud-services 12 | 13 | CloudKit 14 | 15 | com.apple.security.app-sandbox 16 | 17 | com.apple.security.application-groups 18 | 19 | $(TeamIdentifierPrefix)group.codes.rambo.CloudKitchenSink20 20 | 21 | com.apple.security.files.user-selected.read-only 22 | 23 | com.apple.security.network.client 24 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Resources/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIconFile 10 | 11 | CFBundleIdentifier 12 | $(PRODUCT_BUNDLE_IDENTIFIER) 13 | CFBundleInfoDictionaryVersion 14 | 6.0 15 | CFBundleName 16 | $(PRODUCT_NAME) 17 | CFBundlePackageType 18 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 19 | CFBundleShortVersionString 20 | 1.0 21 | CFBundleVersion 22 | 1 23 | LSMinimumSystemVersion 24 | $(MACOSX_DEPLOYMENT_TARGET) 25 | NSHumanReadableCopyright 26 | Copyright © 2020 Guilherme Rambo. All rights reserved. 27 | NSMainStoryboardFile 28 | Main 29 | NSPrincipalClass 30 | NSApplication 31 | NSSupportsAutomaticTermination 32 | 33 | NSSupportsSuddenTermination 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /CloudKitchenSink20/ViewModels/AddRecipeViewModel.swift: -------------------------------------------------------------------------------- 1 | // 2 | // AddRecipeViewModel.swift 3 | // CloudKitchenSink20 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import Combine 11 | import KitchenCore 12 | import Cocoa 13 | 14 | final class AddRecipeViewModel: ObservableObject { 15 | private var initialRecipe: Recipe? 16 | 17 | private var id: String = UUID().uuidString 18 | @Published var title: String = "" 19 | @Published var subtitle: String = "" 20 | @Published var ingredients: String = "" 21 | @Published var instructions: String = "" 22 | @Published var image: Data? = nil 23 | 24 | var platformImage: NSImage? { 25 | guard let data = image else { return nil } 26 | return NSImage(data: data) 27 | } 28 | 29 | var imageURL: URL? { 30 | get { nil } 31 | set { 32 | guard let url = newValue else { 33 | self.image = nil 34 | return 35 | } 36 | self.image = try? Data(contentsOf: url) 37 | } 38 | } 39 | 40 | init(recipe: Recipe? = nil) { 41 | self.initialRecipe = recipe 42 | self.id = recipe?.id ?? UUID().uuidString 43 | self.title = recipe?.title ?? "" 44 | self.subtitle = recipe?.subtitle ?? "" 45 | self.ingredients = recipe?.ingredients.joined(separator: ",") ?? "" 46 | self.instructions = recipe?.instructions ?? "" 47 | self.image = recipe?.image 48 | } 49 | 50 | var recipe: Recipe { 51 | Recipe( 52 | id: initialRecipe?.id ?? id, 53 | createdAt: Date(), 54 | title: title, 55 | subtitle: subtitle, 56 | ingredients: ingredients.components(separatedBy: ","), 57 | instructions: instructions, 58 | image: image 59 | ) 60 | } 61 | } 62 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Views/Aux/DropZoneView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // DropZoneView.swift 3 | // CloudKitchenSink20 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Cocoa 10 | import SwiftUI 11 | import CoreServices 12 | 13 | struct DropZoneView: View { 14 | @State private(set) var url: URL? 15 | @State private(set) var image: NSImage? = nil 16 | @State private var isActive = false 17 | 18 | var dropHandler: (URL) -> Void = { _ in } 19 | 20 | var body: some View { 21 | Group { 22 | ZStack { 23 | if image != nil { 24 | Image(nsImage: image!).resizable() 25 | } 26 | Text("Drop image here") 27 | .padding(4) 28 | .background(Color(NSColor(calibratedWhite: 0.1, alpha: 0.7))) 29 | .foregroundColor(Color(.secondaryLabelColor)) 30 | .cornerRadius(4) 31 | } 32 | } 33 | .frame(width: 120, height: 120) 34 | .background(Color(self.isActive ? .systemBlue : .quaternaryLabelColor)) 35 | .cornerRadius(6) 36 | .onDrop( 37 | of: [kUTTypeFileURL as String], 38 | delegate: ImageDropDelegate( 39 | url: $url, 40 | image: $image, 41 | isActive: $isActive, 42 | handler: self.dropHandler 43 | ) 44 | ) 45 | } 46 | } 47 | 48 | struct DropZoneView_Previews: PreviewProvider { 49 | static var previews: some View { 50 | DropZoneView(url: nil, image: nil) 51 | } 52 | } 53 | 54 | fileprivate struct ImageDropDelegate: DropDelegate { 55 | @Binding var url: URL? 56 | @Binding var image: NSImage? 57 | @Binding var isActive: Bool 58 | var handler: (URL) -> Void = { _ in } 59 | 60 | func validateDrop(info: DropInfo) -> Bool { 61 | info.hasItemsConforming(to: [kUTTypeFileURL as String]) 62 | } 63 | 64 | func dropEntered(info: DropInfo) { 65 | isActive = true 66 | } 67 | 68 | func dropUpdated(info: DropInfo) -> DropProposal? { 69 | isActive = true 70 | 71 | return nil 72 | } 73 | 74 | func dropExited(info: DropInfo) { 75 | isActive = false 76 | } 77 | 78 | func performDrop(info: DropInfo) -> Bool { 79 | guard let provider = info.itemProviders(for: [kUTTypeFileURL as String]).first else { return false } 80 | 81 | provider.loadItem(forTypeIdentifier: kUTTypeFileURL as String, options: nil) { data, error in 82 | guard let data = data as? Data else { 83 | if let error = error { 84 | print(error) 85 | } 86 | return 87 | } 88 | 89 | guard let url = URL(dataRepresentation: data, relativeTo: nil) else { return } 90 | 91 | DispatchQueue.main.async { 92 | self.url = url 93 | self.image = NSImage(contentsOf: url) 94 | self.handler(url) 95 | } 96 | } 97 | 98 | return true 99 | } 100 | 101 | 102 | } 103 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Views/Aux/MacEditorTextView.swift: -------------------------------------------------------------------------------- 1 | /** 2 | * MacEditorTextView 3 | * Copyright (c) Thiago Holanda 2020 4 | * https://twitter.com/tholanda 5 | * 6 | * MIT license 7 | */ 8 | 9 | import Combine 10 | import SwiftUI 11 | 12 | struct MacEditorTextView: NSViewRepresentable { 13 | @Binding var text: String 14 | 15 | var onEditingChanged : () -> Void = {} 16 | var onCommit : () -> Void = {} 17 | var onTextChange : (String) -> Void = { _ in } 18 | 19 | func makeCoordinator() -> Coordinator { 20 | Coordinator(self) 21 | } 22 | 23 | func makeNSView(context: Context) -> CustomTextView { 24 | let textView = CustomTextView(text: self.text) 25 | textView.delegate = context.coordinator 26 | 27 | return textView 28 | } 29 | 30 | func updateNSView(_ view: CustomTextView, context: Context) { 31 | view.text = text 32 | view.selectedRanges = context.coordinator.selectedRanges 33 | } 34 | } 35 | 36 | #if DEBUG 37 | struct MacEditorTextView_Previews: PreviewProvider { 38 | static var previews: some View { 39 | Group { 40 | MacEditorTextView(text: .constant("{ \n planets { \n name \n }\n}")) 41 | .environment(\.colorScheme, .dark) 42 | .previewDisplayName("Dark Mode") 43 | 44 | MacEditorTextView(text: .constant("{ \n planets { \n name \n }\n}")) 45 | .environment(\.colorScheme, .light) 46 | .previewDisplayName("Light Mode") 47 | } 48 | } 49 | } 50 | #endif 51 | 52 | extension MacEditorTextView { 53 | class Coordinator: NSObject, NSTextViewDelegate { 54 | var parent: MacEditorTextView 55 | var selectedRanges: [NSValue] = [] 56 | 57 | init(_ parent: MacEditorTextView) { 58 | self.parent = parent 59 | } 60 | 61 | func textDidBeginEditing(_ notification: Notification) { 62 | guard let textView = notification.object as? NSTextView else { 63 | return 64 | } 65 | 66 | self.parent.text = textView.string 67 | self.parent.onEditingChanged() 68 | } 69 | 70 | func textDidChange(_ notification: Notification) { 71 | guard let textView = notification.object as? NSTextView else { 72 | return 73 | } 74 | 75 | self.parent.text = textView.string 76 | self.selectedRanges = textView.selectedRanges 77 | } 78 | 79 | func textDidEndEditing(_ notification: Notification) { 80 | guard let textView = notification.object as? NSTextView else { 81 | return 82 | } 83 | 84 | self.parent.text = textView.string 85 | self.parent.onCommit() 86 | } 87 | } 88 | } 89 | 90 | final class CustomTextView: NSView { 91 | private var isEditable: Bool 92 | private var font: NSFont 93 | 94 | weak var delegate: NSTextViewDelegate? 95 | 96 | var text: String { 97 | didSet { 98 | textView.string = text 99 | } 100 | } 101 | 102 | var selectedRanges: [NSValue] = [] { 103 | didSet { 104 | guard selectedRanges.count > 0 else { 105 | return 106 | } 107 | 108 | textView.selectedRanges = selectedRanges 109 | } 110 | } 111 | 112 | private lazy var scrollView: NSScrollView = { 113 | let scrollView = NSScrollView() 114 | scrollView.drawsBackground = true 115 | scrollView.borderType = .noBorder 116 | scrollView.hasVerticalScroller = true 117 | scrollView.hasHorizontalRuler = false 118 | scrollView.autoresizingMask = [.width, .height] 119 | scrollView.translatesAutoresizingMaskIntoConstraints = false 120 | 121 | return scrollView 122 | }() 123 | 124 | private lazy var textView: NSTextView = { 125 | let contentSize = scrollView.contentSize 126 | let textStorage = NSTextStorage() 127 | 128 | 129 | let layoutManager = NSLayoutManager() 130 | textStorage.addLayoutManager(layoutManager) 131 | 132 | 133 | let textContainer = NSTextContainer(containerSize: scrollView.frame.size) 134 | textContainer.widthTracksTextView = true 135 | textContainer.containerSize = NSSize( 136 | width: contentSize.width, 137 | height: CGFloat.greatestFiniteMagnitude 138 | ) 139 | 140 | layoutManager.addTextContainer(textContainer) 141 | 142 | 143 | let textView = NSTextView(frame: .zero, textContainer: textContainer) 144 | textView.autoresizingMask = .width 145 | textView.backgroundColor = NSColor.textBackgroundColor 146 | textView.delegate = self.delegate 147 | textView.drawsBackground = true 148 | textView.font = self.font 149 | textView.isEditable = self.isEditable 150 | textView.isHorizontallyResizable = false 151 | textView.isVerticallyResizable = true 152 | textView.maxSize = NSSize(width: CGFloat.greatestFiniteMagnitude, height: CGFloat.greatestFiniteMagnitude) 153 | textView.minSize = NSSize(width: 0, height: contentSize.height) 154 | textView.textColor = NSColor.labelColor 155 | 156 | return textView 157 | }() 158 | 159 | // MARK: - Init 160 | init(text: String, isEditable: Bool = true, font: NSFont = NSFont.monospacedDigitSystemFont(ofSize: 14, weight: .regular)) { 161 | self.font = font 162 | self.isEditable = isEditable 163 | self.text = text 164 | 165 | super.init(frame: .zero) 166 | } 167 | 168 | required init?(coder: NSCoder) { 169 | fatalError("init(coder:) has not been implemented") 170 | } 171 | 172 | // MARK: - Life cycle 173 | 174 | override func viewWillDraw() { 175 | super.viewWillDraw() 176 | 177 | setupScrollViewConstraints() 178 | setupTextView() 179 | } 180 | 181 | func setupScrollViewConstraints() { 182 | scrollView.translatesAutoresizingMaskIntoConstraints = false 183 | 184 | addSubview(scrollView) 185 | 186 | NSLayoutConstraint.activate([ 187 | scrollView.topAnchor.constraint(equalTo: topAnchor), 188 | scrollView.trailingAnchor.constraint(equalTo: trailingAnchor), 189 | scrollView.bottomAnchor.constraint(equalTo: bottomAnchor), 190 | scrollView.leadingAnchor.constraint(equalTo: leadingAnchor) 191 | ]) 192 | } 193 | 194 | func setupTextView() { 195 | scrollView.documentView = textView 196 | } 197 | } 198 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Views/RecipeFormView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RecipeFormView.swift 3 | // CloudKitchenSink20 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | import KitchenCore 11 | 12 | struct RecipeFormView: View { 13 | @EnvironmentObject var store: RecipeStore 14 | @ObservedObject private var viewModel = AddRecipeViewModel() 15 | 16 | private let title: String 17 | 18 | var onSave: () -> Void 19 | 20 | init(recipe: Recipe? = nil, onSave: @escaping () -> Void = { }) { 21 | self.viewModel = AddRecipeViewModel(recipe: recipe) 22 | self.title = recipe == nil ? "New Recipe" : "Edit Recipe" 23 | self.onSave = onSave 24 | } 25 | 26 | var body: some View { 27 | VStack(alignment: .leading) { 28 | Text(title).font(.system(.headline, design: .rounded)) 29 | Form { 30 | HStack { 31 | DropZoneView(image: self.viewModel.platformImage, dropHandler: { url in 32 | self.viewModel.imageURL = url 33 | }) 34 | VStack { 35 | TextField("Title", text: $viewModel.title) 36 | TextField("Subtitle", text: $viewModel.subtitle) 37 | TextField("Ingredients", text: $viewModel.ingredients) 38 | } 39 | Spacer() 40 | } 41 | MacEditorTextView(text: $viewModel.instructions) 42 | Button(action: save, label: { Text("Save") }).buttonStyle(DefaultButtonStyle()) 43 | } 44 | } 45 | .padding() 46 | .frame(minWidth: 500, maxWidth: .infinity, minHeight: 300, maxHeight: .infinity) 47 | } 48 | 49 | private func save() { 50 | store.addOrUpdate(viewModel.recipe) 51 | onSave() 52 | } 53 | } 54 | 55 | struct RecipeFormView_Previews: PreviewProvider { 56 | static var previews: some View { 57 | RecipeFormView(recipe: Recipe.previewRecipes[0]) 58 | .environmentObject(RecipeStore(recipes: Recipe.previewRecipes)) 59 | } 60 | } 61 | -------------------------------------------------------------------------------- /CloudKitchenSink20/Views/RecipeListView.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RecipeListView.swift 3 | // CloudKitchenSink20 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import SwiftUI 10 | import KitchenCore 11 | 12 | struct RecipeListView: View { 13 | @EnvironmentObject var store: RecipeStore 14 | 15 | @State private var formVisible = false 16 | 17 | var body: some View { 18 | List { 19 | ForEach(store.recipes.sorted(by: { $0.createdAt > $1.createdAt })) { recipe in 20 | HStack { 21 | if recipe.platformImage != nil { 22 | Image(nsImage: recipe.platformImage!) 23 | .resizable(resizingMode: .stretch) 24 | .frame(width: 60, height: 60) 25 | .cornerRadius(6) 26 | } 27 | VStack(alignment: .leading, spacing: 6) { 28 | Text(recipe.title) 29 | .font(.system(.headline, design: .rounded)) 30 | Text(recipe.subtitle) 31 | .font(.system(size: 14)) 32 | } 33 | Spacer() 34 | } 35 | .padding() 36 | } 37 | } 38 | .frame(minWidth: 200, maxWidth: .infinity, minHeight: 200, maxHeight: .infinity) 39 | .overlay(VStack(alignment: .trailing) { 40 | Spacer() 41 | HStack { 42 | Spacer() 43 | Button(action: { self.formVisible.toggle() }, label: { Text("Add Recipe") }) 44 | } 45 | }.padding()) 46 | .sheet(isPresented: $formVisible, content: { 47 | RecipeFormView(recipe: nil) { 48 | self.formVisible = false 49 | }.environmentObject(self.store) 50 | }) 51 | } 52 | } 53 | 54 | 55 | struct ContentView_Previews: PreviewProvider { 56 | static var previews: some View { 57 | RecipeListView().environmentObject(RecipeStore(recipes: Recipe.previewRecipes)) 58 | } 59 | } 60 | 61 | #if os(macOS) 62 | 63 | extension Recipe { 64 | var platformImage: NSImage? { 65 | guard let data = image else { return nil } 66 | return NSImage(data: data) 67 | } 68 | } 69 | 70 | #else 71 | 72 | import UIKit 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /KitchenCore/Info.plist: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CFBundleDevelopmentRegion 6 | $(DEVELOPMENT_LANGUAGE) 7 | CFBundleExecutable 8 | $(EXECUTABLE_NAME) 9 | CFBundleIdentifier 10 | $(PRODUCT_BUNDLE_IDENTIFIER) 11 | CFBundleInfoDictionaryVersion 12 | 6.0 13 | CFBundleName 14 | $(PRODUCT_NAME) 15 | CFBundlePackageType 16 | $(PRODUCT_BUNDLE_PACKAGE_TYPE) 17 | CFBundleShortVersionString 18 | 1.0 19 | CFBundleVersion 20 | $(CURRENT_PROJECT_VERSION) 21 | NSHumanReadableCopyright 22 | Copyright © 2020 Guilherme Rambo. All rights reserved. 23 | 24 | 25 | -------------------------------------------------------------------------------- /KitchenCore/KitchenCore.h: -------------------------------------------------------------------------------- 1 | // 2 | // KitchenCore.h 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | #import 10 | 11 | //! Project version number for KitchenCore. 12 | FOUNDATION_EXPORT double KitchenCoreVersionNumber; 13 | 14 | //! Project version string for KitchenCore. 15 | FOUNDATION_EXPORT const unsigned char KitchenCoreVersionString[]; 16 | 17 | // In this header, you should import all the public headers of your framework using statements like #import 18 | 19 | 20 | -------------------------------------------------------------------------------- /KitchenCore/Source/Definitions/SyncConstants.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SyncConstants.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CloudKit 11 | 12 | public struct SyncConstants { 13 | 14 | public static let containerIdentifier = "iCloud.codes.rambo.CloudKitchenSink20" 15 | 16 | public static let appGroup = "8C7439RJLG.group.codes.rambo.CloudKitchenSink20" 17 | 18 | public static let subsystemName = "codes.rambo.KitchenCore" 19 | 20 | public static let customZoneID: CKRecordZone.ID = { 21 | CKRecordZone.ID(zoneName: "KitchenZone", ownerName: CKCurrentUserDefaultName) 22 | }() 23 | 24 | } 25 | -------------------------------------------------------------------------------- /KitchenCore/Source/Extensions/Error+CloudKit.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Error+CloudKit.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CloudKit 11 | import os.log 12 | 13 | public extension Error { 14 | 15 | /// Whether this error is a CloudKit server record changed error, representing a record conflict 16 | var isCloudKitConflict: Bool { 17 | let effectiveError = self as? CKError 18 | 19 | return effectiveError?.code == CKError.Code.serverRecordChanged 20 | } 21 | 22 | /// Whether this error represents a "zone not found" or a "user deleted zone" error 23 | var isCloudKitZoneDeleted: Bool { 24 | guard let effectiveError = self as? CKError else { return false } 25 | 26 | return [.zoneNotFound, .userDeletedZone].contains(effectiveError.code) 27 | } 28 | 29 | /// Uses the `resolver` closure to resolve a conflict, returning the conflict-free record 30 | /// 31 | /// - Parameter resolver: A closure that will receive the client record as the first param and the server record as the second param. 32 | /// This closure is responsible for handling the conflict and returning the conflict-free record. 33 | /// - Returns: The conflict-free record returned by `resolver` 34 | func resolveConflict(with resolver: (CKRecord, CKRecord) -> CKRecord?) -> CKRecord? { 35 | guard let effectiveError = self as? CKError else { 36 | os_log("resolveConflict called on an error that was not a CKError. The error was %{public}@", 37 | log: .default, 38 | type: .fault, 39 | String(describing: self)) 40 | return nil 41 | } 42 | 43 | guard effectiveError.code == .serverRecordChanged else { 44 | os_log("resolveConflict called on a CKError that was not a serverRecordChanged error. The error was %{public}@", 45 | log: .default, 46 | type: .fault, 47 | String(describing: effectiveError)) 48 | return nil 49 | } 50 | 51 | guard let clientRecord = effectiveError.userInfo[CKRecordChangedErrorClientRecordKey] as? CKRecord else { 52 | os_log("Failed to obtain client record from serverRecordChanged error. The error was %{public}@", 53 | log: .default, 54 | type: .fault, 55 | String(describing: effectiveError)) 56 | return nil 57 | } 58 | 59 | guard let serverRecord = effectiveError.userInfo[CKRecordChangedErrorServerRecordKey] as? CKRecord else { 60 | os_log("Failed to obtain server record from serverRecordChanged error. The error was %{public}@", 61 | log: .default, 62 | type: .fault, 63 | String(describing: effectiveError)) 64 | return nil 65 | } 66 | 67 | return resolver(clientRecord, serverRecord) 68 | } 69 | 70 | /// Retries a CloudKit operation if the error suggests it 71 | /// 72 | /// - Parameters: 73 | /// - log: The logger to use for logging information about the error handling, uses the default one if not set 74 | /// - block: The block that will execute the operation later if it can be retried 75 | /// - Returns: Whether or not it was possible to retry the operation 76 | @discardableResult func retryCloudKitOperationIfPossible(_ log: OSLog? = nil, with block: @escaping () -> Void) -> Bool { 77 | let effectiveLog: OSLog = log ?? .default 78 | 79 | guard let effectiveError = self as? CKError else { return false } 80 | 81 | guard let retryDelay: Double = effectiveError.retryAfterSeconds else { 82 | os_log("Error is not recoverable", log: effectiveLog, type: .error) 83 | return false 84 | } 85 | 86 | os_log("Error is recoverable. Will retry after %{public}f seconds", log: effectiveLog, type: .error, retryDelay) 87 | 88 | DispatchQueue.main.asyncAfter(deadline: .now() + retryDelay) { 89 | block() 90 | } 91 | 92 | return true 93 | } 94 | 95 | } 96 | 97 | -------------------------------------------------------------------------------- /KitchenCore/Source/Models/Recipe+CloudKit.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Recipe+CloudKit.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CloudKit 11 | 12 | extension CKRecord.RecordType { 13 | static let recipe = "Recipe" 14 | } 15 | 16 | extension Recipe { 17 | 18 | struct RecordError: LocalizedError { 19 | var localizedDescription: String 20 | 21 | static func missingKey(_ key: RecordKey) -> RecordError { 22 | RecordError(localizedDescription: "Missing required key \(key.rawValue)") 23 | } 24 | } 25 | 26 | enum RecordKey: String { 27 | case title 28 | case subtitle 29 | case ingredients 30 | case instructions 31 | case image 32 | } 33 | 34 | var recordID: CKRecord.ID { 35 | CKRecord.ID(recordName: id, zoneID: SyncConstants.customZoneID) 36 | } 37 | 38 | var imageAsset: CKAsset? { 39 | guard let data = image else { return nil } 40 | 41 | let url = FileManager.default.temporaryDirectory.appendingPathComponent(id) 42 | 43 | do { 44 | try data.write(to: url) 45 | } catch { 46 | return nil 47 | } 48 | 49 | return CKAsset(fileURL: url) 50 | } 51 | 52 | var record: CKRecord { 53 | let r = CKRecord(recordType: .recipe, recordID: recordID) 54 | 55 | r[.title] = title 56 | r[.subtitle] = subtitle 57 | r[.ingredients] = ingredients 58 | r[.instructions] = instructions 59 | r[.image] = imageAsset 60 | 61 | return r 62 | } 63 | 64 | init(record: CKRecord) throws { 65 | guard let title = record[.title] as? String else { 66 | throw RecordError.missingKey(.title) 67 | } 68 | guard let subtitle = record[.subtitle] as? String else { 69 | throw RecordError.missingKey(.subtitle) 70 | } 71 | guard let ingredients = record[.ingredients] as? [String] else { 72 | throw RecordError.missingKey(.ingredients) 73 | } 74 | guard let instructions = record[.instructions] as? String else { 75 | throw RecordError.missingKey(.instructions) 76 | } 77 | 78 | var imageData: Data? 79 | 80 | if let imageAsset = record[.image] as? CKAsset { 81 | imageData = imageAsset.data 82 | } 83 | 84 | self.ckData = record.encodedSystemFields 85 | self.id = record.recordID.recordName 86 | self.createdAt = record.creationDate ?? Date() 87 | self.title = title 88 | self.subtitle = subtitle 89 | self.ingredients = ingredients 90 | self.instructions = instructions 91 | self.image = imageData 92 | } 93 | 94 | } 95 | 96 | extension CKRecord { 97 | 98 | var encodedSystemFields: Data { 99 | let coder = NSKeyedArchiver(requiringSecureCoding: true) 100 | encodeSystemFields(with: coder) 101 | coder.finishEncoding() 102 | 103 | return coder.encodedData 104 | } 105 | 106 | } 107 | 108 | extension CKAsset { 109 | var data: Data? { 110 | guard let url = fileURL else { return nil } 111 | return try? Data(contentsOf: url) 112 | } 113 | } 114 | 115 | extension Recipe { 116 | static func resolveConflict(clientRecord: CKRecord, serverRecord: CKRecord) -> CKRecord? { 117 | // Custom logic for resolving conflicts. 118 | // In this example, the client values will always overwrite all server values. 119 | // 120 | // The server record has the latest changeTag and must be returned. 121 | 122 | // Merge all client record keys/values into the server record 123 | for key in clientRecord.allKeys() { 124 | serverRecord[key] = clientRecord[key] 125 | } 126 | 127 | return serverRecord 128 | } 129 | } 130 | 131 | fileprivate extension CKRecord { 132 | subscript(key: Recipe.RecordKey) -> Any? { 133 | get { 134 | return self[key.rawValue] 135 | } 136 | set { 137 | self[key.rawValue] = newValue as? CKRecordValue 138 | } 139 | } 140 | } 141 | 142 | -------------------------------------------------------------------------------- /KitchenCore/Source/Models/Recipe+Preview.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Recipe+Preview.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public extension Recipe { 12 | static let previewRecipes: [Recipe] = [ 13 | Recipe( 14 | id: "1", 15 | createdAt: Date(), 16 | title: "Simple Cajun Seasoning", 17 | subtitle: "A delicious mix of spices that goes great with chicken, french fries, and much more.", 18 | ingredients: [ 19 | "2 1/2 tablespoons salt", 20 | "1 tablespoon dried oregano", 21 | "1 tablespoon paprika", 22 | "1 tablespoon cayenne pepper", 23 | "1 tablespoon ground black pepper" 24 | ], instructions: "Mix all ingredients together and process in a blender or coffee grinder until a fine powder is formed.", 25 | image: Bundle.main.data(named: "1", withExtension: "jpg") 26 | ), 27 | Recipe( 28 | id: "2", 29 | createdAt: Date(), 30 | title: "Spaghetti Carbonara", 31 | subtitle: "The easiest pasta dish you will ever make with just 5 ingredients in 15 min.", 32 | ingredients: [ 33 | "8 ounces spaghetti", 34 | "2 large eggs", 35 | "1/2 cup freshly grated Parmesan", 36 | "4 slices bacon, diced", 37 | "4 cloves garlic, minced", 38 | "Kosher salt and freshly ground black pepper, to taste", 39 | "2 tablespoons chopped fresh parsley leaves" 40 | ], instructions: """ 41 | In a large pot of boiling salted water, cook pasta according to package instructions; reserve 1/2 cup water and drain well. 42 | 43 | In a small bowl, whisk together eggs and Parmesan; set aside. 44 | 45 | Heat a large skillet over medium high heat. Add bacon and cook until brown and crispy, about 6-8 minutes; reserve excess fat. 46 | 47 | Stir in garlic until fragrant, about 1 minute. Reduce heat to low. 48 | 49 | Working quickly, stir in pasta and egg mixture, and gently toss to combine; season with salt and pepper, to taste. Add reserved pasta water, one tablespoon at a time, until desired consistency is reached. 50 | 51 | Serve immediately, garnished with parsley, if desired. 52 | """, 53 | image: Bundle.main.data(named: "2", withExtension: "jpg") 54 | ) 55 | ] 56 | } 57 | 58 | fileprivate extension Bundle { 59 | func data(named name: String, withExtension ext: String) -> Data? { 60 | guard let url = self.url(forResource: name, withExtension: ext) else { return nil } 61 | return try? Data(contentsOf: url) 62 | } 63 | } 64 | -------------------------------------------------------------------------------- /KitchenCore/Source/Models/Recipe.swift: -------------------------------------------------------------------------------- 1 | // 2 | // Recipe.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | 11 | public struct Recipe: Hashable, Codable, Identifiable { 12 | 13 | /// Used to store the encoded `CKRecord.ID` so that local records can be matched with 14 | /// records on the server. This ensures updates don't cause duplication of records. 15 | var ckData: Data? = nil 16 | 17 | public let id: String 18 | public let createdAt: Date 19 | public let title: String 20 | public let subtitle: String 21 | public let ingredients: [String] 22 | public let instructions: String 23 | public let image: Data? 24 | 25 | public init(title: String, 26 | subtitle: String, 27 | ingredients: [String], 28 | instructions: String, 29 | image: Data? = nil) 30 | { 31 | self.id = UUID().uuidString 32 | self.createdAt = Date() 33 | self.title = title 34 | self.subtitle = subtitle 35 | self.ingredients = ingredients 36 | self.instructions = instructions 37 | self.image = image 38 | } 39 | 40 | public init(id: String, 41 | createdAt: Date, 42 | title: String, 43 | subtitle: String, 44 | ingredients: [String], 45 | instructions: String, 46 | image: Data? = nil) 47 | { 48 | self.id = id 49 | self.createdAt = createdAt 50 | self.title = title 51 | self.subtitle = subtitle 52 | self.ingredients = ingredients 53 | self.instructions = instructions 54 | self.image = image 55 | } 56 | 57 | } 58 | -------------------------------------------------------------------------------- /KitchenCore/Source/Storage/RecipeStore.swift: -------------------------------------------------------------------------------- 1 | // 2 | // RecipeStore.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import Combine 11 | import CloudKit 12 | import os.log 13 | 14 | public final class RecipeStore: ObservableObject { 15 | 16 | @Published public private(set) var recipes: [Recipe] = [] 17 | 18 | private let log = OSLog(subsystem: SyncConstants.subsystemName, category: String(describing: RecipeStore.self)) 19 | 20 | private let fileManager = FileManager() 21 | 22 | private let queue = DispatchQueue(label: "RecipeStore") 23 | 24 | private let container: CKContainer 25 | private let defaults: UserDefaults 26 | private var syncEngine: SyncEngine? 27 | 28 | public init(recipes: [Recipe] = []) { 29 | self.container = CKContainer(identifier: SyncConstants.containerIdentifier) 30 | 31 | guard let defaults = UserDefaults(suiteName: SyncConstants.appGroup) else { 32 | fatalError("Invalid app group") 33 | } 34 | self.defaults = defaults 35 | 36 | if !recipes.isEmpty { 37 | self.recipes = recipes 38 | save() 39 | } else { 40 | load() 41 | } 42 | 43 | self.syncEngine = SyncEngine( 44 | defaults: self.defaults, 45 | initialRecipes: self.recipes 46 | ) 47 | 48 | self.syncEngine?.didUpdateModels = { [weak self] recipes in 49 | self?.updateAfterSync(recipes) 50 | } 51 | 52 | self.syncEngine?.didDeleteModels = { [weak self] identifiers in 53 | self?.recipes.removeAll(where: { identifiers.contains($0.id) }) 54 | self?.save() 55 | } 56 | } 57 | 58 | private var storeURL: URL { 59 | let baseURL: URL 60 | 61 | if let containerURL = fileManager.containerURL(forSecurityApplicationGroupIdentifier: SyncConstants.appGroup) { 62 | baseURL = containerURL 63 | } else { 64 | os_log("Failed to get container URL for app security group %@", log: self.log, type: .fault, SyncConstants.appGroup) 65 | 66 | baseURL = fileManager.temporaryDirectory 67 | } 68 | 69 | let url = baseURL.appendingPathComponent("RecipeStore.plist") 70 | 71 | if !fileManager.fileExists(atPath: url.path) { 72 | os_log("Creating store file at %@", log: self.log, type: .debug, url.path) 73 | 74 | if !fileManager.createFile(atPath: url.path, contents: nil, attributes: nil) { 75 | os_log("Failed to create store file at %@", log: self.log, type: .fault, url.path) 76 | } 77 | } 78 | 79 | return url 80 | } 81 | 82 | private func updateAfterSync(_ recipes: [Recipe]) { 83 | os_log("%{public}@", log: log, type: .debug, #function) 84 | 85 | recipes.forEach { updatedRecipe in 86 | guard let idx = self.recipes.firstIndex(where: { $0.id == updatedRecipe.id }) else { return } 87 | self.recipes[idx] = updatedRecipe 88 | } 89 | 90 | save() 91 | } 92 | 93 | public func addOrUpdate(_ recipe: Recipe) { 94 | if let idx = recipes.lastIndex(where: { $0.id == recipe.id }) { 95 | recipes[idx] = recipe 96 | } else { 97 | recipes.append(recipe) 98 | } 99 | 100 | syncEngine?.upload(recipe) 101 | save() 102 | } 103 | 104 | public func delete(with id: String) { 105 | guard let recipe = self.recipe(with: id) else { 106 | os_log("Recipe not found with id %@ for deletion.", log: self.log, type: .error, id) 107 | return 108 | } 109 | 110 | syncEngine?.delete(recipe) 111 | save() 112 | } 113 | 114 | public func recipe(with id: String) -> Recipe? { 115 | recipes.first(where: { $0.id == id }) 116 | } 117 | 118 | private func save() { 119 | os_log("%{public}@", log: log, type: .debug, #function) 120 | 121 | do { 122 | let data = try PropertyListEncoder().encode(recipes) 123 | try data.write(to: storeURL) 124 | } catch { 125 | os_log("Failed to save recipes: %{public}@", log: self.log, type: .error, String(describing: error)) 126 | } 127 | } 128 | 129 | private func load() { 130 | os_log("%{public}@", log: log, type: .debug, #function) 131 | 132 | do { 133 | let data = try Data(contentsOf: storeURL) 134 | 135 | guard !data.isEmpty else { return } 136 | 137 | self.recipes = try PropertyListDecoder().decode([Recipe].self, from: data) 138 | } catch { 139 | os_log("Failed to load recipes: %{public}@", log: self.log, type: .error, String(describing: error)) 140 | } 141 | } 142 | 143 | public func processSubscriptionNotification(with userInfo: [AnyHashable : Any]) { 144 | syncEngine?.processSubscriptionNotification(with: userInfo) 145 | } 146 | 147 | } 148 | -------------------------------------------------------------------------------- /KitchenCore/Source/Storage/SyncEngine+Notifications.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SyncEngine+Notifications.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CloudKit 11 | import os.log 12 | 13 | extension SyncEngine { 14 | 15 | @discardableResult func processSubscriptionNotification(with userInfo: [AnyHashable : Any]) -> Bool { 16 | os_log("%{public}@", log: log, type: .debug, #function) 17 | 18 | guard let notification = CKNotification(fromRemoteNotificationDictionary: userInfo) else { 19 | os_log("Not a CKNotification", log: self.log, type: .error) 20 | return false 21 | } 22 | 23 | guard notification.subscriptionID == privateSubscriptionId else { 24 | os_log("Not our subscription ID", log: self.log, type: .debug) 25 | return false 26 | } 27 | 28 | os_log("Received remote CloudKit notification for user data", log: log, type: .debug) 29 | 30 | fetchRemoteChanges() 31 | 32 | return true 33 | } 34 | } 35 | -------------------------------------------------------------------------------- /KitchenCore/Source/Storage/SyncEngine.swift: -------------------------------------------------------------------------------- 1 | // 2 | // SyncEngine.swift 3 | // KitchenCore 4 | // 5 | // Created by Guilherme Rambo on 24/02/20. 6 | // Copyright © 2020 Guilherme Rambo. All rights reserved. 7 | // 8 | 9 | import Foundation 10 | import CloudKit 11 | import os.log 12 | 13 | final class SyncEngine { 14 | 15 | let log = OSLog(subsystem: SyncConstants.subsystemName, category: String(describing: SyncEngine.self)) 16 | 17 | private let defaults: UserDefaults 18 | 19 | private(set) lazy var container: CKContainer = { 20 | CKContainer(identifier: SyncConstants.containerIdentifier) 21 | }() 22 | 23 | private(set) lazy var privateDatabase: CKDatabase = { 24 | container.privateCloudDatabase 25 | }() 26 | 27 | private(set) lazy var privateSubscriptionId: String = { 28 | return "\(SyncConstants.customZoneID.zoneName).subscription" 29 | }() 30 | 31 | private var buffer: [Recipe] 32 | 33 | /// Called after models are updated with CloudKit data. 34 | var didUpdateModels: ([Recipe]) -> Void = { _ in } 35 | 36 | /// Called when models are deleted remotely. 37 | var didDeleteModels: ([String]) -> Void = { _ in } 38 | 39 | init(defaults: UserDefaults, initialRecipes: [Recipe]) { 40 | self.defaults = defaults 41 | self.buffer = initialRecipes 42 | 43 | start() 44 | } 45 | 46 | private let workQueue = DispatchQueue(label: "SyncEngine.Work", qos: .userInitiated) 47 | private let cloudQueue = DispatchQueue(label: "SyncEngine.Cloud", qos: .userInitiated) 48 | 49 | // MARK: - Setup boilerplate 50 | 51 | private func start() { 52 | prepareCloudEnvironment { [weak self] in 53 | guard let self = self else { return } 54 | 55 | os_log("Cloud environment preparation done", log: self.log, type: .debug) 56 | 57 | self.uploadLocalDataNotUploadedYet() 58 | self.fetchRemoteChanges() 59 | } 60 | } 61 | 62 | private lazy var cloudOperationQueue: OperationQueue = { 63 | let q = OperationQueue() 64 | 65 | q.underlyingQueue = cloudQueue 66 | q.name = "SyncEngine.Cloud" 67 | q.maxConcurrentOperationCount = 1 68 | 69 | return q 70 | }() 71 | 72 | private lazy var createdCustomZoneKey: String = { 73 | return "CREATEDZONE-\(SyncConstants.customZoneID.zoneName)" 74 | }() 75 | 76 | private var createdCustomZone: Bool { 77 | get { 78 | return defaults.bool(forKey: createdCustomZoneKey) 79 | } 80 | set { 81 | defaults.set(newValue, forKey: createdCustomZoneKey) 82 | } 83 | } 84 | 85 | private lazy var createdPrivateSubscriptionKey: String = { 86 | return "CREATEDSUBDB-\(SyncConstants.customZoneID.zoneName)" 87 | }() 88 | 89 | private var createdPrivateSubscription: Bool { 90 | get { 91 | return defaults.bool(forKey: createdPrivateSubscriptionKey) 92 | } 93 | set { 94 | defaults.set(newValue, forKey: createdPrivateSubscriptionKey) 95 | } 96 | } 97 | 98 | private func prepareCloudEnvironment(then block: @escaping () -> Void) { 99 | workQueue.async { [weak self] in 100 | guard let self = self else { return } 101 | 102 | self.createCustomZoneIfNeeded() 103 | self.cloudOperationQueue.waitUntilAllOperationsAreFinished() 104 | guard self.createdCustomZone else { return } 105 | 106 | self.createPrivateSubscriptionsIfNeeded() 107 | self.cloudOperationQueue.waitUntilAllOperationsAreFinished() 108 | guard self.createdPrivateSubscription else { return } 109 | 110 | DispatchQueue.main.async { block() } 111 | } 112 | } 113 | 114 | private func createCustomZoneIfNeeded() { 115 | guard !createdCustomZone else { 116 | os_log("Already have custom zone, skipping creation but checking if zone really exists", log: log, type: .debug) 117 | 118 | checkCustomZone() 119 | 120 | return 121 | } 122 | 123 | os_log("Creating CloudKit zone %@", log: log, type: .info, SyncConstants.customZoneID.zoneName) 124 | 125 | let zone = CKRecordZone(zoneID: SyncConstants.customZoneID) 126 | let operation = CKModifyRecordZonesOperation(recordZonesToSave: [zone], recordZoneIDsToDelete: nil) 127 | 128 | operation.modifyRecordZonesCompletionBlock = { [weak self] _, _, error in 129 | guard let self = self else { return } 130 | 131 | if let error = error { 132 | os_log("Failed to create custom CloudKit zone: %{public}@", 133 | log: self.log, 134 | type: .error, 135 | String(describing: error)) 136 | 137 | error.retryCloudKitOperationIfPossible(self.log) { self.createCustomZoneIfNeeded() } 138 | } else { 139 | os_log("Zone created successfully", log: self.log, type: .info) 140 | self.createdCustomZone = true 141 | } 142 | } 143 | 144 | operation.qualityOfService = .userInitiated 145 | operation.database = privateDatabase 146 | 147 | cloudOperationQueue.addOperation(operation) 148 | } 149 | 150 | private func checkCustomZone() { 151 | let operation = CKFetchRecordZonesOperation(recordZoneIDs: [SyncConstants.customZoneID]) 152 | 153 | operation.fetchRecordZonesCompletionBlock = { [weak self] ids, error in 154 | guard let self = self else { return } 155 | 156 | if let error = error { 157 | os_log("Failed to check for custom zone existence: %{public}@", log: self.log, type: .error, String(describing: error)) 158 | 159 | if !error.retryCloudKitOperationIfPossible(self.log, with: { self.checkCustomZone() }) { 160 | os_log("Irrecoverable error when fetching custom zone, assuming it doesn't exist: %{public}@", log: self.log, type: .error, String(describing: error)) 161 | 162 | DispatchQueue.main.async { 163 | self.createdCustomZone = false 164 | self.createCustomZoneIfNeeded() 165 | } 166 | } 167 | } else if ids == nil || ids?.count == 0 { 168 | os_log("Custom zone reported as existing, but it doesn't exist. Creating.", log: self.log, type: .error) 169 | self.createdCustomZone = false 170 | self.createCustomZoneIfNeeded() 171 | } 172 | } 173 | 174 | operation.qualityOfService = .userInitiated 175 | operation.database = privateDatabase 176 | 177 | cloudOperationQueue.addOperation(operation) 178 | } 179 | 180 | private func createPrivateSubscriptionsIfNeeded() { 181 | guard !createdPrivateSubscription else { 182 | os_log("Already subscribed to private database changes, skipping subscription but checking if it really exists", log: log, type: .debug) 183 | 184 | checkSubscription() 185 | 186 | return 187 | } 188 | 189 | let subscription = CKRecordZoneSubscription(zoneID: SyncConstants.customZoneID, subscriptionID: privateSubscriptionId) 190 | 191 | let notificationInfo = CKSubscription.NotificationInfo() 192 | notificationInfo.shouldSendContentAvailable = true 193 | 194 | subscription.notificationInfo = notificationInfo 195 | subscription.recordType = .recipe 196 | 197 | let operation = CKModifySubscriptionsOperation(subscriptionsToSave: [subscription], subscriptionIDsToDelete: nil) 198 | 199 | operation.database = privateDatabase 200 | operation.qualityOfService = .userInitiated 201 | 202 | operation.modifySubscriptionsCompletionBlock = { [weak self] _, _, error in 203 | guard let self = self else { return } 204 | 205 | if let error = error { 206 | os_log("Failed to create private CloudKit subscription: %{public}@", 207 | log: self.log, 208 | type: .error, 209 | String(describing: error)) 210 | 211 | error.retryCloudKitOperationIfPossible(self.log) { self.createPrivateSubscriptionsIfNeeded() } 212 | } else { 213 | os_log("Private subscription created successfully", log: self.log, type: .info) 214 | self.createdPrivateSubscription = true 215 | } 216 | } 217 | 218 | cloudOperationQueue.addOperation(operation) 219 | } 220 | 221 | private func checkSubscription() { 222 | let operation = CKFetchSubscriptionsOperation(subscriptionIDs: [privateSubscriptionId]) 223 | 224 | operation.fetchSubscriptionCompletionBlock = { [weak self] ids, error in 225 | guard let self = self else { return } 226 | 227 | if let error = error { 228 | os_log("Failed to check for private zone subscription existence: %{public}@", log: self.log, type: .error, String(describing: error)) 229 | 230 | if !error.retryCloudKitOperationIfPossible(self.log, with: { self.checkSubscription() }) { 231 | os_log("Irrecoverable error when fetching private zone subscription, assuming it doesn't exist: %{public}@", log: self.log, type: .error, String(describing: error)) 232 | 233 | DispatchQueue.main.async { 234 | self.createdPrivateSubscription = false 235 | self.createPrivateSubscriptionsIfNeeded() 236 | } 237 | } 238 | } else if ids == nil || ids?.count == 0 { 239 | os_log("Private subscription reported as existing, but it doesn't exist. Creating.", log: self.log, type: .error) 240 | 241 | DispatchQueue.main.async { 242 | self.createdPrivateSubscription = false 243 | self.createPrivateSubscriptionsIfNeeded() 244 | } 245 | } 246 | } 247 | 248 | operation.qualityOfService = .userInitiated 249 | operation.database = privateDatabase 250 | 251 | cloudOperationQueue.addOperation(operation) 252 | } 253 | 254 | // MARK: - Upload 255 | 256 | private func uploadLocalDataNotUploadedYet() { 257 | os_log("%{public}@", log: log, type: .debug, #function) 258 | 259 | let recipes = buffer.filter({ $0.ckData == nil }) 260 | 261 | guard !recipes.isEmpty else { return } 262 | 263 | os_log("Found %d local recipe(s) which haven't been uploaded yet.", log: self.log, type: .debug, recipes.count) 264 | 265 | let records = recipes.map { $0.record } 266 | 267 | uploadRecords(records) 268 | } 269 | 270 | func upload(_ recipe: Recipe) { 271 | os_log("%{public}@", log: log, type: .debug, #function) 272 | 273 | buffer.append(recipe) 274 | 275 | uploadRecords([recipe.record]) 276 | } 277 | 278 | func delete(_ recipe: Recipe) { 279 | fatalError("Deletion not implemented") 280 | } 281 | 282 | private func uploadRecords(_ records: [CKRecord]) { 283 | guard !records.isEmpty else { return } 284 | 285 | os_log("%{public}@ with %d record(s)", log: log, type: .debug, #function, records.count) 286 | 287 | let operation = CKModifyRecordsOperation(recordsToSave: records, recordIDsToDelete: nil) 288 | 289 | operation.perRecordCompletionBlock = { [weak self] record, error in 290 | guard let self = self else { return } 291 | 292 | // We're only interested in conflict errors here 293 | guard let error = error, error.isCloudKitConflict else { return } 294 | 295 | os_log("CloudKit conflict with record of type %{public}@", log: self.log, type: .error, record.recordType) 296 | 297 | guard let resolvedRecord = error.resolveConflict(with: Recipe.resolveConflict) else { 298 | os_log( 299 | "Resolving conflict with record of type %{public}@ returned a nil record. Giving up.", 300 | log: self.log, 301 | type: .error, 302 | record.recordType 303 | ) 304 | return 305 | } 306 | 307 | os_log("Conflict resolved, will retry upload", log: self.log, type: .info) 308 | 309 | self.uploadRecords([resolvedRecord]) 310 | } 311 | 312 | operation.modifyRecordsCompletionBlock = { [weak self] serverRecords, _, error in 313 | guard let self = self else { return } 314 | 315 | if let error = error { 316 | os_log("Failed to upload records: %{public}@", log: self.log, type: .error, String(describing: error)) 317 | 318 | DispatchQueue.main.async { 319 | self.handleUploadError(error, records: records) 320 | } 321 | } else { 322 | os_log("Successfully uploaded %{public}d record(s)", log: self.log, type: .info, records.count) 323 | 324 | DispatchQueue.main.async { 325 | guard let serverRecords = serverRecords else { return } 326 | self.updateLocalModelsAfterUpload(with: serverRecords) 327 | } 328 | } 329 | } 330 | 331 | operation.savePolicy = .ifServerRecordUnchanged 332 | operation.qualityOfService = .userInitiated 333 | operation.database = privateDatabase 334 | 335 | cloudOperationQueue.addOperation(operation) 336 | } 337 | 338 | private func handleUploadError(_ error: Error, records: [CKRecord]) { 339 | guard let ckError = error as? CKError else { 340 | os_log("Error was not a CKError, giving up: %{public}@", log: self.log, type: .fault, String(describing: error)) 341 | return 342 | } 343 | 344 | if ckError.code == CKError.Code.limitExceeded { 345 | os_log("CloudKit batch limit exceeded, sending records in chunks", log: self.log, type: .error) 346 | 347 | fatalError("Not implemented: batch uploads. Here we should divide the records in chunks and upload in batches instead of trying everything at once.") 348 | } else { 349 | let result = error.retryCloudKitOperationIfPossible(self.log) { self.uploadRecords(records) } 350 | 351 | if !result { 352 | os_log("Error is not recoverable: %{public}@", log: self.log, type: .error, String(describing: error)) 353 | } 354 | } 355 | } 356 | 357 | private func updateLocalModelsAfterUpload(with records: [CKRecord]) { 358 | let models: [Recipe] = records.compactMap { r in 359 | guard var model = buffer.first(where: { $0.id == r.recordID.recordName }) else { return nil } 360 | 361 | model.ckData = r.encodedSystemFields 362 | 363 | return model 364 | } 365 | 366 | DispatchQueue.main.async { 367 | self.didUpdateModels(models) 368 | self.buffer = [] 369 | } 370 | } 371 | 372 | // MARK: - Remote change tracking 373 | 374 | private lazy var privateChangeTokenKey: String = { 375 | return "TOKEN-\(SyncConstants.customZoneID.zoneName)" 376 | }() 377 | 378 | private var privateChangeToken: CKServerChangeToken? { 379 | get { 380 | guard let data = defaults.data(forKey: privateChangeTokenKey) else { return nil } 381 | guard !data.isEmpty else { return nil } 382 | 383 | do { 384 | let token = try NSKeyedUnarchiver.unarchivedObject(ofClass: CKServerChangeToken.self, from: data) 385 | 386 | return token 387 | } catch { 388 | os_log("Failed to decode CKServerChangeToken from defaults key privateChangeToken", log: log, type: .error) 389 | return nil 390 | } 391 | } 392 | set { 393 | guard let newValue = newValue else { 394 | defaults.setValue(Data(), forKey: privateChangeTokenKey) 395 | return 396 | } 397 | 398 | do { 399 | let data = try NSKeyedArchiver.archivedData(withRootObject: newValue, requiringSecureCoding: true) 400 | 401 | defaults.set(data, forKey: privateChangeTokenKey) 402 | } catch { 403 | os_log("Failed to encode private change token: %{public}@", log: self.log, type: .error, String(describing: error)) 404 | } 405 | } 406 | } 407 | 408 | func fetchRemoteChanges() { 409 | os_log("%{public}@", log: log, type: .debug, #function) 410 | 411 | var changedRecords: [CKRecord] = [] 412 | var deletedRecordIDs: [CKRecord.ID] = [] 413 | 414 | let operation = CKFetchRecordZoneChangesOperation() 415 | 416 | let token: CKServerChangeToken? = privateChangeToken 417 | 418 | let config = CKFetchRecordZoneChangesOperation.ZoneConfiguration( 419 | previousServerChangeToken: token, 420 | resultsLimit: nil, 421 | desiredKeys: nil 422 | ) 423 | 424 | operation.configurationsByRecordZoneID = [SyncConstants.customZoneID: config] 425 | 426 | operation.recordZoneIDs = [SyncConstants.customZoneID] 427 | operation.fetchAllChanges = true 428 | 429 | operation.recordZoneChangeTokensUpdatedBlock = { [weak self] _, changeToken, _ in 430 | guard let self = self else { return } 431 | 432 | guard let changeToken = changeToken else { return } 433 | 434 | self.privateChangeToken = changeToken 435 | } 436 | 437 | operation.recordZoneFetchCompletionBlock = { [weak self] _, token, _, _, error in 438 | guard let self = self else { return } 439 | 440 | if let error = error as? CKError { 441 | os_log("Failed to fetch record zone changes: %{public}@", 442 | log: self.log, 443 | type: .error, 444 | String(describing: error)) 445 | 446 | if error.code == .changeTokenExpired { 447 | os_log("Change token expired, resetting token and trying again", log: self.log, type: .error) 448 | 449 | self.privateChangeToken = nil 450 | 451 | DispatchQueue.main.async { self.fetchRemoteChanges() } 452 | } else { 453 | error.retryCloudKitOperationIfPossible(self.log) { self.fetchRemoteChanges() } 454 | } 455 | } else { 456 | os_log("Commiting new change token", log: self.log, type: .debug) 457 | 458 | self.privateChangeToken = token 459 | } 460 | } 461 | 462 | operation.recordChangedBlock = { changedRecords.append($0) } 463 | 464 | operation.recordWithIDWasDeletedBlock = { recordID, _ in 465 | // In the future we may need to use the second arg to this closure and map 466 | // between record types and deleted record IDs (when we need to sync more types) 467 | deletedRecordIDs.append(recordID) 468 | } 469 | 470 | operation.fetchRecordZoneChangesCompletionBlock = { [weak self] error in 471 | guard let self = self else { return } 472 | 473 | if let error = error { 474 | os_log("Failed to fetch record zone changes: %{public}@", 475 | log: self.log, 476 | type: .error, 477 | String(describing: error)) 478 | 479 | error.retryCloudKitOperationIfPossible(self.log) { self.fetchRemoteChanges() } 480 | } else { 481 | os_log("Finished fetching record zone changes", log: self.log, type: .info) 482 | 483 | DispatchQueue.main.async { self.commitServerChangesToDatabase(with: changedRecords, deletedRecordIDs: deletedRecordIDs) } 484 | } 485 | } 486 | 487 | operation.qualityOfService = .userInitiated 488 | operation.database = privateDatabase 489 | 490 | cloudOperationQueue.addOperation(operation) 491 | } 492 | 493 | private func commitServerChangesToDatabase(with changedRecords: [CKRecord], deletedRecordIDs: [CKRecord.ID]) { 494 | guard !changedRecords.isEmpty || !deletedRecordIDs.isEmpty else { 495 | os_log("Finished record zone changes fetch with no changes", log: log, type: .info) 496 | return 497 | } 498 | 499 | os_log("Will commit %d changed record(s) and %d deleted record(s) to the database", log: log, type: .info, changedRecords.count, deletedRecordIDs.count) 500 | 501 | let models: [Recipe] = changedRecords.compactMap { record in 502 | do { 503 | return try Recipe(record: record) 504 | } catch { 505 | os_log("Error decoding recipe from record: %{public}@", log: self.log, type: .error, String(describing: error)) 506 | return nil 507 | } 508 | } 509 | 510 | let deletedIdentifiers = deletedRecordIDs.map { $0.recordName } 511 | 512 | didUpdateModels(models) 513 | didDeleteModels(deletedIdentifiers) 514 | } 515 | 516 | } 517 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2017 Guilherme Rambo 2 | 3 | Redistribution and use in source and binary forms, with or without 4 | modification, are permitted provided that the following conditions are met: 5 | 6 | - Redistributions of source code must retain the above copyright notice, this 7 | list of conditions and the following disclaimer. 8 | 9 | - Redistributions in binary form must reproduce the above copyright notice, 10 | this list of conditions and the following disclaimer in the documentation 11 | and/or other materials provided with the distribution. 12 | 13 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 14 | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 15 | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 16 | DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 17 | FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 18 | DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 19 | SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 20 | CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 21 | OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 22 | OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | A sample app showing how to sync a user's private data using CloudKit. 2 | 3 | This app was written as a companion for [my article on CloudKit](https://rambo.codes/posts/2020-02-25-cloudkit-101). It's written in SwiftUI and requires a paid developer account so that you can set up a CloudKit container to be used with the app. 4 | 5 | ![screenshot](./screenshots/main.png) -------------------------------------------------------------------------------- /screenshots/main.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/insidegui/CloudKitchenSink20/9f0d253220ddb81d8c0f18211c187ac97e21cf96/screenshots/main.png --------------------------------------------------------------------------------