-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from developersancho/feature/favorite
Feature/favorite
- Loading branch information
Showing
31 changed files
with
798 additions
and
95 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
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
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
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,30 @@ | ||
// | ||
// EmptyStateViewModifier.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 12.03.2022. | ||
// | ||
|
||
import Foundation | ||
import SwiftUI | ||
|
||
struct EmptyStateViewModifier<EmptyContent>: ViewModifier where EmptyContent: View { | ||
var isEmpty: Bool | ||
let emptyContent: () -> EmptyContent | ||
|
||
func body(content: Content) -> some View { | ||
if isEmpty { | ||
emptyContent() | ||
} | ||
else { | ||
content | ||
} | ||
} | ||
} | ||
|
||
extension View { | ||
func emptyState<EmptyContent>(_ isEmpty: Bool, | ||
emptyContent: @escaping () -> EmptyContent) -> some View where EmptyContent: View { | ||
modifier(EmptyStateViewModifier(isEmpty: isEmpty, emptyContent: emptyContent)) | ||
} | ||
} |
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,83 @@ | ||
// | ||
// Logger.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 9.03.2022. | ||
// | ||
|
||
import Foundation | ||
import OSLog | ||
|
||
|
||
import Foundation | ||
import OSLog | ||
|
||
public class Log { | ||
|
||
/// Show message while debugging | ||
static func debug(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().debug("[Debug] \(message)") | ||
} | ||
|
||
/// Show message as information | ||
static func info(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().info("[Info] \(message)") | ||
} | ||
|
||
/// Show message when a condition may require special handling | ||
static func notice(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().notice("[Notice] \(message)") | ||
} | ||
|
||
/// Show message when a condition requires special handling | ||
static func warning(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().warning("[Warning] \(message)") | ||
} | ||
|
||
/// Show message as error | ||
static func error(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().error("[Error] \(message)") | ||
} | ||
|
||
/// Show message when require immediate attention | ||
static func critical(category: String = "MyApp", _ message: String) { | ||
|
||
Logger.message().critical("[Attention] \(message)") | ||
} | ||
} | ||
|
||
extension Logger { | ||
|
||
private static var bundleIdentifier = Bundle.main.bundleIdentifier! | ||
|
||
static func message(_ category: String = "MyApp") -> Logger { | ||
|
||
Logger(subsystem: "\(bundleIdentifier)", category: "MyApp") | ||
} | ||
|
||
static func retrieveLogs(interval: Double = -86400) throws -> [OSLogEntryLog] { | ||
// Open the log store. | ||
let logStore = try OSLogStore(scope: .currentProcessIdentifier) | ||
|
||
// Get all the logs from the last hour. | ||
let oneDayAgo = logStore.position(date: Date().addingTimeInterval(interval)) | ||
|
||
// Fetch log objects. | ||
let allEntries = try logStore.getEntries(at: oneDayAgo) | ||
|
||
// Filter the log to be relevant for our specific subsystem | ||
// and remove other elements (signposts, etc). | ||
return allEntries | ||
.compactMap { | ||
$0 as? OSLogEntryLog | ||
} | ||
.filter { | ||
$0.subsystem == Bundle.main.bundleIdentifier! | ||
} | ||
} | ||
} |
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,83 @@ | ||
// | ||
// CoreDataManager.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 7.03.2022. | ||
// | ||
|
||
import Foundation | ||
import CoreData | ||
|
||
class CoreDataManager { | ||
let persistentContainer: NSPersistentContainer | ||
|
||
init() { | ||
persistentContainer = NSPersistentContainer(name: "RortyDatabase") | ||
persistentContainer.loadPersistentStores { (description, error) in | ||
if let error = error { | ||
fatalError("Core Data Store Failed \(error.localizedDescription)") | ||
} else { | ||
print("Successfully Loaded Core Data :)") | ||
} | ||
} | ||
} | ||
|
||
func getFavoriteList() -> [FavoriteEntity] { | ||
let fetchRequest: NSFetchRequest<FavoriteEntity> = FavoriteEntity.fetchRequest() | ||
do { | ||
return try persistentContainer.viewContext.fetch(fetchRequest) | ||
} catch { | ||
return [] | ||
} | ||
} | ||
|
||
func getFavorite(id: Int) -> FavoriteEntity? { | ||
let fetchRequest: NSFetchRequest<FavoriteEntity> = FavoriteEntity.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "id = %@", "\(id)") | ||
fetchRequest.fetchLimit = 1 | ||
|
||
let context = persistentContainer.viewContext | ||
|
||
do { | ||
return try context.fetch(fetchRequest).first | ||
} catch { | ||
return nil | ||
} | ||
} | ||
|
||
func saveFavorite(id: Int, status: String?, gender: String?, name: String?, imageUrl: String?, species: String?) { | ||
let favorite = FavoriteEntity(context: persistentContainer.viewContext) | ||
favorite.id = Int32(id) | ||
favorite.status = status | ||
favorite.gender = gender | ||
favorite.name = name | ||
favorite.imageUrl = imageUrl | ||
favorite.species = species | ||
|
||
do { | ||
try persistentContainer.viewContext.save() | ||
} catch { | ||
print("Failed to save favorite \(error)") | ||
} | ||
} | ||
|
||
func deleteFavorite(id: Int) { | ||
let fetchRequest: NSFetchRequest<FavoriteEntity> = FavoriteEntity.fetchRequest() | ||
fetchRequest.predicate = NSPredicate(format: "id = %@", "\(id)") | ||
fetchRequest.includesPropertyValues = false | ||
// Get a reference to a managed object context | ||
let context = persistentContainer.viewContext | ||
|
||
do { | ||
let objects = try context.fetch(fetchRequest) | ||
// Delete the objects | ||
for object in objects { | ||
context.delete(object) | ||
} | ||
try context.save() | ||
} catch { | ||
persistentContainer.viewContext.rollback() | ||
print("Failed to save favorite \(error)") | ||
} | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
SwiftRorty.iOS/data/local/RortyDatabase.xcdatamodeld/RortyDatabase.xcdatamodel/contents
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,14 @@ | ||
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> | ||
<model type="com.apple.IDECoreDataModeler.DataModel" documentVersion="1.0" lastSavedToolsVersion="19574" systemVersion="21D62" minimumToolsVersion="Automatic" sourceLanguage="Swift" userDefinedModelVersionIdentifier=""> | ||
<entity name="FavoriteEntity" representedClassName="FavoriteEntity" syncable="YES" codeGenerationType="class"> | ||
<attribute name="gender" optional="YES" attributeType="String"/> | ||
<attribute name="id" optional="YES" attributeType="Integer 32" defaultValueString="0" usesScalarValueType="YES"/> | ||
<attribute name="imageUrl" optional="YES" attributeType="String"/> | ||
<attribute name="name" optional="YES" attributeType="String"/> | ||
<attribute name="species" optional="YES" attributeType="String"/> | ||
<attribute name="status" optional="YES" attributeType="String"/> | ||
</entity> | ||
<elements> | ||
<element name="FavoriteEntity" positionX="-63" positionY="-18" width="128" height="133"/> | ||
</elements> | ||
</model> |
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
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
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,30 @@ | ||
// | ||
// FavoriteRepository.swift | ||
// SwiftRorty.iOS | ||
// | ||
// Created by developersancho on 9.03.2022. | ||
// | ||
|
||
import Foundation | ||
import Resolver | ||
|
||
class FavoriteRepository { | ||
@Injected | ||
private var coreDataManager: CoreDataManager | ||
|
||
func saveFavorite(id: Int, status: String?, gender: String?, name: String?, imageUrl: String?, species: String?) { | ||
coreDataManager.saveFavorite(id: id, status: status, gender: gender, name: name, imageUrl: imageUrl, species: species) | ||
} | ||
|
||
func deleteFavorite(id: Int) { | ||
coreDataManager.deleteFavorite(id: id) | ||
} | ||
|
||
func getFavoriteList() -> [FavoriteEntity] { | ||
return coreDataManager.getFavoriteList() | ||
} | ||
|
||
func getFavorite(id: Int) -> FavoriteEntity? { | ||
return coreDataManager.getFavorite(id: id) | ||
} | ||
} |
Oops, something went wrong.