-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eed0f87
commit 5d8dd2e
Showing
8 changed files
with
340 additions
and
27 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
RealiOSApps/CoreDataImpl/PetSave/AnimalsNearYou/services/AnimalStoreService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/// Copyright (c) 2022 Razeware LLC | ||
/// | ||
/// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/// of this software and associated documentation files (the "Software"), to deal | ||
/// in the Software without restriction, including without limitation the rights | ||
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
/// copies of the Software, and to permit persons to whom the Software is | ||
/// furnished to do so, subject to the following conditions: | ||
/// | ||
/// The above copyright notice and this permission notice shall be included in | ||
/// all copies or substantial portions of the Software. | ||
/// | ||
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, | ||
/// distribute, sublicense, create a derivative work, and/or sell copies of the | ||
/// Software in any work that is designed, intended, or marketed for pedagogical or | ||
/// instructional purposes related to programming, coding, application development, | ||
/// or information technology. Permission for such use, copying, modification, | ||
/// merger, publication, distribution, sublicensing, creation of derivative works, | ||
/// or sale is expressly withheld. | ||
/// | ||
/// This project and source code may use libraries or frameworks that are | ||
/// released under various Open-Source licenses. Use of those libraries and | ||
/// frameworks are governed by their own individual licenses. | ||
/// | ||
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
/// THE SOFTWARE. | ||
|
||
import CoreData | ||
|
||
struct AnimalStoreService { | ||
private let context: NSManagedObjectContext | ||
|
||
init(context: NSManagedObjectContext) { | ||
self.context = context | ||
} | ||
} | ||
|
||
// MARK: - AnimalStore | ||
extension AnimalStoreService: AnimalStore { | ||
func save(animals: [Animal]) async throws { | ||
for var animal in animals { | ||
animal.toManagedObject(context: context) | ||
} | ||
|
||
try context.save() | ||
} | ||
} |
62 changes: 62 additions & 0 deletions
62
RealiOSApps/CoreDataImpl/PetSave/AnimalsNearYou/services/FetchAnimalsService.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/// Copyright (c) 2022 Razeware LLC | ||
/// | ||
/// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/// of this software and associated documentation files (the "Software"), to deal | ||
/// in the Software without restriction, including without limitation the rights | ||
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
/// copies of the Software, and to permit persons to whom the Software is | ||
/// furnished to do so, subject to the following conditions: | ||
/// | ||
/// The above copyright notice and this permission notice shall be included in | ||
/// all copies or substantial portions of the Software. | ||
/// | ||
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, | ||
/// distribute, sublicense, create a derivative work, and/or sell copies of the | ||
/// Software in any work that is designed, intended, or marketed for pedagogical or | ||
/// instructional purposes related to programming, coding, application development, | ||
/// or information technology. Permission for such use, copying, modification, | ||
/// merger, publication, distribution, sublicensing, creation of derivative works, | ||
/// or sale is expressly withheld. | ||
/// | ||
/// This project and source code may use libraries or frameworks that are | ||
/// released under various Open-Source licenses. Use of those libraries and | ||
/// frameworks are governed by their own individual licenses. | ||
/// | ||
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
/// THE SOFTWARE. | ||
|
||
import Foundation | ||
|
||
struct FetchAnimalsService { | ||
private let requestManager: RequestManagerProtocol | ||
|
||
init(requestManager: RequestManagerProtocol) { | ||
self.requestManager = requestManager | ||
} | ||
} | ||
|
||
extension FetchAnimalsService: AnimalsFetcher { | ||
func fetchAnimals(page: Int) async -> [Animal] { | ||
let requestData = AnimalsRequest.getAnimalsWith(page: page, latitude: nil, longitude: nil) | ||
do { | ||
let animalsContainer: AnimalsContainer = try await | ||
requestManager.perform(requestData) | ||
return animalsContainer.animals | ||
} catch { | ||
print(error.localizedDescription) | ||
return [] | ||
} | ||
} | ||
} | ||
|
||
struct AnimalsFetcherMock: AnimalsFetcher { | ||
func fetchAnimals(page: Int) async -> [Animal] { | ||
Animal.mock | ||
} | ||
} | ||
|
75 changes: 75 additions & 0 deletions
75
RealiOSApps/CoreDataImpl/PetSave/AnimalsNearYou/viewModels/AnimalsNearYouViewModel.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/// Copyright (c) 2022 Razeware LLC | ||
/// | ||
/// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/// of this software and associated documentation files (the "Software"), to deal | ||
/// in the Software without restriction, including without limitation the rights | ||
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
/// copies of the Software, and to permit persons to whom the Software is | ||
/// furnished to do so, subject to the following conditions: | ||
/// | ||
/// The above copyright notice and this permission notice shall be included in | ||
/// all copies or substantial portions of the Software. | ||
/// | ||
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, | ||
/// distribute, sublicense, create a derivative work, and/or sell copies of the | ||
/// Software in any work that is designed, intended, or marketed for pedagogical or | ||
/// instructional purposes related to programming, coding, application development, | ||
/// or information technology. Permission for such use, copying, modification, | ||
/// merger, publication, distribution, sublicensing, creation of derivative works, | ||
/// or sale is expressly withheld. | ||
/// | ||
/// This project and source code may use libraries or frameworks that are | ||
/// released under various Open-Source licenses. Use of those libraries and | ||
/// frameworks are governed by their own individual licenses. | ||
/// | ||
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
/// THE SOFTWARE. | ||
|
||
import Foundation | ||
|
||
protocol AnimalsFetcher { | ||
func fetchAnimals(page: Int) async -> [Animal] | ||
} | ||
|
||
protocol AnimalStore { | ||
func save(animals: [Animal]) async throws | ||
} | ||
|
||
@MainActor | ||
final class AnimalsNearYouViewModel: ObservableObject { | ||
@Published var isLoading = false | ||
@Published var hasMoreAnimals = true | ||
|
||
private let animalFetcher: AnimalsFetcher | ||
private let animalStore: AnimalStore | ||
private(set) var page = 1 | ||
|
||
init(isLoading: Bool = true, animalFetcher: AnimalsFetcher, animalStore: AnimalStore) { | ||
self.isLoading = isLoading | ||
self.animalFetcher = animalFetcher | ||
self.animalStore = animalStore | ||
} | ||
|
||
func fetchMoreAnimals() async { | ||
page += 1 | ||
await fetchAnimals() | ||
} | ||
|
||
func fetchAnimals() async { | ||
let animals = await animalFetcher.fetchAnimals(page: page) | ||
|
||
do { | ||
try await animalStore.save(animals: animals) | ||
} catch { | ||
print("Error storing animals... \(error.localizedDescription)") | ||
} | ||
|
||
isLoading = false | ||
hasMoreAnimals = !animals.isEmpty | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
RealiOSApps/CoreDataImpl/PetSave/AnimalsNearYou/views/AnimalAttributesCard.swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/// Copyright (c) 2022 Razeware LLC | ||
/// | ||
/// Permission is hereby granted, free of charge, to any person obtaining a copy | ||
/// of this software and associated documentation files (the "Software"), to deal | ||
/// in the Software without restriction, including without limitation the rights | ||
/// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
/// copies of the Software, and to permit persons to whom the Software is | ||
/// furnished to do so, subject to the following conditions: | ||
/// | ||
/// The above copyright notice and this permission notice shall be included in | ||
/// all copies or substantial portions of the Software. | ||
/// | ||
/// Notwithstanding the foregoing, you may not use, copy, modify, merge, publish, | ||
/// distribute, sublicense, create a derivative work, and/or sell copies of the | ||
/// Software in any work that is designed, intended, or marketed for pedagogical or | ||
/// instructional purposes related to programming, coding, application development, | ||
/// or information technology. Permission for such use, copying, modification, | ||
/// merger, publication, distribution, sublicensing, creation of derivative works, | ||
/// or sale is expressly withheld. | ||
/// | ||
/// This project and source code may use libraries or frameworks that are | ||
/// released under various Open-Source licenses. Use of those libraries and | ||
/// frameworks are governed by their own individual licenses. | ||
/// | ||
/// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
/// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
/// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
/// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
/// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
/// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
/// THE SOFTWARE. | ||
|
||
import SwiftUI | ||
|
||
struct AnimalAttributesCard: ViewModifier { | ||
let color: Color | ||
|
||
func body(content: Content) -> some View { | ||
content | ||
.padding(4) | ||
.background(color.opacity(0.2)) | ||
.cornerRadius(8) | ||
.foregroundColor(color) | ||
.font(.subheadline) | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.